code stringlengths 1 1.05M | repo_name stringlengths 6 83 | path stringlengths 3 242 | language stringclasses 222 values | license stringclasses 20 values | size int64 1 1.05M |
|---|---|---|---|---|---|
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "crypt_errno.h"
#include "crypt_eal_cipher.h"
#include "bsl_sal.h"
#include "pthread.h"
#include "securec.h"
#include "eal_cipher_local.h"
#define BLOCKSIZE 16
#define KEYSIZE 32
#define MAXSIZE 1024
#define MAX_OUTPUT 5000
#define MAX_DATASZIE 20000
/* END_HEADER */
static int SetPadding(int isSetPadding, CRYPT_EAL_CipherCtx *ctxEnc, int padding)
{
if (isSetPadding == 1) {
return CRYPT_EAL_CipherSetPadding(ctxEnc, padding);
}
return CRYPT_SUCCESS;
}
static int Sm4CipherFinal(
int algId, CRYPT_EAL_CipherCtx *ctx, uint8_t *outTmp, uint32_t *finLen)
{
if (algId != CRYPT_CIPHER_SM4_GCM) {
return CRYPT_EAL_CipherFinal(ctx, outTmp, finLen);
}
*finLen = 0;
return CRYPT_SUCCESS;
}
/**
* @test SDV_CRYPTO_SM4_INIT_API_TC001
* @title Impact of IV validity on algorithm module initialization Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface, ctx is not NULL, iv is NULL, ivLen is 0, and key is normal value. Expected result 2 is obtained.
* 3.Call the Init interface, ctx is not NULL, iv is not NULL, ivLen is 0, and key is normal value. Expected result 3 is obtained.
* 4.Call the Init interface, ctx is not NULL, iv is not NULL, ivLen is 15, and key is normal value. Expected result 4 is obtained.
* 5.Call the Init interface, ctx is not NULL, iv is not NULL, ivLen is 17, and key is normal value. Expected result 5 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.Failed.
* 3.Failed.
* 4.Failed except for the GCM algorithm.
* 5.Failed except for the GCM algorithm.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_INIT_API_TC001(int id, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, NULL, 0, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 0, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, BLOCKSIZE - 1, true);
if (id == CRYPT_CIPHER_SM4_GCM) {
ASSERT_TRUE(ret == CRYPT_SUCCESS);
} else {
ASSERT_TRUE(ret != CRYPT_SUCCESS);
}
if (id == CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, BLOCKSIZE, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
} else {
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, BLOCKSIZE + 1, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
}
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_INIT_API_TC002
* @title Impact of input parameters on the CRYPT_EAL_CipherInit interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx with CRYPT_CIPHER_SM4_XTS. Expected result 1 is obtained.
* 2.Call the Init interface, ctx is NULL. Expected result 2 is obtained.
* 3.Call the Init interface, ctx is not NULL, key is NULL. Expected result 3 is obtained.
* 4.Call the Init interface, ctx is not NULL, key is not NULL, iv is NULL. Expected result 4 is obtained.
* 5.Call the Init interface, ctx, key and iv is not NULL, keyLen is less than or greater than 32 bytes. Expected result 5 is obtained.
* 6.Call the Init interface, ctx, key and iv is not NULL, keyLen is 32 bytes and the previous and next 16 bytes are the same. Expected result 6 is obtained.
* 7.Call the Init interface, ctx, key and iv is not NULL, ivLen is less than or greater than 16 bytes. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.Initialization failed.
* 3.Initialization failed.
* 4.Initialization failed.
* 5.Initialization failed.
* 6.Initialization failed.
* 7.Initialization failed.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_INIT_API_TC002(Hex *key, Hex *iv, int enc)
{
uint8_t unsafe_key[KEYSIZE] = {0};
TestMemInit();
int32_t ret;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_SM4_XTS);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(NULL, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherInit(ctx, NULL, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, NULL, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherInit(ctx, key->x, 0, iv->x, iv->len, enc);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, 1, iv->x, iv->len, enc);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, 16, iv->x, iv->len, enc);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, 33, iv->x, iv->len, enc);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, unsafe_key, KEYSIZE, iv->x, iv->len, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, unsafe_key, KEYSIZE, iv->x, iv->len, false);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 0, enc), CRYPT_MODES_IVLEN_ERROR);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 1, enc), CRYPT_MODES_IVLEN_ERROR);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 15, enc), CRYPT_MODES_IVLEN_ERROR);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 17, enc), CRYPT_MODES_IVLEN_ERROR);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_INIT_API_TC003
* @title Impact of key validity on algorithm module initialization Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface, ctx is NULL. Expected result 2 is obtained.
* 3.Call the Init interface, ctx is not NULL, key is NULL, keyLen is 0. Expected result 3 is obtained.
* 4.Call the Init interface, ctx is not NULL, key is not NULL, keyLen is 0. Expected result 4 is obtained.
* 5.Call the Init interface, ctx is not NULL, key is not NULL, keyLen is 15. Expected result 5 is obtained.
* 6.Call the Init interface, ctx is not NULL, key is not NULL, keyLen is 17. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.Initialization failed.
* 3.Initialization failed.
* 4.Initialization failed.
* 5.Initialization failed.
* 6.Initialization failed.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_INIT_API_TC003(int id, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(NULL, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherInit(ctx, NULL, 0, iv->x, iv->len, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, 0, iv->x, iv->len, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, BLOCKSIZE - 1, iv->x, iv->len, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctx, key->x, BLOCKSIZE + 1, iv->x, iv->len, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_DEINIT_API_TC001
* @title Impact of input parameters on the CRYPT_EAL_CipherDeinit interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Deinit interface, ctx is NULL. Expected result 2 is obtained.
* 3.Call the Deinit interface. All parameters are normal. Expected result 3 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The function is executed successfully.
* 3.The function is executed successfully.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_DEINIT_API_TC001(int id)
{
TestMemInit();
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherDeinit(NULL);
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_REINIT_API_TC001
* @title CRYPT_EAL_CipherReinit for iv Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Reinit interface. Expected result 2 is obtained.
* 3.Call the Init interface. Expected result 3 is obtained.
* 4.Call the Reinit interface, ctx is NULL, iv is not NULL, ivLen is not 0. Expected result 4 is obtained.
* 5.Call the Reinit interface, ctx is not NULL, iv is NULL, ivLen is not 0. Expected result 5 is obtained.
* 6.Call the Reinit interface, ctx is not NULL, iv is not NULL, ivLen is 0. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.Failed. Return CRYPT_EAL_ERR_STATE.
* 3.The init is successful and return CRYPT_SUCCESS.
* 4.Failed. Return CRYPT_NULL_INPUT.
* 5.Failed. Return CRYPT_NULL_INPUT.
* 6.Failed. Return CRYPT_NULL_INPUT/CRYPT_MODES_IVLEN_ERROR.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_REINIT_API_TC001(int id, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, iv->len);
ASSERT_TRUE(ret == CRYPT_EAL_ERR_STATE);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherReinit(NULL, iv->x, iv->len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherReinit(ctx, NULL, iv->len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, 0);
if (id == CRYPT_CIPHER_SM4_GCM) {
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
} else {
ASSERT_TRUE(ret == CRYPT_MODES_IVLEN_ERROR);
}
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_UPDATE_API_TC001
* @title Impact of input parameters on the CRYPT_EAL_CipherUpdate interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface, ctx is NULL. Expected result 3 is obtained.
* 4.Call the Update interface, ctx is not NULL, in is NULL. Expected result 4 is obtained.
* 5.Call the Update interface, ctx is not NULL, in is not NULL, out is NULL. Expected result 5 is obtained.
* 6.Call the Update interface, ctx, in, out is NULL, inLen is 1. Expected result 6 is obtained.
* 7.Call the Update interface, ctx, in, out is NULL, outLen is NULL. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.Failed. Return CRYPT_NULL_INPUT.
* 4.Failed. Return CRYPT_NULL_INPUT.
* 5.Failed. Return CRYPT_NULL_INPUT.
* 6.Failed When is the XTS algorithm.
* 7.Failed. Return CRYPT_NULL_INPUT.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_UPDATE_API_TC001(int id, Hex *key, Hex *iv, Hex *in, int enc)
{
TestMemInit();
int32_t ret;
uint8_t out[BLOCKSIZE * 32] = {0};
uint32_t len = BLOCKSIZE * 32;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(NULL, in->x, in->len, out, &len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(ctx, NULL, in->len, out, &len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, NULL, &len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, 0, out, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, 1, out, &len);
if (id == CRYPT_CIPHER_SM4_XTS) {
ASSERT_TRUE(ret != CRYPT_SUCCESS);
} else {
ASSERT_TRUE(ret == CRYPT_SUCCESS);
}
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, out, NULL);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
len = BLOCKSIZE * 32;
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, out, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(len == in->len);
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_CTRL_API_TC001
* @title Impact of the setting type on the Ctrl setting parameters Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface, iv is IV1. Expected result 2 is obtained.
* 3.Call the Ctrl interface to get iv. Expected result 3 is obtained.
* 4.Call the Update interface to encrypt, inLen is 15. Expected result 4 is obtained.
* 5.Call the Ctrl interface to get iv. Expected result 5 is obtained.
* 6.Call the Update interface to encrypt, inLen is 1. Expected result 6 is obtained.
* 7.Call the Ctrl interface to get iv, record as IV2. Expected result 7 is obtained.
* 8.Call the Update interface to encrypt, inLen is 16. Expected result 8 is obtained.
* 9.Call the Ctrl interface to get iv. Expected result 9 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.Iv value is equal to IV1.
* 4.Success. Return CRYPT_SUCCESS.
* 5.Iv value is equal to IV1.
* 6.Success. Return CRYPT_SUCCESS.
* 7.Iv value is not equal to IV1.
* 8.Success. Return CRYPT_SUCCESS.
* 9.Iv value is not equal to IV2.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_CTRL_API_TC001(Hex *key, Hex *iv, Hex *msg)
{
TestMemInit();
int32_t ret;
uint8_t iv1[BLOCKSIZE] = {0};
uint8_t iv2[BLOCKSIZE] = {0};
const uint32_t len = BLOCKSIZE;
uint8_t out[MAXSIZE] = {0};
uint32_t outlen = MAXSIZE;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_SM4_CBC);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, iv->x, iv->len), CRYPT_MODES_CTRL_TYPE_ERROR);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, iv1, len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(iv1, iv->x, iv->len) == 0);
(void)memset_s(iv1, BLOCKSIZE, 0, BLOCKSIZE);
ret = CRYPT_EAL_CipherUpdate(ctx, msg->x, BLOCKSIZE - 1, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, iv1, len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(iv1, iv->x, iv->len) == 0);
(void)memset_s(iv1, BLOCKSIZE, 0, BLOCKSIZE);
outlen = MAXSIZE;
ret = CRYPT_EAL_CipherUpdate(ctx, msg->x, 1, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, iv1, len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(iv1, iv->x, iv->len) != 0);
outlen = MAXSIZE;
ret = CRYPT_EAL_CipherUpdate(ctx, msg->x, BLOCKSIZE, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, iv2, len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(iv2, iv1, BLOCKSIZE) != 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_CTRL_API_TC003
* @title Impact of input parameters on the CRYPT_EAL_CipherCtrl interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Ctrl interface, ctx is not NULL, type is get iv, other parameters are normal. Expected result 3 is obtained.
* 4.Call the Ctrl interface, ctx is not NULL, type is get blocksize, other parameters are normal. Expected result 4 is obtained.
* 5.Call the Ctrl interface, ctx is not NULL, type is get iv, data is NULL, len is 16. Expected result 5 is obtained.
* 6.Call the Ctrl interface, ctx is not NULL, type is get iv, data is not NULL, len is 0. Expected result 6 is obtained.
* 7.Call the Ctrl interface, ctx is not NULL, type is get blocksize, data is not NULL, len is 0. Expected result 7 is obtained.
* 8.Call the Ctrl interface, ctx is not NULL, type is invalid value. Expected result 8 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.Success. Return CRYPT_SUCCESS.
* 4.Success. Return CRYPT_SUCCESS.
* 5.Failed. Return CRYPT_NULL_INPUT.
* 6.Failed. Return CRYPT_MODE_ERR_INPUT_LEN.
* 7.Failed. Return CRYPT_MODE_ERR_INPUT_LEN.
* 8.Failed. CRYPT_MODES_METHODS_NOT_SUPPORT.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_CTRL_API_TC003(Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
uint8_t *ivGet[BLOCKSIZE] = {0};
const uint32_t len = BLOCKSIZE;
uint32_t blockSizeGet = 0;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_SM4_XTS);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, ivGet, len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, (uint8_t *)&blockSizeGet, sizeof(uint32_t));
ASSERT_TRUE(blockSizeGet == 1);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, NULL, len);
ASSERT_TRUE(ret == CRYPT_NULL_INPUT);
ASSERT_EQ(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, ivGet, 0), CRYPT_MODE_ERR_INPUT_LEN);
ASSERT_EQ(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, (uint8_t *)&blockSizeGet, 0),
CRYPT_MODE_ERR_INPUT_LEN);
ASSERT_EQ(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_MAX, iv->x, iv->len), CRYPT_MODES_CTRL_TYPE_ERROR);
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC001
* @title Call Final interface without call Update interface Test.
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Set the following padding algorithm CRYPT_PADDING_PKCS7 CRYPT_PADDING_PKCS5 CRYPT_PADDING_X923 CRYPT_PADDING_ISO7816 CRYPT_PADDING_ZEROS. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* 5.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 5 is obtained.
* 6.Call the Final interface. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.Succeeded in setting the padding algorithm.
* 4.The ciphertext is consistent with the test vector.
* 5.The update is successful, return CRYPT_SUCCESS.
* 6.The plaintext is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC001(int id, Hex *key, Hex *iv, int padding, int isSetPadding)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAXSIZE] = {0};
uint8_t result[MAXSIZE] = {0};
uint32_t totalLen = 0;
uint32_t decLen = MAXSIZE;
uint32_t len = MAXSIZE;
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ctxEnc = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctxEnc != NULL);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ctxDec = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctxDec != NULL);
ret = CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxDec, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxDec, outTmp, len, result, &decLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += decLen;
decLen = MAXSIZE - totalLen;
ret = CRYPT_EAL_CipherFinal(ctxDec, result + totalLen, &decLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC002
* @title Encryption and decryption with setting padding algorithm Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Set the following padding algorithm CRYPT_PADDING_PKCS7 CRYPT_PADDING_PKCS5 CRYPT_PADDING_X923 CRYPT_PADDING_ISO7816 CRYPT_PADDING_ZEROS. Expected result 3 is obtained.
* 4.Call the Update interface. Expected result 4 is obtained.
* 5.Call the Final interface. Expected result 5 is obtained.
* 6.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 6 is obtained.
* 7.Call the Final interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.Succeeded in setting the padding algorithm.
* 4.The update is successful and return CRYPT_SUCCESS.
* 5.The ciphertext is consistent with the test vector.
* 6.The update is successful and return CRYPT_SUCCESS.
* 7.The plaintext is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC002(int algId, Hex *key, Hex *iv, int inLen, int padding)
{
TestMemInit();
uint8_t input[MAXSIZE] = {0};
uint8_t outTmp[MAXSIZE] = {0};
uint8_t result[MAXSIZE] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAXSIZE;
uint32_t len = MAXSIZE;
(void)memset_s(outTmp, MAXSIZE, 0xAA, MAXSIZE);
(void)memset_s(input, MAXSIZE, 0xAA, MAXSIZE);
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ASSERT_TRUE(inLen <= MAXSIZE);
ctxEnc = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxEnc != NULL);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherSetPadding(ctxEnc, padding), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherUpdate(ctxEnc, input, inLen, outTmp, &len), CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
ASSERT_EQ(CRYPT_EAL_CipherFinal(ctxEnc, outTmp + totalLen, &leftLen), CRYPT_SUCCESS);
totalLen += leftLen;
len = MAXSIZE;
leftLen = MAXSIZE;
ctxDec = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxDec != NULL);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherSetPadding(ctxDec, padding), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len), CRYPT_SUCCESS);
leftLen -= len;
ASSERT_EQ(CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen), CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(input, result, inLen) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC003
* @title Input data of different lengths encryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC003(int isProvider, int id, Hex *key, Hex *plainText, Hex *cipherText, Hex *iv)
{
if (IsSm4AlgDisabled(id)) {
SKIP_TEST();
}
TestMemInit();
uint8_t out[MAXSIZE] = {0};
uint32_t len = plainText->len;
CRYPT_EAL_CipherCtx *ctx = TestCipherNewCtx(NULL, id, "provider=default", isProvider);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherUpdate(ctx, plainText->x, plainText->len, out, &len), CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(out, cipherText->x, len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC004
* @title Input data of different lengths decryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC004(int isProvider, int id, Hex *key, Hex *plainText, Hex *cipherText, Hex *iv)
{
if (IsSm4AlgDisabled(id)) {
SKIP_TEST();
}
TestMemInit();
int32_t ret;
uint8_t out[MAXSIZE] = {0};
uint32_t len = cipherText->len;
CRYPT_EAL_CipherCtx *ctx = TestCipherNewCtx(NULL, id, "provider=default", isProvider);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, cipherText->x, cipherText->len, out, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(out, plainText->x, len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_MULTI_UPDATE_TC001
* @title Multi update encryption and decryption
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface with plaintext for multi times. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* 5.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 5 is obtained.
* 6.Call the Final interface. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The final is successful, return CRYPT_SUCCESS. The cipher result is consistent with the test vector.
* 5.The update is successful, return CRYPT_SUCCESS.
* 6.The final is successful, return CRYPT_SUCCESS. The plain result is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_MULTI_UPDATE_TC001(int algId, Hex *key, Hex *iv, Hex *in, int updateTimes, int padding,
int isSetPadding)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAXSIZE * 4] = {0};
uint8_t result[MAXSIZE * 4] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAXSIZE * 4;
uint32_t len = MAXSIZE * 4;
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ctxEnc = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxEnc != NULL);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
for (int i = 0; i < updateTimes; i++) {
ret = CRYPT_EAL_CipherUpdate(ctxEnc, in->x, in->len, outTmp + totalLen, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
len = leftLen;
}
if (algId != CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp + totalLen, &leftLen);
}
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += leftLen;
len = MAXSIZE * 4;
leftLen = MAXSIZE * 4;
ctxDec = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxDec != NULL);
ret = CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len);
leftLen -= len;
ASSERT_TRUE(ret == CRYPT_SUCCESS);
if (algId != CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen);
}
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(in->x, result, in->len) == 0);
ASSERT_TRUE(memcmp(in->x, result + in->len, in->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_MULTI_UPDATE_TC002
* @title Multi update with different data length in encryption and decryption
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface with plaintext for 5 times. The length of the first, third, and fifth plaintext is 15 bytes.
* The length of the second and fourth plaintexts is 16 bytes. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* 5.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 5 is obtained.
* 6.Call the Final interface. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The final is successful, return CRYPT_SUCCESS. The cipher result is consistent with the test vector.
* 5.The update is successful, return CRYPT_SUCCESS.
* 6.The final is successful, return CRYPT_SUCCESS. The plain result is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_MULTI_UPDATE_TC002(int algId, Hex *key, Hex *iv, Hex *in, int padding, int isSetPadding)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAXSIZE] = {0};
uint8_t result[MAXSIZE] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAXSIZE;
uint32_t len = MAXSIZE;
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ASSERT_TRUE(in->len >= BLOCKSIZE);
ctxEnc = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxEnc != NULL);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
for (uint32_t i = 0; i < 2; i++) { // 15bytes + 16bytes, run two times.
ret = CRYPT_EAL_CipherUpdate(ctxEnc, in->x, BLOCKSIZE - 1, outTmp + totalLen, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
len = leftLen;
ret = CRYPT_EAL_CipherUpdate(ctxEnc, in->x, BLOCKSIZE, outTmp + totalLen, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
len = leftLen;
}
ret = CRYPT_EAL_CipherUpdate(ctxEnc, in->x, BLOCKSIZE - 1, outTmp + totalLen, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
len = leftLen;
if (algId != CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp + totalLen, &leftLen);
}
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += leftLen;
len = MAXSIZE;
leftLen = MAXSIZE;
ctxDec = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxDec != NULL);
ret = CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len);
leftLen -= len;
ASSERT_TRUE(ret == CRYPT_SUCCESS);
if (algId != CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen);
}
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(in->x, result, BLOCKSIZE - 1) == 0);
ASSERT_TRUE(memcmp(in->x, result + BLOCKSIZE - 1, BLOCKSIZE) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_CTRL_API_TC004
* @title Obtaining the IV through the Ctrl interface Test in encryption
* @precon Registering memory-related functions.
* @brief
* 1.Call the init interface to set the IV and call interface to obtain the IV. Expected result 1 is obtained.
* @expect
* 1.The two IVs are consistent.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_CTRL_API_TC004(int id, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
uint8_t niv[BLOCKSIZE] = {0};
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, niv, iv->len);
ASSERT_TRUE(memcmp(niv, iv->x, iv->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_CTRL_API_TC005
* @title Obtaining the IV through the Ctrl interface Test in decryption
* @precon Registering memory-related functions.
* @brief
* 1.Call the init interface to set the IV and call interface to obtain the IV. Expected result 1 is obtained.
* @expect
* 1.The two IVs are consistent.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_CTRL_API_TC005(int id, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
uint8_t niv[BLOCKSIZE] = {0};
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, niv, iv->len);
ASSERT_TRUE(memcmp(niv, iv->x, iv->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_REINIT_API_TC002
* @title Impact of input parameter validity on the iv reset interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface, iv is NULL, iv len is 0. Expected result 2 is obtained.
* 3.Call the Reinit interface, iv is NULL, iv len is 0. Expected result 3 is obtained.
* 4.Call the Reinit interface, iv is not NULL, iv len is 0. Expected result 4 is obtained.
* 5.Call the Reinit interface, iv is not NULL, iv len is 15. Expected result 5 is obtained.
* 6.Call the Reinit interface, iv is not NULL, iv len is 17. Expected result 6 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.The interface returns a failure.
* 4.The interface returns a failure.
* 5.The interface returns a failure.
* 6.The interface returns a failure.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_REINIT_API_TC002(int algId, Hex *key, Hex *iv)
{
TestMemInit();
int32_t ret;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, NULL, 0, true);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherReinit(ctx, NULL, 0);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, 0);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, BLOCKSIZE - 1);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, BLOCKSIZE + 1);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC005
* @title Data encryption and decryption after reinit Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* 4.Call the Reinit interface. Expected result 4 is obtained.
* 5.Call the Update interface. Expected result 5 is obtained.
* 6.Call the Reinit interface. Expected result 6 is obtained.
* 7.Call the Update interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.Success. The ciphertext is as expected.
* 4.The reinit is successful and return CRYPT_SUCCESS.
* 5.Success. The ciphertext is as expected.
* 6.The reinit is successful and return CRYPT_SUCCESS.
* 7.Success. The ciphertext is as expected.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC005(int isProvider, Hex *key, Hex *in1, Hex *out1,
Hex *iv1, Hex *in2, Hex *out2, Hex *iv2, int enc)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAXSIZE] = {0};
uint32_t len = MAXSIZE;
CRYPT_EAL_CipherCtx *ctx = TestCipherNewCtx(NULL, CRYPT_CIPHER_SM4_XTS, "provider=default", isProvider);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv1->x, iv1->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in1->x, in1->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, out1->x, out1->len) == 0);
(void)memset_s(outTmp, MAXSIZE, 0, MAXSIZE);
len = MAXSIZE;
ret = CRYPT_EAL_CipherReinit(ctx, iv2->x, iv2->len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in2->x, in2->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, out2->x, out2->len) == 0);
(void)memset_s(outTmp, MAXSIZE, 0, MAXSIZE);
len = MAXSIZE;
ret = CRYPT_EAL_CipherReinit(ctx, iv1->x, iv1->len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in1->x, in1->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, out1->x, out1->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
typedef struct {
int algId;
uint8_t *key;
uint32_t keyLen;
uint8_t *iv;
uint32_t ivLen;
uint8_t *in;
uint32_t inLen;
uint8_t *out;
uint32_t outLen;
int enc;
} TestVector;
void SM4_MultiThreadTest(void *arg)
{
TestVector *pTestVector = (TestVector *)arg;
int32_t ret;
uint8_t outTmp[MAXSIZE] = {0};
uint32_t len = MAXSIZE;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(pTestVector->algId);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, pTestVector->key, pTestVector->keyLen, pTestVector->iv, pTestVector->ivLen,
pTestVector->enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, pTestVector->in, pTestVector->inLen, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, pTestVector->out, pTestVector->outLen) == 0);
EXIT:
CRYPT_EAL_CipherDeinit(ctx);
CRYPT_EAL_CipherFreeCtx(ctx);
}
/**
* @test SDV_CRYPTO_SM4_MULTI_THREAD_TC001
* @title Multi-thread Test
* @precon Registering memory-related functions.
* @brief
* 1.Start three threads. Expected result 1 is obtained.
* 2.Call the eal interface in the thread for encryption. Expected result 2 is obtained.
* 3.Call the eal interface in the thread for decryption. Expected result 2 is obtained.
* @expect
* 1.Success.
* 2.The encryption is successful. The ciphertext and tag are the same as the vector.
* 3.The decryption is successful. The plaintext and tag are consistent with the vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_MULTI_THREAD_TC001(int algId, Hex *key, Hex *in, Hex *out, Hex *iv, int enc)
{
#define THREAD_NUM 3
TestMemInit();
int32_t ret;
pthread_t thrd[THREAD_NUM];
TestVector testVt = {
.algId = algId,
.key = key->x,
.keyLen = key->len,
.iv = iv->x,
.ivLen = iv->len,
.in = in->x,
.inLen = in->len,
.out = out->x,
.outLen = out->len,
.enc = enc
};
for (uint32_t i = 0; i < THREAD_NUM; i++) {
ret = pthread_create(&thrd[i], NULL, (void *)SM4_MultiThreadTest, &testVt);
ASSERT_TRUE(ret == 0);
}
for (uint32_t i = 0; i < THREAD_NUM; i++) {
pthread_join(thrd[i], NULL);
}
EXIT:
return;
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC006
* @title Impact of the msg with no padding on the Update interface Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface with plain len is 17. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful and return CRYPT_SUCCESS.
* 3.The Update is successful.
* 4.The Final is failed.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC006(int id, Hex *key, Hex *iv, Hex *msg, int enc)
{
TestMemInit();
int32_t ret;
uint8_t out[MAX_OUTPUT] = {0};
uint32_t outlen = MAX_OUTPUT;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(msg->len == BLOCKSIZE + 1);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, msg->x, msg->len, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
outlen = MAX_OUTPUT;
ret = CRYPT_EAL_CipherFinal(ctx, out, &outlen);
ASSERT_TRUE(ret != CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC007
* @title Update 0 message length Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface, data is NULL, dataLen is 0. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The Update is successful.
* 4.The outlen is consistent with expect.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC007(int id, Hex *key, Hex *iv, int enc)
{
TestMemInit();
int32_t ret;
uint8_t out[MAX_OUTPUT] = {0};
uint32_t outlen = MAX_OUTPUT;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, NULL, 0, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
if (id != CRYPT_CIPHER_SM4_GCM) {
outlen = MAX_OUTPUT;
ret = CRYPT_EAL_CipherFinal(ctx, out, &outlen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
}
ASSERT_TRUE(outlen == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC008
* @title Impact of updating IV on encryption and decryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* 5.Call the Reinit interface to update iv. Expected result 5 is obtained.
* 6.Call the Update interface. Expected result 6 is obtained.
* 7.Call the Final interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The final is successful, return CRYPT_SUCCESS. The result is consistent with the test vector.
* 5.The reinit is successful, return CRYPT_SUCCESS.
* 6.The update is successful, return CRYPT_SUCCESS.
* 7.The final is successful, return CRYPT_SUCCESS. The result is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC008(int isProvider, int algId, Hex *key, Hex *iv, Hex *in, Hex *out, int enc)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAX_OUTPUT] = {0};
uint32_t len = MAX_OUTPUT;
uint32_t finLen;
CRYPT_EAL_CipherCtx *ctx = TestCipherNewCtx(NULL, algId, "provider=default", isProvider);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
finLen = MAX_OUTPUT - len;
ret = Sm4CipherFinal(algId, ctx, outTmp + len, &finLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_COMPARE("Cipher compare", out->x, out->len, outTmp, len + finLen);
(void)memset_s(outTmp, MAX_OUTPUT, 0, MAX_OUTPUT);
len = MAX_OUTPUT;
ret = CRYPT_EAL_CipherReinit(ctx, iv->x, iv->len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
finLen = MAX_OUTPUT - len;
ret = Sm4CipherFinal(algId, ctx, outTmp + len, &finLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_COMPARE("Cipher compare", out->x, out->len, outTmp, len + finLen);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC009
* @title Impact of updating IV and Key on encryption and decryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* 5.Call the Init interface to update iv and key. Expected result 5 is obtained.
* 6.Call the Update interface. Expected result 6 is obtained.
* 7.Call the Final interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The final is successful, return CRYPT_SUCCESS. The result is consistent with the test vector.
* 5.The Init is successful, return CRYPT_SUCCESS.
* 6.The update is successful, return CRYPT_SUCCESS.
* 7.The final is successful, return CRYPT_SUCCESS. The result is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC009(int isProvider, int algId, Hex *key, Hex *iv, Hex *in, Hex *out, int enc)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAX_OUTPUT] = {0};
uint32_t len = MAX_OUTPUT;
uint32_t finLen;
CRYPT_EAL_CipherCtx *ctx = TestCipherNewCtx(NULL, algId, "provider=default", isProvider);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
finLen = MAX_OUTPUT - len;
ret = Sm4CipherFinal(algId, ctx, outTmp + len, &finLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, out->x, out->len) == 0);
(void)memset_s(outTmp, MAX_OUTPUT, 0, MAX_OUTPUT);
len = MAX_OUTPUT;
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
finLen = MAX_OUTPUT - len;
ret = Sm4CipherFinal(algId, ctx, outTmp + len, &finLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(outTmp, out->x, out->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC010
* @title Impact of the padding algorithm on encryption and decryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the set padding interface with CRYPT_PADDING_PKCS7, CRYPT_PADDING_PKCS5, CRYPT_PADDING_X923
* CRYPT_PADDING_ISO7816, CRYPT_PADDING_ZEROS, CRYPT_PADDING_NONE. Expected result 3 is obtained.
* 4.Call the Update interface. Expected result 4 is obtained.
* 5.Call the Final interface. Expected result 5 is obtained.
* 6.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 6 is obtained.
* 7.Call the Final interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The setting is successful, return CRYPT_SUCCESS.
* 4.The update is successful, return CRYPT_SUCCESS.
* 5.The final is successful, return CRYPT_SUCCESS.
* 6.The update is successful, return CRYPT_SUCCESS.
* 7.The final is successful, and the plaintext is consistent with the origin data.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC010(int algId, Hex *key, Hex *iv, int inLen, int padding)
{
TestMemInit();
int32_t ret;
uint8_t input[MAX_DATASZIE] = {0};
uint8_t outTmp[MAX_DATASZIE] = {0};
uint8_t result[MAX_DATASZIE] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAX_DATASZIE;
uint32_t len = MAX_DATASZIE;
(void)memset_s(outTmp, MAX_DATASZIE, 0xAA, MAX_DATASZIE);
(void)memset_s(input, MAX_DATASZIE, 0xAA, MAX_DATASZIE);
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ASSERT_TRUE(inLen <= MAX_DATASZIE);
ctxEnc = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxEnc != NULL);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherSetPadding(ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxEnc, input, inLen, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp + totalLen, &leftLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += leftLen;
len = MAX_DATASZIE;
leftLen = MAX_DATASZIE;
ctxDec = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxDec != NULL);
ret = CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherSetPadding(ctxDec, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
leftLen -= len;
ret = CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(input, result, inLen) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC011
* @title The input and output start addresses are the same Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call the set padding interface. Expected result 3 is obtained.
* 4.Call the Update interface with the input and output buff are same. Expected result 4 is obtained.
* 5.Call the Final interface. Expected result 5 is obtained.
* 6.Use the SM4 decryption handle to call the Update interface with the ciphertext. Expected result 6 is obtained.
* 7.Call the Final interface. Expected result 7 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The setting is successful, return CRYPT_SUCCESS.
* 4.The update is successful, return CRYPT_SUCCESS.
* 5.The final is successful, return CRYPT_SUCCESS.
* 6.The update is successful, return CRYPT_SUCCESS.
* 7.The final is successful, and the plaintext is consistent with the origin data.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC011(int algId, Hex *key, Hex *iv, int inLen, int padding, int isSetPadding)
{
TestMemInit();
int32_t ret;
uint8_t input[MAX_DATASZIE] = {0};
uint8_t outTmp[MAX_DATASZIE] = {0};
uint8_t result[MAX_DATASZIE] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAX_DATASZIE;
uint32_t len = MAX_DATASZIE;
(void)memset_s(outTmp, MAX_DATASZIE, 0xAA, MAX_DATASZIE);
(void)memset_s(input, MAX_DATASZIE, 0xAA, MAX_DATASZIE);
CRYPT_EAL_CipherCtx *ctxEnc = NULL;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
ASSERT_TRUE(inLen <= MAX_DATASZIE);
ctxEnc = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxEnc != NULL);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxEnc, outTmp, inLen, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen -= len;
if (algId != CRYPT_CIPHER_SM4_GCM) {
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp + totalLen, &leftLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += leftLen;
}
len = MAX_OUTPUT;
leftLen = MAX_OUTPUT;
ctxDec = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctxDec != NULL);
ret = CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = SetPadding(isSetPadding, ctxDec, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
leftLen -= len;
ret = Sm4CipherFinal(algId, ctxDec, result + len, &leftLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ASSERT_TRUE(memcmp(input, result, inLen) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC012
* @title Impact of the key and iv of all 0s/all Fs on encryption and decryption Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface, with key and iv of all 0s/all Fs. Expected result 2 is obtained.
* 3.Call the Update interface. Expected result 3 is obtained.
* 4.Call the Final interface. Expected result 4 is obtained.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The final is successful, and the result is consistent with expect.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC012(int algId, Hex *key, Hex *iv, Hex *in, Hex *out, int enc)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAX_OUTPUT] = {0};
uint32_t len = MAX_OUTPUT;
uint32_t totalLen = 0;
CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(algId);
ASSERT_TRUE(ctx != NULL);
ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
len = MAX_OUTPUT - len;
if (algId != CRYPT_CIPHER_SM4_GCM){
ret = CRYPT_EAL_CipherFinal(ctx, outTmp + totalLen, &len);
totalLen += len;
ASSERT_TRUE(totalLen == out->len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
}
ASSERT_TRUE(memcmp(outTmp, out->x, out->len) == 0);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC013
* @title SM4-GCM encryption full vector test
* @precon Registering memory-related functions.
* @brief
* 1.Call the Init interface. Expected result 1 is obtained.
* 2.Call the Ctrl interface to set parameters. Expected result 2 is obtained.
* 3.Call the update interface to update message. Expected result 3 is obtained.
* 4.Call the Ctrl interface to get tag. Expected result 4 is obtained.
* 5.Compare the ciphertext data. Expected result 5 is obtained.
* 6.Compare the tag data. Expected result 6 is obtained.
* @expect
* 1.The init is successful, return CRYPT_SUCCESS.
* 2.The setting is successful, return CRYPT_SUCCESS.
* 3.The update is successful, return CRYPT_SUCCESS.
* 4.The getting is successful, return CRYPT_SUCCESS.
* 5.Ciphertext is consistent with the test vector.
* 6.Tag is consistent with the test vector.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC013(Hex *key, Hex *iv, Hex *aad, Hex *pt, Hex *ct, Hex *tag, int enc)
{
TestMemInit();
CRYPT_EAL_CipherCtx *ctx = NULL;
uint8_t *outTag = NULL;
uint8_t *out = NULL;
uint32_t tagLen = tag->len;
uint32_t outLen;
if (ct->len > 0) {
out = (uint8_t *)BSL_SAL_Malloc(ct->len * sizeof(uint8_t));
outLen = ct->len * sizeof(uint8_t);
ASSERT_TRUE(out != NULL);
} else {
out = (uint8_t *)BSL_SAL_Malloc(1 * sizeof(uint8_t));
outLen = 1 * sizeof(uint8_t);
ASSERT_TRUE(out != NULL);
}
ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_SM4_GCM);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, enc) == CRYPT_SUCCESS);
ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) == CRYPT_SUCCESS);
ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, aad->x, aad->len) == CRYPT_SUCCESS);
ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, pt->x, pt->len, (uint8_t *)out, &outLen) == CRYPT_SUCCESS);
outTag = (uint8_t *)BSL_SAL_Malloc(sizeof(uint8_t) * tagLen);
ASSERT_TRUE(outTag != NULL);
ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, (uint8_t *)outTag, tagLen) == CRYPT_SUCCESS);
if (ct->x != NULL) {
ASSERT_TRUE(memcmp(out, ct->x, ct->len) == 0);
}
ASSERT_COMPARE("Compare Tag", outTag, tagLen, tag->x, tag->len);
EXIT:
CRYPT_EAL_CipherFreeCtx(ctx);
free(out);
free(outTag);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC014
* @title Encryption in different padding modes Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call SetPadding interface setting padding mode to CRYPT_PADDING_ISO7816,
* CRYPT_PADDING_X923, CRYPT_PADDING_PKCS7. Expected result 3 is obtained.
* 4.Call the Update interface. Expected result 4 is obtained.
* 5.Call the Final interface. Expected result 5 is obtained.
* 6.Call the init, update, and final interfaces to decrypt and verifiy the result.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The setting is successful, return CRYPT_SUCCESS.
* 4.The update is successful, return CRYPT_SUCCESS.
* 5.The final is successful, return CRYPT_SUCCESS.
* 6.The verification is successful, return CRYPT_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC014(int isProvider, int algId, Hex *key, Hex *iv, Hex *in, Hex *out, int padding)
{
TestMemInit();
int32_t ret;
uint8_t outTmp[MAX_OUTPUT] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAX_OUTPUT;
uint32_t len = MAX_OUTPUT;
CRYPT_EAL_CipherCtx *ctxEnc = TestCipherNewCtx(NULL, algId, "provider=default", isProvider);
ASSERT_TRUE(ctxEnc != NULL);
ret = CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherSetPadding(ctxEnc, padding);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
ret = CRYPT_EAL_CipherUpdate(ctxEnc, in->x, in->len, outTmp, &len);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += len;
leftLen = leftLen - len;
ret = CRYPT_EAL_CipherFinal(ctxEnc, outTmp + len, &leftLen);
ASSERT_TRUE(ret == CRYPT_SUCCESS);
totalLen += leftLen;
ASSERT_TRUE(totalLen == out->len);
ASSERT_TRUE(memcmp(out->x, outTmp, out->len) == 0);
EXIT:
CRYPT_EAL_CipherDeinit(ctxEnc);
CRYPT_EAL_CipherFreeCtx(ctxEnc);
}
/* END_CASE */
/**
* @test SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC015
* @title Decryption in different padding modes Test
* @precon Registering memory-related functions.
* @brief
* 1.Create the context ctx. Expected result 1 is obtained.
* 2.Call the Init interface. Expected result 2 is obtained.
* 3.Call SetPadding interface setting padding mode to CRYPT_PADDING_ISO7816,
* CRYPT_PADDING_X923, CRYPT_PADDING_PKCS7. Expected result 3 is obtained.
* 4.Call the Update interface. Expected result 4 is obtained.
* 5.Call the Final interface. Expected result 5 is obtained.
* 6.Call the init, update, and final interfaces to decrypt and verifiy the result.
* @expect
* 1.The creation is successful and the ctx is not empty.
* 2.The init is successful, return CRYPT_SUCCESS.
* 3.The setting is successful, return CRYPT_SUCCESS.
* 4.The update is successful, return CRYPT_SUCCESS.
* 5.The final is successful, return CRYPT_SUCCESS.
* 6.The verification is successful, return CRYPT_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_CRYPTO_SM4_ENCRYPT_FUNC_TC015(int isProvider, int algId, Hex *key, Hex *iv, Hex *in, Hex *out, int padding)
{
TestMemInit();
uint8_t result[MAX_OUTPUT] = {0};
uint32_t totalLen = 0;
uint32_t leftLen = MAX_OUTPUT;
uint32_t len = MAX_OUTPUT;
CRYPT_EAL_CipherCtx *ctxDec = NULL;
len = MAX_OUTPUT;
leftLen = MAX_OUTPUT;
ctxDec = TestCipherNewCtx(NULL, algId, "provider=default", isProvider);
ASSERT_TRUE(ctxDec != NULL);
ASSERT_EQ(CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherSetPadding(ctxDec, padding), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_CipherUpdate(ctxDec, in->x, in->len, result, &len), CRYPT_SUCCESS);
totalLen += len;
leftLen = leftLen - len;
ASSERT_EQ(CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen), CRYPT_SUCCESS);
totalLen += leftLen;
ASSERT_TRUE(totalLen == out->len);
ASSERT_TRUE(memcmp(out->x, result, out->len) == 0);
EXIT:
CRYPT_EAL_CipherDeinit(ctxDec);
CRYPT_EAL_CipherFreeCtx(ctxDec);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/crypto/sm4/test_suite_sdv_eal_sm4.c | C | unknown | 65,643 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "securec.h"
#include "bsl_err.h"
#include "bsl_sal.h"
#include "crypt_errno.h"
#include "crypt_algid.h"
#include "crypt_eal_pkey.h"
#include "crypt_util_rand.h"
#include "crypt_bn.h"
#include "eal_pkey_local.h"
#include "stub_replace.h"
#include "test.h"
/* END_HEADER */
uint32_t g_stubRandCounter = 0;
uint8_t **g_stubRand = NULL;
uint32_t *g_stubRandLen = NULL;
void RandInjectionInit()
{
g_stubRandCounter = 0;
g_stubRand = NULL;
g_stubRandLen = NULL;
}
void RandInjectionSet(uint8_t **rand, uint32_t *len)
{
g_stubRand = rand;
g_stubRandLen = len;
}
int32_t RandInjection(uint8_t *rand, uint32_t randLen)
{
(void)memcpy_s(rand, randLen, g_stubRand[g_stubRandCounter], randLen);
g_stubRandCounter++;
return CRYPT_SUCCESS;
}
int32_t RandInjectionEx(void *libCtx, uint8_t *rand, uint32_t randLen)
{
(void)libCtx;
return RandInjection(rand, randLen);
}
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_API_NEW_TC001(void)
{
TestMemInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_GENKEY_TC001(int isProvider)
{
TestMemInit();
TestRandInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
#ifdef HITLS_CRYPTO_PROVIDER
if (isProvider == 1) {
pkey = CRYPT_EAL_ProviderPkeyNewCtx(NULL, CRYPT_PKEY_XMSS, CRYPT_EAL_PKEY_SIGN_OPERATE, "provider=default");
} else
#endif
{
(void)isProvider;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
}
ASSERT_TRUE(pkey != NULL);
ASSERT_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_PARA_BY_ID, NULL, 0) == CRYPT_INVALID_ARG);
ASSERT_TRUE(CRYPT_EAL_PkeyGen(pkey) == CRYPT_XMSS_ERR_INVALID_ALGID);
CRYPT_PKEY_ParaId algId = CRYPT_XMSS_SHA2_10_256;
ASSERT_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_PARA_BY_ID, (void *)&algId, sizeof(algId)) ==
CRYPT_SUCCESS);
ASSERT_TRUE(CRYPT_EAL_PkeyGen(pkey) == CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_GETSET_KEY_TC001(void)
{
TestMemInit();
TestRandInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
int32_t algId = CRYPT_XMSS_SHA2_10_256;
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, algId), CRYPT_SUCCESS);
CRYPT_EAL_PkeyPub pub;
uint8_t pubSeed[32] = {0};
uint8_t pubRoot[32] = {0};
(void)memset_s(&pub, sizeof(CRYPT_EAL_PkeyPub), 0, sizeof(CRYPT_EAL_PkeyPub));
pub.id = CRYPT_PKEY_XMSS;
ASSERT_EQ(CRYPT_EAL_PkeyGetPub(pkey, &pub), CRYPT_NULL_INPUT);
ASSERT_EQ(CRYPT_EAL_PkeySetPub(pkey, &pub), CRYPT_NULL_INPUT);
pub.key.xmssPub.seed = pubSeed;
pub.key.xmssPub.root = pubRoot;
pub.key.xmssPub.len = 16;
ASSERT_EQ(CRYPT_EAL_PkeyGetPub(pkey, &pub), CRYPT_XMSS_ERR_INVALID_KEYLEN);
ASSERT_EQ(CRYPT_EAL_PkeySetPub(pkey, &pub), CRYPT_XMSS_ERR_INVALID_KEYLEN);
CRYPT_EAL_PkeyPrv prv;
uint8_t prvSeed[32] = {0};
uint8_t prvPrf[32] = {0};
uint64_t index = 0;
(void)memset_s(&prv, sizeof(CRYPT_EAL_PkeyPrv), 0, sizeof(CRYPT_EAL_PkeyPrv));
prv.id = CRYPT_PKEY_XMSS;
ASSERT_EQ(CRYPT_EAL_PkeyGetPrv(pkey, &prv), CRYPT_NULL_INPUT);
ASSERT_EQ(CRYPT_EAL_PkeySetPrv(pkey, &prv), CRYPT_NULL_INPUT);
prv.key.xmssPrv.index = index;
prv.key.xmssPrv.seed = prvSeed;
prv.key.xmssPrv.prf = prvPrf;
prv.key.xmssPrv.pub.seed = pubSeed;
prv.key.xmssPrv.pub.root = pubRoot;
prv.key.xmssPrv.pub.len = 16;
ASSERT_EQ(CRYPT_EAL_PkeyGetPrv(pkey, &prv), CRYPT_XMSS_ERR_INVALID_KEYLEN);
ASSERT_EQ(CRYPT_EAL_PkeySetPrv(pkey, &prv), CRYPT_XMSS_ERR_INVALID_KEYLEN);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_GETSET_KEY_TC002(void)
{
TestMemInit();
TestRandInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
int32_t algId = CRYPT_XMSS_SHA2_10_256;
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, algId), CRYPT_SUCCESS);
ASSERT_TRUE(CRYPT_EAL_PkeyGen(pkey) == CRYPT_SUCCESS);
CRYPT_EAL_PkeyPub pub;
uint8_t pubSeed[32] = {0};
uint8_t pubRoot[32] = {0};
(void)memset_s(&pub, sizeof(CRYPT_EAL_PkeyPub), 0, sizeof(CRYPT_EAL_PkeyPub));
pub.id = CRYPT_PKEY_XMSS;
pub.key.xmssPub.seed = pubSeed;
pub.key.xmssPub.root = pubRoot;
pub.key.xmssPub.len = 32;
ASSERT_EQ(CRYPT_EAL_PkeyGetPub(pkey, &pub), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeySetPub(pkey, &pub), CRYPT_SUCCESS);
CRYPT_EAL_PkeyPrv prv;
uint8_t prvSeed[32] = {0};
uint8_t prvPrf[32] = {0};
uint64_t index = 0;
(void)memset_s(&prv, sizeof(CRYPT_EAL_PkeyPrv), 0, sizeof(CRYPT_EAL_PkeyPrv));
prv.id = CRYPT_PKEY_XMSS;
prv.key.xmssPrv.index = index;
prv.key.xmssPrv.seed = prvSeed;
prv.key.xmssPrv.prf = prvPrf;
prv.key.xmssPrv.pub.seed = pubSeed;
prv.key.xmssPrv.pub.root = pubRoot;
prv.key.xmssPrv.pub.len = 32;
ASSERT_EQ(CRYPT_EAL_PkeyGetPrv(pkey, &prv), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeySetPrv(pkey, &prv), CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_GENKEY_KAT_TC001(int id, Hex *key, Hex *root)
{
TestMemInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
int32_t algId = id;
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, algId), CRYPT_SUCCESS);
uint32_t keyLen = 0;
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_GET_XMSS_KEY_LEN, (void *)&keyLen, sizeof(keyLen)), CRYPT_SUCCESS);
RandInjectionInit();
uint8_t *stubRand[3] = {key->x, key->x + keyLen, key->x + keyLen * 2};
uint32_t stubRandLen[3] = {keyLen, keyLen, keyLen};
RandInjectionSet(stubRand, stubRandLen);
CRYPT_RandRegist(RandInjection);
CRYPT_RandRegistEx(RandInjectionEx);
ASSERT_TRUE(CRYPT_EAL_PkeyGen(pkey) == CRYPT_SUCCESS);
uint8_t pubSeed[64] = {0};
uint8_t pubRoot[64] = {0};
CRYPT_EAL_PkeyPub pubOut;
(void)memset_s(&pubOut, sizeof(CRYPT_EAL_PkeyPub), 0, sizeof(CRYPT_EAL_PkeyPub));
pubOut.id = CRYPT_PKEY_XMSS;
pubOut.key.xmssPub.seed = pubSeed;
pubOut.key.xmssPub.root = pubRoot;
pubOut.key.xmssPub.len = keyLen;
ASSERT_EQ(CRYPT_EAL_PkeyGetPub(pkey, &pubOut), CRYPT_SUCCESS);
ASSERT_EQ(memcmp(pubOut.key.xmssPub.seed, root->x, keyLen), 0);
ASSERT_EQ(memcmp(pubOut.key.xmssPub.root, root->x + keyLen, keyLen), 0);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_SIGN_KAT_TC001(int id, int index, Hex *key, Hex *msg, Hex *sig, int result)
{
(void)key;
(void)msg;
(void)sig;
TestMemInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
int32_t algId = id;
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, algId), CRYPT_SUCCESS);
uint32_t keyLen = 0;
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_GET_XMSS_KEY_LEN, (void *)&keyLen, sizeof(keyLen)), CRYPT_SUCCESS);
CRYPT_EAL_PkeyPrv prv;
(void)memset_s(&prv, sizeof(CRYPT_EAL_PkeyPrv), 0, sizeof(CRYPT_EAL_PkeyPrv));
prv.id = CRYPT_PKEY_XMSS;
prv.key.xmssPrv.index = index;
prv.key.xmssPrv.seed = key->x;
prv.key.xmssPrv.prf = key->x + keyLen;
prv.key.xmssPrv.pub.seed = key->x + keyLen * 2;
prv.key.xmssPrv.pub.root = key->x + keyLen * 3;
prv.key.xmssPrv.pub.len = keyLen;
ASSERT_EQ(CRYPT_EAL_PkeySetPrv(pkey, &prv), CRYPT_SUCCESS);
uint8_t sigOut[50000] = {0};
uint32_t sigOutLen = sizeof(sigOut);
ASSERT_EQ(CRYPT_EAL_PkeySign(pkey, 0, msg->x, msg->len, sigOut, &sigOutLen), result);
if (result == CRYPT_SUCCESS) {
ASSERT_TRUE(sigOutLen == sig->len);
ASSERT_TRUE(memcmp(sigOut, sig->x, sigOutLen) == 0);
}
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPTO_XMSS_VERIFY_KAT_TC001(int id, Hex *key, Hex *msg, Hex *sig, int result)
{
TestMemInit();
CRYPT_EAL_PkeyCtx *pkey = NULL;
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_XMSS);
ASSERT_TRUE(pkey != NULL);
int32_t algId = id;
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, algId), CRYPT_SUCCESS);
uint32_t keyLen = 0;
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_GET_XMSS_KEY_LEN, (void *)&keyLen, sizeof(keyLen)), CRYPT_SUCCESS);
CRYPT_EAL_PkeyPub pub;
(void)memset_s(&pub, sizeof(CRYPT_EAL_PkeyPub), 0, sizeof(CRYPT_EAL_PkeyPub));
pub.id = CRYPT_PKEY_XMSS;
pub.key.xmssPub.seed = key->x;
pub.key.xmssPub.root = key->x + keyLen;
pub.key.xmssPub.len = keyLen;
ASSERT_EQ(CRYPT_EAL_PkeySetPub(pkey, &pub), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeyVerify(pkey, 0, msg->x, msg->len, sig->x, sig->len), result);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return;
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/crypto/xmss/test_suite_sdv_eal_xmss.c | C | unknown | 9,685 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "stub_replace.h"
#include "hitls_pki_cert.h"
#include "hitls_pki_csr.h"
#include "hitls_pki_errno.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "hitls_cert_local.h"
#include "bsl_init.h"
#include "bsl_obj_internal.h"
#include "sal_time.h"
#include "sal_file.h"
#include "crypt_encode_decode_key.h"
#include "crypt_eal_codecs.h"
#include "hitls_x509_local.h"
#include "stub_replace.h"
/* END_HEADER */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_FUNC_TC001(int format, char *path)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = NULL;
int32_t ret = HITLS_X509_CertParseFile(format, path, &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_VERSION_FUNC_TC001(char *path, int version)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.version, version);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SERIALNUM_FUNC_TC001(char *path, Hex *serialNum)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.serialNum.tag, 2);
ASSERT_COMPARE("serialNum", cert->tbs.serialNum.buff, cert->tbs.serialNum.len,
serialNum->x, serialNum->len);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_TBS_SIGNALG_FUNC_TC001(char *path, int signAlg,
int rsaPssHash, int rsaPssMgf1, int rsaPssSaltLen)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.signAlgId.algId, signAlg);
ASSERT_EQ(cert->tbs.signAlgId.rsaPssParam.mdId, rsaPssHash);
ASSERT_EQ(cert->tbs.signAlgId.rsaPssParam.mgfId, rsaPssMgf1);
ASSERT_EQ(cert->tbs.signAlgId.rsaPssParam.saltLen, rsaPssSaltLen);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_ISSUERNAME_FUNC_TC001(char *path, int count,
Hex *type1, int tag1, Hex *value1,
Hex *type2, int tag2, Hex *value2,
Hex *type3, int tag3, Hex *value3,
Hex *type4, int tag4, Hex *value4,
Hex *type5, int tag5, Hex *value5,
Hex *type6, int tag6, Hex *value6)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x},
{6, type2->len, type2->x}, {(uint8_t)tag2, value2->len, value2->x},
{6, type3->len, type3->x}, {(uint8_t)tag3, value3->len, value3->x},
{6, type4->len, type4->x}, {(uint8_t)tag4, value4->len, value4->x},
{6, type5->len, type5->x}, {(uint8_t)tag5, value5->len, value5->x},
{6, type6->len, type6->x}, {(uint8_t)tag6, value6->len, value6->x},
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.issuerName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.issuerName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_ISSUERNAME_FUNC_TC002(char *path, int count,
Hex *type1, int tag1, Hex *value1)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x}
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.issuerName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.issuerName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_ISSUERNAME_FUNC_TC003(char *path, int count,
Hex *type1, int tag1, Hex *value1,
Hex *type2, int tag2, Hex *value2,
Hex *type3, int tag3, Hex *value3,
Hex *type4, int tag4, Hex *value4,
Hex *type5, int tag5, Hex *value5)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x},
{6, type2->len, type2->x}, {(uint8_t)tag2, value2->len, value2->x},
{6, type3->len, type3->x}, {(uint8_t)tag3, value3->len, value3->x},
{6, type4->len, type4->x}, {(uint8_t)tag4, value4->len, value4->x},
{6, type5->len, type5->x}, {(uint8_t)tag5, value5->len, value5->x}
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.issuerName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.issuerName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.issuerName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_TIME_FUNC_TC001(char *path)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_X509_ERR_CHECK_TAG);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_START_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.validTime.start.year, year);
ASSERT_EQ(cert->tbs.validTime.start.month, month);
ASSERT_EQ(cert->tbs.validTime.start.day, day);
ASSERT_EQ(cert->tbs.validTime.start.hour, hour);
ASSERT_EQ(cert->tbs.validTime.start.minute, minute);
ASSERT_EQ(cert->tbs.validTime.start.second, second);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERTPEM_PARSE_START_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_PEM, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.validTime.start.year, year);
ASSERT_EQ(cert->tbs.validTime.start.month, month);
ASSERT_EQ(cert->tbs.validTime.start.day, day);
ASSERT_EQ(cert->tbs.validTime.start.hour, hour);
ASSERT_EQ(cert->tbs.validTime.start.minute, minute);
ASSERT_EQ(cert->tbs.validTime.start.second, second);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERTPEM_PARSE_END_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_PEM, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.validTime.end.year, year);
ASSERT_EQ(cert->tbs.validTime.end.month, month);
ASSERT_EQ(cert->tbs.validTime.end.day, day);
ASSERT_EQ(cert->tbs.validTime.end.hour, hour);
ASSERT_EQ(cert->tbs.validTime.end.minute, minute);
ASSERT_EQ(cert->tbs.validTime.end.second, second);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_END_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.validTime.end.year, year);
ASSERT_EQ(cert->tbs.validTime.end.month, month);
ASSERT_EQ(cert->tbs.validTime.end.day, day);
ASSERT_EQ(cert->tbs.validTime.end.hour, hour);
ASSERT_EQ(cert->tbs.validTime.end.minute, minute);
ASSERT_EQ(cert->tbs.validTime.end.second, second);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SUBJECTNAME_FUNC_TC001(char *path, int count,
Hex *type1, int tag1, Hex *value1,
Hex *type2, int tag2, Hex *value2,
Hex *type3, int tag3, Hex *value3,
Hex *type4, int tag4, Hex *value4,
Hex *type5, int tag5, Hex *value5,
Hex *type6, int tag6, Hex *value6)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x},
{6, type2->len, type2->x}, {(uint8_t)tag2, value2->len, value2->x},
{6, type3->len, type3->x}, {(uint8_t)tag3, value3->len, value3->x},
{6, type4->len, type4->x}, {(uint8_t)tag4, value4->len, value4->x},
{6, type5->len, type5->x}, {(uint8_t)tag5, value5->len, value5->x},
{6, type6->len, type6->x}, {(uint8_t)tag6, value6->len, value6->x},
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.subjectName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.subjectName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SUBJECTNAME_FUNC_TC002(char *path, int count,
Hex *type1, int tag1, Hex *value1)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x}
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.subjectName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.subjectName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SUBJECTNAME_FUNC_TC003(char *path, int count,
Hex *type1, int tag1, Hex *value1,
Hex *type2, int tag2, Hex *value2,
Hex *type3, int tag3, Hex *value3,
Hex *type4, int tag4, Hex *value4,
Hex *type5, int tag5, Hex *value5)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x},
{6, type2->len, type2->x}, {(uint8_t)tag2, value2->len, value2->x},
{6, type3->len, type3->x}, {(uint8_t)tag3, value3->len, value3->x},
{6, type4->len, type4->x}, {(uint8_t)tag4, value4->len, value4->x},
{6, type5->len, type5->x}, {(uint8_t)tag5, value5->len, value5->x}
};
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.subjectName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(cert->tbs.subjectName);
for (int i = 0; i < count; i += 2) {
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(cert->tbs.subjectName);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_CTRL_FUNC_TC001(char *path, int expRawDataLen, int expSignAlg, int expSignMdAlg,
int expKuDigitailSign, int expKuCertSign, int expKuKeyAgreement, int expKeyUsage)
{
HITLS_X509_Cert *cert = NULL;
uint32_t keyUsage = 0;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
int32_t rawDataLen;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ENCODELEN, &rawDataLen, sizeof(rawDataLen)), HITLS_PKI_SUCCESS);
ASSERT_EQ(rawDataLen, expRawDataLen);
uint8_t *rawData = NULL;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ENCODE, &rawData, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(rawData, NULL);
void *ealKey = NULL;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_PUBKEY, &ealKey, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(ealKey, NULL);
CRYPT_EAL_PkeyFreeCtx(ealKey);
int32_t alg = 0;
int32_t mdAlg = 0;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SIGNALG, &alg, sizeof(alg)), HITLS_PKI_SUCCESS);
ASSERT_EQ(alg, expSignAlg);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SIGN_MDALG, &mdAlg, sizeof(mdAlg) - 1), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SIGN_MDALG, &mdAlg, sizeof(mdAlg)), HITLS_PKI_SUCCESS);
ASSERT_EQ(mdAlg, expSignMdAlg);
int32_t ref = 0;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_REF_UP, &ref, sizeof(ref)), HITLS_PKI_SUCCESS);
ASSERT_EQ(ref, 2);
HITLS_X509_CertFree(cert);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_GET_KUSAGE, &keyUsage, sizeof(keyUsage)), HITLS_PKI_SUCCESS);
ASSERT_EQ(keyUsage, expKeyUsage);
if (expKeyUsage != HITLS_X509_EXT_KU_NONE) {
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_DIGITAL_SIGN) != 0, expKuDigitailSign);
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_KEY_CERT_SIGN) != 0, expKuCertSign);
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_KEY_AGREEMENT) != 0, expKuKeyAgreement);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_CTRL_FUNC_TC002(char *path, char *expectedSerialNum, char *expectedSubjectName,
char *expectedIssueName, char *expectedBeforeTime, char *expectedAfterTime)
{
HITLS_X509_Cert *cert = NULL;
BSL_Buffer subjectName = { NULL, 0 };
BSL_Buffer issuerName = { NULL, 0 };
BSL_Buffer serialNum = { NULL, 0 };
BSL_Buffer beforeTime = { NULL, 0 };
BSL_Buffer afterTime = { NULL, 0 };
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SUBJECT_DN_STR, &subjectName, sizeof(BSL_Buffer)), 0);
ASSERT_NE(subjectName.data, NULL);
ASSERT_EQ(subjectName.dataLen, strlen(expectedSubjectName));
ASSERT_EQ(strcmp((char *)subjectName.data, expectedSubjectName), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ISSUER_DN_STR, &issuerName, sizeof(BSL_Buffer)), 0);
ASSERT_NE(issuerName.data, NULL);
ASSERT_EQ(issuerName.dataLen, strlen(expectedIssueName));
ASSERT_EQ(strcmp((char *)issuerName.data, expectedIssueName), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SERIALNUM_STR, &serialNum, sizeof(BSL_Buffer)), 0);
ASSERT_NE(serialNum.data, NULL);
ASSERT_EQ(serialNum.dataLen, strlen(expectedSerialNum));
ASSERT_EQ(strcmp((char *)serialNum.data, expectedSerialNum), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_BEFORE_TIME_STR, &beforeTime, sizeof(BSL_Buffer)), 0);
ASSERT_NE(beforeTime.data, NULL);
ASSERT_EQ(beforeTime.dataLen, strlen(expectedBeforeTime));
ASSERT_EQ(strcmp((char *)beforeTime.data, expectedBeforeTime), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_AFTER_TIME_STR, &afterTime, sizeof(BSL_Buffer)), 0);
ASSERT_NE(afterTime.data, NULL);
ASSERT_EQ (afterTime.dataLen, strlen(expectedAfterTime));
ASSERT_EQ(strcmp((char *)afterTime.data, expectedAfterTime), 0);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_FREE(subjectName.data);
BSL_SAL_FREE(issuerName.data);
BSL_SAL_FREE(serialNum.data);
BSL_SAL_FREE(beforeTime.data);
BSL_SAL_FREE(afterTime.data);
}
/* END_CASE */
// subkey
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_PUBKEY_FUNC_TC001(char *path, char *path2)
{
HITLS_X509_Cert *cert = NULL;
HITLS_X509_Cert *cert2 = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path2, &cert2), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CheckSignature(cert2->tbs.ealPubKey, cert->tbs.tbsRawData, cert->tbs.tbsRawDataLen,
&cert->signAlgId, &cert->signature), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(cert);
HITLS_X509_CertFree(cert2);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_DUP_FUNC_TC001(char *path, int expSignAlg,
int expKuDigitailSign, int expKuCertSign, int expKuKeyAgreement, int expKeyUsage)
{
uint32_t keyUsage = 0;
HITLS_X509_Cert *cert = NULL;
HITLS_X509_Cert *dest = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
dest = HITLS_X509_CertDup(cert);
ASSERT_NE(dest, NULL);
int32_t alg = 0;
ASSERT_EQ(HITLS_X509_CertCtrl(dest, HITLS_X509_GET_SIGNALG, &alg, sizeof(alg)), HITLS_PKI_SUCCESS);
ASSERT_EQ(alg, expSignAlg);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_GET_KUSAGE, &keyUsage, sizeof(keyUsage)), HITLS_PKI_SUCCESS);
ASSERT_EQ(keyUsage, expKeyUsage);
if (expKeyUsage != HITLS_X509_EXT_KU_NONE) {
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_DIGITAL_SIGN) != 0, expKuDigitailSign);
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_KEY_CERT_SIGN) != 0, expKuCertSign);
ASSERT_EQ((keyUsage & HITLS_X509_EXT_KU_KEY_AGREEMENT) != 0, expKuKeyAgreement);
}
EXIT:
HITLS_X509_CertFree(cert);
HITLS_X509_CertFree(dest);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_EXT_ERROR_TC001(char *path, int ret)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), ret);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_EXTENSIONS_FUNC_TC001(char *path, int extNum, int isCA, int maxPathLen, int keyUsage,
int cid1, Hex *oid1, int cr1, Hex *val1,
int cid2, Hex *oid2, int cr2, Hex *val2,
int cid3, Hex *oid3, int cr3, Hex *val3)
{
HITLS_X509_Cert *cert = NULL;
HITLS_X509_ExtEntry **node = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
HITLS_X509_CertExt *certExt = (HITLS_X509_CertExt *)cert->tbs.ext.extData;
ASSERT_EQ(certExt->isCa, isCA);
ASSERT_EQ(certExt->maxPathLen, maxPathLen);
ASSERT_EQ(certExt->keyUsage, keyUsage);
ASSERT_EQ(BSL_LIST_COUNT(cert->tbs.ext.extList), extNum);
HITLS_X509_ExtEntry arr[] = {
{cid1, {BSL_ASN1_TAG_OBJECT_ID, oid1->len, oid1->x}, cr1, {BSL_ASN1_TAG_OCTETSTRING, val1->len, val1->x}},
{cid2, {BSL_ASN1_TAG_OBJECT_ID, oid2->len, oid2->x}, cr2, {BSL_ASN1_TAG_OCTETSTRING, val2->len, val2->x}},
{cid3, {BSL_ASN1_TAG_OBJECT_ID, oid3->len, oid3->x}, cr3, {BSL_ASN1_TAG_OCTETSTRING, val3->len, val3->x}},
};
node = BSL_LIST_First(cert->tbs.ext.extList);
for (int i = 0; i < 3; i++) { // Check the first 3 extensions
ASSERT_NE((*node), NULL);
ASSERT_EQ((*node)->critical, arr[i].critical);
ASSERT_EQ((*node)->extnId.tag, arr[i].extnId.tag);
ASSERT_COMPARE("oid", (*node)->extnId.buff, (*node)->extnId.len, arr[i].extnId.buff, arr[i].extnId.len);
ASSERT_EQ((*node)->extnValue.tag, arr[i].extnValue.tag);
ASSERT_COMPARE(
"value", (*node)->extnValue.buff, (*node)->extnValue.len, arr[i].extnValue.buff, arr[i].extnValue.len);
node = BSL_LIST_Next(cert->tbs.ext.extList);
}
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
// sign alg
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SIGNALG_FUNC_TC001(char *path, int signAlg,
int rsaPssHash, int rsaPssMgf1, int rsaPssSaltLen)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->signAlgId.algId, signAlg);
ASSERT_EQ(cert->signAlgId.rsaPssParam.mdId, rsaPssHash);
ASSERT_EQ(cert->signAlgId.rsaPssParam.mgfId, rsaPssMgf1);
ASSERT_EQ(cert->signAlgId.rsaPssParam.saltLen, rsaPssSaltLen);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
// signature
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_SIGNATURE_FUNC_TC001(char *path, Hex *buff, int unusedBits)
{
HITLS_X509_Cert *cert = NULL;
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->signature.len, buff->len);
ASSERT_COMPARE("signature", cert->signature.buff, cert->signature.len, buff->x, buff->len);
ASSERT_EQ(cert->signature.unusedBits, unusedBits);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_MUL_CERT_PARSE_FUNC_TC001(int format, char *path, int certNum)
{
TestMemInit();
HITLS_X509_List *list = NULL;
int32_t ret = HITLS_X509_CertParseBundleFile(format, path, &list);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), certNum);
EXIT:
BSL_LIST_FREE(list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_PARSE_BUNDLE_BUFF_FUNC_TC001(int format, char *path, int certNum)
{
TestMemInit();
HITLS_X509_List *list = NULL;
BSL_Buffer encodeData = {0};
// Read certificate bundle file into buffer
ASSERT_EQ(BSL_SAL_ReadFile(path, &encodeData.data, &encodeData.dataLen), BSL_SUCCESS);
// Parse certificates from buffer
ASSERT_EQ(HITLS_X509_CertParseBundleBuff(format, &encodeData, &list), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), certNum);
EXIT:
BSL_LIST_FREE(list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
BSL_SAL_Free(encodeData.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_PROVIDER_CERT_PARSE_BUNDLE_BUFF_FUNC_TC001(char *format, char *path, int certNum)
{
TestMemInit();
HITLS_X509_List *list = NULL;
BSL_Buffer encodeData = {0};
// Read certificate bundle file into buffer
ASSERT_EQ(BSL_SAL_ReadFile(path, &encodeData.data, &encodeData.dataLen), BSL_SUCCESS);
// Parse certificates from buffer using provider mechanism
ASSERT_EQ(HITLS_X509_ProviderCertParseBundleBuff(NULL, NULL, format, &encodeData, &list), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), certNum);
EXIT:
BSL_LIST_FREE(list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
BSL_SAL_Free(encodeData.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_SET_VERIOSN_FUNC_TC001(void)
{
TestMemInit();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(cert->tbs.version, HITLS_X509_VERSION_1);
int32_t version = HITLS_X509_VERSION_2;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &version, sizeof(int32_t)), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.version, version);
version = HITLS_X509_VERSION_3;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &version, sizeof(int32_t)), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.version, version);
// valLen
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &version, 1), HITLS_X509_ERR_INVALID_PARAM);
// val
version = HITLS_X509_VERSION_3 + 1;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &version, sizeof(int32_t)),
HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_SET_SERIAL_FUNC_TC001(Hex *serial)
{
TestMemInit();
uint8_t *val = serial->x;
uint32_t valLen = serial->len;
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(cert->tbs.serialNum.len, 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SERIALNUM, val, 0), HITLS_X509_ERR_CERT_INVALID_SERIAL_NUM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SERIALNUM, val, valLen), HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->tbs.serialNum.len, valLen);
ASSERT_COMPARE("serial", cert->tbs.serialNum.buff, valLen, val, valLen);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_SET_TIME_FUNC_TC001(void)
{
TestMemInit();
BSL_TIME time = {2024, 8, 22, 1, 1, 0, 1, 0};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(cert->tbs.validTime.flag, 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_BEFORE_TIME, &time, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_BEFORE_TIME, &time, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_TRUE((cert->tbs.validTime.flag & BSL_TIME_BEFORE_SET) != 0);
ASSERT_EQ(BSL_SAL_DateTimeCompare(&cert->tbs.validTime.start, &time, NULL), BSL_TIME_CMP_EQUAL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_AFTER_TIME, &time, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_TRUE((cert->tbs.validTime.flag & BSL_TIME_AFTER_SET) != 0);
ASSERT_EQ(BSL_SAL_DateTimeCompare(&cert->tbs.validTime.end, &time, NULL), BSL_TIME_CMP_EQUAL);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_ENCODE_CERT_EXT_TC001(char *path, Hex *expectExt)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = NULL;
BSL_ASN1_Buffer ext = {0};
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path, &cert), HITLS_PKI_SUCCESS);
uint8_t tag = 0xA3;
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &ext), HITLS_PKI_SUCCESS);
ASSERT_EQ(ext.len, expectExt->len);
if (expectExt->len != 0) {
ASSERT_EQ(ext.tag, tag);
ASSERT_COMPARE("extensions", ext.buff, ext.len, expectExt->x, expectExt->len);
}
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(ext.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_GEN_BUFF_API_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
BSL_Buffer buff = {0};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_UNKNOWN, cert, &buff), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, NULL, &buff), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, NULL), HITLS_X509_ERR_INVALID_PARAM);
cert->tbs.version = HITLS_X509_VERSION_1;
cert->tbs.ext.extList->count = 1;
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &buff), HITLS_X509_ERR_CERT_NOT_SIGNED);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_GEN_FILE_API_TC001(char *destPath)
{
TestMemInit();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_TRUE(cert != NULL);
ASSERT_EQ(HITLS_X509_CertGenFile(BSL_FORMAT_UNKNOWN, cert, destPath), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertGenFile(BSL_FORMAT_ASN1, NULL, destPath), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertGenFile(BSL_FORMAT_ASN1, cert, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_SIGN_API_TC001(void)
{
HITLS_X509_Cert *cert = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
prvKey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(prvKey, NULL);
// Test null parameters
ASSERT_EQ(HITLS_X509_CertSign(BSL_CID_SHA256, NULL, &algParam, cert), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertSign(BSL_CID_SHA256, prvKey, &algParam, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CertFree(cert);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_FORMAT_CONVERT_FUNC_TC001(char *inCert, int inForm, char *outCert, int outForm)
{
TestRandInit();
HITLS_X509_Cert *cert = NULL;
BSL_Buffer encodeCert = {0};
BSL_Buffer expectCert = {0};
ASSERT_EQ(HITLS_X509_CertParseFile(inForm, inCert, &cert), 0);
ASSERT_EQ(BSL_SAL_ReadFile(outCert, &expectCert.data, &expectCert.dataLen), 0);
ASSERT_EQ(HITLS_X509_CertGenBuff(outForm, cert, &encodeCert), 0);
ASSERT_COMPARE("Format convert", expectCert.data, expectCert.dataLen, encodeCert.data, encodeCert.dataLen);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(expectCert.data);
BSL_SAL_Free(encodeCert.data);
}
/* END_CASE */
static int32_t SetCert(HITLS_X509_Cert *raw, HITLS_X509_Cert *new)
{
int32_t ret = 1;
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_VERSION, &raw->tbs.version, sizeof(int32_t)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_SERIALNUM, raw->tbs.serialNum.buff, raw->tbs.serialNum.len), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_BEFORE_TIME, &raw->tbs.validTime.start, sizeof(BSL_TIME)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_AFTER_TIME, &raw->tbs.validTime.end, sizeof(BSL_TIME)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_PUBKEY, raw->tbs.ealPubKey, sizeof(void *)), 0);
BslList *rawSubject = NULL;
ASSERT_EQ(HITLS_X509_CertCtrl(raw, HITLS_X509_GET_SUBJECT_DN, &rawSubject, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_SUBJECT_DN, rawSubject, sizeof(BslList)), 0);
BslList *rawIssuer = NULL;
ASSERT_EQ(HITLS_X509_CertCtrl(raw, HITLS_X509_GET_ISSUER_DN, &rawIssuer, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(new, HITLS_X509_SET_ISSUER_DN, rawIssuer, sizeof(BslList)), 0);
ret = 0;
EXIT:
return ret;
}
/* BEGIN_CASE */
void SDV_X509_CERT_SETANDGEN_TC001(char *derCertPath, char *privPath, int keyType, int pkeyId, int pad, int mdId,
int mgfId, int saltLen)
{
HITLS_X509_Cert *raw = NULL;
HITLS_X509_Cert *new = NULL;
HITLS_X509_Cert *parse = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
BSL_Buffer encodeRaw = {0};
BSL_Buffer encodeNew = {0};
BslList *tmp = NULL;
HITLS_X509_SignAlgParam algParam = {0};
HITLS_X509_SignAlgParam *algParamPtr = NULL;
memset_s(&algParam, sizeof(HITLS_X509_SignAlgParam), 0, sizeof(HITLS_X509_SignAlgParam));
if (pad == 0) {
algParamPtr = NULL;
} else if (pad == CRYPT_EMSA_PSS) {
algParam.algId = BSL_CID_RSASSAPSS;
algParam.rsaPss.mdId = mdId;
algParam.rsaPss.mgfId = mgfId;
algParam.rsaPss.saltLen = saltLen;
algParamPtr = &algParam;
}
TestMemInit();
TestRandInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, keyType, privPath, NULL, &privKey), 0);
ASSERT_EQ(BSL_SAL_ReadFile(derCertPath, &encodeRaw.data, &encodeRaw.dataLen), 0);
ASSERT_EQ(HITLS_X509_CertParseBuff(BSL_FORMAT_ASN1, &encodeRaw, &raw), 0);
// generate new cert
new = HITLS_X509_CertNew();
ASSERT_TRUE(new != NULL);
ASSERT_EQ(SetCert(raw, new), 0);
// Skip extension settings, directly use the extensions from raw certificate
tmp = new->tbs.ext.extList;
new->tbs.ext.extList = raw->tbs.ext.extList;
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, algParamPtr, new), 0);
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, new, &encodeNew), 0);
if (pkeyId == CRYPT_PKEY_RSA && pad == CRYPT_EMSA_PKCSV15) {
ASSERT_COMPARE("Gen cert", encodeNew.data, encodeNew.dataLen, encodeRaw.data, encodeRaw.dataLen);
}
EXIT:
HITLS_X509_CertFree(raw);
BSL_SAL_Free(encodeRaw.data);
if (tmp != NULL) {
new->tbs.ext.extList = tmp;
}
HITLS_X509_CertFree(new);
HITLS_X509_CertFree(parse);
CRYPT_EAL_PkeyFreeCtx(privKey);
BSL_SAL_Free(encodeNew.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_GEN_PROCESS_TC001(char *derCertPath, char *privPath, int keyType, int mdId)
{
HITLS_X509_Cert *cert = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
int32_t ver = 0;
BSL_Buffer encodeCert = {0};
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, keyType, privPath, NULL, &privKey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, derCertPath, &cert), HITLS_PKI_SUCCESS);
/* Cannot repeat parse */
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, derCertPath, &cert), HITLS_X509_ERR_INVALID_PARAM);
/* Sign with invalid parameters */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot sign after parsing */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_SIGN_AFTER_PARSE);
/* Cannot set after parsing */
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &ver, sizeof(int32_t)), HITLS_X509_ERR_SET_AFTER_PARSE);
/* Generate cert after parsing is allowed. */
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), 0);
BSL_SAL_Free(encodeCert.data);
encodeCert.data = NULL;
encodeCert.dataLen = 0;
/* Repeat generate is allowed. */
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), 0);
EXIT:
CRYPT_EAL_PkeyFreeCtx(privKey);
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encodeCert.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_GEN_PROCESS_TC002(char *csrPath, char *privPath, int keyType, int mdId, Hex *serial)
{
HITLS_X509_Csr *csr = NULL;
HITLS_X509_Cert *cert = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
CRYPT_EAL_PkeyCtx *pubKey = NULL;
uint8_t md[64] = {0}; // 64 : max md len
uint32_t mdLen = 64; // 64 : max md len
BslList *tmp = NULL;
BSL_TIME beforeTime = {2024, 8, 22, 1, 1, 0, 1, 0};
BSL_TIME afterTime = {2050, 8, 22, 1, 1, 0, 1, 0};
BSL_Buffer encodeCert = {0};
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, keyType, privPath, NULL, &privKey), 0);
ASSERT_EQ(HITLS_X509_CsrParseFile(BSL_FORMAT_ASN1, csrPath, &csr), HITLS_PKI_SUCCESS);
cert = HITLS_X509_CertNew();
ASSERT_TRUE(cert != NULL);
/* Cannot parse after new */
ASSERT_EQ(HITLS_X509_CertParseBuff(BSL_FORMAT_ASN1, &encodeCert, &cert), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot digest before signing */
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, md, &mdLen), HITLS_X509_ERR_CERT_NOT_SIGNED);
/* Cannot generate before signing */
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), HITLS_X509_ERR_CERT_NOT_SIGNED);
/* Invalid parameters */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot sign before setting serial number */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_SERIAL_NUM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SERIALNUM, serial->x, serial->len), 0);
/* Cannot sign before setting issuer and subject */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_DN);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SUBJECT_DN, &tmp, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SUBJECT_DN, tmp, sizeof(BslList)), 0);
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_DN);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_ISSUER_DN, tmp, sizeof(BslList)), 0);
/* Cannot sign before setting after time and before time */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_TIME);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), 0);
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_TIME);
/* Before time is later than after time */
afterTime.year = beforeTime.year - 1;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), 0);
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_START_TIME_LATER);
afterTime.year = beforeTime.year + 1;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), 0);
/* Cannot sign before setting public key */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), HITLS_X509_ERR_CERT_INVALID_PUBKEY);
/* Set public key */
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_PUBKEY, &pubKey, 0), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_PUBKEY, pubKey, 0), 0);
/* Cannot generate before signing */
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), HITLS_X509_ERR_CERT_NOT_SIGNED);
/* Repeat sign is allowed. */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), 0);
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), 0);
/* Cannot parse after signing */
ASSERT_EQ(HITLS_X509_CertParseBuff(BSL_FORMAT_ASN1, &encodeCert, &cert), HITLS_X509_ERR_INVALID_PARAM);
/* Sing after generating is allowed. */
ASSERT_EQ(HITLS_X509_CertSign(mdId, privKey, NULL, cert), 0);
/* Repeat digest is allowed. */
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, md, &mdLen), 0);
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, md, &mdLen), 0);
/* Repeat generate is allowed. */
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), 0);
BSL_SAL_Free(encodeCert.data);
encodeCert.data = NULL;
encodeCert.dataLen = 0;
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encodeCert), 0);
/* Cannot parse after generating */
ASSERT_EQ(HITLS_X509_CertParseBuff(BSL_FORMAT_ASN1, &encodeCert, &cert), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
CRYPT_EAL_PkeyFreeCtx(privKey);
CRYPT_EAL_PkeyFreeCtx(pubKey);
HITLS_X509_CsrFree(csr);
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encodeCert.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_DIGEST_API_TC001(char *inCert, int inForm, int mdId)
{
TestRandInit();
HITLS_X509_Cert *cert = NULL;
uint8_t md[64] = {0}; // 64 : max md len
uint32_t mdLen = 64; // 64 : max md len
ASSERT_EQ(HITLS_X509_CertParseFile(inForm, inCert, &cert), 0);
/* Invalid parameters */
ASSERT_EQ(HITLS_X509_CertDigest(NULL, mdId, md, &mdLen), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, NULL, &mdLen), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, md, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_DIGEST_FUNC_TC001(char *inCert, int inForm, int mdId, Hex *expect)
{
TestRandInit();
BSL_Buffer encodeRaw = {0};
BSL_Buffer encodeNew = {0};
HITLS_X509_Cert *cert = NULL;
uint8_t md[64] = {0}; // 64 : max md len
uint32_t mdLen = 64; // 64 : max md len
ASSERT_EQ(BSL_SAL_ReadFile(inCert, &encodeRaw.data, &encodeRaw.dataLen), 0);
ASSERT_EQ(HITLS_X509_CertParseBuff(inForm, &encodeRaw, &cert), 0);
ASSERT_EQ(HITLS_X509_CertDigest(cert, mdId, md, &mdLen), 0);
ASSERT_COMPARE("cert digest", expect->x, expect->len, md, mdLen);
ASSERT_EQ(HITLS_X509_CertGenBuff(inForm, cert, &encodeNew), 0);
ASSERT_COMPARE("digest then gen", encodeRaw.data, encodeRaw.dataLen, encodeNew.data, encodeNew.dataLen);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encodeRaw.data);
BSL_SAL_Free(encodeNew.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CERT_SET_CSR_EXT_FUNC_TC001(int inForm, char *inCsr, int ret, Hex *expect)
{
TestRandInit();
BSL_ASN1_Buffer encodeExt = {0};
HITLS_X509_Csr *csr = NULL;
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CsrParseFile(inForm, inCsr, &csr), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_CSR_EXT, csr, 0), ret);
ASSERT_EQ(HITLS_X509_EncodeExt(0, cert->tbs.ext.extList, &encodeExt), 0);
if (expect->len != 0) {
ASSERT_TRUE((cert->tbs.ext.flag & HITLS_X509_EXT_FLAG_PARSE) == 0);
ASSERT_TRUE((cert->tbs.ext.flag & HITLS_X509_EXT_FLAG_GEN) != 0);
ASSERT_COMPARE("Csr ext", encodeExt.buff, encodeExt.len, expect->x, expect->len);
}
EXIT:
HITLS_X509_CertFree(cert);
HITLS_X509_CsrFree(csr);
BSL_SAL_Free(encodeExt.buff);
}
/* END_CASE */
extern int32_t HITLS_X509_ParseCertTbs(BSL_ASN1_Buffer *asnArr, HITLS_X509_Cert *cert);
static int32_t STUB_HITLS_X509_ParseCertTbs(BSL_ASN1_Buffer *asnArr, HITLS_X509_Cert *cert)
{
(void)asnArr;
(void)cert;
return BSL_MALLOC_FAIL;
}
/* BEGIN_CASE */
void SDV_X509_CERT_INVALIED_TEST_TC001(int format, char *path)
{
TestMemInit();
FuncStubInfo tmpRpInfo = {0};
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_ParseCertTbs, STUB_HITLS_X509_ParseCertTbs) == 0);
HITLS_X509_Cert *cert = NULL;
ASSERT_NE(HITLS_X509_CertParseFile(format, path, &cert), HITLS_PKI_SUCCESS);
EXIT:
STUB_Reset(&tmpRpInfo);
HITLS_X509_CertFree(cert);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/cert/test_suite_sdv_x509_cert.c | C | unknown | 46,810 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "sal_file.h"
#include "bsl_init.h"
#include "crypt_encode_decode_key.h"
#include "crypt_eal_codecs.h"
#include "crypt_eal_rand.h"
#include "crypt_errno.h"
#include "hitls_cms_local.h"
#include "hitls_pki_errno.h"
/* END_HEADER */
/**
* For test parse p7-encryptData of wrong conditions.
*/
/* BEGIN_CASE */
void SDV_CMS_PARSE_ENCRYPTEDDATA_TC001(Hex *buff)
{
BSL_Buffer output = {0};
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
int32_t ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
BSL_SAL_Free(output.data);
output.data = NULL;
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, NULL, (const uint8_t *)pwd, pwdlen, &output);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, NULL, pwdlen, &output);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, NULL);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, 8192, &output);
ASSERT_EQ(ret, CRYPT_INVALID_ARG);
char *pwd1 = "123456@123";
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd1, strlen(pwd1),
&output);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
char *pwd2 = "";
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd2, strlen(pwd2),
&output);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
(void)memset_s(buff->x + buff->len - 20, 16, 0, 16); // modify the ciphertext, 16 and 20 are random number.
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
&output);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
EXIT:
return;
}
/* END_CASE */
/**
* For test parse p7-encryptData of right conditions.
*/
/* BEGIN_CASE */
void SDV_CMS_PARSE_ENCRYPTEDDATA_TC002(Hex *buff)
{
BSL_Buffer output = {0};
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
int32_t ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
&output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_Free(output.data);
return;
}
/* END_CASE */
/**
* For test parse p7-DigestInfo of wrong conditions.
*/
/* BEGIN_CASE */
void SDV_CMS_PARSE_DIGESTINFO_TC001(Hex *buff, int alg, Hex *digest)
{
BSL_Buffer output = {0};
BslCid cid = BSL_CID_UNKNOWN;
int32_t ret = HITLS_CMS_ParseDigestInfo(NULL, &cid, &output);
ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
ret = HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, NULL);
ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
ret = HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(alg, cid);
ASSERT_EQ(memcmp(output.data, digest->x, digest->len), 0);
EXIT:
BSL_SAL_Free(output.data);
return;
}
/* END_CASE */
/**
* For test parse p7-DigestInfo of right conditions.
*/
/* BEGIN_CASE */
void SDV_CMS_PARSE_DIGESTINFO_TC002(Hex *buff, int alg, Hex *digest)
{
BSL_Buffer output = {0};
BslCid cid = BSL_CID_UNKNOWN;
int32_t ret = HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(alg, cid);
ASSERT_EQ(memcmp(output.data, digest->x, digest->len), 0);
EXIT:
BSL_SAL_Free(output.data);
return;
}
/* END_CASE */
/**
* For test encode p7-encryptData.
*/
/* BEGIN_CASE */
void SDV_CMS_ENCODE_ENCRYPTEDDATA_TC001(Hex *buff)
{
BSL_Buffer data = {buff->x, buff->len};
BSL_Buffer output = {0};
BSL_Buffer verify = {0};
char *pwd = "123456";
CRYPT_Pbkdf2Param param = {0};
param.pbesId = BSL_CID_PBES2;
param.pbkdfId = BSL_CID_PBKDF2;
param.hmacId = CRYPT_MAC_HMAC_SHA256;
param.symId = CRYPT_CIPHER_AES256_CBC;
param.pwd = (uint8_t *)pwd;
param.pwdLen = strlen(pwd);
param.saltLen = 16;
param.itCnt = 2048;
CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, ¶m};
ASSERT_EQ(TestRandInit(), HITLS_PKI_SUCCESS);
int32_t ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, NULL, NULL, NULL);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, NULL, NULL);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, ¶mEx, NULL);
ASSERT_EQ(ret, CRYPT_NULL_INPUT);
param.hmacId = CRYPT_MAC_MAX;
ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, ¶mEx, &output);
ASSERT_EQ(ret, CRYPT_ERR_ALGID);
param.hmacId = CRYPT_MAC_HMAC_SHA256;
ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, ¶mEx, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, &output, (const uint8_t *)pwd, strlen(pwd), &verify);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_COMPARE("encode p7-encryptData", data.data, data.dataLen, verify.data, verify.dataLen);
EXIT:
TestRandDeInit();
BSL_SAL_FREE(verify.data);
BSL_SAL_FREE(output.data);
return;
}
/* END_CASE */
/**
* For test encode p7-DigestInfo.
*/
/* BEGIN_CASE */
void SDV_CMS_ENCODE_DIGESTINFO_TC001()
{
BSL_Buffer input = {0};
BSL_Buffer output = {0};
BslCid cid = 0;
BSL_Buffer digest = {0};
int32_t ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, NULL, NULL);
ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, NULL);
ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
input.dataLen = 1;
ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
input.dataLen = 0;
ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
ASSERT_EQ(ret, HITLS_CMS_ERR_INVALID_DATA);
BSL_SAL_FREE(output.data);
input.data = (uint8_t *)"123456";
input.dataLen = 6;
ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(cid, BSL_CID_MD5);
EXIT:
BSL_SAL_FREE(digest.data);
BSL_SAL_FREE(output.data);
return;
}
/* END_CASE */
/**
* For test encode p7-DigestInfo vector.
*/
/* BEGIN_CASE */
void SDV_CMS_ENCODE_DIGESTINFO_TC002(int algid, Hex *in)
{
BSL_Buffer input = {in->x, in->len};
BSL_Buffer output = {0};
BslCid cid = 0;
BSL_Buffer digest = {0};
int32_t ret = HITLS_CMS_EncodeDigestInfoBuff(algid, &input, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(cid, algid);
EXIT:
BSL_SAL_FREE(digest.data);
BSL_SAL_FREE(output.data);
return;
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/cms/test_suite_sdv_cms.c | C | unknown | 7,913 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "hitls_error.h"
#include "hitls_pki_cert.h"
#include "hitls_pki_utils.h"
#include "hitls_pki_errno.h"
#include "hitls_x509_verify.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "hitls_cert_local.h"
#include "hitls_crl_local.h"
#include "bsl_init.h"
#include "bsl_obj_internal.h"
#include "bsl_uio.h"
#include "crypt_errno.h"
#include "crypt_eal_codecs.h"
#include "crypt_eal_rand.h"
#include "hitls_x509_local.h"
#include "hitls_print_local.h"
#include "bsl_params.h"
#include "crypt_params_key.h"
#define MAX_BUFF_SIZE 4096
#define PATH_MAX_LEN 4096
#define PWD_MAX_LEN 4096
/* END_HEADER */
static void FreeListData(void *data)
{
(void)data;
return;
}
static void FreeSanListData(void *data)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_GeneralName *name = (HITLS_X509_GeneralName *)data;
if (name->type == HITLS_X509_GN_DNNAME) {
HITLS_X509_DnListFree((BslList *)name->value.data);
}
}
static int32_t TestSignCb(int32_t mdId, CRYPT_EAL_PkeyCtx *prvKey, HITLS_X509_Asn1AlgId *signAlgId, void *obj)
{
(void)signAlgId;
uint32_t signLen = CRYPT_EAL_PkeyGetSignLen(prvKey);
uint8_t *sign = (uint8_t *)BSL_SAL_Malloc(signLen);
if (sign == NULL) {
return BSL_MALLOC_FAIL;
}
uint8_t *data = (uint8_t *)obj;
int32_t ret = CRYPT_EAL_PkeySign(prvKey, mdId, data, 1, sign, &signLen);
BSL_SAL_Free(sign);
return ret;
}
/* BEGIN_CASE */
void SDV_HITLS_X509_FreeStoreCtx_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_StoreCtxFree(NULL);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_CtrlStoreCtx_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_StoreCtxCtrl(NULL, 0, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_StoreCtx storeCtx = {0};
ASSERT_EQ(HITLS_X509_StoreCtxCtrl(&storeCtx, 0, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_VerifyCert_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertVerify(NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_StoreCtx storeCtx = {0};
ASSERT_EQ(HITLS_X509_CertVerify(&storeCtx, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_BuildCertChain_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertChainBuild(NULL, false, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_StoreCtx storeCtx = {0};
ASSERT_EQ(HITLS_X509_CertChainBuild(&storeCtx, false, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_Cert cert = {0};
ASSERT_EQ(HITLS_X509_CertChainBuild(&storeCtx, false, &cert, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_FreeCert_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_CertFree(NULL);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_ParseBuffCert_TC001(void)
{
TestMemInit();
HITLS_X509_Cert *cert = NULL;
uint8_t buffData[10] = {0};
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertParseBuff(0, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
BSL_Buffer buff = {0};
ASSERT_EQ(HITLS_X509_CertParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
buff.data = buffData;
ASSERT_EQ(HITLS_X509_CertParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
buff.dataLen = 1;
ASSERT_EQ(HITLS_X509_CertParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertParseBuff(0xff, &buff, &cert), HITLS_X509_ERR_FORMAT_UNSUPPORT);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_ParseFileCert_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, NULL, NULL), BSL_NULL_INPUT);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/asn1/nist384ca.crt", NULL),
HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_CtrlCert_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertCtrl(NULL, 0xff, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_Cert cert = {0};
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, 0x7fffffff, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_ENCODELEN, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_ENCODELEN, &cert, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_ENCODE, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_PUBKEY, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_PUBKEY, &cert, 0), CRYPT_NULL_INPUT);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SIGNALG, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SIGNALG, &cert, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SIGN_MDALG, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SIGN_MDALG, &cert, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_REF_UP, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_REF_UP, &cert, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SUBJECT_DN_STR, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_ISSUER_DN_STR, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_SERIALNUM, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_BEFORE_TIME_STR, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_GET_AFTER_TIME_STR, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_EXT_GET_KUSAGE, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(&cert, HITLS_X509_EXT_GET_KUSAGE, &cert, 0), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_DupCert_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert src = {0};
ASSERT_EQ(HITLS_X509_CertDup(NULL), NULL);
ASSERT_EQ(HITLS_X509_CertDup(&src), NULL);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_FreeCrl_TC001(void)
{
HITLS_X509_CrlFree(NULL);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_CtrlCrl_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CrlCtrl(NULL, 0xff, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
HITLS_X509_Crl crl = {0};
ASSERT_EQ(HITLS_X509_CrlCtrl(&crl, 0xff, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlCtrl(&crl, HITLS_X509_REF_UP, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlCtrl(&crl, HITLS_X509_REF_UP, &crl, 0), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_ParseBuffCrl_TC001(void)
{
TestMemInit();
HITLS_X509_Crl *crl = NULL;
uint8_t buffData[10] = {0};
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CrlParseBuff(0, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
BSL_Buffer buff = {0};
ASSERT_EQ(HITLS_X509_CrlParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
buff.data = buffData;
ASSERT_EQ(HITLS_X509_CrlParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
buff.dataLen = 1;
ASSERT_EQ(HITLS_X509_CrlParseBuff(0, &buff, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlParseBuff(0xff, &buff, &crl), HITLS_X509_ERR_FORMAT_UNSUPPORT);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */ // todo
void SDV_HITLS_X509_ParseFileCrl_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, NULL, NULL), BSL_NULL_INPUT);
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, "../testdata/cert/asn1/ca-1-rsa-sha256-v2.der",
NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseBuffPubKey_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
BSL_Buffer buff = {0};
CRYPT_EAL_PkeyCtx *pkey = NULL;
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, 0xff, &buff, NULL, 0, &pkey), CRYPT_DECODE_NO_SUPPORT_TYPE);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_SUBKEY, NULL, NULL, 0, &pkey), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_RSA, NULL, NULL, 0, &pkey), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_SUBKEY, &buff, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_RSA, &buff, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_SUBKEY, &buff, NULL, 0, &pkey), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PUBKEY_RSA, &buff, NULL, 0, &pkey), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseFilePubKey_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(0xff, 0, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PUBKEY_SUBKEY, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PUBKEY_SUBKEY,
"../testdata/cert/asn1/prime256v1pub.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PUBKEY_RSA, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PUBKEY_RSA,
"../testdata/cert/asn1/rsa2048pub_pkcs1.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseBuffPriKey_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
BSL_Buffer buff = {0};
uint8_t pwd = 0;
CRYPT_EAL_PkeyCtx *key = NULL;
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, 0xff, &buff, &pwd, 0, &key), CRYPT_DECODE_NO_SUPPORT_TYPE);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_ECC, NULL, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_RSA, NULL, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_UNENCRYPT, NULL, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_ENCRYPT, NULL, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_ECC, &buff, NULL, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_RSA, &buff, NULL, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_UNENCRYPT, &buff, NULL, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_ENCRYPT, &buff, NULL, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_ECC, &buff, &pwd, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_RSA, &buff, &pwd, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_UNENCRYPT, &buff, &pwd, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_ENCRYPT, &buff, &pwd, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_ECC, &buff, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_RSA, &buff, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_UNENCRYPT, &buff, &pwd, 0, &key), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(0, CRYPT_PRIKEY_PKCS8_ENCRYPT, &buff, &pwd, 0, &key), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseFilePriKey_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(0xff, 0, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_ECC, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_ECC,
"../testdata/cert/asn1/prime256v1.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_RSA, NULL, NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_RSA,
"../testdata/cert/asn1/rsa2048key_pkcs1.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, NULL, NULL, 0,
NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT,
"../testdata/cert/asn1/prime256v1_pkcs8.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_ENCRYPT, NULL, NULL, 0, NULL),
CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_ENCRYPT,
"../testdata/cert/asn1/prime256v1_pkcs8_enc.der", NULL, 0, NULL), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseFilePriKeyFormat_TC001(int format, int type, char *path)
{
TestMemInit();
BSL_GLOBAL_Init();
CRYPT_EAL_PkeyCtx *key = NULL;
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(format, type, path, NULL, 0, &key), CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_PkeyFreeCtx(key);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_ParseFilePubKeyFormat_TC001(int format, int type, char *path)
{
TestMemInit();
BSL_GLOBAL_Init();
CRYPT_EAL_PkeyCtx *key = NULL;
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(format, type, path, NULL, 0, &key), CRYPT_SUCCESS);
EXIT:
CRYPT_EAL_PkeyFreeCtx(key);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EncodeNameList_TC001(int format, char *certPath, Hex *expect)
{
HITLS_X509_Cert *cert = NULL;
BSL_ASN1_Buffer name = {0};
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_CertParseFile(format, certPath, &cert), 0);
ASSERT_EQ(HITLS_X509_EncodeNameList(cert->tbs.issuerName, &name), 0);
ASSERT_COMPARE("Encode names", name.buff, name.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(name.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_SetBCons_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
HITLS_X509_Ext *ext = &cert->tbs.ext;
HITLS_X509_CertExt *certExt = (HITLS_X509_CertExt *)ext->extData;
ASSERT_EQ(certExt->extFlags, 0);
ASSERT_EQ(certExt->isCa, false);
ASSERT_EQ(certExt->maxPathLen, -1);
HITLS_X509_ExtBCons bCons = {true, true, 1};
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_BCONS, &bCons, 0), HITLS_X509_ERR_EXT_UNSUPPORT);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_BCONS, &bCons, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_BCONS, &bCons, sizeof(HITLS_X509_ExtBCons)), 0);
ASSERT_EQ(BSL_LIST_COUNT(ext->extList), 1);
ASSERT_NE(certExt->extFlags & HITLS_X509_EXT_FLAG_BCONS, 0);
ASSERT_EQ(certExt->isCa, true);
ASSERT_EQ(certExt->maxPathLen, 1);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_SetAkiSki_TC001(Hex *kid)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
HITLS_X509_Ext *ext = &cert->tbs.ext;
HITLS_X509_ExtAki aki = {true, {kid->x, kid->len}, NULL, {0}};
HITLS_X509_ExtSki ski = {true, {kid->x, kid->len}};
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SKI, &ski, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_AKI, &aki, 0), HITLS_X509_ERR_INVALID_PARAM);
aki.kid.dataLen = 0;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)),
HITLS_X509_ERR_EXT_KID);
aki.kid.dataLen = kid->len;
ski.kid.dataLen = 0;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SKI, &ski, sizeof(HITLS_X509_ExtSki)),
HITLS_X509_ERR_EXT_KID);
ski.kid.dataLen = kid->len;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)), 0);
ASSERT_EQ(BSL_LIST_COUNT(ext->extList), 1);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SKI, &ski, sizeof(HITLS_X509_ExtSki)), 0);
ASSERT_NE(ext->flag & HITLS_X509_EXT_FLAG_GEN, 0);
ASSERT_EQ(BSL_LIST_COUNT(ext->extList), 1 + 1);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_SetKeyUsage_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
HITLS_X509_Ext *ext = &cert->tbs.ext;
HITLS_X509_CertExt *certExt = (HITLS_X509_CertExt *)ext->extData;
ASSERT_EQ(certExt->keyUsage, 0);
HITLS_X509_ExtKeyUsage ku = {true, 0};
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)),
HITLS_X509_ERR_EXT_KU);
ku.keyUsage = HITLS_X509_EXT_KU_DIGITAL_SIGN | HITLS_X509_EXT_KU_NON_REPUDIATION |
HITLS_X509_EXT_KU_KEY_ENCIPHERMENT | HITLS_X509_EXT_KU_DATA_ENCIPHERMENT | HITLS_X509_EXT_KU_KEY_AGREEMENT |
HITLS_X509_EXT_KU_KEY_CERT_SIGN | HITLS_X509_EXT_KU_CRL_SIGN | HITLS_X509_EXT_KU_ENCIPHER_ONLY |
HITLS_X509_EXT_KU_DECIPHER_ONLY;
ku.keyUsage = ~ku.keyUsage;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)),
HITLS_X509_ERR_EXT_KU);
ku.keyUsage = ~ku.keyUsage;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(BSL_LIST_COUNT(ext->extList), 1);
ASSERT_NE(certExt->extFlags & HITLS_X509_EXT_FLAG_KUSAGE, 0);
ASSERT_NE(ext->flag & HITLS_X509_EXT_FLAG_GEN, 0);
EXIT:
HITLS_X509_CertFree(cert);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_SetExtendKeyUsage_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
BslList *oidList = BSL_LIST_New(sizeof(BSL_Buffer));
ASSERT_NE(oidList, NULL);
HITLS_X509_Ext *ext = &cert->tbs.ext;
HITLS_X509_ExtExKeyUsage exku = {true, NULL};
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, 0), HITLS_X509_ERR_INVALID_PARAM);
// error: list is null
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)),
HITLS_X509_ERR_EXT_EXTENDED_KU);
// werror: list is empty
exku.oidList = oidList;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)),
HITLS_X509_ERR_EXT_EXTENDED_KU);
// error: oid is null
BSL_Buffer emptyOid = {0};
ASSERT_EQ(BSL_LIST_AddElement(oidList, &emptyOid, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)),
HITLS_X509_ERR_EXT_EXTENDED_KU_ELE);
BSL_LIST_DeleteAll(oidList, FreeListData);
// success: normal oid
BslOidString *oid = BSL_OBJ_GetOID(BSL_CID_KP_SERVERAUTH);
ASSERT_NE(oid, NULL);
BSL_Buffer oidBuff = {(uint8_t *)oid->octs, oid->octetLen};
ASSERT_EQ(BSL_LIST_AddElement(oidList, &oidBuff, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)), 0);
ASSERT_NE(ext->flag & HITLS_X509_EXT_FLAG_GEN, 0);
EXIT:
HITLS_X509_CertFree(cert);
BSL_LIST_FREE(oidList, FreeListData);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_SetSan_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
BslList *list = BSL_LIST_New(sizeof(HITLS_X509_GeneralName));
ASSERT_NE(list, NULL);
HITLS_X509_Ext *ext = &cert->tbs.ext;
HITLS_X509_ExtSan san = {true, NULL};
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, 0), HITLS_X509_ERR_INVALID_PARAM);
// error: list is null
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)),
HITLS_X509_ERR_EXT_SAN);
// error: list is empty
san.names = list;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)),
HITLS_X509_ERR_EXT_SAN);
// error: list data content is null
HITLS_X509_GeneralName empty = {0};
ASSERT_EQ(BSL_LIST_AddElement(list, &empty, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)),
HITLS_X509_ERR_EXT_SAN_ELE);
BSL_LIST_DeleteAll(list, FreeListData);
// error: name type
char *email = "test@a.com";
HITLS_X509_GeneralName errType = {HITLS_X509_GN_IP + 1, {(uint8_t *)email, (uint32_t)strlen(email)}};
ASSERT_EQ(BSL_LIST_AddElement(list, &errType, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)),
HITLS_X509_ERR_EXT_GN_UNSUPPORT);
BSL_LIST_DeleteAll(list, FreeListData);
// success
HITLS_X509_GeneralName nomal = {HITLS_X509_GN_EMAIL, {(uint8_t *)email, (uint32_t)strlen(email)}};
ASSERT_EQ(BSL_LIST_AddElement(list, &nomal, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)), 0);
ASSERT_NE(ext->flag & HITLS_X509_EXT_FLAG_GEN, 0);
EXIT:
HITLS_X509_CertFree(cert);
BSL_LIST_FREE(list, FreeListData);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_EncodeBCons_TC001(int critical, int isCa, int maxPathLen, Hex *expect)
{
TestMemInit();
BSL_GLOBAL_Init();
int8_t tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
HITLS_X509_ExtBCons bCons = {critical, isCa, maxPathLen};
BSL_ASN1_Buffer encode = {0};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_BCONS, &bCons, sizeof(HITLS_X509_ExtBCons)), 0);
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.len, expect->len);
ASSERT_COMPARE("Ext: bCons", encode.buff, encode.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encode.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_EncodeExtendKeyUsage_TC001(int critical, Hex *oid1, Hex *oid2, Hex *expect)
{
TestMemInit();
BSL_GLOBAL_Init();
int8_t tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
BSL_ASN1_Buffer encode = {0};
HITLS_X509_ExtExKeyUsage exku = {critical, NULL};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
exku.oidList = BSL_LIST_New(sizeof(BSL_Buffer));
ASSERT_NE(exku.oidList, NULL);
ASSERT_EQ(BSL_LIST_AddElement(exku.oidList, oid1, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(exku.oidList, oid2, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)),
0);
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.len, expect->len);
ASSERT_COMPARE("Ext: extendKeyUsage", encode.buff, encode.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_LIST_DeleteAll(exku.oidList, FreeListData);
BSL_SAL_Free(exku.oidList);
BSL_SAL_Free(encode.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_AddDnName_TC001(int unknownCid, int cid, Hex *oid, Hex *value)
{
TestMemInit();
BSL_GLOBAL_Init();
BslList *list = BSL_LIST_New(1);
ASSERT_TRUE(list != NULL);
HITLS_X509_DN unknownName[1] = {{unknownCid, value->x, value->len}};
HITLS_X509_DN dnName[1] = {{cid, value->x, value->len}};
HITLS_X509_DN dnNullName[1] = {{cid, NULL, value->len}};
HITLS_X509_DN dnZeroLenName[1] = {{cid, value->x, 0}};
ASSERT_EQ(HITLS_X509_AddDnName(list, unknownName, 1), HITLS_X509_ERR_SET_DNNAME_UNKNOWN);
ASSERT_EQ(HITLS_X509_AddDnName(list, dnName, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AddDnName(list, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AddDnName(list, dnNullName, 1), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AddDnName(list, dnZeroLenName, 1), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AddDnName(list, dnName, 1), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), 2); // layer 1 and layer 2
HITLS_X509_NameNode **node = BSL_LIST_First(list);
ASSERT_EQ((*node)->layer, 1); // layer 1
ASSERT_EQ((*node)->nameType.tag, 0);
ASSERT_EQ((*node)->nameType.buff, NULL);
ASSERT_EQ((*node)->nameType.len, 0);
ASSERT_EQ((*node)->nameValue.tag, 0);
ASSERT_EQ((*node)->nameValue.buff, NULL);
ASSERT_EQ((*node)->nameValue.len, 0);
node = BSL_LIST_Next(list);
ASSERT_EQ((*node)->layer, 2); // layer 2
ASSERT_EQ((*node)->nameType.tag, BSL_ASN1_TAG_OBJECT_ID);
ASSERT_COMPARE("nameOid", (*node)->nameType.buff, (*node)->nameType.len, oid->x, oid->len);
ASSERT_EQ((*node)->nameValue.tag, BSL_ASN1_TAG_UTF8STRING);
ASSERT_COMPARE("nameValue", (*node)->nameValue.buff, (*node)->nameValue.len, value->x, value->len);
/* subject name can add repeat name */
ASSERT_EQ(HITLS_X509_AddDnName(list, dnName, 1), HITLS_PKI_SUCCESS);
list->count = 100; // 100: the max number of name type.
ASSERT_EQ(HITLS_X509_AddDnName(list, dnName, 1), HITLS_X509_ERR_SET_DNNAME_TOOMUCH);
EXIT:
BSL_LIST_FREE(list, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeNameNode);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_EncodeSan_TC001(int critical, int type1, int type2, int type3, int type4, int dirCid1,
int dirCid2, Hex *value, Hex *expect)
{
TestMemInit();
BSL_GLOBAL_Init();
int8_t tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
BSL_ASN1_Buffer encode = {0};
HITLS_X509_ExtSan san = {critical, NULL};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
san.names = BSL_LIST_New(sizeof(BSL_Buffer));
ASSERT_NE(san.names, NULL);
// Generate san
BslList *dirNames = HITLS_X509_DnListNew();
ASSERT_NE(dirNames, NULL);
HITLS_X509_DN dnName1[1] = {{(BslCid)dirCid1, value->x, value->len}};
HITLS_X509_DN dnName2[1] = {{(BslCid)dirCid2, value->x, value->len}};
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName1, 1), 0);
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName2, 1), 0);
HITLS_X509_GeneralName names[] = {
{type1, {value->x, value->len}},
{type2, {value->x, value->len}},
{type3, {value->x, value->len}},
{type4, {value->x, value->len}},
{HITLS_X509_GN_DNNAME, {(uint8_t *)dirNames, sizeof(BslList *)}},
};
for (uint32_t i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
ASSERT_EQ(BSL_LIST_AddElement(san.names, &names[i], BSL_LIST_POS_END), 0);
}
// set san and encode ext
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)), 0);
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.len, expect->len);
ASSERT_COMPARE("Ext: san", encode.buff, encode.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_LIST_DeleteAll(san.names, FreeSanListData);
BSL_SAL_Free(san.names);
BSL_SAL_Free(encode.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_EncodeKeyUsage_TC001(int critical, int usage, Hex *expect)
{
TestMemInit();
BSL_GLOBAL_Init();
int8_t tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
HITLS_X509_ExtKeyUsage ku = {critical, usage};
BSL_ASN1_Buffer encode = {0};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.len, expect->len);
ASSERT_COMPARE("Ext: keyUsage", encode.buff, encode.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encode.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_EncodeAKiSki_TC001(int critical1, int critical2, Hex *kid1, Hex *kid2, Hex *expect)
{
TestMemInit();
BSL_GLOBAL_Init();
int8_t tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
HITLS_X509_ExtAki aki = {critical1, {kid1->x, kid1->len}, NULL, {0}};
HITLS_X509_ExtSki ski = {critical2, {kid2->x, kid2->len}};
BSL_ASN1_Buffer encode = {0};
HITLS_X509_Cert *cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SKI, &ski, sizeof(HITLS_X509_ExtSki)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)), 0);
ASSERT_EQ(HITLS_X509_EncodeExt(tag, cert->tbs.ext.extList, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.len, expect->len);
ASSERT_COMPARE("Ext:aki ski", encode.buff, encode.len, expect->x, expect->len);
EXIT:
HITLS_X509_CertFree(cert);
BSL_SAL_Free(encode.buff);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
typedef struct {
int32_t type;
Hex *value;
} TestGeneralNameMap;
/* BEGIN_CASE */
void SDV_X509_EXT_ParseGeneralNames_TC001(Hex *encode, Hex *ip, Hex *uri, Hex *rfc822, Hex *regId, Hex *dns)
{
TestGeneralNameMap map[] = {
{HITLS_X509_GN_DNNAME, NULL},
{HITLS_X509_GN_IP, ip},
{HITLS_X509_GN_URI, uri},
{HITLS_X509_GN_OTHER, NULL},
{HITLS_X509_GN_EMAIL, rfc822},
{HITLS_X509_GN_RID, regId},
{HITLS_X509_GN_DNS, dns},
};
TestMemInit();
BslList *list = BSL_LIST_New(sizeof(HITLS_X509_GeneralName));
ASSERT_NE(list, NULL);
ASSERT_EQ(HITLS_X509_ParseGeneralNames(encode->x, encode->len, list), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), sizeof(map) / sizeof(map[0]));
HITLS_X509_GeneralName *name = NULL;
uint32_t idx = 0;
for (name = BSL_LIST_GET_FIRST(list); name != NULL; name = BSL_LIST_GET_NEXT(list), idx++) {
ASSERT_EQ(name->type, map[idx].type);
if (map[idx].value != NULL) {
ASSERT_COMPARE("gn", name->value.data, name->value.dataLen, map[idx].value->x, map[idx].value->len);
}
}
EXIT:
HITLS_X509_ClearGeneralNames(list);
BSL_SAL_Free(list);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_ParseGeneralNames_Error_TC001(Hex *encode, int ret)
{
BSL_GLOBAL_Init();
TestMemInit();
BslList *list = BSL_LIST_New(sizeof(HITLS_X509_GeneralName));
ASSERT_NE(list, NULL);
ASSERT_EQ(HITLS_X509_ParseGeneralNames(encode->x, encode->len, list), ret);
EXIT:
BSL_SAL_Free(list);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_ParseSki_TC001(Hex *encode, int ret, Hex *kid)
{
BSL_GLOBAL_Init();
TestMemInit();
HITLS_X509_ExtSki ski = {0};
HITLS_X509_ExtEntry entry = {BSL_CID_CE_SUBJECTKEYIDENTIFIER, {0}, true, {0, encode->len, encode->x}};
ASSERT_EQ(HITLS_X509_ParseSubjectKeyId(&entry, &ski), ret);
if (ret == 0) {
ASSERT_EQ(ski.critical, entry.critical);
ASSERT_COMPARE("Subject kid", kid->x, kid->len, ski.kid.data, ski.kid.dataLen);
}
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_ParseExtendedKu_TC001(Hex *encode, Hex *ku1, Hex *ku2, Hex *ku3)
{
BSL_GLOBAL_Init();
TestMemInit();
Hex *values[] = {ku1, ku2, ku3};
uint32_t cnt = sizeof(values) / sizeof(values[0]);
HITLS_X509_ExtExKeyUsage exku = {0};
HITLS_X509_ExtEntry entry = {BSL_CID_CE_EXTKEYUSAGE, {0}, true, {0, encode->len, encode->x}};
ASSERT_EQ(HITLS_X509_ParseExtendedKeyUsage(&entry, &exku), 0);
ASSERT_EQ(exku.critical, entry.critical);
ASSERT_EQ(BSL_LIST_COUNT(exku.oidList), cnt);
uint32_t idx = 0;
for (BSL_Buffer *data = BSL_LIST_GET_FIRST(exku.oidList); data != NULL; data = BSL_LIST_GET_NEXT(exku.oidList)) {
ASSERT_COMPARE("Extended key usage", values[idx]->x, values[idx]->len, data->data, data->dataLen);
idx++;
}
EXIT:
HITLS_X509_ClearExtendedKeyUsage(&exku);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_ParseAki_TC001(Hex *encode, Hex *kid, Hex *serial, int nameCnt)
{
HITLS_X509_ExtAki aki = {0};
HITLS_X509_ExtEntry entry = {BSL_CID_CE_AUTHORITYKEYIDENTIFIER, {0}, true, {0, encode->len, encode->x}};
TestMemInit();
ASSERT_EQ(HITLS_X509_ParseAuthorityKeyId(&entry, &aki), 0);
ASSERT_EQ(aki.critical, entry.critical);
ASSERT_COMPARE("kid", aki.kid.data, aki.kid.dataLen, kid->x, kid->len);
ASSERT_COMPARE("serial", aki.serialNum.data, aki.serialNum.dataLen, serial->x, serial->len);
ASSERT_EQ(BSL_LIST_COUNT(aki.issuerName), nameCnt);
EXIT:
HITLS_X509_ClearAuthorityKeyId(&aki);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_ParseSan_TC001(Hex *encode, int ret, int gnNameCnt, int gnType1, int gnType2, Hex *rfc822, Hex *dnType,
Hex *dnValue)
{
HITLS_X509_ExtSan san = {0};
HITLS_X509_ExtEntry entry = {BSL_CID_CE_SUBJECTALTNAME, {0}, true, {0, encode->len, encode->x}};
HITLS_X509_GeneralName *gnName = NULL;
BslList *dirNameList = NULL;
HITLS_X509_NameNode *dirName = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_ParseSubjectAltName(&entry, &san), ret);
if (ret == 0) {
ASSERT_EQ(san.critical, entry.critical);
ASSERT_EQ(BSL_LIST_COUNT(san.names), gnNameCnt);
gnName = BSL_LIST_GET_FIRST(san.names);
ASSERT_EQ(gnName->type, gnType1);
ASSERT_COMPARE("gnName 1", rfc822->x, rfc822->len, gnName->value.data, gnName->value.dataLen);
gnName = BSL_LIST_GET_NEXT(san.names);
ASSERT_EQ(gnName->type, gnType2);
dirNameList = (BslList *)gnName->value.data;
ASSERT_EQ(BSL_LIST_COUNT(dirNameList), 1 + 1); // layer 1 and layer 2
dirName = BSL_LIST_GET_FIRST(dirNameList); // layer 1
dirName = BSL_LIST_GET_NEXT(dirNameList); // layer 2
ASSERT_COMPARE("dnname type", dirName->nameType.buff, dirName->nameType.len, dnType->x, dnType->len);
ASSERT_COMPARE("dnname value", dirName->nameValue.buff, dirName->nameValue.len, dnValue->x, dnValue->len);
}
EXIT:
HITLS_X509_ClearSubjectAltName(&san);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_EXT_GetSki_TC001(Hex *encode, int ret, int critical, Hex *kid)
{
TestMemInit();
BSL_ASN1_Buffer asnExt = {BSL_ASN1_CLASS_CTX_SPECIFIC | BSL_ASN1_TAG_CONSTRUCTED, encode->len, encode->x};
bool getIsExist;
HITLS_X509_ExtSki ski = {0};
HITLS_X509_Ext *ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
ASSERT_NE(ext, NULL);
ASSERT_EQ(HITLS_X509_ParseExt(&asnExt, ext), 0);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_CHECK_SKI, &getIsExist, sizeof(bool)), 0);
ASSERT_EQ(getIsExist, ret == HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_GET_SKI, &ski, sizeof(HITLS_X509_ExtSki)), ret);
ASSERT_EQ(ski.critical, critical);
ASSERT_COMPARE("Get ski", ski.kid.data, ski.kid.dataLen, kid->x, kid->len);
EXIT:
HITLS_X509_ExtFree(ext);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_ExtParamCheck_TC001(void)
{
TestMemInit();
BSL_GLOBAL_Init();
ASSERT_EQ(HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CERT), NULL);
ASSERT_EQ(HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CRL), NULL);
HITLS_X509_Ext *ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
ASSERT_NE(ext, NULL);
HITLS_X509_ExtFree(ext);
EXIT:
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_SIGN_Api_TC001(void)
{
CRYPT_EAL_PkeyCtx *prvKey = NULL;
uint8_t obj = 1;
TestMemInit();
prvKey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(prvKey, NULL);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA3_384, prvKey, NULL, &obj, TestSignCb), HITLS_X509_ERR_HASHID);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA384, prvKey, NULL, &obj, TestSignCb), BSL_MALLOC_FAIL);
EXIT:
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_SIGN_Func_TC001(char *keyPath, int keyFormat, int keyType, int mdId, int pad, int hashId, int mgfId,
int saltLen, int ret)
{
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
HITLS_X509_SignAlgParam *algParamPtr = NULL;
uint8_t obj = 1;
if (pad == 0) {
algParamPtr = NULL;
} else if (pad == CRYPT_EMSA_PSS) {
algParam.algId = BSL_CID_RSASSAPSS;
algParam.rsaPss.mdId = hashId;
algParam.rsaPss.mgfId = mgfId;
algParam.rsaPss.saltLen = saltLen;
algParamPtr = &algParam;
}
TestMemInit();
TestRandInit();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(keyFormat, keyType, keyPath, NULL, 0, &prvKey), 0);
ASSERT_EQ(HITLS_X509_Sign(mdId, prvKey, algParamPtr, &obj, TestSignCb), ret);
EXIT:
CRYPT_EAL_PkeyFreeCtx(prvKey);
TestRandDeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_SIGN_Func_TC002(void)
{
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
uint8_t obj = 1;
CRYPT_RsaPadType pad = CRYPT_EMSA_PKCSV15;
CRYPT_EAL_PkeyPara para = {0};
uint8_t e[] = {1, 0, 1};
para.id = CRYPT_PKEY_RSA;
para.para.rsaPara.e = e;
para.para.rsaPara.eLen = 3;
para.para.rsaPara.bits = 1024;
TestMemInit();
TestRandInit();
prvKey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(prvKey, NULL);
ASSERT_EQ(CRYPT_EAL_PkeySetPara(prvKey, ¶), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeyGen(prvKey), 0);
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(prvKey, CRYPT_CTRL_SET_RSA_PADDING, &pad, sizeof(CRYPT_RsaPadType)), 0);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA224, prvKey, NULL, &obj, TestSignCb), 0);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA224, prvKey, &algParam, &obj, TestSignCb), HITLS_X509_ERR_SIGN_PARAM);
pad = CRYPT_EMSA_PSS;
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(prvKey, CRYPT_CTRL_SET_RSA_PADDING, &pad, sizeof(CRYPT_RsaPadType)), 0);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA224, prvKey, NULL, &obj, TestSignCb), 0);
CRYPT_RSA_PssPara pssPara = {1, CRYPT_MD_SHA256, CRYPT_MD_SHA256};
BSL_Param pssParam[4] = {
{CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &pssPara.mdId, sizeof(pssPara.mdId), 0},
{CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &pssPara.mgfId, sizeof(pssPara.mgfId), 0},
{CRYPT_PARAM_RSA_SALTLEN, BSL_PARAM_TYPE_INT32, &pssPara.saltLen, sizeof(pssPara.saltLen), 0},
BSL_PARAM_END};
ASSERT_EQ(CRYPT_EAL_PkeyCtrl(prvKey, CRYPT_CTRL_SET_RSA_EMSA_PSS, pssParam, 0), 0);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA224, prvKey, NULL, &obj, TestSignCb), HITLS_X509_ERR_MD_NOT_MATCH);
ASSERT_EQ(HITLS_X509_Sign(CRYPT_MD_SHA256, prvKey, NULL, &obj, TestSignCb), 0);
EXIT:
CRYPT_EAL_PkeyFreeCtx(prvKey);
TestRandDeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_PrintCtrl_TC001(void)
{
TestMemInit();
BSL_UIO *uio = BSL_UIO_New(BSL_UIO_BufferMethod());
ASSERT_NE(uio, NULL);
uint32_t flag = 0;
BslList list = {0};
ASSERT_EQ(HITLS_PKI_PrintCtrl(0xff, NULL, 0, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_SET_PRINT_FLAG, NULL, 0, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_SET_PRINT_FLAG, &flag, 0, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_PRINT_DNNAME, NULL, sizeof(BslList), uio), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_PRINT_DNNAME, &list, sizeof(BslList), NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_PRINT_DNNAME, &list, 0, uio), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
BSL_UIO_Free(uio);
return;
}
/* END_CASE */
static int32_t ReadFile(const char *filePath, uint8_t *buff, uint32_t buffLen, uint32_t *outLen)
{
FILE *fp = NULL;
int32_t ret = -1;
fp = fopen(filePath, "rb");
if (fp == NULL) {
return ret;
}
if (fseek(fp, 0, SEEK_END) != 0) {
goto EXIT;
}
long fileSize = ftell(fp);
if (fileSize < 0 || (uint32_t)fileSize > buffLen) {
goto EXIT;
}
rewind(fp);
size_t readSize = fread(buff, 1, fileSize, fp);
if (readSize != (size_t)fileSize) {
goto EXIT;
}
*outLen = (uint32_t)fileSize;
ret = 0;
EXIT:
(void)fclose(fp);
return ret;
}
static int32_t PrintBuffTest(int cmd, BSL_Buffer *data, char *log, Hex *expect, bool isExpectFile)
{
int32_t ret = -1;
uint8_t dnBuf[MAX_BUFF_SIZE] = {};
uint32_t dnBufLen = sizeof(dnBuf);
uint8_t expectBuf[MAX_BUFF_SIZE] = {};
uint32_t expectBufLen = sizeof(expectBuf);
BSL_UIO *uio = BSL_UIO_New(BSL_UIO_MemMethod());
ASSERT_NE(uio, NULL);
ASSERT_EQ(HITLS_PKI_PrintCtrl(cmd, data->data, data->dataLen, uio),
HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_UIO_Read(uio, dnBuf, MAX_BUFF_SIZE, &dnBufLen), 0);
if (isExpectFile) {
ASSERT_EQ(ReadFile((char *)expect->x, expectBuf, MAX_BUFF_SIZE, &expectBufLen), 0);
ASSERT_COMPARE(log, expectBuf, expectBufLen, dnBuf, dnBufLen - 1); // Ignore line break differences
} else {
ASSERT_COMPARE(log, expect->x, expect->len, dnBuf, dnBufLen - 1); // Ignore line break differences
}
ret = 0;
EXIT:
BSL_UIO_Free(uio);
return ret;
}
/* BEGIN_CASE */
void SDV_HITLS_X509_PrintDn_TC002(char *certPath, int format, int printFlag, char *expect, Hex *multiExpect)
{
TestMemInit();
HITLS_X509_Cert *cert = NULL;
BslList *rawIssuer = NULL;
Hex expectName = {};
if (printFlag == HITLS_PKI_PRINT_DN_MULTILINE) {
expectName.x = multiExpect->x;
expectName.len = multiExpect->len;
} else {
expectName.x = (uint8_t *)expect;
expectName.len = strlen(expect);
}
ASSERT_EQ(HITLS_X509_CertParseFile(format, certPath, &cert), HITLS_PKI_SUCCESS);
ASSERT_NE(cert, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ISSUER_DN, &rawIssuer, sizeof(BslList *)), HITLS_PKI_SUCCESS);
BSL_Buffer data = {(uint8_t *)rawIssuer, sizeof(BslList)};
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_SET_PRINT_FLAG, &printFlag, sizeof(int), NULL), HITLS_PKI_SUCCESS);
ASSERT_EQ(PrintBuffTest(HITLS_PKI_PRINT_DNNAME, &data, "Print Distinguish name", &expectName, false), 0);
EXIT:
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_DecodeBuffKey_Ex_TC001(void)
{
#ifndef HITLS_CRYPTO_PROVIDER
SKIP_TEST();
#else
TestMemInit();
BSL_GLOBAL_Init();
CRYPT_EAL_PkeyCtx *key = NULL;
BSL_Buffer encode = {0};
BSL_Buffer pwd = {0};
uint8_t data[10] = {0};
uint8_t pwdData[10] = {0};
// Test NULL parameters
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PUBKEY_RSA", NULL, NULL, NULL),
CRYPT_INVALID_ARG);
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PUBKEY_RSA", &encode, NULL, NULL),
CRYPT_INVALID_ARG);
// Test invalid encode buffer
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PUBKEY_RSA", &encode, &pwd, &key),
CRYPT_INVALID_ARG);
encode.data = data;
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PUBKEY_RSA", &encode, &pwd, &key),
CRYPT_INVALID_ARG);
// Test invalid format
encode.dataLen = sizeof(data);
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "UNKNOWN_FORMAT", "PUBKEY_RSA", &encode, &pwd, &key),
CRYPT_DECODE_ERR_NO_USABLE_DECODER);
// Test invalid type
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "UNKNOWN_TYPE", &encode, &pwd, &key),
CRYPT_DECODE_ERR_NO_USABLE_DECODER);
// Test invalid password buffer for encrypted private key
pwd.data = pwdData;
pwd.dataLen = PWD_MAX_LEN + 1;
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PRIKEY_PKCS8_ENCRYPT",
&encode, &pwd, &key), CRYPT_INVALID_ARG);
// Test NULL password data with non-zero length
pwd.data = NULL;
pwd.dataLen = 10;
ASSERT_EQ(CRYPT_EAL_ProviderDecodeBuffKey(NULL, NULL, BSL_CID_UNKNOWN, "ASN1", "PRIKEY_PKCS8_ENCRYPT",
&encode, &pwd, &key), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_CRYPT_EAL_DecodeFileKey_Ex_TC001(void)
{
#ifndef HITLS_CRYPTO_PROVIDER
SKIP_TEST();
#else
TestMemInit();
BSL_GLOBAL_Init();
CRYPT_EAL_PkeyCtx *key = NULL;
BSL_Buffer pwd = {0};
uint8_t pwdData[10] = {0};
// Test NULL parameters
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_RSA, "ASN1", "PUBKEY_RSA", NULL, NULL, NULL),
CRYPT_INVALID_ARG);
// Test invalid path
char longPath[PATH_MAX_LEN + 2] = {0};
memset(longPath, 'a', PATH_MAX_LEN + 1);
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_RSA, "ASN1", "PUBKEY_RSA", longPath, &pwd, &key),
CRYPT_INVALID_ARG);
// Test invalid format
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_RSA, "UNKNOWN_FORMAT", "PUBKEY_RSA",
"../testdata/cert/asn1/rsa2048pub_pkcs1.der", &pwd, &key), CRYPT_DECODE_NO_SUPPORT_FORMAT);
// Test invalid type
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_RSA, "ASN1", "UNKNOWN_TYPE",
"../testdata/cert/asn1/rsa2048pub_pkcs1.der", &pwd, &key), CRYPT_DECODE_NO_SUPPORT_TYPE);
// Test invalid password buffer for encrypted private key
pwd.data = pwdData;
pwd.dataLen = PWD_MAX_LEN + 1;
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_RSA, "ASN1", "PRIKEY_PKCS8_ENCRYPT",
"../testdata/cert/asn1/prime256v1_pkcs8_enc.der", &pwd, &key), CRYPT_INVALID_ARG);
// Test NULL password data with non-zero length
pwd.data = NULL;
pwd.dataLen = 10;
ASSERT_EQ(CRYPT_EAL_ProviderDecodeFileKey(NULL, NULL, CRYPT_PKEY_ECDSA, "ASN1", "PRIKEY_PKCS8_ENCRYPT",
"../testdata/cert/asn1/prime256v1_pkcs8_enc.der", &pwd, &key), CRYPT_INVALID_ARG);
EXIT:
BSL_GLOBAL_DeInit();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_HITLS_X509_PrintCrl_TC001(char *certPath, int format, int printFlag, char *expectFile)
{
TestMemInit();
HITLS_X509_Crl *crl = NULL;
int32_t *version = NULL;
Hex expect = { (uint8_t *)expectFile, 0};
ASSERT_EQ(HITLS_X509_CrlParseFile(format, certPath, &crl), HITLS_PKI_SUCCESS);
ASSERT_NE(crl, NULL);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_VERSION, &version, sizeof(int32_t)), HITLS_PKI_SUCCESS);
BSL_Buffer data = {(uint8_t *)crl, sizeof(HITLS_X509_Crl *)};
ASSERT_EQ(HITLS_PKI_PrintCtrl(HITLS_PKI_SET_PRINT_FLAG, &printFlag, sizeof(int), NULL), HITLS_PKI_SUCCESS);
ASSERT_EQ(PrintBuffTest(HITLS_PKI_PRINT_CRL, &data, "Print crl file", &expect, true), 0);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/common/test_suite_sdv_common.c | C | unknown | 49,180 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdbool.h>
#include "securec.h"
#include "bsl_sal.h"
#include "bsl_list.h"
#include "bsl_obj.h"
#include "bsl_types.h"
#include "crypt_errno.h"
#include "crypt_types.h"
#include "crypt_params_key.h"
#include "crypt_eal_pkey.h"
#include "crypt_eal_codecs.h"
#include "crypt_eal_rand.h"
#include "crypt_util_rand.h"
#include "hitls_pki_cert.h"
#include "hitls_pki_crl.h"
#include "hitls_pki_csr.h"
#include "hitls_pki_errno.h"
#include "hitls_pki_types.h"
#include "hitls_pki_utils.h"
#include "hitls_x509_verify.h"
/* END_HEADER */
static inline void UnusedParam1(int param1, int param2, int param3)
{
(void)param1;
(void)param2;
(void)param3;
}
static inline void UnusedParam2(int param1, int param2, void *param3)
{
(void)param1;
(void)param2;
(void)param3;
}
static bool PkiSkipTest(int32_t algId, int32_t format)
{
#ifndef HITLS_BSL_PEM
if (format == BSL_FORMAT_PEM) {
return true;
}
#else
(void)format;
#endif
switch (algId) {
#ifdef HITLS_CRYPTO_RSA
case CRYPT_PKEY_RSA:
case BSL_CID_RSASSAPSS:
return false;
#endif
#ifdef HITLS_CRYPTO_ECDSA
case CRYPT_PKEY_ECDSA:
return false;
#endif
#ifdef HITLS_CRYPTO_SM2
case CRYPT_PKEY_SM2:
return false;
#endif
#ifdef HITLS_CRYPTO_ED25519
case CRYPT_PKEY_ED25519:
return false;
#endif
default:
return true;
}
}
#ifdef HITLS_CRYPTO_KEY_ENCODE
#ifdef HITLS_CRYPTO_RSA
static int32_t SetRsaPara(CRYPT_EAL_PkeyCtx *pkey)
{
uint8_t e[] = {1, 0, 1}; // RSA public exponent
CRYPT_EAL_PkeyPara para = {0};
para.id = CRYPT_PKEY_RSA;
para.para.rsaPara.e = e;
para.para.rsaPara.eLen = 3; // public exponent length = 3
para.para.rsaPara.bits = 2048;
return CRYPT_EAL_PkeySetPara(pkey, ¶);
}
static int32_t SetRsaPssPara(CRYPT_EAL_PkeyCtx *pkey)
{
int32_t mdId = CRYPT_MD_SHA256;
int32_t saltLen = 20; // 20 bytes salt
BSL_Param pssParam[4] = {
{CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0},
{CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0},
{CRYPT_PARAM_RSA_SALTLEN, BSL_PARAM_TYPE_INT32, &saltLen, sizeof(saltLen), 0},
BSL_PARAM_END};
return CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_RSA_EMSA_PSS, pssParam, 0);
}
#endif // HITLS_CRYPT_RSA
static CRYPT_EAL_PkeyCtx *GenKey(int32_t algId, int32_t curveId)
{
CRYPT_EAL_PkeyCtx *pkey = CRYPT_EAL_PkeyNewCtx(algId == BSL_CID_RSASSAPSS ? BSL_CID_RSA : algId);
ASSERT_NE(pkey, NULL);
if (algId == CRYPT_PKEY_ECDSA) {
ASSERT_EQ(CRYPT_EAL_PkeySetParaById(pkey, curveId), CRYPT_SUCCESS);
}
#ifdef HITLS_CRYPTO_RSA
if (algId == CRYPT_PKEY_RSA) {
ASSERT_EQ(SetRsaPara(pkey), CRYPT_SUCCESS);
}
if (algId == BSL_CID_RSASSAPSS) {
ASSERT_EQ(SetRsaPara(pkey), CRYPT_SUCCESS);
ASSERT_EQ(SetRsaPssPara(pkey), CRYPT_SUCCESS);
}
#endif
ASSERT_EQ(CRYPT_EAL_PkeyGen(pkey), CRYPT_SUCCESS);
return pkey;
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return NULL;
}
/**
* Generate DER/PEM public/private key: rsa, ecc, sm2, ed25519
*/
static int32_t TestEncodeKey(int32_t algId, int32_t type, int32_t curveId, char *path)
{
BSL_Buffer encode = {0};
int32_t ret = CRYPT_MEM_ALLOC_FAIL;
CRYPT_EAL_PkeyCtx *pkey = GenKey(algId, curveId);
ASSERT_NE(pkey, NULL);
#ifdef HITLS_BSL_SAL_FILE
if (path != NULL) {
ASSERT_EQ(CRYPT_EAL_EncodeFileKey(pkey, NULL, BSL_FORMAT_ASN1, type, path), CRYPT_SUCCESS);
}
#ifdef HITLS_BSL_PEM
if (path != NULL) {
ASSERT_EQ(CRYPT_EAL_EncodeFileKey(pkey, NULL, BSL_FORMAT_PEM, type, path), CRYPT_SUCCESS);
}
#endif
#else
(void)path;
#endif
ASSERT_EQ(CRYPT_EAL_EncodeBuffKey(pkey, NULL, BSL_FORMAT_ASN1, type, &encode), CRYPT_SUCCESS);
BSL_SAL_FREE(encode.data);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(CRYPT_EAL_EncodeBuffKey(pkey, NULL, BSL_FORMAT_PEM, type, &encode), CRYPT_SUCCESS);
BSL_SAL_FREE(encode.data);
#endif
ret = CRYPT_SUCCESS;
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
return ret;
}
#endif // HITLS_CRYPTO_KEY_ENCODE
#if defined(HITLS_PKI_X509_CRL_GEN) || defined(HITLS_PKI_X509_CSR_GEN) || defined(HITLS_PKI_X509_CRT_GEN)
static char g_sm2DefaultUserid[] = "1234567812345678";
static void SetSignParam(int32_t algId, int32_t mdId, HITLS_X509_SignAlgParam *algParam, CRYPT_RSA_PssPara *pssParam)
{
if (algId == BSL_CID_RSASSAPSS) {
algParam->algId = BSL_CID_RSASSAPSS;
pssParam->mdId = mdId;
pssParam->mgfId = mdId;
pssParam->saltLen = 20; // 20 bytes salt
algParam->rsaPss = *pssParam;
}
if (algId == BSL_CID_SM2DSA) {
algParam->algId = BSL_CID_SM2DSAWITHSM3;
algParam->sm2UserId.data = (uint8_t *)g_sm2DefaultUserid;
algParam->sm2UserId.dataLen = (uint32_t)strlen(g_sm2DefaultUserid);
}
}
#endif
#if defined(HITLS_PKI_X509_CRL_GEN) || defined(HITLS_PKI_X509_CRT_GEN)
static BslList* GenDNList(void)
{
HITLS_X509_DN dnName1[1] = {{BSL_CID_AT_COMMONNAME, (uint8_t *)"OH", 2}};
HITLS_X509_DN dnName2[1] = {{BSL_CID_AT_COUNTRYNAME, (uint8_t *)"CN", 2}};
BslList *dirNames = HITLS_X509_DnListNew();
ASSERT_NE(dirNames, NULL);
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName1, 1), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName2, 1), HITLS_PKI_SUCCESS);
return dirNames;
EXIT:
HITLS_X509_DnListFree(dirNames);
return NULL;
}
static BslList* GenGeneralNameList(void)
{
char *str = "test";
HITLS_X509_GeneralName *email = NULL;
HITLS_X509_GeneralName *dns = NULL;
HITLS_X509_GeneralName *dname = NULL;
HITLS_X509_GeneralName *uri = NULL;
HITLS_X509_GeneralName *ip = NULL;
BslList *names = BSL_LIST_New(sizeof(HITLS_X509_GeneralName));
ASSERT_NE(names, NULL);
email = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
dns = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
dname = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
uri = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
ip = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
ASSERT_TRUE(email != NULL && dns != NULL && dname != NULL && uri != NULL && ip != NULL);
email->type = HITLS_X509_GN_EMAIL;
dns->type = HITLS_X509_GN_DNS;
uri->type = HITLS_X509_GN_URI;
dname->type = HITLS_X509_GN_DNNAME;
ip->type = HITLS_X509_GN_IP;
email->value.dataLen = strlen(str);
dns->value.dataLen = strlen(str);
uri->value.dataLen = strlen(str);
dname->value.dataLen = sizeof(BslList *);
ip->value.dataLen = strlen(str);
email->value.data = BSL_SAL_Dump(str, strlen(str));
dns->value.data = BSL_SAL_Dump(str, strlen(str));
uri->value.data = BSL_SAL_Dump(str, strlen(str));
dname->value.data = (uint8_t *)GenDNList();
ip->value.data = BSL_SAL_Dump(str, strlen(str));
ASSERT_TRUE(email->value.data != NULL && dns->value.data != NULL && uri->value.data != NULL && dname->value.data != NULL && ip->value.data != NULL);
ASSERT_EQ(BSL_LIST_AddElement(names, email, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, dns, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, uri, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, dname, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, ip, BSL_LIST_POS_END), 0);
return names;
EXIT:
HITLS_X509_FreeGeneralName(email);
HITLS_X509_FreeGeneralName(dns);
HITLS_X509_FreeGeneralName(dname);
HITLS_X509_FreeGeneralName(uri);
HITLS_X509_FreeGeneralName(ip);
BSL_LIST_FREE(names, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeGeneralName);
return NULL;
}
#endif
#ifdef HITLS_PKI_X509_CRL_GEN
static int32_t SetCrlEntry(HITLS_X509_Crl *crl)
{
int32_t ret = 1;
BSL_TIME revokeTime = {2030, 1, 1, 0, 0, 0, 0, 0};
uint8_t serialNum[4] = {0x11, 0x22, 0x33, 0x44};
HITLS_X509_RevokeExtReason reason = {0, 1}; // keyCompromise
BSL_TIME invalidTime = revokeTime;
HITLS_X509_RevokeExtTime invalidTimeExt = {false, invalidTime};
HITLS_X509_CrlEntry *entry = HITLS_X509_CrlEntryNew();
ASSERT_NE(entry, NULL);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_SERIALNUM, serialNum, sizeof(serialNum)),0);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REVOKE_TIME, &revokeTime, sizeof(BSL_TIME)),0);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REASON, &reason,
sizeof(HITLS_X509_RevokeExtReason)), 0);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_INVALID_TIME, &invalidTimeExt,
sizeof(HITLS_X509_RevokeExtTime)), 0);
HITLS_X509_RevokeExtCertIssuer certIssuer = {true, NULL};
certIssuer.issuerName = GenGeneralNameList();
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_CERTISSUER,
&certIssuer, sizeof(HITLS_X509_ExtSan)), 0);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_CRL_ADD_REVOKED_CERT, entry, 0), 0);
ret = 0;
EXIT:
HITLS_X509_CrlEntryFree(entry);
BSL_LIST_FREE(certIssuer.issuerName, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeGeneralName);
return ret;
}
#endif // HITLS_PKI_X509_CRL_GEN
#ifdef HITLS_PKI_X509_CSR_GEN
static int32_t FillExt(HITLS_X509_Ext *ext)
{
HITLS_X509_ExtBCons bCons = {true, false, 1};
HITLS_X509_ExtKeyUsage ku = {true, HITLS_X509_EXT_KU_DIGITAL_SIGN | HITLS_X509_EXT_KU_NON_REPUDIATION};
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_BCONS, &bCons, sizeof(HITLS_X509_ExtBCons)), 0);
return 0;
EXIT:
return 1;
}
#endif // HITLS_PKI_X509_CSR_GEN
#ifdef HITLS_PKI_X509_CRT_GEN
static void FreeListData(void *data)
{
(void)data;
return;
}
static int32_t SetCertExt(HITLS_X509_Cert *cert)
{
int32_t ret = 1;
uint8_t kid[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
HITLS_X509_ExtBCons bCons = {true, true, 1};
HITLS_X509_ExtKeyUsage ku = {true, HITLS_X509_EXT_KU_DIGITAL_SIGN | HITLS_X509_EXT_KU_NON_REPUDIATION};
HITLS_X509_ExtAki aki = {true, {kid, sizeof(kid)}, NULL, {0}};
HITLS_X509_ExtSki ski = {true, {kid, sizeof(kid)}};
HITLS_X509_ExtExKeyUsage exku = {true, NULL};
HITLS_X509_ExtSan san = {true, NULL};
BSL_Buffer oidBuff = {0};
BslOidString *oid = NULL;
BslList *oidList = BSL_LIST_New(sizeof(BSL_Buffer));
ASSERT_TRUE(oidList != NULL);
oid = BSL_OBJ_GetOID(BSL_CID_KP_SERVERAUTH);
ASSERT_NE(oid, NULL);
oidBuff.data = (uint8_t *)oid->octs;
oidBuff.dataLen = oid->octetLen;
ASSERT_EQ(BSL_LIST_AddElement(oidList, &oidBuff, BSL_LIST_POS_END), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_BCONS, &bCons, sizeof(HITLS_X509_ExtBCons)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SKI, &ski, sizeof(HITLS_X509_ExtSki)), 0);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)), 0);
exku.oidList = oidList;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_EXKUSAGE, &exku, sizeof(HITLS_X509_ExtExKeyUsage)), 0);
san.names = GenGeneralNameList();
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_SET_SAN, &san, sizeof(HITLS_X509_ExtSan)), 0);
ret = 0;
EXIT:
BSL_LIST_FREE(oidList, (BSL_LIST_PFUNC_FREE)FreeListData);
BSL_LIST_FREE(san.names, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeGeneralName);
return ret;
}
#endif // HITLS_PKI_X509_CRT_GEN
/* BEGIN_CASE */
void SDV_PKI_GEN_KEY_TC001(int algId, int type, int curveId)
{
#ifdef HITLS_CRYPTO_KEY_ENCODE
if (PkiSkipTest(algId, BSL_FORMAT_ASN1)) {
SKIP_TEST();
}
char *path = "tmp.key";
TestMemInit();
ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
ASSERT_EQ(TestEncodeKey(algId, type, curveId, path), CRYPT_SUCCESS);
EXIT:
TestRandDeInit();
remove(path);
#else
UnusedParam1(algId, type, curveId);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_GEN_ENCKEY_TC001(int algId, int curveId, int symId, Hex *pwd)
{
#if defined(HITLS_CRYPTO_KEY_ENCODE) && defined(HITLS_CRYPTO_KEY_EPKI)
if (PkiSkipTest(algId, BSL_FORMAT_ASN1)) {
SKIP_TEST();
}
CRYPT_Pbkdf2Param param = {
.pbesId = BSL_CID_PBES2,
.pbkdfId = BSL_CID_PBKDF2,
.hmacId = CRYPT_MAC_HMAC_SHA256,
.symId = symId,
.pwd = pwd->x,
.pwdLen = pwd->len,
.saltLen = 16,
.itCnt = 2000,
};
CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, ¶m};
CRYPT_EAL_PkeyCtx *pkey = NULL;
BSL_Buffer encode = {0};
TestMemInit();
ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
pkey = GenKey(algId, curveId);
ASSERT_NE(pkey, NULL);
ASSERT_EQ(CRYPT_EAL_EncodeBuffKey(pkey, ¶mEx, BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_ENCRYPT, &encode), 0);
EXIT:
TestRandDeInit();
CRYPT_EAL_PkeyFreeCtx(pkey);
BSL_SAL_FREE(encode.data);
#else
(void)algId;
(void)curveId;
(void)symId;
(void)pwd;
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_KEY_FILE_TC001(int algId, int format, int type, char *path)
{
#if defined(HITLS_BSL_SAL_FILE) && defined(HITLS_CRYPTO_KEY_DECODE)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
CRYPT_EAL_PkeyCtx *pkey = NULL;
TestMemInit();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(format, type, path, NULL, 0, &pkey), CRYPT_SUCCESS);
ASSERT_NE(pkey, NULL);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
#else
(void)algId;
(void)format;
(void)type;
(void)path;
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_ENCKEY_FILE_TC001(int algId, int format, int type, char *path, Hex *pass)
{
#if defined(HITLS_BSL_SAL_FILE) && defined(HITLS_CRYPTO_KEY_DECODE) && defined(HITLS_CRYPTO_KEY_EPKI)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
CRYPT_EAL_PkeyCtx *pkey = NULL;
TestMemInit();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(format, type, path, pass->x, pass->len, &pkey), CRYPT_SUCCESS);
ASSERT_NE(pkey, NULL);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
#else
(void)algId;
(void)format;
(void)type;
(void)path;
(void)pass;
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_KEY_BUFF_TC001(int algId, int format, int type, Hex *encode)
{
#ifdef HITLS_CRYPTO_KEY_DECODE
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
CRYPT_EAL_PkeyCtx *pkey = NULL;
TestMemInit();
ASSERT_EQ(CRYPT_EAL_DecodeBuffKey(format, type, (BSL_Buffer *)encode, NULL, 0, &pkey), CRYPT_SUCCESS);
ASSERT_NE(pkey, NULL);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
#else
(void)algId;
(void)format;
(void)type;
(void)encode;
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_GEN_CRL_TC001(int algId, int hashId, int curveId)
{
#ifdef HITLS_PKI_X509_CRL_GEN
if (PkiSkipTest(algId, BSL_FORMAT_ASN1)) {
SKIP_TEST();
}
char *path = "tmp.crl";
HITLS_X509_Crl *crl = NULL;
uint32_t version = 1;
BslList *issuer = NULL;
BSL_TIME beforeTime = {2025, 1, 1, 0, 0, 0, 0, 0};
BSL_TIME afterTime = {2035, 1, 1, 0, 0, 0, 0, 0};
uint8_t crlNumber[1] = {0x11};
HITLS_X509_SignAlgParam algParam = {0};
CRYPT_RSA_PssPara pssParam = {0};
BSL_Buffer encode = {0};
HITLS_X509_ExtCrlNumber crlNumberExt = {false, {crlNumber, 1}};
ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
CRYPT_EAL_PkeyCtx *prvKey = GenKey(algId, curveId);
ASSERT_NE(prvKey, NULL);
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
issuer = GenDNList();
ASSERT_NE(issuer, NULL);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuer, sizeof(BslList)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_EQ(SetCrlEntry(crl), 0);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_CRLNUMBER, &crlNumberExt, sizeof(HITLS_X509_ExtCrlNumber)), 0);
SetSignParam(algId, hashId, &algParam, &pssParam);
if (algId == CRYPT_PKEY_RSA) {
ASSERT_EQ(HITLS_X509_CrlSign(hashId, prvKey, NULL, crl), HITLS_PKI_SUCCESS);
} else {
ASSERT_EQ(HITLS_X509_CrlSign(hashId, prvKey, &algParam, crl), HITLS_PKI_SUCCESS);
}
#ifdef HITLS_BSL_SAL_FILE
ASSERT_EQ(HITLS_X509_CrlGenFile(BSL_FORMAT_ASN1, crl, path), HITLS_PKI_SUCCESS);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CrlGenFile(BSL_FORMAT_PEM, crl, path), HITLS_PKI_SUCCESS);
#endif
#endif
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encode), 0);
BSL_SAL_FREE(encode.data);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_PEM, crl, &encode), 0);
BSL_SAL_FREE(encode.data);
#endif
EXIT:
TestRandDeInit();
CRYPT_EAL_PkeyFreeCtx(prvKey);
HITLS_X509_CrlFree(crl);
HITLS_X509_DnListFree(issuer);
remove(path);
#else
UnusedParam1(algId, hashId, curveId);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CRL_FILE_TC001(int algId, int format, char *path)
{
#if defined(HITLS_PKI_X509_CRL_PARSE) && defined(HITLS_BSL_SAL_FILE)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Crl *crl = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CrlParseFile(format, path, &crl), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CrlFree(crl);
#else
UnusedParam2(algId, format, path);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CRL_BUFF_TC001(int algId, int format, Hex *encode)
{
#ifdef HITLS_PKI_X509_CRL_PARSE
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Crl *crl = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CrlParseBuff(format, (BSL_Buffer *)encode, &crl), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CrlFree(crl);
#else
UnusedParam2(algId, format, encode);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_GEN_CSR_TC001(int algId, int hashId, int curveId)
{
#ifdef HITLS_PKI_X509_CSR_GEN
if (PkiSkipTest(algId, BSL_FORMAT_ASN1)) {
SKIP_TEST();
}
char *path = "tmp.csr";
HITLS_X509_Csr *csr = NULL;
BSL_Buffer encode = {0};
HITLS_X509_DN dnName1[1] = {{BSL_CID_AT_COMMONNAME, (uint8_t *)"OH", 2}};
HITLS_X509_DN dnName2[1] = {{BSL_CID_AT_COUNTRYNAME, (uint8_t *)"CN", 2}};
HITLS_X509_Attrs *attrs = NULL;
HITLS_X509_Ext *ext = NULL;
HITLS_X509_SignAlgParam algParam = {0};
CRYPT_RSA_PssPara pssParam = {0};
TestMemInit();
ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
CRYPT_EAL_PkeyCtx *key = GenKey(algId, curveId);
ASSERT_NE(key, NULL);
csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
ASSERT_NE(ext, NULL);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_SET_PUBKEY, key, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_ADD_SUBJECT_NAME, dnName1, 1), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_ADD_SUBJECT_NAME, dnName2, 1), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_EQ(FillExt(ext), 0);
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, HITLS_X509_ATTR_SET_REQUESTED_EXTENSIONS, ext, 0), 0);
SetSignParam(algId, hashId, &algParam, &pssParam);
if (algId == CRYPT_PKEY_RSA) {
ASSERT_EQ(HITLS_X509_CsrSign(hashId, key, NULL, csr), HITLS_PKI_SUCCESS);
} else {
ASSERT_EQ(HITLS_X509_CsrSign(hashId, key, &algParam, csr), HITLS_PKI_SUCCESS);
}
#ifdef HITLS_BSL_SAL_FILE
ASSERT_EQ(HITLS_X509_CsrGenFile(BSL_FORMAT_ASN1, csr, path), 0);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CsrGenFile(BSL_FORMAT_PEM, csr, path), 0);
#endif
#endif
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, csr, &encode), 0);
BSL_SAL_FREE(encode.data);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_PEM, csr, &encode), 0);
BSL_SAL_FREE(encode.data);
#endif
EXIT:
TestRandDeInit();
CRYPT_EAL_PkeyFreeCtx(key);
HITLS_X509_CsrFree(csr);
HITLS_X509_ExtFree(ext);
remove(path);
#else
UnusedParam1(algId, hashId, curveId);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CSR_FILE_TC001(int algId, int format, char *path)
{
#if defined(HITLS_PKI_X509_CSR_PARSE) && defined(HITLS_BSL_SAL_FILE)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Csr *csr = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CsrParseFile(format, path, &csr), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CsrFree(csr);
#else
UnusedParam2(algId, format, path);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CSR_BUFF_TC001(int algId, int format, Hex *encode)
{
#if defined(HITLS_PKI_X509_CSR_PARSE)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Csr *csr = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CsrParseBuff(format, (BSL_Buffer *)encode, &csr), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CsrFree(csr);
#else
UnusedParam2(algId, format, encode);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_GEN_CERT_TC001(int algId, int hashId, int curveId)
{
#ifdef HITLS_PKI_X509_CRT_GEN
if (PkiSkipTest(algId, BSL_FORMAT_ASN1)) {
SKIP_TEST();
}
char *path = "tmp.cert";
HITLS_X509_Cert *cert = NULL;
uint32_t version = 2; // v3 cert
uint8_t serialNum[4] = {0x11, 0x22, 0x33, 0x44};
BSL_TIME beforeTime = {2025, 1, 1, 0, 0, 0, 0, 0};
BSL_TIME afterTime = {2035, 1, 1, 0, 0, 0, 0, 0};
BslList *dnList = NULL;
HITLS_X509_SignAlgParam algParam = {0};
CRYPT_RSA_PssPara pssParam = {0};
BSL_Buffer encode = {0};
TestMemInit();
ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
CRYPT_EAL_PkeyCtx *key = GenKey(algId, curveId);
ASSERT_NE(key, NULL);
cert = HITLS_X509_CertNew();
ASSERT_NE(cert, NULL);
// set cert info
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SERIALNUM, serialNum, sizeof(serialNum)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_PUBKEY, key, 0), HITLS_PKI_SUCCESS);
dnList = GenDNList();
ASSERT_NE(dnList, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_ISSUER_DN, dnList, sizeof(BslList)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_SET_SUBJECT_DN, dnList, sizeof(BslList)), HITLS_PKI_SUCCESS);
ASSERT_EQ(SetCertExt(cert), 0);
// sign cert
SetSignParam(algId, hashId, &algParam, &pssParam);
if (algId == CRYPT_PKEY_RSA) {
ASSERT_EQ(HITLS_X509_CertSign(hashId, key, NULL, cert), HITLS_PKI_SUCCESS);
} else {
ASSERT_EQ(HITLS_X509_CertSign(hashId, key, &algParam, cert), HITLS_PKI_SUCCESS);
}
// generate cert file
#ifdef HITLS_BSL_SAL_FILE
ASSERT_EQ(HITLS_X509_CertGenFile(BSL_FORMAT_ASN1, cert, path), HITLS_PKI_SUCCESS);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CertGenFile(BSL_FORMAT_PEM, cert, path), HITLS_PKI_SUCCESS);
#endif
#endif
// generate cert buff
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, cert, &encode), 0);
BSL_SAL_FREE(encode.data);
#ifdef HITLS_BSL_PEM
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_PEM, cert, &encode), 0);
BSL_SAL_FREE(encode.data);
#endif
EXIT:
TestRandDeInit();
CRYPT_EAL_PkeyFreeCtx(key);
HITLS_X509_CertFree(cert);
HITLS_X509_DnListFree(dnList);
remove(path);
#else
UnusedParam1(algId, hashId, curveId);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CERT_FILE_TC001(int algId, int format, char *path)
{
#if defined(HITLS_PKI_X509_CRT_PARSE) && defined(HITLS_BSL_SAL_FILE)
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Cert *cert = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CertParseFile(format, path, &cert), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(cert);
#else
UnusedParam2(algId, format, path);
SKIP_TEST();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_PKI_PARSE_CERT_BUFF_TC001(int algId, int format, Hex *encode)
{
#ifdef HITLS_PKI_X509_CRT_PARSE
if (PkiSkipTest(algId, format)) {
SKIP_TEST();
}
HITLS_X509_Cert *cert = NULL;
TestMemInit();
ASSERT_EQ(HITLS_X509_CertParseBuff(format, (BSL_Buffer *)encode, &cert), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(cert);
#else
UnusedParam2(algId, format, encode);
SKIP_TEST();
#endif
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/common/test_suite_sdv_x509.c | C | unknown | 25,953 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "hitls_pki_crl.h"
#include "hitls_pki_cert.h"
#include "hitls_pki_errno.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "bsl_obj.h"
#include "crypt_encode_decode_key.h"
#include "sal_file.h"
#include "bsl_init.h"
#include "crypt_errno.h"
#include "hitls_crl_local.h"
#include "hitls_cert_local.h"
#include "stub_replace.h"
#include "hitls_pki_utils.h"
static char g_sm2DefaultUserid[] = "1234567812345678";
/* END_HEADER */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_FUNC_TC001(int format, char *path)
{
TestMemInit();
BSL_GLOBAL_Init();
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile((int32_t)format, path, &crl), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CrlFree(crl);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_CTRL_FUNC_TC001(char *path)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
int32_t ref = 0;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_REF_UP, &ref, sizeof(ref)), HITLS_PKI_SUCCESS);
ASSERT_EQ(ref, 2);
HITLS_X509_CrlFree(crl);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_VERSION_FUNC_TC001(char *path, int version)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->tbs.version, version);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_TBS_SIGNALG_FUNC_TC001(char *path, int signAlg,
int rsaPssHash, int rsaPssMgf1, int rsaPssSaltLen)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->tbs.signAlgId.algId, signAlg);
ASSERT_EQ(crl->tbs.signAlgId.rsaPssParam.mdId, rsaPssHash);
ASSERT_EQ(crl->tbs.signAlgId.rsaPssParam.mgfId, rsaPssMgf1);
ASSERT_EQ(crl->tbs.signAlgId.rsaPssParam.saltLen, rsaPssSaltLen);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_ISSUERNAME_FUNC_TC001(char *path, int count,
Hex *type1, int tag1, Hex *value1,
Hex *type2, int tag2, Hex *value2,
Hex *type3, int tag3, Hex *value3,
Hex *type4, int tag4, Hex *value4,
Hex *type5, int tag5, Hex *value5)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
BSL_ASN1_Buffer expAsan1Arr[] = {
{6, type1->len, type1->x}, {(uint8_t)tag1, value1->len, value1->x},
{6, type2->len, type2->x}, {(uint8_t)tag2, value2->len, value2->x},
{6, type3->len, type3->x}, {(uint8_t)tag3, value3->len, value3->x},
{6, type4->len, type4->x}, {(uint8_t)tag4, value4->len, value4->x},
{6, type5->len, type5->x}, {(uint8_t)tag5, value5->len, value5->x},
};
ASSERT_EQ(BSL_LIST_COUNT(crl->tbs.issuerName), count);
HITLS_X509_NameNode **nameNode = NULL;
nameNode = BSL_LIST_First(crl->tbs.issuerName);
for (int i = 0; i < count; i += 2) { // Iteration with step=2
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 1);
ASSERT_EQ((*nameNode)->nameType.tag, 0);
ASSERT_EQ((*nameNode)->nameType.buff, NULL);
ASSERT_EQ((*nameNode)->nameType.len, 0);
ASSERT_EQ((*nameNode)->nameValue.tag, 0);
ASSERT_EQ((*nameNode)->nameValue.buff, NULL);
ASSERT_EQ((*nameNode)->nameValue.len, 0);
nameNode = BSL_LIST_Next(crl->tbs.issuerName);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->layer, 2);
ASSERT_EQ((*nameNode)->nameType.tag, expAsan1Arr[i].tag);
ASSERT_COMPARE("nameType", (*nameNode)->nameType.buff, (*nameNode)->nameType.len,
expAsan1Arr[i].buff, expAsan1Arr[i].len);
ASSERT_EQ((*nameNode)->nameValue.tag, expAsan1Arr[i + 1].tag);
ASSERT_COMPARE("nameVlaue", (*nameNode)->nameValue.buff, (*nameNode)->nameValue.len,
expAsan1Arr[i + 1].buff, expAsan1Arr[i + 1].len);
nameNode = BSL_LIST_Next(crl->tbs.issuerName);
}
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_REVOKED_FUNC_TC001(char *path)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), BSL_SAL_ERR_FILE_LENGTH);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_REVOKED_FUNC_TC003(char *path, int count, int num,
int tag1, Hex *value1, int year1, int month1, int day1, int hour1, int minute1, int second1)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(crl->tbs.revokedCerts), count);
HITLS_X509_CrlEntry *nameNode = NULL;
nameNode = BSL_LIST_GET_FIRST(crl->tbs.revokedCerts);
for (int i = 1; i < num; i++) {
nameNode = BSL_LIST_GET_NEXT(crl->tbs.revokedCerts);
}
ASSERT_EQ(nameNode->serialNumber.tag, tag1);
ASSERT_COMPARE("", nameNode->serialNumber.buff, nameNode->serialNumber.len,
value1->x, value1->len);
ASSERT_EQ(nameNode->time.year, year1);
ASSERT_EQ(nameNode->time.month, month1);
ASSERT_EQ(nameNode->time.day, day1);
ASSERT_EQ(nameNode->time.hour, hour1);
ASSERT_EQ(nameNode->time.minute, minute1);
ASSERT_EQ(nameNode->time.second, second1);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_TIME_FUNC_TC001(char *path)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_X509_ERR_CHECK_TAG);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_START_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->tbs.validTime.start.year, year);
ASSERT_EQ(crl->tbs.validTime.start.month, month);
ASSERT_EQ(crl->tbs.validTime.start.day, day);
ASSERT_EQ(crl->tbs.validTime.start.hour, hour);
ASSERT_EQ(crl->tbs.validTime.start.minute, minute);
ASSERT_EQ(crl->tbs.validTime.start.second, second);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_END_TIME_FUNC_TC001(char *path,
int year, int month, int day, int hour, int minute, int second)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->tbs.validTime.end.year, year);
ASSERT_EQ(crl->tbs.validTime.end.month, month);
ASSERT_EQ(crl->tbs.validTime.end.day, day);
ASSERT_EQ(crl->tbs.validTime.end.hour, hour);
ASSERT_EQ(crl->tbs.validTime.end.minute, minute);
ASSERT_EQ(crl->tbs.validTime.end.second, second);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_EXTENSIONS_FUNC_TC001(char *path,
int tag1, Hex *value1, int tag2, Hex *value2)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(crl->tbs.crlExt.extList), 1);
HITLS_X509_ExtEntry **nameNode = NULL;
nameNode = BSL_LIST_First(crl->tbs.crlExt.extList);
ASSERT_NE((*nameNode), NULL);
ASSERT_EQ((*nameNode)->critical, 0);
ASSERT_EQ((*nameNode)->extnId.tag, tag1);
ASSERT_COMPARE("extnId", (*nameNode)->extnId.buff, (*nameNode)->extnId.len, value1->x, value1->len);
ASSERT_EQ((*nameNode)->extnValue.tag, tag2);
ASSERT_COMPARE("extnValue", (*nameNode)->extnValue.buff, (*nameNode)->extnValue.len, value2->x, value2->len);
EXIT:
HITLS_X509_CrlFree(crl);
BSL_GLOBAL_DeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_SIGNALG_FUNC_TC001(char *path, int signAlg,
int rsaPssHash, int rsaPssMgf1, int rsaPssSaltLen)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->signAlgId.algId, signAlg);
ASSERT_EQ(crl->signAlgId.rsaPssParam.mdId, rsaPssHash);
ASSERT_EQ(crl->signAlgId.rsaPssParam.mgfId, rsaPssMgf1);
ASSERT_EQ(crl->signAlgId.rsaPssParam.saltLen, rsaPssSaltLen);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_SIGNATURE_FUNC_TC001(char *path, Hex *buff, int unusedBits)
{
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->signature.len, buff->len);
ASSERT_COMPARE("signature", crl->signature.buff, crl->signature.len, buff->x, buff->len);
ASSERT_EQ(crl->signature.unusedBits, unusedBits);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_MUL_CRL_PARSE_FUNC_TC001(int format, char *path, int crlNum)
{
BSL_GLOBAL_Init();
HITLS_X509_List *list = NULL;
int32_t ret = HITLS_X509_CrlParseBundleFile(format, path, &list);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(list), crlNum);
EXIT:
BSL_LIST_FREE(list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CrlFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Encode_TC001(int format, char *path)
{
BSL_GLOBAL_Init();
HITLS_X509_Crl *crl = NULL;
BSL_Buffer encode = {0};
uint8_t *data = NULL;
uint32_t dataLen = 0;
int32_t ret = BSL_SAL_ReadFile(path, &data, &dataLen);
ASSERT_EQ(ret, BSL_SUCCESS);
BSL_Buffer ori = {data, dataLen};
ret = HITLS_X509_CrlParseBuff(format, &ori, &crl);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CrlGenBuff(format, crl, &encode);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
if (format == BSL_FORMAT_ASN1) {
ASSERT_EQ(dataLen, encode.dataLen);
} else {
ASSERT_EQ(dataLen, strlen((char *)encode.data));
}
ASSERT_EQ(memcmp(encode.data, data, dataLen), 0);
EXIT:
BSL_SAL_Free(data);
HITLS_X509_CrlFree(crl);
BSL_SAL_Free(encode.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_EncodeParam_TC001(void)
{
BSL_GLOBAL_Init();
HITLS_X509_Crl *crl = NULL;
BSL_Buffer encode = {0};
uint8_t *data = NULL;
uint32_t dataLen = 0;
ASSERT_EQ(BSL_SAL_ReadFile("../testdata/cert/pem/crl/crl_v2.pem", &data, &dataLen), BSL_SUCCESS);
BSL_Buffer ori = {data, dataLen};
ASSERT_EQ(HITLS_X509_CrlParseBuff(BSL_FORMAT_PEM, &ori, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, NULL, &encode), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_UNKNOWN, crl, &encode), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encode), 0);
EXIT:
BSL_SAL_Free(data);
HITLS_X509_CrlFree(crl);
BSL_SAL_Free(encode.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_EncodeFile_TC001(int format, char *path)
{
BSL_GLOBAL_Init();
HITLS_X509_Crl *crl = NULL;
uint8_t *data = NULL;
uint32_t dataLen = 0;
uint8_t *res = NULL;
uint32_t resLen;
int32_t ret = BSL_SAL_ReadFile(path, &data, &dataLen);
ASSERT_EQ(ret, BSL_SUCCESS);
BSL_Buffer ori = {data, dataLen};
ret = HITLS_X509_CrlParseBuff(format, &ori, &crl);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CrlGenFile(format, crl, "res.crl");
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_SAL_ReadFile("res.crl", &res, &resLen), BSL_SUCCESS);
ASSERT_COMPARE("crl_file com", data, dataLen, res, resLen);
EXIT:
BSL_SAL_Free(data);
HITLS_X509_CrlFree(crl);
BSL_SAL_Free(res);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Check_TC001(char *capath, char *crlpath, int res)
{
BSL_GLOBAL_Init();
HITLS_X509_Crl *crl = NULL;
HITLS_X509_Cert *cert = NULL;
void *pubKey = NULL;
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_UNKNOWN, crlpath, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_UNKNOWN, capath, &cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_PUBKEY, &pubKey, sizeof(void *)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlVerify(pubKey, crl), res);
EXIT:
CRYPT_EAL_PkeyFreeCtx(pubKey);
HITLS_X509_CrlFree(crl);
HITLS_X509_CertFree(cert);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_CTRL_ParamCheck_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
BSL_TIME time = {0};
BSL_ASN1_List *issuer = NULL;
uint32_t version = 1;
// Test null pointer parameter
ASSERT_EQ(HITLS_X509_CrlCtrl(NULL, HITLS_X509_SET_VERSION, &version, sizeof(version)),
HITLS_X509_ERR_INVALID_PARAM);
// Create a CRL object for subsequent tests
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
// Test invalid command
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, 0x7FFFFFFF, &version, sizeof(version)), HITLS_X509_ERR_INVALID_PARAM);
// Test null value pointer
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, NULL, sizeof(uint8_t)), HITLS_X509_ERR_INVALID_PARAM);
// Test incorrect length for version parameter
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, 0), HITLS_X509_ERR_INVALID_PARAM);
// Test invalid version number
version = 3; // Out of valid range
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_X509_ERR_INVALID_PARAM);
// Test incorrect length for time parameter
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &time, sizeof(time) - 1),
HITLS_X509_ERR_INVALID_PARAM);
// Test incorrect length for issuer parameter
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuer, sizeof(BSL_ASN1_List) - 1),
HITLS_X509_ERR_INVALID_PARAM);
// Test empty buffer for get command
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_VERSION, NULL, sizeof(version)), HITLS_X509_ERR_INVALID_PARAM);
// Test incorrect buffer length for get command
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_VERSION, &version, 0), HITLS_X509_ERR_INVALID_PARAM);
// Test normal parameters - set version number
version = 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS);
// Test normal parameters - get version number
uint32_t getVersion = 0;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_VERSION, &getVersion, sizeof(getVersion)), HITLS_PKI_SUCCESS);
ASSERT_EQ(getVersion, version);
// Test normal parameters - set last update time
ASSERT_EQ(BSL_SAL_SysTimeGet(&time), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &time, sizeof(time)), HITLS_PKI_SUCCESS);
// Test normal parameters - get last update time
BSL_TIME getTime = {0};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_BEFORE_TIME, &getTime, sizeof(getTime)), HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(&getTime, &time, sizeof(BSL_TIME)), 0);
EXIT:
// Clean up resources
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_CTRL_RevokedParamCheck_TC001(void)
{
HITLS_X509_CrlEntry *entry = NULL;
BSL_TIME time = {0};
// Test HITLS_X509_CrlEntryNew
entry = HITLS_X509_CrlEntryNew();
ASSERT_NE(entry, NULL);
// Test HITLS_X509_CrlEntryCtrl with invalid command
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, 0xFFFF, &time, sizeof(time)), HITLS_X509_ERR_INVALID_PARAM);
// Test HITLS_X509_CrlEntryCtrl with NULL entry
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(NULL, HITLS_X509_CRL_GET_REVOKED_REVOKE_TIME, &time, sizeof(time)),
HITLS_X509_ERR_INVALID_PARAM);
// Test HITLS_X509_CrlEntryCtrl with NULL value pointer
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REVOKE_TIME, NULL, sizeof(time)),
HITLS_X509_ERR_INVALID_PARAM);
// Test HITLS_X509_CrlEntryCtrl with invalid value length
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REVOKE_TIME, &time, 0),
HITLS_X509_ERR_INVALID_PARAM);
// Test setting/getting revoke time
ASSERT_EQ(BSL_SAL_SysTimeGet(&time), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REVOKE_TIME, &time, sizeof(time)),
HITLS_PKI_SUCCESS);
BSL_TIME getTime = {0};
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REVOKE_TIME, &getTime, sizeof(getTime)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(&time, &getTime, sizeof(BSL_TIME)), 0);
// Test setting/getting reason
HITLS_X509_RevokeExtReason reasonExt = {false, 1};
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REASON, &reasonExt,
sizeof(HITLS_X509_RevokeExtReason)), HITLS_PKI_SUCCESS);
int32_t getReason = 0;
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REASON, &getReason, sizeof(getReason)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(reasonExt.reason, getReason);
// Test setting/getting serial number
uint8_t serial[] = {0x01, 0x02, 0x03, 0x04};
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_SERIALNUM, serial, 4),
HITLS_PKI_SUCCESS);
BSL_Buffer getSerial = {0};
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_SERIALNUM, &getSerial, sizeof(getSerial)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(4, getSerial.dataLen);
ASSERT_EQ(memcmp(serial, getSerial.data, getSerial.dataLen), 0);
// Test HITLS_X509_CrlEntryFree with NULL
HITLS_X509_CrlEntryFree(NULL); // Should not crash
// Test HITLS_X509_CrlEntryFree with valid entry
HITLS_X509_CrlEntryFree(entry);
EXIT:
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_PARSE_REVOKEDLIST_FUNC_TC001(char *parh, int revokedNum)
{
HITLS_X509_Crl *crl = NULL;
HITLS_X509_CrlEntry *entry = NULL;
BslList *revokeList = NULL;
BSL_TIME time = {0};
int32_t reason = 0;
BSL_Buffer serialNum = {0};
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_PEM, parh, &crl), HITLS_PKI_SUCCESS);
ASSERT_NE(crl, NULL);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_REVOKELIST, &revokeList, sizeof(BslList *)), HITLS_PKI_SUCCESS);
ASSERT_NE(revokeList, NULL);
ASSERT_EQ(BSL_LIST_COUNT(revokeList), revokedNum);
for (entry = (HITLS_X509_CrlEntry *)BSL_LIST_GET_FIRST(revokeList); entry != NULL; entry =
(HITLS_X509_CrlEntry *)BSL_LIST_GET_NEXT(revokeList)) {
ASSERT_TRUE(entry->serialNumber.buff != NULL);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_SERIALNUM, &serialNum,
sizeof(BSL_Buffer)), HITLS_PKI_SUCCESS);
ASSERT_TRUE(serialNum.dataLen > 0 && serialNum.dataLen <= 20);
ASSERT_NE(serialNum.data, NULL);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REVOKE_TIME, &time, sizeof(BSL_TIME)),
HITLS_PKI_SUCCESS);
ASSERT_NE(time.year, 0);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_GET_REVOKED_REASON,
&reason, sizeof(int32_t)), HITLS_PKI_SUCCESS);
ASSERT_TRUE(reason >= 0 && reason <= 11);
reason = 0;
memset(&time, 0, sizeof(BSL_TIME));
memset(&serialNum, 0, sizeof(serialNum));
}
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_CTRL_GetFunc_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
uint32_t version = 0;
BSL_TIME beforeTime = {0};
BSL_TIME afterTime = {0};
BslList *issuerDN = NULL;
BslList *revokeList = NULL;
// Parse the test CRL file
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_PEM, "../testdata/cert/pem/crl/crl_v2.mul3.crl", &crl),
HITLS_PKI_SUCCESS);
ASSERT_NE(crl, NULL);
// Test getting the version number
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_VERSION, &version, sizeof(uint32_t)), HITLS_PKI_SUCCESS);
// The CRL version should be 0 (v1) or 1 (v2)
ASSERT_TRUE(version == 1);
// Test getting the last update time
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_NE(beforeTime.year, 0);
// Test getting the next update time
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
// The next update time should be later than the last update time
ASSERT_TRUE(afterTime.month > beforeTime.month);
// Test getting the issuer DN name
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_ISSUER_DN, &issuerDN, sizeof(BslList *)), HITLS_PKI_SUCCESS);
ASSERT_NE(issuerDN, NULL);
ASSERT_NE(BSL_LIST_COUNT(issuerDN), 0);
// Test getting extensions (using CRL Number as an example)
ASSERT_NE(crl->tbs.crlExt.extList, NULL);
ASSERT_EQ(crl->tbs.crlExt.type, HITLS_X509_EXT_TYPE_CRL);
ASSERT_EQ(BSL_LIST_COUNT(crl->tbs.crlExt.extList), 1);
// Test getting the revoke list
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_REVOKELIST, &revokeList, sizeof(BslList *)), HITLS_PKI_SUCCESS);
ASSERT_NE(revokeList, NULL);
ASSERT_EQ(BSL_LIST_COUNT(revokeList), 3);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_ExtCtrl_FuncTest_TC001(void)
{
uint8_t keyId[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
uint8_t serialNum[4] = {0x11, 0x22, 0x33, 0x44};
HITLS_X509_Crl *crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
// set CRL Number
HITLS_X509_ExtCrlNumber crlNumberExt = {false, {serialNum, 4}};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_CRLNUMBER, &crlNumberExt, sizeof(HITLS_X509_ExtCrlNumber)),
HITLS_PKI_SUCCESS);
HITLS_X509_ExtCrlNumber crlNumExt = {0};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_GET_CRLNUMBER, &crlNumExt, sizeof(HITLS_X509_ExtCrlNumber)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(crlNumExt.critical, crlNumberExt.critical);
ASSERT_EQ(crlNumExt.crlNumber.dataLen, crlNumberExt.crlNumber.dataLen);
ASSERT_EQ(memcmp(crlNumExt.crlNumber.data, crlNumberExt.crlNumber.data, crlNumberExt.crlNumber.dataLen), 0);
HITLS_X509_ExtAki aki = {false, {keyId, sizeof(keyId)}, NULL, {NULL, 0}};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)), HITLS_PKI_SUCCESS);
HITLS_X509_ExtAki getaki = {0};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_GET_AKI, &getaki, sizeof(HITLS_X509_ExtAki)), HITLS_PKI_SUCCESS);
ASSERT_EQ(getaki.critical, aki.critical);
ASSERT_EQ(getaki.kid.dataLen, aki.kid.dataLen);
ASSERT_EQ(memcmp(getaki.kid.data, aki.kid.data, aki.kid.dataLen), 0);
EXIT:
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_CTRL_SetFunc_TC001(char *capath)
{
uint8_t serialNum[4] = {0x11, 0x22, 0x33, 0x44};
BSL_TIME beforeTime = {0};
BSL_TIME afterTime = {0};
HITLS_X509_Cert *cert = NULL;
HITLS_X509_Crl *crl = HITLS_X509_CrlNew();
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_UNKNOWN, capath, &cert), HITLS_PKI_SUCCESS);
ASSERT_NE(crl, NULL);
uint32_t version = 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(uint32_t)), HITLS_PKI_SUCCESS);
BslList *issuerDN = NULL;
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ISSUER_DN, &issuerDN, sizeof(BslList *)),
HITLS_PKI_SUCCESS);
ASSERT_NE(issuerDN, NULL);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuerDN, sizeof(BslList)), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_SAL_SysTimeGet(&beforeTime), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
afterTime = beforeTime;
afterTime.year += 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
HITLS_X509_ExtSki ski = {0};
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_GET_SKI, &ski, sizeof(HITLS_X509_ExtSki)), HITLS_PKI_SUCCESS);
ASSERT_TRUE(ski.kid.data != NULL);
HITLS_X509_ExtAki aki = {false, {ski.kid.data, ski.kid.dataLen}, cert->tbs.issuerName,
{cert->tbs.serialNum.buff, cert->tbs.serialNum.len}};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)), HITLS_PKI_SUCCESS);
HITLS_X509_ExtCrlNumber crlNumberExt = {false, {serialNum, 4}};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_CRLNUMBER, &crlNumberExt, sizeof(HITLS_X509_ExtCrlNumber)),
HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(cert);
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Sign_ParamCheck_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
// Create a basic CRL object
TestMemInit();
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
prvKey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(prvKey, NULL);
// Test null parameters
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, NULL, &algParam, crl), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CrlFree(crl);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Gen_Process_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
const char *keyPath = "../testdata/cert/asn1/rsa_cert/rsa_p1.key.der";
const char *crlPath = "../testdata/cert/asn1/rsa_crl/crl_v1.der";
uint32_t ver = 1;
BSL_Buffer encodeCrl = {0};
BslList *tmp = NULL;
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, CRYPT_PRIKEY_RSA, keyPath, NULL, &prvKey), 0);
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, crlPath, &crl), HITLS_PKI_SUCCESS);
/* Cannot repeat parse */
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, crlPath, &crl), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot sign after parsing */
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_SIGN_AFTER_PARSE);
/* Cannot set after parsing */
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &ver, sizeof(uint32_t)), HITLS_X509_ERR_SET_AFTER_PARSE);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_GET_ISSUER_DN, &tmp, sizeof(BslList *)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, tmp, 0), HITLS_X509_ERR_SET_AFTER_PARSE);
/* Generate crl after parsing is allowed. */
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encodeCrl), 0);
BSL_SAL_Free(encodeCrl.data);
encodeCrl.data = NULL;
encodeCrl.dataLen = 0;
/* Repeat generate is allowed. */
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encodeCrl), 0);
EXIT:
HITLS_X509_CrlFree(crl);
CRYPT_EAL_PkeyFreeCtx(prvKey);
BSL_SAL_Free(encodeCrl.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Gen_Process_TC002(void)
{
HITLS_X509_Crl *crl = NULL;
HITLS_X509_Cert *cert = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
CRYPT_EAL_PkeyCtx *pubKey = NULL;
const char *keyPath = "../testdata/cert/asn1/rsa_cert/rsa_p8.key.der";
const char *certPath = "../testdata/cert/asn1/rsa_cert/rsa_p8.crt.der";
uint32_t mdId = BSL_CID_SHA256;
BSL_TIME thisUpdate = {2024, 8, 22, 1, 1, 0, 1, 0};
BSL_TIME nextUpdate = {2024, 8, 22, 1, 1, 0, 1, 0};
BslList *issuerDN = NULL;
BSL_Buffer encodeCrl = {0};
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, keyPath, NULL,
&prvKey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &cert), 0);
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
/* Invalid parameters */
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
/* Test Crl sign with invalid fields */
/* Set invalid version number */
crl->tbs.version = 2; // 2 is invalid
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_X509_ERR_CRL_INACCURACY_VERSION);
/* Set invalid version number in extensions */
crl->tbs.version = 0;
BslList *extList = crl->tbs.crlExt.extList;
crl->tbs.crlExt.extList = cert->tbs.ext.extList;
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_X509_ERR_CRL_INACCURACY_VERSION);
crl->tbs.crlExt.extList = extList;
/* issuer name is empty */
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_X509_ERR_CRL_ISSUER_EMPTY);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ISSUER_DN, &issuerDN, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuerDN, sizeof(BslList)), 0);
/* thisUpdate is not set */
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_X509_ERR_CRL_THISUPDATE_UNEXIST);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &thisUpdate, sizeof(BSL_TIME)), 0);
/* nextUpdate is before thisUpdate */
nextUpdate.year = thisUpdate.year - 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &nextUpdate, sizeof(BSL_TIME)), 0);
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_X509_ERR_CRL_TIME_INVALID);
nextUpdate.year = thisUpdate.year + 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &nextUpdate, sizeof(BSL_TIME)), 0);
/* Cannot generate before signing */
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encodeCrl), HITLS_X509_ERR_CRL_NOT_SIGNED);
/* Cannot verify before signing */
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_PUBKEY, &pubKey, sizeof(CRYPT_EAL_PkeyCtx *)), 0);
ASSERT_EQ(HITLS_X509_CrlVerify(pubKey, crl), HITLS_X509_ERR_CRL_NOT_SIGNED);
/* Repeat sign is allowed. */
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), 0);
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), 0);
/* Verify after signing is allowed. */
ASSERT_EQ(HITLS_X509_CrlVerify(pubKey, crl), 0);
/* Cannot parse after signing */
ASSERT_EQ(HITLS_X509_CrlParseBuff(BSL_FORMAT_ASN1, &encodeCrl, &crl), HITLS_X509_ERR_INVALID_PARAM);
/* Repeat generate is allowed. */
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encodeCrl), 0);
BSL_SAL_Free(encodeCrl.data);
encodeCrl.data = NULL;
encodeCrl.dataLen = 0;
ASSERT_EQ(HITLS_X509_CrlGenBuff(BSL_FORMAT_ASN1, crl, &encodeCrl), 0);
/* Sing after generating is allowed. */
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), 0);
/* Verify after generating is allowed. */
ASSERT_EQ(HITLS_X509_CrlVerify(pubKey, crl), 0);
/* Cannot parse after generating */
ASSERT_EQ(HITLS_X509_CrlParseBuff(BSL_FORMAT_ASN1, &encodeCrl, &crl), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CrlFree(crl);
HITLS_X509_CertFree(cert);
CRYPT_EAL_PkeyFreeCtx(prvKey);
CRYPT_EAL_PkeyFreeCtx(pubKey);
BSL_SAL_Free(encodeCrl.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CRL_Sign_AlgParamCheck_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
HITLS_X509_Cert *cert = NULL;
HITLS_X509_SignAlgParam algParam = {0};
CRYPT_EAL_PkeyCtx *prvKey = NULL;
const char *keyPath = "../testdata/cert/asn1/rsa_cert/rsa_p1.key.der";
const char *certPath = "../testdata/cert/asn1/rsa_cert/rsa_p8.crt.der";
BSL_TIME thisUpdate = {2024, 8, 22, 1, 1, 0, 1, 0};
BslList *issuerDN = NULL;
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, CRYPT_PRIKEY_RSA, keyPath, NULL, &prvKey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &cert), 0);
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ISSUER_DN, &issuerDN, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuerDN, sizeof(BslList)), 0);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &thisUpdate, sizeof(BSL_TIME)), 0);
/* Test invalid mdId */
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHAKE128, prvKey, &algParam, crl), HITLS_X509_ERR_HASHID);
/* Test empty algParam */
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_MD_NOT_MATCH);
/* Test invalid mdId for RSA-PSS */
algParam.algId = BSL_CID_RSASSAPSS;
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_MD_NOT_MATCH);
/* Test invalid mgfId for RSA-PSS */
algParam.rsaPss.mdId = (CRYPT_MD_AlgId)BSL_CID_SHA256;
algParam.rsaPss.mgfId = (CRYPT_MD_AlgId)BSL_CID_UNKNOWN;
algParam.rsaPss.saltLen = 32;
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), CRYPT_EAL_ERR_ALGID);
EXIT:
HITLS_X509_CrlFree(crl);
HITLS_X509_CertFree(cert);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
#if defined(HITLS_PKI_X509_CRL_GEN) || defined(HITLS_PKI_X509_CRT_GEN)
static BslList* GenDNList(void)
{
HITLS_X509_DN dnName1[1] = {{BSL_CID_AT_COMMONNAME, (uint8_t *)"OH", 2}};
HITLS_X509_DN dnName2[1] = {{BSL_CID_AT_COUNTRYNAME, (uint8_t *)"CN", 2}};
BslList *dirNames = HITLS_X509_DnListNew();
ASSERT_NE(dirNames, NULL);
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName1, 1), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_AddDnName(dirNames, dnName2, 1), HITLS_PKI_SUCCESS);
return dirNames;
EXIT:
HITLS_X509_DnListFree(dirNames);
return NULL;
}
#endif
static BslList* GenGeneralNameList(void)
{
char *str = "test";
HITLS_X509_GeneralName *email = NULL;
HITLS_X509_GeneralName *dns = NULL;
HITLS_X509_GeneralName *dname = NULL;
HITLS_X509_GeneralName *uri = NULL;
HITLS_X509_GeneralName *ip = NULL;
BslList *names = BSL_LIST_New(sizeof(HITLS_X509_GeneralName));
ASSERT_NE(names, NULL);
email = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
dns = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
dname = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
uri = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
ip = BSL_SAL_Malloc(sizeof(HITLS_X509_GeneralName));
ASSERT_TRUE(email != NULL && dns != NULL && dname != NULL && uri != NULL && ip != NULL);
email->type = HITLS_X509_GN_EMAIL;
dns->type = HITLS_X509_GN_DNS;
uri->type = HITLS_X509_GN_URI;
dname->type = HITLS_X509_GN_DNNAME;
ip->type = HITLS_X509_GN_IP;
email->value.dataLen = strlen(str);
dns->value.dataLen = strlen(str);
uri->value.dataLen = strlen(str);
dname->value.dataLen = sizeof(BslList *);
ip->value.dataLen = strlen(str);
email->value.data = BSL_SAL_Dump(str, strlen(str));
dns->value.data = BSL_SAL_Dump(str, strlen(str));
uri->value.data = BSL_SAL_Dump(str, strlen(str));
dname->value.data = (uint8_t *)GenDNList();
ip->value.data = BSL_SAL_Dump(str, strlen(str));
ASSERT_TRUE(email->value.data != NULL && dns->value.data != NULL && uri->value.data != NULL
&& dname->value.data != NULL && ip->value.data != NULL);
ASSERT_EQ(BSL_LIST_AddElement(names, email, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, dns, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, uri, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, dname, BSL_LIST_POS_END), 0);
ASSERT_EQ(BSL_LIST_AddElement(names, ip, BSL_LIST_POS_END), 0);
return names;
EXIT:
HITLS_X509_FreeGeneralName(email);
HITLS_X509_FreeGeneralName(dns);
HITLS_X509_FreeGeneralName(dname);
HITLS_X509_FreeGeneralName(uri);
HITLS_X509_FreeGeneralName(ip);
BSL_LIST_FREE(names, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeGeneralName);
return NULL;
}
static int32_t SetCrlRevoked(HITLS_X509_Crl *crl, int8_t ser)
{
uint8_t serialNum[4] = {0x11, 0x22, 0x33, 0x44};
serialNum[3] = ser;
HITLS_X509_CrlEntry *entry = HITLS_X509_CrlEntryNew();
ASSERT_NE(entry, NULL);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_SERIALNUM,
serialNum, sizeof(serialNum)), HITLS_PKI_SUCCESS);
BSL_TIME revokeTime = {0};
ASSERT_EQ(BSL_SAL_SysTimeGet(&revokeTime), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REVOKE_TIME, &revokeTime, sizeof(BSL_TIME)),
HITLS_PKI_SUCCESS);
HITLS_X509_RevokeExtReason reason = {0, 1}; // keyCompromise
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_REASON, &reason,
sizeof(HITLS_X509_RevokeExtReason)), HITLS_PKI_SUCCESS);
// Set invalid time (optional)
BSL_TIME invalidTime = revokeTime;
HITLS_X509_RevokeExtTime invalidTimeExt = {false, invalidTime};
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_INVALID_TIME,
&invalidTimeExt, sizeof(HITLS_X509_RevokeExtTime)), HITLS_PKI_SUCCESS);
// Set certificate issuer (optional, only needed for indirect CRLs)
HITLS_X509_RevokeExtCertIssuer certIssuer = {true, NULL};
certIssuer.issuerName = GenGeneralNameList();
ASSERT_EQ(HITLS_X509_CrlEntryCtrl(entry, HITLS_X509_CRL_SET_REVOKED_CERTISSUER,
&certIssuer, sizeof(HITLS_X509_RevokeExtCertIssuer)), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_CRL_ADD_REVOKED_CERT, entry, sizeof(HITLS_X509_CrlEntry)),
HITLS_PKI_SUCCESS);
HITLS_X509_CrlEntryFree(entry);
BSL_LIST_FREE(certIssuer.issuerName, (BSL_LIST_PFUNC_FREE)HITLS_X509_FreeGeneralName);
return HITLS_PKI_SUCCESS;
EXIT:
return -1;
}
/* BEGIN_CASE */
void SDV_X509_CRL_Sign_RevokedCheck_TC001(void)
{
HITLS_X509_Crl *crl = NULL;
HITLS_X509_Cert *cert = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
HITLS_X509_CrlEntry *entry = NULL;
BSL_TIME beforeTime = {0};
BSL_TIME afterTime = {0};
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_ASN1, CRYPT_PRIKEY_RSA,
"../testdata/cert/asn1/rsa_cert/rsa_p1.key.der", NULL, &prvKey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/asn1/rsa_cert/rsa_p1_v1.crt.der", &cert),
HITLS_PKI_SUCCESS);
// Create a basic CRL object and set necessary fields
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
BslList *issueList = crl->tbs.issuerName;
// Set basic fields (version, time, issuer, etc.)
crl->tbs.version = 1;
crl->tbs.issuerName = cert->tbs.subjectName;
ASSERT_EQ(BSL_SAL_SysTimeGet(&beforeTime), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
afterTime = beforeTime;
afterTime.year += 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)), HITLS_PKI_SUCCESS);
ASSERT_EQ(SetCrlRevoked(crl, 1), HITLS_PKI_SUCCESS);
entry = BSL_LIST_GET_FIRST(crl->tbs.revokedCerts);
ASSERT_TRUE(entry != NULL);
crl->tbs.version = 0;
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_CRL_INACCURACY_VERSION);
crl->tbs.version = 1;
uint8_t *serialNum = entry->serialNumber.buff;
entry->serialNumber.buff = NULL;
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_CRL_ENTRY);
entry->serialNumber.buff = serialNum;
uint32_t year = entry->time.year;
entry->time.year = 0;
ASSERT_EQ(HITLS_X509_CrlSign(BSL_CID_SHA256, prvKey, &algParam, crl), HITLS_X509_ERR_CRL_TIME_INVALID);
entry->time.year = year;
EXIT:
crl->tbs.issuerName = issueList;
HITLS_X509_CrlFree(crl);
HITLS_X509_CertFree(cert);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
static int32_t SetCrl(HITLS_X509_Crl *crl, HITLS_X509_Cert *cert, bool isV2)
{
BSL_TIME beforeTime = {0};
BSL_TIME afterTime = {0};
BslList *issuerDN = NULL;
uint8_t crlNumber[1] = {0x01};
// Set CRL version (v2)
uint32_t version = 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_VERSION, &version, sizeof(version)), HITLS_PKI_SUCCESS);
// Set issuer DN from certificate
ASSERT_EQ(HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SUBJECT_DN, &issuerDN, sizeof(BslList *)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_ISSUER_DN, issuerDN, sizeof(BslList)),
HITLS_PKI_SUCCESS);
// Set validity period
ASSERT_EQ(BSL_SAL_SysTimeGet(&beforeTime), BSL_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_BEFORE_TIME, &beforeTime, sizeof(BSL_TIME)),
HITLS_PKI_SUCCESS);
afterTime = beforeTime;
afterTime.year += 1;
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_SET_AFTER_TIME, &afterTime, sizeof(BSL_TIME)),
HITLS_PKI_SUCCESS);
for (int i = 0; i < 3; i++) {
ASSERT_EQ(SetCrlRevoked(crl, i), HITLS_PKI_SUCCESS);
}
if (isV2) {
HITLS_X509_ExtSki ski = {0};
int32_t ret = HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_GET_SKI, &ski, sizeof(HITLS_X509_ExtSki));
if (ret == HITLS_PKI_SUCCESS) {
HITLS_X509_ExtAki aki = {false, {ski.kid.data, ski.kid.dataLen}, NULL, {NULL, 0}};
// Set SKI extension
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_AKI, &aki, sizeof(HITLS_X509_ExtAki)),
HITLS_PKI_SUCCESS);
}
// Set CRL Number extension
HITLS_X509_ExtCrlNumber crlNumberExt = {
false, // non-critical
{crlNumber, sizeof(crlNumber)}
};
ASSERT_EQ(HITLS_X509_CrlCtrl(crl, HITLS_X509_EXT_SET_CRLNUMBER, &crlNumberExt,
sizeof(HITLS_X509_ExtCrlNumber)), HITLS_PKI_SUCCESS);
}
return HITLS_PKI_SUCCESS;
EXIT:
return -1;
}
/* BEGIN_CASE */
void SDV_X509_CRL_Sign_Func_TC001(char *cert, char *key, int keytype, int pad, int mdId, int isV2,
char *tmp, int isUseSm2UserId)
{
HITLS_X509_Crl *crl = NULL;
HITLS_X509_Crl *parseCrl = NULL;
HITLS_X509_Cert *issuerCert = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
TestRandInit();
// Parse issuer certificate and private key
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_UNKNOWN, cert, &issuerCert), HITLS_PKI_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(BSL_FORMAT_UNKNOWN, keytype, key, NULL, &prvKey), 0);
// Create and initialize CRL
crl = HITLS_X509_CrlNew();
ASSERT_NE(crl, NULL);
ASSERT_EQ(SetCrl(crl, issuerCert, (bool)isV2), 0);
// Set signature algorithm parameters
if (pad == CRYPT_EMSA_PSS) {
algParam.algId = BSL_CID_RSASSAPSS;
CRYPT_RSA_PssPara pssParam = {0};
pssParam.mdId = mdId;
pssParam.mgfId = mdId;
pssParam.saltLen = 32;
algParam.rsaPss = pssParam;
} else if (isUseSm2UserId != 0) {
algParam.algId = BSL_CID_SM2DSAWITHSM3;
algParam.sm2UserId.data = (uint8_t *)g_sm2DefaultUserid;
algParam.sm2UserId.dataLen = (uint32_t)strlen(g_sm2DefaultUserid);
}
if (pad == CRYPT_EMSA_PSS || isUseSm2UserId != 0) {
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, &algParam, crl), HITLS_PKI_SUCCESS);
} else {
ASSERT_EQ(HITLS_X509_CrlSign(mdId, prvKey, NULL, crl), HITLS_PKI_SUCCESS);
}
// Verify the signature is present
ASSERT_NE(crl->signature.buff, NULL);
ASSERT_NE(crl->signature.len, 0);
ASSERT_EQ(HITLS_X509_CrlGenFile(BSL_FORMAT_ASN1, crl, tmp), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlVerify(issuerCert->tbs.ealPubKey, crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CrlParseFile(BSL_FORMAT_UNKNOWN, tmp, &parseCrl), HITLS_PKI_SUCCESS);
ASSERT_NE(parseCrl, NULL);
if (isUseSm2UserId != 0) {
ASSERT_EQ(HITLS_X509_CrlCtrl(parseCrl, HITLS_X509_SET_VFY_SM2_USER_ID, g_sm2DefaultUserid,
strlen(g_sm2DefaultUserid)), HITLS_PKI_SUCCESS);
}
ASSERT_EQ(HITLS_X509_CrlVerify(issuerCert->tbs.ealPubKey, parseCrl), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CrlFree(crl);
HITLS_X509_CrlFree(parseCrl);
HITLS_X509_CertFree(issuerCert);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
static int32_t STUB_HITLS_X509_ParseNameList(BSL_ASN1_Buffer *name, BSL_ASN1_List *list)
{
(void)name;
(void)list;
return BSL_MALLOC_FAIL;
}
/* BEGIN_CASE */
void SDV_X509_CRL_INVALIED_TEST_TC001(int format, char *path)
{
TestMemInit();
FuncStubInfo tmpRpInfo = {0};
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_ParseNameList, STUB_HITLS_X509_ParseNameList) == 0);
HITLS_X509_Crl *crl = NULL;
ASSERT_NE(HITLS_X509_CrlParseFile((int32_t)format, path, &crl), HITLS_PKI_SUCCESS);
EXIT:
STUB_Reset(&tmpRpInfo);
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/crl/test_suite_sdv_x509_crl.c | C | unknown | 45,585 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "hitls_csr_local.h"
#include "hitls_pki_csr.h"
#include "hitls_pki_utils.h"
#include "bsl_list.h"
#include "sal_file.h"
#include "bsl_obj_internal.h"
#include "hitls_pki_errno.h"
#include "crypt_types.h"
#include "crypt_errno.h"
#include "crypt_encode_decode_key.h"
#include "crypt_eal_codecs.h"
#include "crypt_eal_rand.h"
#include "eal_pkey_local.h"
#include "bsl_list_internal.h"
/* END_HEADER */
#define MAX_DATA_LEN 128
static char g_sm2DefaultUserid[] = "1234567812345678";
void *TestMallocErr(uint32_t len)
{
(void)len;
return NULL;
}
static void *TestMalloc(uint32_t len)
{
return malloc((size_t)len);
}
static void TestMemInitErr()
{
BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, TestMallocErr);
BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
}
static void TestMemInitCorrect()
{
BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, TestMalloc);
BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
}
/* BEGIN_CASE */
void SDV_X509_CSR_New_FUNC_TC001(void)
{
TestMemInitErr();
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_EQ(csr, NULL);
TestMemInitCorrect();
csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_Free_FUNC_TC001(void)
{
TestMemInit();
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
HITLS_X509_CsrFree(csr);
HITLS_X509_CsrFree(NULL);
EXIT:
return;
}
/* END_CASE */
/**
* parse csr file api test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_API_TC001(void)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
const char *path = "../testdata/cert/pem/csr/csr.pem";
ASSERT_NE(HITLS_X509_CsrParseFile(BSL_FORMAT_PEM, path, NULL), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrParseFile(BSL_FORMAT_UNKNOWN, path, &csr), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrParseFile(BSL_FORMAT_PEM, "/errPath/csr.pem", &csr), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrParseFile(BSL_FORMAT_PEM, NULL, &csr), HITLS_PKI_SUCCESS);
/* the csr file don't have read permission */
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/**
* parse csr buffer api test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_API_TC002(void)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
uint8_t data[MAX_DATA_LEN] = {};
BSL_Buffer buffer = {data, sizeof(data)};
BSL_Buffer ori = {NULL, 0};
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &buffer, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &ori, &csr), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &ori, &csr), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_UNKNOWN, &buffer, &csr), HITLS_X509_ERR_FORMAT_UNSUPPORT);
EXIT:
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_FUNC_TC001(int format, char *path, int expRawDataLen, int expSignAlg, Hex *expectedSign,
int expectUnusedbits, int isUseSm2UserId)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
uint32_t rawDataLen = 0;
ASSERT_EQ(HITLS_X509_CsrParseFile(format, path, &csr), HITLS_PKI_SUCCESS);
if (isUseSm2UserId != 0) {
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_SET_VFY_SM2_USER_ID, g_sm2DefaultUserid,
strlen(g_sm2DefaultUserid)), HITLS_PKI_SUCCESS);
}
ASSERT_EQ(HITLS_X509_CsrVerify(csr), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODELEN, &rawDataLen, sizeof(rawDataLen)), 0);
ASSERT_EQ(rawDataLen, expRawDataLen);
uint8_t *rawData = NULL;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODE, &rawData, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(rawData, NULL);
CRYPT_EAL_PkeyCtx *publicKey = NULL;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_PUBKEY, &publicKey, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(publicKey, NULL);
CRYPT_EAL_PkeyFreeCtx(publicKey);
int32_t alg = 0;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SIGNALG, &alg, sizeof(alg)), HITLS_PKI_SUCCESS);
ASSERT_EQ(alg, expSignAlg);
int32_t ref = 0;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_REF_UP, &ref, sizeof(ref)), HITLS_PKI_SUCCESS);
ASSERT_EQ(ref, 2);
HITLS_X509_CsrFree(csr);
ASSERT_NE(csr->signature.buff, NULL);
ASSERT_EQ(csr->signature.len, expectedSign->len);
ASSERT_EQ(memcmp(csr->signature.buff, expectedSign->x, expectedSign->len), 0);
ASSERT_EQ(csr->signature.unusedBits, expectUnusedbits);
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/**
* Test parse csr: check subject name
*/
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_FUNC_TC002(int format, char *path, int expectedNum, char *dnType1,
char *dnName1, char *dnType2, char *dnName2, char *dnType3, char *dnName3, char *dnType4, char *dnName4,
char *dnType5, char *dnName5, char *dnType6, char *dnName6, char *dnType7, char *dnName7)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
ASSERT_EQ(HITLS_X509_CsrParseFile(format, path, &csr), HITLS_PKI_SUCCESS);
BslList *rawSubject = NULL;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SUBJECT_DN, &rawSubject, sizeof(BslList *)), 0);
ASSERT_NE(rawSubject, NULL);
int count = BSL_LIST_COUNT(rawSubject);
ASSERT_EQ(count, expectedNum);
char *dnTypes[7] = {dnType1, dnType2, dnType3, dnType4, dnType5, dnType6, dnType7};
char *dnName[7] = {dnName1, dnName2, dnName3, dnName4, dnName5, dnName6, dnName7};
HITLS_X509_NameNode *nameNode = BSL_LIST_GET_FIRST(rawSubject);
for (int i = 0; i < count && count <= 14 && nameNode != NULL; i++, nameNode = BSL_LIST_GET_NEXT(rawSubject)) {
if (nameNode->layer == 1) {
continue;
}
BSL_ASN1_Buffer nameType = nameNode->nameType;
BSL_ASN1_Buffer nameValue = nameNode->nameValue;
BslOidString typeOid = {
.octs = (char *)nameType.buff,
.octetLen = nameType.len,
};
const char *oidName = BSL_OBJ_GetOidNameFromOid(&typeOid);
ASSERT_NE(oidName, NULL);
ASSERT_EQ(strcmp(dnTypes[i / 2], oidName), 0);
ASSERT_EQ(memcmp(dnName[i / 2], nameValue.buff, strlen(dnName[i / 2])), 0);
}
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/**
* Test parse csr: check the count of the attribute list
*/
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_FUNC_TC003(int format, char *path, int attrNum, int attrCid, Hex *attrValue)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
HITLS_X509_Attrs *rawAttrs = NULL;
ASSERT_EQ(HITLS_X509_CsrParseFile(format, path, &csr), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &rawAttrs, sizeof(HITLS_X509_Attrs *)),
HITLS_PKI_SUCCESS);
ASSERT_NE(rawAttrs, NULL);
ASSERT_EQ(attrNum, BSL_LIST_COUNT(rawAttrs->list));
if (attrNum == 0) {
goto EXIT;
}
HITLS_X509_AttrEntry *entry = BSL_LIST_GET_FIRST(rawAttrs->list);
ASSERT_EQ(attrCid, entry->cid);
BslOidString *oid = BSL_OBJ_GetOID(entry->cid);
ASSERT_NE(oid, NULL);
ASSERT_COMPARE("csr attr oid", entry->attrId.buff, entry->attrId.len, (uint8_t *)oid->octs, oid->octetLen);
ASSERT_COMPARE("csr attr value", entry->attrValue.buff, entry->attrValue.len, attrValue->x, attrValue->len);
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/**
* encode csr buffer api test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_API_TC001(void)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
const char *path = "../testdata/cert/pem/csr/csr.pem";
const char *writePath = "../testdata/cert/pem/csr/genCsr.pem";
int32_t ret = HITLS_X509_CsrParseFile(BSL_FORMAT_PEM, path, &csr);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrGenFile(BSL_FORMAT_PEM, NULL, writePath), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrGenFile(BSL_FORMAT_UNKNOWN, csr, writePath), HITLS_X509_ERR_FORMAT_UNSUPPORT);
ASSERT_EQ(HITLS_X509_CsrGenFile(BSL_FORMAT_PEM, csr, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_NE(HITLS_X509_CsrGenFile(BSL_FORMAT_PEM, csr, "/errPath/csr.pem"), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CsrFree(csr);
return;
}
/* END_CASE */
/**
* encode csr buffer api test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_API_TC002(void)
{
TestMemInit();
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
uint8_t data[MAX_DATA_LEN] = {};
BSL_Buffer buffer = {NULL, 0};
BSL_Buffer buffErr = {data, sizeof(data)};
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_UNKNOWN, csr, &buffer), HITLS_X509_ERR_FORMAT_UNSUPPORT);
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_PEM, NULL, &buffer), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_PEM, csr, NULL), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_PEM, csr, &buffErr), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CsrFree(csr);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_SIGN_API_TC001(void)
{
HITLS_X509_Csr *csr = NULL;
CRYPT_EAL_PkeyCtx *prvKey = NULL;
HITLS_X509_SignAlgParam algParam = {0};
TestMemInit();
csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
prvKey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(prvKey, NULL);
// Test null parameters
ASSERT_EQ(HITLS_X509_CsrSign(BSL_CID_SHA256, NULL, &algParam, csr), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_CsrSign(BSL_CID_SHA256, prvKey, &algParam, NULL), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_CsrFree(csr);
CRYPT_EAL_PkeyFreeCtx(prvKey);
}
/* END_CASE */
/**
* 1. transform format
*/
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_FUNC_TC001(int inFormat, char *csrPath, int outFormat)
{
TestMemInit();
TestRandInit();
HITLS_X509_Csr *csr = NULL;
BSL_Buffer encode = {NULL, 0};
uint8_t *data = NULL;
uint32_t dataLen = 0;
BSL_Buffer asnEncode = {NULL, 0};
ASSERT_EQ(BSL_SAL_ReadFile(csrPath, &data, &dataLen), BSL_SUCCESS);
BSL_Buffer ori = {data, dataLen};
ASSERT_EQ(HITLS_X509_CsrParseBuff(inFormat, &ori, &csr), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrGenBuff(outFormat, csr, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODELEN, &asnEncode.dataLen, sizeof(asnEncode.dataLen)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODE, &asnEncode.data, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrVerify(csr), HITLS_PKI_SUCCESS);
if (inFormat == outFormat) {
ASSERT_EQ(dataLen, encode.dataLen);
ASSERT_EQ(memcmp(encode.data, data, dataLen), 0);
} else if (inFormat == BSL_FORMAT_ASN1 && outFormat == BSL_FORMAT_PEM) {
ASSERT_EQ(dataLen, asnEncode.dataLen);
ASSERT_EQ(memcmp(asnEncode.data, data, dataLen), 0);
} else {
ASSERT_EQ(csr->rawDataLen, encode.dataLen);
ASSERT_EQ(memcmp(encode.data, csr->rawData, encode.dataLen), 0);
}
EXIT:
BSL_SAL_FREE(data);
BSL_SAL_FREE(encode.data);
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
static void ResetCsrNameList(HITLS_X509_Csr *raw)
{
BslList *newSubject = NULL;
(void)HITLS_X509_CsrCtrl(raw, HITLS_X509_GET_SUBJECT_DN, &newSubject, sizeof(BslList **));
newSubject->curr = NULL;
newSubject->last = NULL;
newSubject->first = NULL;
newSubject->dataSize = sizeof(HITLS_X509_NameNode);
newSubject->count = 0;
}
static void ResetCsrAttrsList(HITLS_X509_Csr *raw)
{
HITLS_X509_Attrs *newAttrs = NULL;
(void)HITLS_X509_CsrCtrl(raw, HITLS_X509_CSR_GET_ATTRIBUTES, &newAttrs, sizeof(HITLS_X509_Attrs *));
newAttrs->list->curr = NULL;
newAttrs->list->last = NULL;
newAttrs->list->first = NULL;
newAttrs->list->dataSize = sizeof(HITLS_X509_NameNode);
newAttrs->list->count = 0;
newAttrs->flag = 0;
}
static int32_t SetCsr(HITLS_X509_Csr *raw, HITLS_X509_Csr *new)
{
int32_t ret = 1;
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_SET_PUBKEY, raw->reqInfo.ealPubKey, sizeof(CRYPT_EAL_PkeyCtx *)), 0);
BslList *rawSubject = NULL;
BslList *newSubject = NULL;
ASSERT_EQ(HITLS_X509_CsrCtrl(raw, HITLS_X509_GET_SUBJECT_DN, &rawSubject, sizeof(BslList *)), 0);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_GET_SUBJECT_DN, &newSubject, sizeof(BslList *)), 0);
ASSERT_NE(rawSubject, NULL);
ASSERT_NE(newSubject, NULL);
ASSERT_NE(BSL_LIST_Concat(newSubject, rawSubject), NULL);
HITLS_X509_Attrs *rawAttrs = NULL;
HITLS_X509_Attrs *newAttrs = NULL;
ASSERT_EQ(HITLS_X509_CsrCtrl(raw, HITLS_X509_CSR_GET_ATTRIBUTES, &rawAttrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_CSR_GET_ATTRIBUTES, &newAttrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_NE(rawAttrs, NULL);
ASSERT_NE(newAttrs, NULL);
if (BSL_LIST_COUNT(rawAttrs->list) > 0) {
ASSERT_NE(BSL_LIST_Concat(newAttrs->list, rawAttrs->list), NULL);
}
ret = 0;
EXIT:
return ret;
}
/**
* 1. set subject name, private key, public key, mdId, padding
* 2. generate csr
* 3. compare the generated csr buff
*/
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_FUNC_TC002(int csrFormat, char *csrPath, int keyFormat, char *privPath, int keyType, int pad,
int mdId, int mgfId, int saltLen, int isUseSm2UserId)
{
TestMemInit();
TestRandInit();
HITLS_X509_Csr *raw = NULL;
HITLS_X509_Csr *new = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
BSL_Buffer encode = {NULL, 0};
uint8_t *newCsrEncode = NULL;
uint32_t newCsrEncodeLen = 0;
uint8_t *rawCsrEncode = NULL;
uint32_t rawCsrEncodeLen = 0;
HITLS_X509_SignAlgParam algParam = {0};
HITLS_X509_SignAlgParam *algParamPtr = NULL;
if (pad == CRYPT_EMSA_PSS) {
algParam.algId = BSL_CID_RSASSAPSS;
algParam.rsaPss.mdId = mdId;
algParam.rsaPss.mgfId = mgfId;
algParam.rsaPss.saltLen = saltLen;
algParamPtr = &algParam;
} else if (isUseSm2UserId != 0) {
algParam.algId = BSL_CID_SM2DSAWITHSM3;
algParam.sm2UserId.data = (uint8_t *)g_sm2DefaultUserid;
algParam.sm2UserId.dataLen = (uint32_t)strlen(g_sm2DefaultUserid);
algParamPtr = &algParam;
} else {
algParamPtr = NULL;
}
TestMemInit();
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(keyFormat, keyType, privPath, NULL, 0, &privKey), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrParseFile(csrFormat, csrPath, &raw), HITLS_PKI_SUCCESS);
new = HITLS_X509_CsrNew();
ASSERT_NE(new, NULL);
ASSERT_EQ(SetCsr(raw, new), 0);
ASSERT_EQ(HITLS_X509_CsrSign(mdId, privKey, algParamPtr, new), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrGenBuff(csrFormat, new, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrVerify(new), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_GET_ENCODELEN, &newCsrEncodeLen, sizeof(newCsrEncodeLen)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_GET_ENCODE, &newCsrEncode, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(raw, HITLS_X509_GET_ENCODELEN, &rawCsrEncodeLen, sizeof(rawCsrEncodeLen)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(raw, HITLS_X509_GET_ENCODE, &rawCsrEncode, 0), HITLS_PKI_SUCCESS);
if (pad == CRYPT_EMSA_PSS || new->signAlgId.algId == (BslCid)BSL_CID_SM2DSAWITHSM3) {
ASSERT_EQ(raw->reqInfo.reqInfoRawDataLen, new->reqInfo.reqInfoRawDataLen);
ASSERT_EQ(memcmp(raw->reqInfo.reqInfoRawData, new->reqInfo.reqInfoRawData, raw->reqInfo.reqInfoRawDataLen), 0);
} else {
ASSERT_EQ(newCsrEncodeLen, rawCsrEncodeLen);
ASSERT_EQ(memcmp(newCsrEncode, rawCsrEncode, rawCsrEncodeLen), 0);
}
EXIT:
HITLS_X509_CsrFree(raw);
ResetCsrNameList(new);
ResetCsrAttrsList(new);
HITLS_X509_CsrFree(new);
BSL_SAL_FREE(encode.data);
CRYPT_EAL_PkeyFreeCtx(privKey);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_PROCESS_TC001(char *csrPath, int csrFormat, char *privPath, int keyFormat, int keyType)
{
HITLS_X509_Csr *csr = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
int mdId = CRYPT_MD_SHA256;
BSL_Buffer encodeCsr = {NULL, 0};
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(keyFormat, keyType, privPath, NULL, &privKey), 0);
ASSERT_EQ(HITLS_X509_CsrParseFile(csrFormat, csrPath, &csr), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrSign(mdId, privKey, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot sign after parsing */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, privKey, NULL, csr), HITLS_X509_ERR_SIGN_AFTER_PARSE);
/* Cannot set after parsing */
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_SET_PUBKEY, privKey, 0), HITLS_X509_ERR_SET_AFTER_PARSE);
/* Generate csr after parsing is allowed. */
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, csr, &encodeCsr), 0);
BSL_SAL_Free(encodeCsr.data);
encodeCsr.data = NULL;
encodeCsr.dataLen = 0;
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, csr, &encodeCsr), 0); // Repeat generate is allowed.
EXIT:
CRYPT_EAL_PkeyFreeCtx(privKey);
HITLS_X509_CsrFree(csr);
BSL_SAL_Free(encodeCsr.data);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_GEN_PROCESS_TC002(char *privPath, int keyFormat, int keyType)
{
HITLS_X509_Csr *new = NULL;
CRYPT_EAL_PkeyCtx *key = NULL;
BSL_Buffer encodeCsr = {0};
int mdId = CRYPT_MD_SHA256;
HITLS_X509_DN dnName[1] = {{BSL_CID_AT_COUNTRYNAME, (uint8_t *)"CN", strlen("CN")}};
TestMemInit();
ASSERT_EQ(CRYPT_EAL_PriKeyParseFile(keyFormat, keyType, privPath, NULL, &key), 0);
new = HITLS_X509_CsrNew();
ASSERT_TRUE(new != NULL);
/* Cannot parse after new */
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &encodeCsr, &new), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot generate before signing */
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, new, &encodeCsr), HITLS_X509_ERR_CSR_NOT_SIGNED);
/* Invalid parameters */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, NULL), HITLS_X509_ERR_INVALID_PARAM);
/* Cannot sign before setting pubkey */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, new), HITLS_X509_ERR_CSR_INVALID_PUBKEY);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_SET_PUBKEY, key, 0), 0);
/* Cannot sign before setting subject name */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, new), HITLS_X509_ERR_CSR_INVALID_SUBJECT_DN);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_ADD_SUBJECT_NAME, dnName, 1), 0);
/* Repeat sign is allowed. */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, new), 0);
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, new), 0);
/* Cannot parse after signing */
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &encodeCsr, &new), HITLS_X509_ERR_INVALID_PARAM);
/* Repeat generate is allowed. */
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, new, &encodeCsr), 0);
BSL_SAL_Free(encodeCsr.data);
encodeCsr.data = NULL;
encodeCsr.dataLen = 0;
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_ASN1, new, &encodeCsr), 0);
/* Sing after generating is allowed. */
ASSERT_EQ(HITLS_X509_CsrSign(mdId, key, NULL, new), 0);
/* Cannot parse after generating */
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &encodeCsr, &new), HITLS_X509_ERR_INVALID_PARAM);
EXIT:
CRYPT_EAL_PkeyFreeCtx(key);
HITLS_X509_CsrFree(new);
BSL_SAL_Free(encodeCsr.data);
}
/* END_CASE */
void SetRsaPara(CRYPT_EAL_PkeyPara *para, uint8_t *e, uint32_t eLen, uint32_t bits)
{
para->id = CRYPT_PKEY_RSA;
para->para.rsaPara.e = e;
para->para.rsaPara.eLen = eLen;
para->para.rsaPara.bits = bits;
}
/**
* 1. csr ctrl interface test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_CTRL_SET_API_TC001(char *csrPath)
{
TestMemInit();
BSL_Buffer encodeRaw = { NULL, 0};
HITLS_X509_Csr *csr = NULL;
uint8_t *csrEncode = NULL;
uint32_t csrEncodeLen = 0;
CRYPT_EAL_PkeyCtx *pkey = NULL;
ASSERT_EQ(BSL_SAL_ReadFile(csrPath, &encodeRaw.data, &encodeRaw.dataLen), HITLS_PKI_SUCCESS);
ASSERT_NE(encodeRaw.data, NULL);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &encodeRaw, &csr), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_GET_ENCODE, &csrEncode, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, 0xFFFF, &csrEncode, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODE, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODELEN, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_GET_ENCODELEN, &csrEncodeLen, sizeof(csrEncodeLen)),
HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODELEN, &csrEncodeLen, 0), HITLS_PKI_SUCCESS);
int ref = 0;
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_REF_UP, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_REF_UP, &ref, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_PUBKEY, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_GET_PUBKEY, &pkey, 0), HITLS_PKI_SUCCESS);
int32_t signAlg = 0;
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SIGNALG, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_GET_SIGNALG, &signAlg, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SIGNALG, &signAlg, 0), HITLS_PKI_SUCCESS);
BslList *subjectName = 0;
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SUBJECT_DN, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_GET_SUBJECT_DN, &subjectName, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_SUBJECT_DN, &subjectName, 0), HITLS_PKI_SUCCESS);
HITLS_X509_Attrs attrs = {};
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, 0), HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_FREE(encodeRaw.data);
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
/**
* 1. csr ctrl interface test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_CTRL_SET_API_TC002(char *csrPath)
{
TestMemInit();
TestRandInit();
HITLS_X509_Csr *csr = NULL;
CRYPT_EAL_PkeyCtx *rsaPkey = NULL;
CRYPT_EAL_PkeyCtx *eccPkey = NULL;
uint8_t e[] = {1, 0, 1};
int32_t ret = HITLS_X509_CsrParseFile(BSL_FORMAT_ASN1, csrPath, &csr);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
rsaPkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(rsaPkey, NULL);
CRYPT_EAL_PkeyPara rsaPara = {0};
SetRsaPara(&rsaPara, e, sizeof(e), 2048); // 2048 is rsa key bits
ASSERT_EQ(CRYPT_EAL_PkeySetPara(rsaPkey, &rsaPara), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeyGen(rsaPkey), CRYPT_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(csr, HITLS_X509_SET_PUBKEY, NULL, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(HITLS_X509_CsrCtrl(NULL, HITLS_X509_SET_PUBKEY, rsaPkey, 0), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CsrFree(csr);
CRYPT_EAL_PkeyFreeCtx(rsaPkey);
CRYPT_EAL_PkeyFreeCtx(eccPkey);
TestRandDeInit();
}
/* END_CASE */
/**
* 1. csr ctrl interface test
*/
/* BEGIN_CASE */
void SDV_X509_CSR_CTRL_FUNC_TC001(char *csrPath)
{
TestMemInit();
TestRandInit();
BSL_Buffer encodeRaw = { NULL, 0};
HITLS_X509_Csr *csr = NULL;
uint8_t *csrEncode = NULL;
uint32_t csrEncodeLen = 0;
CRYPT_EAL_PkeyCtx *pkey = NULL;
uint8_t e[] = {1, 0, 1};
HITLS_X509_Csr *newCsr = NULL;
ASSERT_EQ(BSL_SAL_ReadFile(csrPath, &encodeRaw.data, &encodeRaw.dataLen), HITLS_PKI_SUCCESS);
ASSERT_NE(encodeRaw.data, NULL);
ASSERT_EQ(HITLS_X509_CsrParseBuff(BSL_FORMAT_ASN1, &encodeRaw, &csr), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODE, &csrEncode, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_GET_ENCODELEN, &csrEncodeLen, sizeof(csrEncodeLen)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(csrEncodeLen, encodeRaw.dataLen);
ASSERT_EQ(memcmp(encodeRaw.data, csrEncode, encodeRaw.dataLen), 0);
int32_t ref = 0;
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_REF_UP, &ref, sizeof(ref)), HITLS_PKI_SUCCESS);
ASSERT_EQ(ref, 2);
HITLS_X509_CsrFree(csr);
newCsr = HITLS_X509_CsrNew();
ASSERT_NE(newCsr, NULL);
pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_RSA);
ASSERT_NE(pkey, NULL);
CRYPT_EAL_PkeyPara para = {0};
SetRsaPara(¶, e, sizeof(e), 2048); // 2048 is rsa key bits
ASSERT_EQ(CRYPT_EAL_PkeySetPara(pkey, ¶), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_PkeyGen(pkey), CRYPT_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(newCsr, HITLS_X509_SET_PUBKEY, pkey, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrCtrl(newCsr, HITLS_X509_GET_ENCODELEN, &csrEncodeLen, sizeof(csrEncodeLen)),
HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_FREE(encodeRaw.data);
HITLS_X509_CsrFree(csr);
HITLS_X509_CsrFree(newCsr);
CRYPT_EAL_PkeyFreeCtx(pkey);
TestRandDeInit();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_AttrCtrl_API_TC001(void)
{
TestMemInit();
HITLS_X509_Ext *getExt = NULL;
HITLS_X509_Ext *ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
ASSERT_NE(ext, NULL);
HITLS_X509_ExtKeyUsage ku = {0, HITLS_X509_EXT_KU_NON_REPUDIATION};
int32_t cmd = HITLS_X509_ATTR_SET_REQUESTED_EXTENSIONS;
HITLS_X509_Attrs *attrs = NULL;
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, sizeof(HITLS_X509_Attrs *)), 0);
// invalid param
ASSERT_EQ(HITLS_X509_AttrCtrl(NULL, cmd, ext, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, -1, ext, 0), HITLS_X509_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, cmd, NULL, 0), HITLS_X509_ERR_INVALID_PARAM);
// encode ext failed
ext->extList->count = 2;
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, cmd, ext, 0), BSL_INVALID_ARG);
ext->extList->count = 1;
// success
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, cmd, ext, 0), HITLS_PKI_SUCCESS);
// repeat
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, cmd, ext, 0), HITLS_X509_ERR_SET_ATTR_REPEAT);
// get attr
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, HITLS_X509_ATTR_GET_REQUESTED_EXTENSIONS,
&getExt, sizeof(HITLS_X509_Ext *)), HITLS_PKI_SUCCESS);
ASSERT_NE(getExt, NULL);
HITLS_X509_CertExt *certExt = (HITLS_X509_CertExt *)getExt->extData;
ASSERT_EQ(certExt->keyUsage, HITLS_X509_EXT_KU_NON_REPUDIATION);
// not found
X509_ExtFree(getExt, true);
getExt = NULL;
BSL_LIST_DeleteAll(attrs->list, (BSL_LIST_PFUNC_FREE)HITLS_X509_AttrEntryFree);
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, HITLS_X509_ATTR_GET_REQUESTED_EXTENSIONS,
&getExt, sizeof(HITLS_X509_Ext *)), HITLS_X509_ERR_ATTR_NOT_FOUND);
EXIT:
HITLS_X509_CsrFree(csr);
HITLS_X509_ExtFree(ext);
X509_ExtFree(getExt, true);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_EncodeAttrList_FUNC_TC001(int critical1, int maxPath, int critical2, int keyUsage, Hex *expect)
{
TestMemInit();
HITLS_X509_Ext *ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
ASSERT_NE(ext, NULL);
HITLS_X509_Attrs *attrs = NULL;
HITLS_X509_ExtBCons bCons = {critical1, false, maxPath};
HITLS_X509_ExtKeyUsage ku = {critical2, keyUsage};
BSL_ASN1_Buffer encode = {0};
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_NE(csr, NULL);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_NE(attrs, NULL);
// Generate ext
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_BCONS, &bCons, sizeof(HITLS_X509_ExtBCons)), 0);
// Set ext into attr
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, HITLS_X509_ATTR_SET_REQUESTED_EXTENSIONS, ext, 0), 0);
// Test: Encode and check
ASSERT_EQ(HITLS_X509_EncodeAttrList(1, attrs, NULL, &encode), 0);
ASSERT_COMPARE("Encode attrs", expect->x, expect->len, encode.buff, encode.len);
EXIT:
HITLS_X509_CsrFree(csr);
BSL_SAL_Free(encode.buff);
HITLS_X509_ExtFree(ext);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_EncodeAttrList_FUNC_TC002(void)
{
TestMemInit();
HITLS_X509_Ext *ext = HITLS_X509_ExtNew(HITLS_X509_EXT_TYPE_CSR);
HITLS_X509_Attrs *attrs = NULL;
HITLS_X509_ExtKeyUsage ku = {0, HITLS_X509_EXT_KU_NON_REPUDIATION};
BSL_ASN1_Buffer encode = {0};
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
ASSERT_NE(ext, NULL);
ASSERT_NE(csr, NULL);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_NE(attrs->list, NULL);
ASSERT_EQ(HITLS_X509_ExtCtrl(ext, HITLS_X509_EXT_SET_KUSAGE, &ku, sizeof(HITLS_X509_ExtKeyUsage)), 0);
// Test 1: no attr
ASSERT_EQ(HITLS_X509_EncodeAttrList(1, attrs, NULL, &encode), 0);
ASSERT_EQ(encode.buff, NULL);
ASSERT_EQ(encode.len, 0);
// Test 2: encode attr entry failed
attrs->list->count = 1;
ASSERT_EQ(HITLS_X509_EncodeAttrList(1, attrs, NULL, &encode), BSL_INVALID_ARG);
// Set ext into attr
ASSERT_EQ(HITLS_X509_AttrCtrl(attrs, HITLS_X509_ATTR_SET_REQUESTED_EXTENSIONS, ext, 0), 0);
// Test 3: encode list item failed
ASSERT_EQ(HITLS_X509_EncodeAttrList(1, attrs, NULL, &encode), BSL_INVALID_ARG);
EXIT:
HITLS_X509_CsrFree(csr);
HITLS_X509_ExtFree(ext);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_ParseAttrList_FUNC_TC001(Hex *encode, int ret)
{
TestMemInit();
BSL_ASN1_Buffer attrsBuff = {0, encode->len, encode->x};
HITLS_X509_Attrs *attrs = NULL;
HITLS_X509_Csr *csr = HITLS_X509_CsrNew();
csr->flag = 0x01; // HITLS_X509_CSR_PARSE_FLAG
ASSERT_NE(csr, NULL);
ASSERT_EQ(HITLS_X509_CsrCtrl(csr, HITLS_X509_CSR_GET_ATTRIBUTES, &attrs, sizeof(HITLS_X509_Attrs *)), 0);
ASSERT_NE(attrs, NULL);
attrsBuff.tag = BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SEQUENCE;
ASSERT_EQ(HITLS_X509_ParseAttrList(&attrsBuff, attrs, NULL, NULL), ret);
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
static void SetX509Dn(HITLS_X509_DN *dnName, int dnType, char *dnNameStr)
{
dnName->cid = (BslCid)dnType;
dnName->data = (uint8_t *)dnNameStr;
dnName->dataLen = strlen(dnNameStr);
}
static int32_t SetNewCsrInfo(HITLS_X509_Csr *new, CRYPT_EAL_PkeyCtx *key, int dnType1,
char *dnName1, int dnType2, char *dnName2, int dnType3, char *dnName3)
{
int32_t ret = 1;
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_SET_PUBKEY, key, sizeof(CRYPT_EAL_PkeyCtx *)), 0);
HITLS_X509_DN dnName[3] = {0};
int dnTypes[3] = {dnType1, dnType2, dnType3};
char *dnNameStr[3] = {dnName1, dnName2, dnName3};
for (int i = 0; i < 3; i++) {
SetX509Dn(&dnName[i], dnTypes[i], dnNameStr[i]);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_ADD_SUBJECT_NAME, &dnName[i], 1), HITLS_PKI_SUCCESS);
}
BslList *subjectName = 0;
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_GET_SUBJECT_DN, &subjectName, sizeof(BslList *)),
HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(subjectName), 6);
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_ADD_SUBJECT_NAME, dnName, 3), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(subjectName), 10);
ret = 0;
EXIT:
return ret;
}
/* BEGIN_CASE */
void SDV_X509_CSR_AddSubjectName_FUNC_TC001(int keyFormat, int keyType, char *privPath,
int mdId, int dnType1, char *dnName1, int dnType2, char *dnName2, int dnType3, char *dnName3, Hex *expectedReqInfo)
{
TestMemInit();
TestRandInit();
HITLS_X509_Csr *new = NULL;
CRYPT_EAL_PkeyCtx *privKey = NULL;
BSL_Buffer encode = {NULL, 0};
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(keyFormat, keyType, privPath, NULL, 0, &privKey), HITLS_PKI_SUCCESS);
new = HITLS_X509_CsrNew();
ASSERT_NE(new, NULL);
ASSERT_EQ(SetNewCsrInfo(new, privKey, dnType1, dnName1, dnType2, dnName2, dnType3, dnName3), 0);
ASSERT_EQ(HITLS_X509_CsrSign(mdId, privKey, NULL, new), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CsrGenBuff(BSL_FORMAT_PEM, new, &encode), HITLS_PKI_SUCCESS);
ASSERT_EQ(new->reqInfo.reqInfoRawDataLen, expectedReqInfo->len);
ASSERT_EQ(memcmp(new->reqInfo.reqInfoRawData, expectedReqInfo->x, expectedReqInfo->len), 0);
// error length
HITLS_X509_DN dnNameErr[1] = {{BSL_CID_AT_COUNTRYNAME, (uint8_t *)"CNNN", strlen("CNNN")}};
ASSERT_EQ(HITLS_X509_CsrCtrl(new, HITLS_X509_ADD_SUBJECT_NAME, dnNameErr, 1),
HITLS_X509_ERR_SET_DNNAME_INVALID_LEN);
EXIT:
HITLS_X509_CsrFree(new);
BSL_SAL_FREE(encode.data);
CRYPT_EAL_PkeyFreeCtx(privKey);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_CSR_PARSE_FUNC_TC004(int format, char *path, int expectedRet)
{
TestMemInit();
HITLS_X509_Csr *csr = NULL;
ASSERT_EQ(HITLS_X509_CsrParseFile(format, path, &csr), expectedRet);
EXIT:
HITLS_X509_CsrFree(csr);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/csr/test_suite_sdv_x509_csr.c | C | unknown | 34,035 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "hitls_pki_pkcs12.h"
#include "hitls_pki_errno.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "sal_file.h"
#include "bsl_init.h"
#include "hitls_pkcs12_local.h"
#include "hitls_crl_local.h"
#include "hitls_cert_type.h"
#include "hitls_cert_local.h"
#include "bsl_types.h"
#include "crypt_errno.h"
#include "stub_replace.h"
#include "bsl_list_internal.h"
/* END_HEADER */
/**
* For test parse safeBag-p8shroudkeyBag of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_SAFEBAGS_OF_PKCS8SHROUDEDKEYBAG_TC001(int algId, Hex *buff, int keyBits)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)algId;
(void)buff;
(void)keyBits;
SKIP_TEST();
#else
BSL_Buffer safeContent = {0};
char *pwd = "123456";
uint32_t len = strlen(pwd);
int32_t bits = 0;
TestMemInit();
BSL_ASN1_List *bagLists = BSL_LIST_New(sizeof(HITLS_PKCS12_SafeBag));
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(bagLists, NULL);
ASSERT_NE(p12, NULL);
// parse contentInfo
int32_t ret = HITLS_PKCS12_ParseContentInfo(NULL, NULL, (BSL_Buffer *)buff, NULL, 0, &safeContent);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get the safeBag of safeContents, and put in list.
ret = HITLS_PKCS12_ParseAsn1AddList(&safeContent, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get key of the bagList.
ret = HITLS_PKCS12_ParseSafeBagList(bagLists, (const uint8_t *)pwd, len, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
bits = CRYPT_EAL_PkeyGetKeyBits(p12->key->value.key);
if (algId == CRYPT_PKEY_ECDSA) {
ASSERT_EQ(((((keyBits - 1) / 8) + 1) * 2 + 1) * 8, bits); // cal len of pub
} else if (algId == CRYPT_PKEY_RSA) {
ASSERT_EQ(bits, keyBits);
}
EXIT:
BSL_SAL_Free(safeContent.data);
BSL_LIST_DeleteAll(bagLists, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_SafeBagFree);
BSL_SAL_Free(bagLists);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse safeBag-cert of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_SAFEBAGS_OF_CERTBAGS_TC001(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
BSL_Buffer safeContent = {0};
HITLS_PKCS12 *p12 = NULL;
BSL_ASN1_List *bagLists = BSL_LIST_New(sizeof(HITLS_PKCS12_SafeBag));
ASSERT_NE(bagLists, NULL);
p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse contentInfo
int32_t ret = HITLS_PKCS12_ParseContentInfo(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
&safeContent);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get the safeBag of safeContents, and put int list.
ret = HITLS_PKCS12_ParseAsn1AddList(&safeContent, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get cert of the bagList.
ret = HITLS_PKCS12_ParseSafeBagList(bagLists, NULL, 0, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_Free(safeContent.data);
BSL_LIST_DeleteAll(bagLists, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_SafeBagFree);
HITLS_PKCS12_Free(p12);
BSL_SAL_Free(bagLists);
#endif
}
/* END_CASE */
/**
* For test parse attributes of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_SAFEBAGS_OF_ATTRIBUTE_TC001(Hex *buff, Hex *friendlyName, Hex *localKeyId)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
(void)friendlyName;
(void)localKeyId;
SKIP_TEST();
#else
HITLS_X509_Attrs *attrbutes = HITLS_X509_AttrsNew();
ASSERT_NE(attrbutes, NULL);
BSL_ASN1_Buffer asn = {
BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SET,
buff->len,
buff->x,
};
int32_t ret = HITLS_PKCS12_ParseSafeBagAttr(&asn, attrbutes);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_SafeBagAttr *firstAttr = BSL_LIST_GET_FIRST(attrbutes->list);
HITLS_PKCS12_SafeBagAttr *second = BSL_LIST_GET_NEXT(attrbutes->list);
if (firstAttr->attrId == BSL_CID_FRIENDLYNAME) {
BSL_ASN1_Buffer asn = {BSL_ASN1_TAG_BMPSTRING, (uint32_t)friendlyName->len, friendlyName->x};
BSL_ASN1_Buffer encode = {0};
ret = BSL_ASN1_DecodePrimitiveItem(&asn, &encode);
ASSERT_EQ(ret, BSL_SUCCESS);
ASSERT_COMPARE("friendly name", firstAttr->attrValue.data, firstAttr->attrValue.dataLen,
encode.buff, encode.len);
BSL_SAL_FREE(encode.buff);
}
if (firstAttr->attrId == BSL_CID_LOCALKEYID) {
ASSERT_EQ(memcmp(firstAttr->attrValue.data, localKeyId->x, localKeyId->len), 0);
}
if (second == NULL) {
ASSERT_EQ(friendlyName->len, 0);
} else {
if (second->attrId == BSL_CID_FRIENDLYNAME) {
BSL_ASN1_Buffer asn = {BSL_ASN1_TAG_BMPSTRING, (uint32_t)friendlyName->len, friendlyName->x};
BSL_ASN1_Buffer encode = {0};
ret = BSL_ASN1_DecodePrimitiveItem(&asn, &encode);
ASSERT_EQ(ret, BSL_SUCCESS);
ASSERT_COMPARE("friendly name", firstAttr->attrValue.data, firstAttr->attrValue.dataLen,
encode.buff, encode.len);
BSL_SAL_FREE(encode.buff);
}
if (second->attrId == BSL_CID_LOCALKEYID) {
ASSERT_EQ(memcmp(second->attrValue.data, localKeyId->x, localKeyId->len), 0);
}
}
EXIT:
HITLS_X509_AttrsFree(attrbutes, HITLS_PKCS12_AttributesFree);
#endif
}
/* END_CASE */
/**
* For test parse attributes in the incorrect condition.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_SAFEBAGS_OF_ATTRIBUTE_TC002(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
HITLS_X509_Attrs *attrbutes = HITLS_X509_AttrsNew();
ASSERT_NE(attrbutes, NULL);
BSL_ASN1_Buffer asn = {
BSL_ASN1_TAG_CONSTRUCTED | BSL_ASN1_TAG_SET,
0,
buff->x,
};
int32_t ret = HITLS_PKCS12_ParseSafeBagAttr(&asn, attrbutes);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS); // bagAttributes are OPTIONAL
asn.len = buff->len;
ret = HITLS_PKCS12_ParseSafeBagAttr(&asn, attrbutes);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
buff->x[4] = 0x00; // 4 is a random number.
ret = HITLS_PKCS12_ParseSafeBagAttr(&asn, attrbutes);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_AttrsFree(attrbutes, HITLS_PKCS12_AttributesFree);
#endif
}
/* END_CASE */
/**
* For test parse authSafedata of tampering Cert-info with encrypted data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_AUTHSAFE_TC001(Hex *wrongCert)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)wrongCert;
SKIP_TEST();
#else
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse authSafe
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
int32_t ret = HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)wrongCert, (const uint8_t *)pwd, pwdlen, p12);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
char *pwd1 = "123456-789";
uint32_t pwdlen1 = strlen(pwd1);
ret = HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)wrongCert, (const uint8_t *)pwd1, pwdlen1, p12);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
char *pwd2 = "";
uint32_t pwdlen2 = strlen(pwd2);
ret = HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)wrongCert, (const uint8_t *)pwd2, pwdlen2, p12);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse authSafedata of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_AUTHSAFE_TC002(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse authSafe
int32_t ret = HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse 12 of macData parse.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_MACDATA_TC001(Hex *buff, int alg, Hex *digest, Hex *salt, int iterations)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
(void)alg;
(void)digest;
(void)salt;
(void)iterations;
SKIP_TEST();
#else
HITLS_PKCS12_MacData *macData = HITLS_PKCS12_MacDataNew();
int32_t ret = HITLS_PKCS12_ParseMacData((BSL_Buffer *)buff, macData);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(macData->alg, alg);
ASSERT_EQ(macData->iteration, iterations);
ASSERT_EQ(memcmp(macData->macSalt->data, salt->x, salt->len), 0);
ASSERT_EQ(memcmp(macData->mac->data, digest->x, digest->len), 0);
EXIT:
HITLS_PKCS12_MacDataFree(macData);
#endif
}
/* END_CASE */
/**
* For test parse 12 of wrong macData parse.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_MACDATA_TC002(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
HITLS_PKCS12_MacData *macData = HITLS_PKCS12_MacDataNew();
int32_t ret = HITLS_PKCS12_ParseMacData(NULL, macData);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_ParseMacData((BSL_Buffer *)buff, macData);
ASSERT_EQ(ret, HITLS_CMS_ERR_PARSE_TYPE);
EXIT:
HITLS_PKCS12_MacDataFree(macData);
#endif
}
/* END_CASE */
/**
* For test parse 12 of macData cal.
*/
/* BEGIN_CASE */
void SDV_PKCS12_CAL_MACDATA_TC001(Hex *initData, Hex *salt, int alg, int iter, Hex *mac)
{
TestMemInit();
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
BSL_Buffer output = {0};
HITLS_PKCS12_MacData *macData = p12->macData;
macData->alg = alg;
macData->macSalt->data = BSL_SAL_Dump(salt->x, salt->len);
ASSERT_NE(macData->macSalt->data, NULL);
macData->macSalt->dataLen = salt->len;
macData->iteration = iter;
char *pwdData = "123456";
uint32_t pwdlen = strlen(pwdData);
BSL_Buffer pwd = {(uint8_t *)pwdData, pwdlen};
int32_t ret = HITLS_PKCS12_CalMac(p12, &pwd, (BSL_Buffer *)initData, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(output.data, mac->x, mac->len), 0);
EXIT:
BSL_SAL_Free(output.data);
HITLS_PKCS12_Free(p12);
}
/* END_CASE */
/**
* For test cal key according to salt, alg, etc.
*/
/* BEGIN_CASE */
void SDV_PKCS12_CAL_KDF_TC001(Hex *pwd, Hex *salt, int alg, int iter, Hex *key)
{
TestMemInit();
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
HITLS_PKCS12_MacData *macData = p12->macData;
macData->alg = alg;
macData->macSalt->data = BSL_SAL_Dump(salt->x, salt->len);
ASSERT_NE(macData->macSalt->data, NULL);
macData->macSalt->dataLen = salt->len;
macData->iteration = iter;
uint8_t outData[64] = {0};
BSL_Buffer output = {outData, 64};
int32_t ret = HITLS_PKCS12_KDF(p12, pwd->x, pwd->len, HITLS_PKCS12_KDF_MACKEY_ID, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(output.data, key->x, key->len), 0);
EXIT:
HITLS_PKCS12_Free(p12);
return;
}
/* END_CASE */
/**
* For test parse 12 of right conditions.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_TC001(Hex *encode, Hex *cert)
{
(void)cert;
#ifndef HITLS_PKI_PKCS12_PARSE
(void)encode;
SKIP_TEST();
#else
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
BSL_Buffer encodeCert = {0};
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
TestMemInit();
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true), HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
#ifdef HITLS_PKI_PKCS12_GEN
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12->entityCert->value.cert, &encodeCert), HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(encodeCert.data, cert->x, cert->len), 0);
#endif
EXIT:
BSL_SAL_Free(encodeCert.data);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse 12 of right conditions (no Mac).
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_TC002(Hex *encode, Hex *cert)
{
(void)cert;
#ifndef HITLS_PKI_PKCS12_PARSE
(void)encode;
SKIP_TEST();
#else
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
BSL_Buffer encodeCert = {0};
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
};
TestMemInit();
ASSERT_NE(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, false), HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
#ifdef HITLS_PKI_PKCS12_GEN
ASSERT_EQ(HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12->entityCert->value.cert, &encodeCert), HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(encodeCert.data, cert->x, cert->len), 0);
#endif
EXIT:
BSL_SAL_Free(encodeCert.data);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse 12 of right conditions (different keys).
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_TC003(char *path, char *pwd)
{
#if !defined(HITLS_PKI_PKCS12_PARSE) || !defined(HITLS_BSL_SAL_FILE)
(void)path;
(void)pwd;
SKIP_TEST();
#else
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
TestMemInit();
int32_t ret = HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, path, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse 12 of wrong conditions.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_WRONG_CONDITIONS_TC001(Hex *encode)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)encode;
SKIP_TEST();
#else
char *pwd1 = "1234567";
char *pwd2 = "1234567";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd1;
encPwd.dataLen = strlen(pwd1);
BSL_Buffer macPwd;
macPwd.data = (uint8_t *)pwd2;
macPwd.dataLen = strlen(pwd2);
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &macPwd,
};
TestMemInit();
int32_t ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, NULL, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, NULL, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, NULL, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_VERIFY_FAIL);
char *pwd3 = "";
macPwd.data = (uint8_t *)pwd3;
macPwd.dataLen = strlen(pwd3);
param.macPwd = &macPwd;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_VERIFY_FAIL);
param.macPwd = NULL;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_VERIFY_FAIL);
param.encPwd = NULL;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
char *pwd4 = "123456";
param.encPwd = &encPwd;
macPwd.data = (uint8_t *)pwd4;
macPwd.dataLen = strlen(pwd4);
param.macPwd = &macPwd;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
encPwd.data = (uint8_t *)pwd4;
encPwd.dataLen = strlen(pwd4);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PARAM);
HITLS_PKCS12_Free(p12);
p12 = NULL;
encode->x[6] = 0x04; // Modify the version = 4.
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PFX);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test parse 12 of wrong p12-file.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_WRONG_P12FILE_TC001(Hex *encode)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)encode;
SKIP_TEST();
#else
char *pwd1 = "123456";
char *pwd2 = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd1;
encPwd.dataLen = strlen(pwd1);
BSL_Buffer macPwd;
macPwd.data = (uint8_t *)pwd2;
macPwd.dataLen = strlen(pwd2);
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &macPwd,
};
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12 *p12_2 = NULL;
TestMemInit();
int32_t ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12_1, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
encode->x[encode->len - 2] = 0x04; // modify the iteration = 1024;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12_2, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_VERIFY_FAIL);
encode->x[encode->len - 2] = 0x08; // recover the iteration = 2048;
(void)memset_s(encode->x + 96, 16, 0, 16); // modify the contentInfo
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12_2, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_VERIFY_FAIL);
EXIT:
HITLS_PKCS12_Free(p12_1);
HITLS_PKCS12_Free(p12_2);
#endif
}
/* END_CASE */
/**
* For test parse 12 of wrong p12-file, which miss a part of data randomly.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_P12_WRONG_P12FILE_TC002(Hex *encode)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)encode;
SKIP_TEST();
#else
char *pwd1 = "123456";
char *pwd2 = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd1;
encPwd.dataLen = strlen(pwd1);
BSL_Buffer macPwd;
macPwd.data = (uint8_t *)pwd2;
macPwd.dataLen = strlen(pwd2);
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &macPwd,
};
HITLS_PKCS12 *p12 = NULL;
TestMemInit();
int32_t ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, true);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)encode, ¶m, &p12, false);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test encode safeBag-p8shroudkeyBag of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_SAFEBAGS_OF_PKCS8SHROUDEDKEYBAG_TC001(Hex *buff)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
uint32_t len = strlen(pwd);
BSL_Buffer encode = {0};
BSL_ASN1_List *bagLists = BSL_LIST_New(sizeof(HITLS_PKCS12_SafeBag));
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
BSL_ASN1_List *list = BSL_LIST_New(sizeof(HITLS_PKCS12_Bag));
ASSERT_NE(bagLists, NULL);
ASSERT_NE(p12, NULL);
ASSERT_NE(list, NULL);
// get the safeBag of safeContents, and put in list.
int32_t ret = HITLS_PKCS12_ParseAsn1AddList((BSL_Buffer *)buff, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get key of the bagList.
ret = HITLS_PKCS12_ParseSafeBagList(bagLists, (const uint8_t *)pwd, len, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
HITLS_PKCS12_Bag *bag = BSL_SAL_Malloc(sizeof(HITLS_PKCS12_Bag));
bag->attributes = p12->key->attributes;
bag->value.key = p12->key->value.key;
bag->id = BSL_CID_PKCS8SHROUDEDKEYBAG;
ret = BSL_LIST_AddElement(list, bag, BSL_LIST_POS_END);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
CRYPT_Pbkdf2Param param = {0};
param.pbesId = BSL_CID_PBES2;
param.pbkdfId = BSL_CID_PBKDF2;
param.hmacId = CRYPT_MAC_HMAC_SHA256;
param.symId = CRYPT_CIPHER_AES256_CBC;
param.pwd = (uint8_t *)pwd;
param.saltLen = 16;
param.pwdLen = len;
param.itCnt = 2048;
CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, ¶m};
ret = HITLS_PKCS12_EncodeAsn1List(p12, list, BSL_CID_PKCS8SHROUDEDKEYBAG, ¶mEx, &encode);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(encode.dataLen, buff->len);
ret = memcmp(encode.data + encode.dataLen - 37, buff->x + buff->len - 37, 37);
ASSERT_EQ(ret, 0);
EXIT:
BSL_SAL_Free(encode.data);
BSL_LIST_DeleteAll(bagLists, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_SafeBagFree);
BSL_SAL_Free(bagLists);
BSL_LIST_FREE(list, NULL);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test encode encrypted-safecontent.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_SAFEBAGS_OF_CERTBAGS_TC001(Hex *buff)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
BSL_Buffer encode = {0};
BSL_Buffer safeContent = {0};
BSL_Buffer output = {0};
BSL_ASN1_List *bagLists = BSL_LIST_New(sizeof(HITLS_PKCS12_SafeBag));
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(bagLists, NULL);
ASSERT_NE(p12, NULL);
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse contentInfo
int32_t ret = HITLS_PKCS12_ParseContentInfo(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
&safeContent);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get the safeBag of safeContents, and put int list.
ret = HITLS_PKCS12_ParseAsn1AddList(&safeContent, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// get cert of the bagList.
ret = HITLS_PKCS12_ParseSafeBagList(bagLists, NULL, 0, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
CRYPT_Pbkdf2Param param = {0};
param.pbesId = BSL_CID_PBES2;
param.pbkdfId = BSL_CID_PBKDF2;
param.hmacId = CRYPT_MAC_HMAC_SHA256;
param.symId = CRYPT_CIPHER_AES256_CBC;
param.pwd = (uint8_t *)pwd;
param.saltLen = 16;
param.pwdLen = pwdlen;
param.itCnt = 2048;
CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, ¶m};
ret = HITLS_PKCS12_EncodeAsn1List(p12, p12->certList, BSL_CID_CERTBAG, ¶mEx, &encode);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_EncodeContentInfo(NULL, NULL, &encode, BSL_CID_PKCS7_ENCRYPTEDDATA, ¶mEx, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(output.dataLen, buff->len);
ret = memcmp(output.data, buff->x, 69);
ASSERT_EQ(ret, 0);
EXIT:
BSL_SAL_Free(safeContent.data);
BSL_SAL_Free(encode.data);
BSL_SAL_Free(output.data);
BSL_LIST_DeleteAll(bagLists, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_SafeBagFree);
HITLS_PKCS12_Free(p12);
BSL_SAL_Free(bagLists);
#endif
}
/* END_CASE */
/**
* For test encode authSafedata of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_AUTHSAFE_TC001(Hex *buff)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
BSL_Buffer *encode1 = BSL_SAL_Calloc(1, sizeof(BSL_Buffer));
BSL_Buffer *encode2 = BSL_SAL_Calloc(1, sizeof(BSL_Buffer));
BSL_Buffer *encode3 = BSL_SAL_Calloc(1, sizeof(BSL_Buffer));
BSL_Buffer *encode4 = BSL_SAL_Calloc(1, sizeof(BSL_Buffer));
BSL_ASN1_List *list = BSL_LIST_New(sizeof(BSL_Buffer));
BSL_ASN1_List *keyList = BSL_LIST_New(sizeof(HITLS_PKCS12_Bag));
BSL_Buffer encode5 = {0};
HITLS_PKCS12_Bag *bagKey = NULL;
ASSERT_NE(p12, NULL);
ASSERT_NE(encode1, NULL);
ASSERT_NE(encode2, NULL);
ASSERT_NE(encode3, NULL);
ASSERT_NE(encode4, NULL);
ASSERT_NE(list, NULL);
ASSERT_NE(keyList, NULL);
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse authSafe
int32_t ret = HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
CRYPT_Pbkdf2Param param = {0};
param.pbesId = BSL_CID_PBES2;
param.pbkdfId = BSL_CID_PBKDF2;
param.hmacId = CRYPT_MAC_HMAC_SHA256;
param.symId = CRYPT_CIPHER_AES256_CBC;
param.pwd = (uint8_t *)pwd;
param.saltLen = 16;
param.pwdLen = pwdlen;
param.itCnt = 2048;
CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, ¶m};
HITLS_PKCS12_Bag *bag = BSL_SAL_Calloc(1u, sizeof(HITLS_PKCS12_Bag));
bag->attributes = p12->entityCert->attributes;
bag->value.cert = p12->entityCert->value.cert;
bag->id = BSL_CID_CERTBAG;
bag->type = BSL_CID_X509CERTIFICATE;
ret = BSL_LIST_AddElement(p12->certList, bag, BSL_LIST_POS_BEGIN);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
p12->entityCert->attributes = NULL;
p12->entityCert->value.cert = NULL;
ret = HITLS_PKCS12_EncodeAsn1List(p12, p12->certList, BSL_CID_CERTBAG, ¶mEx, encode1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_EncodeContentInfo(NULL, NULL, encode1, BSL_CID_PKCS7_ENCRYPTEDDATA, ¶mEx, encode2);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
bagKey = BSL_SAL_Calloc(1u, sizeof(HITLS_PKCS12_Bag));
bagKey->attributes = p12->key->attributes;
bagKey->value = p12->key->value;
bagKey->id = BSL_CID_PKCS8SHROUDEDKEYBAG;
ret = BSL_LIST_AddElement(keyList, bagKey, BSL_LIST_POS_END);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_EncodeAsn1List(p12, keyList, BSL_CID_PKCS8SHROUDEDKEYBAG, ¶mEx, encode3);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_EncodeContentInfo(NULL, NULL, encode3, BSL_CID_PKCS7_SIMPLEDATA, ¶mEx, encode4);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = BSL_LIST_AddElement(list, encode2, BSL_LIST_POS_END);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = BSL_LIST_AddElement(list, encode4, BSL_LIST_POS_END);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_EncodeAsn1List(p12, list, BSL_CID_PKCS7_CONTENTINFO, ¶mEx, &encode5);
ASSERT_EQ(encode5.dataLen, buff->len);
EXIT:
BSL_SAL_Free(encode1->data);
BSL_SAL_Free(encode2->data);
BSL_SAL_Free(encode3->data);
BSL_SAL_Free(encode4->data);
BSL_SAL_Free(encode1);
BSL_SAL_Free(encode3);
BSL_SAL_Free(encode5.data);
BSL_LIST_FREE(list, NULL);
BSL_LIST_FREE(keyList, NULL);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test encode authSafedata of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_MACDATA_TC001(Hex *buff, Hex *initData, Hex *expectData)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
(void)initData;
(void)expectData;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
BSL_Buffer output = {0};
BSL_Buffer output1 = {0};
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)buff, ¶m, &p12, true), HITLS_PKI_SUCCESS);
HITLS_PKCS12_KdfParam hmacParam = {0};
hmacParam.macId = CRYPT_MD_SHA224;
hmacParam.pwd = (uint8_t *)pwd;
hmacParam.saltLen = p12->macData->macSalt->dataLen;
hmacParam.pwdLen = strlen(pwd);
hmacParam.itCnt = 2048;
HITLS_PKCS12_MacParam macParam = {.para = &hmacParam, .algId = BSL_CID_PKCS12KDF};
ASSERT_EQ(HITLS_PKCS12_EncodeMacData(p12, (BSL_Buffer *)initData, &macParam, &output), HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(output.data, expectData->x, expectData->len), 0);
p12_1 = HITLS_PKCS12_New();
ASSERT_NE(p12_1, NULL);
hmacParam.itCnt = 999;
ASSERT_EQ(HITLS_PKCS12_EncodeMacData(p12_1, (BSL_Buffer *)initData, &macParam, &output),
HITLS_PKCS12_ERR_INVALID_ITERATION);
hmacParam.itCnt = 1024;
hmacParam.saltLen = 0;
ASSERT_EQ(HITLS_PKCS12_EncodeMacData(p12_1, (BSL_Buffer *)initData, &macParam, &output),
HITLS_PKCS12_ERR_INVALID_SALTLEN);
hmacParam.saltLen = 16;
ASSERT_EQ(HITLS_PKCS12_EncodeMacData(p12_1, (BSL_Buffer *)initData, &macParam, &output1), HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_Free(output.data);
BSL_SAL_Free(output1.data);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
#endif
}
/* END_CASE */
/**
* For test encode P12 of correct data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_P12_TC001(Hex *buff, Hex *cert)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
(void)cert;
SKIP_TEST();
#else
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
BSL_Buffer output = {0};
BSL_Buffer encodeCert1 = {0};
BSL_Buffer encodeCert2 = {0};
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12_PwdParam param = {.encPwd = &encPwd, .macPwd = &encPwd};
int32_t ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)buff, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_EncodeParam encodeParam = {0};
CRYPT_Pbkdf2Param pbParam = {0};
pbParam.pbesId = BSL_CID_PBES2;
pbParam.pbkdfId = BSL_CID_PBKDF2;
pbParam.hmacId = CRYPT_MAC_HMAC_SHA256;
pbParam.symId = CRYPT_CIPHER_AES256_CBC;
pbParam.pwd = (uint8_t *)pwd;
pbParam.saltLen = 16;
pbParam.pwdLen = strlen(pwd);
pbParam.itCnt = 2048;
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
encodeParam.encParam = encParam;
HITLS_PKCS12_KdfParam macParam = {0};
macParam.macId = p12->macData->alg;
macParam.pwd = (uint8_t *)pwd;
macParam.saltLen = p12->macData->macSalt->dataLen;
macParam.pwdLen = strlen(pwd);
macParam.itCnt = 2048;
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
encodeParam.macParam = paramTest;
ASSERT_EQ(TestRandInit(), 0);
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(output.dataLen, buff->len);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, ¶m, &p12_1, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12_1->key->value.key, NULL);
ASSERT_NE(p12_1->entityCert->value.cert, NULL);
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12->entityCert->value.cert, &encodeCert1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12_1->entityCert->value.cert, &encodeCert2);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(encodeCert1.data, encodeCert2.data, encodeCert1.dataLen), 0);
ASSERT_EQ(memcmp(encodeCert1.data, cert->x, cert->len), 0);
if (BSL_LIST_COUNT(p12->certList) > 0) {
HITLS_PKCS12_Bag *node1 = BSL_LIST_GET_FIRST(p12->certList);
BSL_Buffer encodeCert3 = {0};
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, node1->value.cert, &encodeCert3);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_Bag *node2 = BSL_LIST_GET_FIRST(p12_1->certList);
BSL_Buffer encodeCert4 = {0};
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, node2->value.cert, &encodeCert4);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(encodeCert1.data, encodeCert2.data, encodeCert1.dataLen), 0);
BSL_SAL_Free(encodeCert3.data);
BSL_SAL_Free(encodeCert4.data);
}
EXIT:
BSL_SAL_Free(output.data);
BSL_SAL_Free(encodeCert1.data);
BSL_SAL_Free(encodeCert2.data);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
#endif
}
/* END_CASE */
/**
* For test encode P12 of correct data(no mac).
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_P12_TC002(Hex *buff, Hex *cert)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
(void)cert;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
BSL_Buffer output = {0};
BSL_Buffer encodeCert1 = {0};
BSL_Buffer encodeCert2 = {0};
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
int32_t ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)buff, ¶m, &p12, false);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_EncodeParam encodeParam = {0};
CRYPT_Pbkdf2Param pbParam = {0};
pbParam.pbesId = BSL_CID_PBES2;
pbParam.pbkdfId = BSL_CID_PBKDF2;
pbParam.hmacId = CRYPT_MAC_HMAC_SHA256;
pbParam.symId = CRYPT_CIPHER_AES256_CBC;
pbParam.pwd = (uint8_t *)pwd;
pbParam.saltLen = 16;
pbParam.pwdLen = strlen(pwd);
pbParam.itCnt = 2048;
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
encodeParam.encParam = encParam;
HITLS_PKCS12_KdfParam macParam = {0};
macParam.macId = p12->macData->alg;
macParam.pwd = (uint8_t *)pwd;
macParam.saltLen = 8;
macParam.pwdLen = strlen(pwd);
macParam.itCnt = 2048;
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
encodeParam.macParam = paramTest;
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(output.dataLen, buff->len);
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, ¶m, &p12_1, false);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12_1->key->value.key, NULL);
ASSERT_NE(p12_1->entityCert->value.cert, NULL);
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12->entityCert->value.cert, &encodeCert1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertGenBuff(BSL_FORMAT_ASN1, p12_1->entityCert->value.cert, &encodeCert2);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(memcmp(encodeCert1.data, encodeCert2.data, encodeCert1.dataLen), 0);
ASSERT_EQ(memcmp(encodeCert1.data, cert->x, cert->len), 0);
EXIT:
BSL_SAL_Free(output.data);
BSL_SAL_Free(encodeCert1.data);
BSL_SAL_Free(encodeCert2.data);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
#endif
}
/* END_CASE */
/**
* For test encode P12 of insufficient data.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_P12_TC003(Hex *buff)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
(void)buff;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
HITLS_PKCS12_EncodeParam encodeParam = {0};
CRYPT_Pbkdf2Param pbParam = {0};
pbParam.pbesId = BSL_CID_PBES2;
pbParam.pbkdfId = BSL_CID_PBKDF2;
pbParam.hmacId = CRYPT_MAC_HMAC_SHA256;
pbParam.symId = CRYPT_CIPHER_AES256_CBC;
pbParam.pwd = (uint8_t *)pwd;
pbParam.saltLen = 16;
pbParam.pwdLen = strlen(pwd);
pbParam.itCnt = 2048;
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
encodeParam.encParam = encParam;
BSL_Buffer output1 = {0};
BSL_Buffer output2 = {0};
BSL_Buffer output3 = {0};
BSL_Buffer output4 = {0};
BSL_Buffer output5 = {0};
BSL_Buffer output6 = {0};
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
// For test p12 has none data, isNeedMac = true.
int32_t ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output1);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NONE_DATA);
// For test p12 has none data, isNeedMac = false.
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output1);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NONE_DATA);
HITLS_PKCS12_Free(p12);
p12 = NULL;
ret = HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, (BSL_Buffer *)buff, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_KdfParam macParam = {0};
macParam.macId = p12->macData->alg;
macParam.pwd = (uint8_t *)pwd;
macParam.saltLen = 8;
macParam.pwdLen = strlen(pwd);
macParam.itCnt = 2048;
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
encodeParam.macParam = paramTest;
HITLS_PKCS12 p12_1 = {0};
// For test gen p12 of wrong input
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_UNKNOWN, &p12_1, &encodeParam, true, &output1);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_FORMAT_UNSUPPORT);
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, NULL, true, &output1);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, NULL);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
(void)memcpy(&p12_1, p12, sizeof(HITLS_PKCS12));
CRYPT_EAL_PkeyCtx *temKey = p12_1.key->value.key;
HITLS_X509_Cert *entityCert = p12_1.entityCert->value.cert;
p12_1.key->value.key = NULL;
p12_1.entityCert->value.cert = NULL; // test p12-encode of key and entityCert = NULL.
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
p12_1.key->value.key = NULL; // test p12-encode of key = NULL.
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output2);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
p12_1.key->value.key = temKey;
p12_1.entityCert->value.cert = NULL; // test p12-encode of entityCert = NULL.
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output3);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// test p12-encode of entityCert attribute = NULL.
p12_1.entityCert->value.cert = entityCert;
HITLS_X509_AttrsFree(p12_1.entityCert->attributes, HITLS_PKCS12_AttributesFree);
p12_1.entityCert->attributes = NULL;
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output4);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// test p12-encode of key attribute = NULL.
HITLS_X509_AttrsFree(p12_1.key->attributes, HITLS_PKCS12_AttributesFree);
p12_1.key->attributes = NULL;
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output5);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
BSL_LIST_DeleteAll(p12_1.certList, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_BagFree); // test p12-encode of key attribute = NULL.
ret = HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, &p12_1, &encodeParam, true, &output6);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_Free(output1.data);
BSL_SAL_Free(output2.data);
BSL_SAL_Free(output3.data);
BSL_SAL_Free(output4.data);
BSL_SAL_Free(output5.data);
BSL_SAL_Free(output6.data);
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
/**
* For test gen p12-file of different password.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_P12_TC004(char *pkeyPath, char *certPath)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_CRYPTO_KEY_DECODE)
(void)pkeyPath;
(void)certPath;
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey), 0);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &enCert), HITLS_PKI_SUCCESS);
certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(certBag, NULL);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, keyBag, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, certBag, 0), HITLS_PKI_SUCCESS);
HITLS_PKCS12_EncodeParam encodeParam = {0};
BSL_Buffer output = {0};
// While the encrypted data is null of cert and key
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output), HITLS_PKCS12_ERR_NO_ENCRYPT_PARAM);
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), HITLS_PKCS12_ERR_NO_ENCRYPT_PARAM);
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {0};
pbParam.pbesId = BSL_CID_PBES2;
pbParam.pbkdfId = BSL_CID_PBKDF2;
pbParam.hmacId = CRYPT_MAC_HMAC_SHA256;
pbParam.symId = CRYPT_CIPHER_AES256_CBC;
pbParam.pwd = (uint8_t *)pwd;
pbParam.saltLen = 16;
pbParam.pwdLen = strlen(pwd);
pbParam.itCnt = 2048;
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {0};
macParam.macId = BSL_CID_SHA256;
macParam.pwd = (uint8_t *)pwd;
macParam.saltLen = 8;
macParam.pwdLen = strlen(pwd);
macParam.itCnt = 2048;
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
encodeParam.encParam = encParam;
encodeParam.macParam = paramTest;
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output), HITLS_PKI_SUCCESS);
BSL_SAL_Free(output.data);
output.data = NULL;
paramTest.algId = BSL_CID_MAX;
encodeParam.macParam = paramTest;
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), HITLS_PKCS12_ERR_INVALID_ALGO);
paramTest.algId = BSL_CID_PKCS12KDF;
paramTest.para = NULL;
encodeParam.macParam = paramTest;
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), HITLS_PKCS12_ERR_NO_MAC_PARAM);
EXIT:
BSL_SAL_Free(output.data);
HITLS_PKCS12_Free(p12);
HITLS_X509_CertFree(enCert);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_PKCS12_BagFree(keyBag);
HITLS_PKCS12_BagFree(certBag);
#endif
}
/* END_CASE */
/**
* For test gen and parse p12-file.
*/
/* BEGIN_CASE */
void SDV_PKCS12_GEN_PARSE_P12FILE_TC001(void)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_PKI_PKCS12_PARSE)
SKIP_TEST();
#else
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
BSL_Buffer encPwd;
encPwd.data = (uint8_t *)pwd;
encPwd.dataLen = strlen(pwd);
HITLS_PKCS12 *p12 = NULL;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12_PwdParam param = {
.encPwd = &encPwd,
.macPwd = &encPwd,
};
HITLS_PKCS12_EncodeParam encodeParam = {0};
CRYPT_Pbkdf2Param pbParam = {0};
pbParam.pbesId = BSL_CID_PBES2;
pbParam.pbkdfId = BSL_CID_PBKDF2;
pbParam.hmacId = CRYPT_MAC_HMAC_SHA256;
pbParam.symId = CRYPT_CIPHER_AES256_CBC;
pbParam.pwd = (uint8_t *)pwd;
pbParam.saltLen = 16;
pbParam.pwdLen = strlen(pwd);
pbParam.itCnt = 2048;
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
encodeParam.encParam = encParam;
const char *path = "../testdata/cert/asn1/pkcs12/chain.p12";
const char *writePath = "../testdata/cert/asn1/pkcs12/chain_cp.p12";
int32_t ret = HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, NULL, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, path, ¶m, &p12, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_PKCS12_KdfParam macParam = {0};
macParam.macId = p12->macData->alg;
macParam.pwd = (uint8_t *)pwd;
macParam.saltLen = 8;
macParam.pwdLen = strlen(pwd);
macParam.itCnt = 2048;
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
encodeParam.macParam = paramTest;
ret = HITLS_PKCS12_GenFile(BSL_FORMAT_ASN1, p12, &encodeParam, true, NULL);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_GenFile(BSL_FORMAT_ASN1, p12, &encodeParam, true, writePath);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, writePath, ¶m, &p12_1, true);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
#endif
}
/* END_CASE */
/**
* For test p12-ctrl.
*/
/* BEGIN_CASE */
void SDV_PKCS12_CTRL_TEST_TC001(char *pkeyPath, char *enCertPath, char *caCertPath)
{
#if !defined(HITLS_PKI_PKCS12_PARSE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)pkeyPath;
(void)enCertPath;
(void)caCertPath;
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_X509_Cert *caCert = NULL;
HITLS_X509_Cert *targetCert = NULL;
CRYPT_EAL_PkeyCtx *targetKey = NULL;
HITLS_PKCS12_Bag *caBag = NULL;
HITLS_PKCS12_Bag *pkeyBag = NULL;
HITLS_PKCS12_Bag *encertBag = NULL;
HITLS_PKCS12_Bag *pkeyBagTmp = NULL;
HITLS_PKCS12_Bag *encertBagTmp = NULL;
HITLS_X509_Cert *tmpCert = NULL;
CRYPT_EAL_PkeyCtx *tmpKey = NULL;
int32_t mdId = CRYPT_MD_SHA1;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
int32_t ret = CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, enCertPath, &enCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, caCertPath, &caCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
caBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, caCert);
ASSERT_NE(caBag, NULL);
pkeyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(pkeyBag, NULL);
encertBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(encertBag, NULL);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, pkeyBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, encertBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GET_ENTITY_CERT, &targetCert, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(targetCert, NULL);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GET_ENTITY_KEY, &targetKey, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(targetKey, NULL);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, 0);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PARAM);
mdId = BSL_CID_MD4 - 1;
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId));
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PARAM);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, caBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(p12->key->value.key, NULL);
ASSERT_NE(p12->entityCert->value.cert, NULL);
ASSERT_EQ(BSL_LIST_COUNT(p12->key->attributes->list), 1);
ASSERT_EQ(BSL_LIST_COUNT(p12->entityCert->attributes->list), 1);
ASSERT_EQ(BSL_LIST_COUNT(p12->certList), 1);
// get bag from p12-ctx.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GET_ENTITY_CERTBAG, &encertBagTmp, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(encertBagTmp, NULL);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GET_ENTITY_KEYBAG, &pkeyBagTmp, 0), HITLS_PKI_SUCCESS);
ASSERT_NE(pkeyBagTmp, NULL);
// get value from bag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(encertBagTmp, HITLS_PKCS12_BAG_GET_VALUE, &tmpCert, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(tmpCert, p12->entityCert->value.cert);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(pkeyBagTmp, HITLS_PKCS12_BAG_GET_VALUE, &tmpKey, 0), HITLS_PKI_SUCCESS);
ASSERT_EQ(tmpKey, p12->key->value.key);
EXIT:
HITLS_X509_CertFree(targetCert);
CRYPT_EAL_PkeyFreeCtx(targetKey);
HITLS_PKCS12_BagFree(pkeyBag);
HITLS_PKCS12_BagFree(encertBag);
HITLS_PKCS12_BagFree(caBag);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_PKCS12_Free(p12);
HITLS_X509_CertFree(enCert);
HITLS_X509_CertFree(caCert);
HITLS_X509_CertFree(tmpCert);
CRYPT_EAL_PkeyFreeCtx(tmpKey);
HITLS_PKCS12_BagFree(encertBagTmp);
HITLS_PKCS12_BagFree(pkeyBagTmp);
#endif
}
/* END_CASE */
/**
* For test p12-ctrl in invalid params.
*/
/* BEGIN_CASE */
void SDV_PKCS12_CTRL_TEST_TC002(char *enCertPath)
{
#if !defined(HITLS_PKI_PKCS12_PARSE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)enCertPath;
SKIP_TEST();
#else
HITLS_X509_Cert *enCert = NULL;
HITLS_X509_Cert *target = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
HITLS_PKCS12_Bag keyBag = {0};
HITLS_PKCS12_Bag *entityCertBag = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
int32_t mdId = CRYPT_MD_SHA1;
int32_t ret = HITLS_PKCS12_Ctrl(NULL, HITLS_PKCS12_SET_ENTITY_KEYBAG, &keyBag, 0); // p12 == NULL.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, NULL, 0); // keyBag == NULL.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID - 1, &mdId, sizeof(CRYPT_MD_AlgId)); // cmd is invalid.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PARAM);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GET_ENTITY_CERT, &target, 0); // no cert to obtain.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NO_ENTITYCERT);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId)); // no key and cert
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, entityCertBag, 0); // enCertBag is invalid.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
#if defined(HITLS_CRYPTO_KEY_DECODE) && defined(HITLS_BSL_SAL_FILE)
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, enCertPath, &enCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
entityCertBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(entityCertBag, NULL);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, entityCertBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
#else
(void)enCertPath;
SKIP_TEST();
#endif
// no key to set localKeyId.
p12->key = &keyBag;
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId));
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NO_PAIRED_CERT_AND_KEY);
p12->key = NULL;
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, certBag, 0); // certBag is NULL.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
keyBag.type = BSL_CID_PKCS8SHROUDEDKEYBAG;
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, &keyBag, 0); // certBag-type is wrong.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_PARAM);
EXIT:
HITLS_PKCS12_Free(p12);
HITLS_X509_CertFree(enCert);
HITLS_PKCS12_BagFree(entityCertBag);
#endif
}
/* END_CASE */
/**
* For test p12-bag creat, set, and free.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_TEST_TC001(char *pkeyPath, char *certPath)
{
#if !defined(HITLS_PKI_PKCS12_PARSE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)pkeyPath;
(void)certPath;
SKIP_TEST();
#else
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
char *name = "friendlyName";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer = {.data = (uint8_t *)name, .dataLen = nameLen};
int32_t id = 0;
int32_t type = 0;
int32_t ret = CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &enCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(certBag, NULL);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(NULL, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME),
HITLS_PKCS12_ERR_NULL_POINTER);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_GET_ID, NULL, sizeof(uint32_t)),
HITLS_PKCS12_ERR_NULL_POINTER);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_GET_TYPE, NULL, sizeof(uint32_t)),
HITLS_PKCS12_ERR_NULL_POINTER);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint8_t)),
HITLS_PKCS12_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint8_t)),
HITLS_PKCS12_ERR_INVALID_PARAM);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_CERTBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, BSL_CID_X509CERTIFICATE);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
EXIT:
HITLS_PKCS12_BagFree(keyBag);
HITLS_PKCS12_BagFree(certBag);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_X509_CertFree(enCert);
#endif
}
/* END_CASE */
/**
* For test p12-bag in invalid params.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_TEST_TC002(char *pkeyPath)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)pkeyPath;
SKIP_TEST();
#else
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
BSL_Buffer buffer = {0};
int32_t ret = CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_MAX, 0, pkey); // invalid bag-id.
ASSERT_EQ(keyBag, NULL);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
ret = HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, NULL, BSL_CID_FRIENDLYNAME); // Attribute is null.
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
char *name = "friendlyName";
uint32_t nameLen = strlen(name);
buffer.data = (uint8_t *)name;
buffer.dataLen = nameLen;
ret = HITLS_PKCS12_BagCtrl(NULL, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_NULL_POINTER);
ret = HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_EXTEND);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_INVALID_SAFEBAG_ATTRIBUTES);
ret = HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME);
ASSERT_EQ(ret, HITLS_X509_ERR_SET_ATTR_REPEAT);
EXIT:
HITLS_PKCS12_BagFree(keyBag);
CRYPT_EAL_PkeyFreeCtx(pkey);
#endif
}
/* END_CASE */
/**
* For test p12-ctrl in invalid params of repeated attributes.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_TEST_TC003(char *pkeyPath, char *certPath)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)pkeyPath;
(void)certPath;
SKIP_TEST();
#else
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
int32_t mdId = CRYPT_MD_SHA1;
int32_t ret = CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &enCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(certBag, NULL);
uint8_t keyId[32] = {0};
uint32_t idLen = 32;
BSL_Buffer attr = {.data = keyId, .dataLen = idLen};
ret = HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_ADD_ATTR, &attr, BSL_CID_LOCALKEYID);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, keyBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, keyBag, 0);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_REPEATED_SET_KEY); // Repeat setting.
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, certBag, 0);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, certBag, 0);
ASSERT_EQ(ret, HITLS_PKCS12_ERR_REPEATED_SET_ENTITYCERT); // Repeat setting.
// The key bag has pushed the localKey-id attribute.
ret = HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId));
ASSERT_EQ(ret, HITLS_X509_ERR_SET_ATTR_REPEAT);
EXIT:
HITLS_PKCS12_Free(p12);
HITLS_X509_CertFree(enCert);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_PKCS12_BagFree(keyBag);
HITLS_PKCS12_BagFree(certBag);
#endif
}
/* END_CASE */
/**
* For test generating a .p12 from reading buffer.
*/
/* BEGIN_CASE */
void SDV_PKCS12_GEN_FROM_DATA_TC001(char *pkeyPath, char *enCertPath, char *ca1CertPath, char *otherCertPath)
{
#ifndef HITLS_PKI_PKCS12_GEN
(void)pkeyPath;
(void)enCertPath;
(void)ca1CertPath;
(void)otherCertPath;
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SHA256, CRYPT_CIPHER_AES256_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SHA256, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
#ifdef HITLS_PKI_PKCS12_PARSE
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
#endif
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_X509_Cert *ca1Cert = NULL;
HITLS_X509_Cert *otherCert = NULL;
HITLS_PKCS12_Bag *pkeyBag = NULL;
HITLS_PKCS12_Bag *encertBag = NULL;
HITLS_PKCS12_Bag *ca1Bag = NULL;
HITLS_PKCS12_Bag *otherCertBag = NULL;
HITLS_X509_Cert *targetCert = NULL;
CRYPT_EAL_PkeyCtx *targetKey = NULL;
char *name = "entity";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer1 = {0};
buffer1.data = (uint8_t *)name;
buffer1.dataLen = nameLen;
char *name1 = "ca1";
uint32_t nameLen1 = strlen(name1);
BSL_Buffer buffer2 = {0};
buffer2.data = (uint8_t *)name1;
buffer2.dataLen = nameLen1;
BSL_Buffer output = {0};
int32_t mdId = CRYPT_MD_SHA1;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, enCertPath, &enCert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, ca1CertPath, &ca1Cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, otherCertPath, &otherCert), HITLS_PKI_SUCCESS);
pkeyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey); // new a key Bag
ASSERT_NE(pkeyBag, NULL);
ca1Bag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, ca1Cert); // new a cert Bag
ASSERT_NE(ca1Bag, NULL);
encertBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert); // new a cert Bag
ASSERT_NE(encertBag, NULL);
otherCertBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, otherCert);
ASSERT_NE(otherCertBag, NULL);
// Add a attribute to the keyBag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(pkeyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer1, BSL_CID_FRIENDLYNAME), 0);
// Add a attribute to the certBag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(encertBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer1, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(ca1Bag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer2, BSL_CID_FRIENDLYNAME), 0);
// Set entity-key to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, pkeyBag, 0), 0);
// Set entity-cert to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, encertBag, 0), 0);
// Set ca-cert to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, ca1Bag, 0), 0);
// Set the second cert, which has no attr.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, otherCertBag, 0), 0);
// Cal localKeyId to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId)), 0);
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), 0);
#ifdef HITLS_PKI_PKCS12_PARSE
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, true), 0);
// Attempt to get a entity-cert from p12 we parsed.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_ENTITY_CERT, &targetCert, 0), 0);
ASSERT_NE(targetCert, NULL);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_ENTITY_KEY, &targetKey, 0), 0); // Attempt to get a entity-key.
ASSERT_NE(targetKey, NULL);
ASSERT_EQ(BSL_LIST_COUNT(p12_1->certList), 2);
#endif
EXIT:
HITLS_X509_CertFree(targetCert);
CRYPT_EAL_PkeyFreeCtx(targetKey);
HITLS_PKCS12_BagFree(pkeyBag);
HITLS_PKCS12_BagFree(encertBag);
HITLS_PKCS12_BagFree(ca1Bag);
HITLS_PKCS12_BagFree(otherCertBag);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_X509_CertFree(enCert);
HITLS_X509_CertFree(ca1Cert);
HITLS_X509_CertFree(otherCert);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
/**
* For test generating a .p12, which including a secret-bag.
*/
/* BEGIN_CASE */
void SDV_PKCS12_GEN_SECRET_BAG_TC001(char *pkeyPath, char *certPath)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
const char *writePath = "../testdata/cert/asn1/pkcs12/secret.p12";
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
char *pwd = "123456";
// All adopt smX algorithms
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SM3, CRYPT_CIPHER_SM4_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SM3, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
HITLS_PKCS12_Bag *secretBag = NULL;
uint8_t secretData[20] = {1};
uint32_t secretDataLen = 20;
BSL_Buffer secret = {.data = (uint8_t *)secretData, .dataLen = secretDataLen};
char *name = "secret";
uint32_t nameLen = strlen(name);
BSL_Buffer friendlyName = {.data = (uint8_t *)name, .dataLen = nameLen};
BSL_Buffer output = {0};
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
secretBag = HITLS_PKCS12_BagNew(BSL_CID_SECRETBAG, BSL_CID_CE_EXTKEYUSAGE, &secret);
ASSERT_NE(secretBag, NULL);
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &enCert), 0);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(certBag, NULL);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, keyBag, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, certBag, 0), 0);
// Add a attribute to the secretBag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag, HITLS_PKCS12_BAG_ADD_ATTR, &friendlyName, BSL_CID_FRIENDLYNAME), 0);
// Set the secretBag to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_SECRETBAG, secretBag, 0), 0);
// no mac
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output), 0);
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, false), HITLS_PKI_SUCCESS);
BSL_SAL_FREE(output.data);
HITLS_PKCS12_Free(p12_1);
p12_1 = NULL;
// with mac
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), 0);
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, true), HITLS_PKI_SUCCESS);
HITLS_PKCS12_Free(p12_1);
p12_1 = NULL;
// no mac
ASSERT_EQ(HITLS_PKCS12_GenFile(BSL_FORMAT_ASN1, p12, &encodeParam, false, writePath), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, writePath, &pwdParam, &p12_1, false), HITLS_PKI_SUCCESS);
HITLS_PKCS12_Free(p12_1);
p12_1 = NULL;
// with mac
ASSERT_EQ(HITLS_PKCS12_GenFile(BSL_FORMAT_ASN1, p12, &encodeParam, true, writePath), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, writePath, &pwdParam, &p12_1, true), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_CertFree(enCert);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_PKCS12_BagFree(secretBag);
HITLS_PKCS12_BagFree(keyBag);
HITLS_PKCS12_BagFree(certBag);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
/**
* For test bag-ctrl, including get-id, get-type, get-value in secret-bag.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_CTRL_TC001(void)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SHA256, CRYPT_CIPHER_AES256_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SHA256, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
uint32_t id;
uint32_t type;
BSL_ASN1_List *secretBags = NULL;
HITLS_PKCS12_Bag *secretBag1 = NULL;
HITLS_PKCS12_Bag *secretBag2 = NULL;
uint8_t secretData1[20];
uint32_t secretDataLen1 = 20;
(void)memset_s(secretData1, secretDataLen1, 'F', secretDataLen1);
BSL_Buffer secret1 = {.data = (uint8_t *)secretData1, .dataLen = secretDataLen1};
uint8_t secretData2[30];
uint32_t secretDataLen2 = 30;
(void)memset_s(secretData2, secretDataLen2, 'A', secretDataLen2);
BSL_Buffer secret2 = {.data = (uint8_t *)secretData2, .dataLen = secretDataLen2};
uint8_t outputSecret[30] = {2};
uint32_t outputSecretLen = 30;
BSL_Buffer outBuf = {.data = (uint8_t *)outputSecret, .dataLen = outputSecretLen};
char *name = "secret1"; // friendlyName of secretBag1
uint32_t nameLen = strlen(name);
BSL_Buffer friendlyName1 = {.data = (uint8_t *)name, .dataLen = nameLen};
char *name2 = "secret2"; // friendlyName of secretBag2
uint32_t nameLen2 = strlen(name2);
BSL_Buffer friendlyName2 = {.data = (uint8_t *)name2, .dataLen = nameLen2};
BSL_Buffer output = {0};
HITLS_PKCS12_Bag *tmpBag = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
HITLS_PKCS12 *p12_1 = NULL;
secretBag1 = HITLS_PKCS12_BagNew(BSL_CID_SECRETBAG, BSL_CID_CE_EXTKEYUSAGE, &secret1);
ASSERT_NE(secretBag1, NULL);
secretBag2 = HITLS_PKCS12_BagNew(BSL_CID_SECRETBAG, BSL_CID_CE_KEYUSAGE, &secret2);
ASSERT_NE(secretBag2, NULL);
// Add a attribute to the secretBag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag1, HITLS_PKCS12_BAG_ADD_ATTR, &friendlyName1, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag2, HITLS_PKCS12_BAG_ADD_ATTR, &friendlyName2, BSL_CID_FRIENDLYNAME), 0);
// Set the secretBag to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_SECRETBAG, secretBag1, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_SECRETBAG, secretBag2, 0), 0);
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), 0);
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, true), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_SECRETBAGS, &secretBags, 0), 0);
ASSERT_EQ(BSL_LIST_COUNT(secretBags), 2);
tmpBag = BSL_LIST_GET_FIRST(secretBags);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_SECRETBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, BSL_CID_CE_EXTKEYUSAGE);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_VALUE, &outBuf, sizeof(BSL_Buffer)), 0);
ASSERT_COMPARE("compare secret1", outBuf.data, outBuf.dataLen, secretData1, secretDataLen1);
outBuf.dataLen = 30; // init len is 30.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_ATTR, &outBuf, BSL_CID_FRIENDLYNAME), 0);
ASSERT_COMPARE("compare attr1", outBuf.data, outBuf.dataLen, name, nameLen);
tmpBag = (HITLS_PKCS12_Bag *)BSL_LIST_GET_LAST(secretBags);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_SECRETBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, BSL_CID_CE_KEYUSAGE);
outBuf.dataLen = 30; // init len is 30.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_ATTR, &outBuf, BSL_CID_FRIENDLYNAME), 0);
ASSERT_COMPARE("compare attr2", outBuf.data, outBuf.dataLen, name2, nameLen2);
outBuf.dataLen = 30; // init len is 30.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_VALUE, &outBuf, sizeof(BSL_Buffer)), 0);
ASSERT_COMPARE("compare secret2", outBuf.data, outBuf.dataLen, secretData2, secretDataLen2);
EXIT:
HITLS_PKCS12_BagFree(secretBag1);
HITLS_PKCS12_BagFree(secretBag2);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
/**
* For test bag-ctrl, including get-id, get-type, get-value in cert-bag and key-bag.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_CTRL_TC002(char *pkeyPath, char *certPath)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
(void)pkeyPath;
(void)certPath;
SKIP_TEST();
#else
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_PKCS12_Bag *certBag = NULL;
HITLS_PKCS12_Bag *keyBag = NULL;
CRYPT_EAL_PkeyCtx *tmpKey = NULL;
HITLS_X509_Cert *tmpCert = NULL;
uint32_t id;
uint32_t type;
uint8_t keyId[32] = {0};
uint32_t idLen = 32;
BSL_Buffer attr = {.data = keyId, .dataLen = idLen};
int32_t ret = CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
keyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, pkey);
ASSERT_NE(keyBag, NULL);
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, certPath, &enCert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, enCert);
ASSERT_NE(certBag, NULL);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_ADD_ATTR, &attr, BSL_CID_LOCALKEYID), 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &attr, BSL_CID_LOCALKEYID), 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_CERTBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, BSL_CID_X509CERTIFICATE);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_VALUE, NULL, 0), HITLS_PKCS12_ERR_NULL_POINTER);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_GET_VALUE, &tmpCert, 0), 0);
ASSERT_NE(tmpCert, NULL);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_PKCS8SHROUDEDKEYBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, 0);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_GET_VALUE, &tmpKey, 0), 0);
ASSERT_NE(tmpKey, NULL);
EXIT:
HITLS_X509_CertFree(enCert);
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_X509_CertFree(tmpCert);
CRYPT_EAL_PkeyFreeCtx(tmpKey);
HITLS_PKCS12_BagFree(keyBag);
HITLS_PKCS12_BagFree(certBag);
#endif
}
/* END_CASE */
/**
* For test secret-bag with custom oid.
*/
/* BEGIN_CASE */
void SDV_PKCS12_BAG_CUSTOM_OID_TC001(void)
{
#if !defined(HITLS_CRYPTO_KEY_DECODE) || !defined(HITLS_BSL_SAL_FILE) || !defined(HITLS_PKI_PKCS12_GEN)
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
const char *writePath = "../testdata/cert/asn1/pkcs12/secret.p12";
HITLS_PKCS12_Bag *tmpBag = NULL;
BSL_ASN1_List *secretBags = NULL;
char *pwd = "123456";
// All adopt smX algorithms
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SM3, CRYPT_CIPHER_SM4_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SM3, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
HITLS_PKCS12_Bag *secretBag = NULL;
uint8_t secretData1[20];
uint32_t secretDataLen1 = 20;
(void)memset_s(secretData1, secretDataLen1, 'F', secretDataLen1);
BSL_Buffer secret = {.data = (uint8_t *)secretData1, .dataLen = secretDataLen1};
char *name = "secret";
uint32_t nameLen = strlen(name);
BSL_Buffer friendlyName = {.data = (uint8_t *)name, .dataLen = nameLen};
BSL_Buffer output = {0};
uint32_t id;
uint32_t type;
char *oid = "\1\1\1\3\1";
int32_t ourCid = BSL_CID_MAX + 1;
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
ASSERT_EQ(BSL_OBJ_Create(oid, (uint32_t)strlen(oid), "Our OID", ourCid), HITLS_PKI_SUCCESS);
secretBag = HITLS_PKCS12_BagNew(BSL_CID_SECRETBAG, ourCid, &secret);
ASSERT_NE(secretBag, NULL);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, ourCid);
// Add a attribute to the secretBag.
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag, HITLS_PKCS12_BAG_ADD_ATTR, &friendlyName, BSL_CID_FRIENDLYNAME), 0);
// Set the secretBag to p12.
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_SECRETBAG, secretBag, 0), 0);
// with mac
ASSERT_EQ(HITLS_PKCS12_GenFile(BSL_FORMAT_ASN1, p12, &encodeParam, false, writePath), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_ParseFile(BSL_FORMAT_ASN1, writePath, &pwdParam, &p12_1, false), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_SECRETBAGS, &secretBags, 0), 0);
ASSERT_EQ(BSL_LIST_COUNT(secretBags), 1);
tmpBag = BSL_LIST_GET_FIRST(secretBags);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(uint32_t)), 0);
ASSERT_EQ(id, BSL_CID_SECRETBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(tmpBag, HITLS_PKCS12_BAG_GET_TYPE, &type, sizeof(uint32_t)), 0);
ASSERT_EQ(type, ourCid);
EXIT:
HITLS_PKCS12_BagFree(secretBag);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
static int32_t STUB_HITLS_X509_AddListItemDefault1(void *item, uint32_t len, BSL_ASN1_List *list)
{
(void)item;
(void)len;
(void)list;
return BSL_MALLOC_FAIL;
}
static int32_t marked = 0;
static int32_t STUB_HITLS_X509_AddListItemDefault2(void *item, uint32_t len, BSL_ASN1_List *list)
{
if (marked < 2) {
marked++;
void *node = BSL_SAL_Malloc(len);
if (node == NULL) {
return BSL_MALLOC_FAIL;
}
(void)memcpy_s(node, len, item, len);
return BSL_LIST_AddElement(list, node, BSL_LIST_POS_AFTER);
}
return BSL_MALLOC_FAIL;
}
/**
* For test parse safeBag-cert in incorrect condition.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_SAFEBAGS_INVALID_TC001(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
FuncStubInfo tmpRpInfo = {0};
BSL_Buffer safeContent = {0};
HITLS_PKCS12 *p12 = NULL;
BSL_ASN1_List *bagLists = BSL_LIST_New(sizeof(HITLS_PKCS12_SafeBag));
ASSERT_NE(bagLists, NULL);
p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
// parse contentInfo
int32_t ret = HITLS_PKCS12_ParseContentInfo(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
&safeContent);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_AddListItemDefault, STUB_HITLS_X509_AddListItemDefault1) == 0);
// get the safeBag of safeContents, and put int list.
ret = HITLS_PKCS12_ParseAsn1AddList(&safeContent, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_AddListItemDefault, STUB_HITLS_X509_AddListItemDefault2) == 0);
// get the safeBag of safeContents, and put int list.
ret = HITLS_PKCS12_ParseAsn1AddList(&safeContent, bagLists, BSL_CID_SAFECONTENTSBAG);
ASSERT_NE(ret, HITLS_PKI_SUCCESS);
EXIT:
marked = 0;
BSL_SAL_Free(safeContent.data);
BSL_LIST_DeleteAll(bagLists, (BSL_LIST_PFUNC_FREE)HITLS_PKCS12_SafeBagFree);
HITLS_PKCS12_Free(p12);
BSL_SAL_Free(bagLists);
STUB_Reset(&tmpRpInfo);
#endif
}
/* END_CASE */
static int32_t STUB_HITLS_PKCS12_ParseAsn1AddList(BSL_Buffer *encode, BSL_ASN1_List *list, uint32_t parseType)
{
(void)encode;
(void)list;
(void)parseType;
return BSL_MALLOC_FAIL;
}
static int32_t STUB_HITLS_PKCS12_ParseContentInfo(HITLS_PKI_LibCtx *libCtx, const char *attrName, BSL_Buffer *encode,
const uint8_t *password, uint32_t passLen, BSL_Buffer *data)
{
(void)libCtx;
(void)attrName;
(void)encode;
(void)password;
(void)passLen;
(void)data;
return BSL_MALLOC_FAIL;
}
static int32_t STUB_CRYPT_EAL_ProviderDecodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
const char *format, const char *type, BSL_Buffer *encode, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey)
{
(void)libCtx;
(void)attrName;
(void)keyType;
(void)format;
(void)type;
(void)encode;
(void)pwd;
(void)ealPKey;
return BSL_MALLOC_FAIL;
}
static HITLS_PKCS12_Bag *STUB_HITLS_PKCS12_BagNew(uint32_t bagId, uint32_t bagType, void *bagValue)
{
(void)bagId;
(void)bagType;
(void)bagValue;
return NULL;
}
static int32_t STUB_HITLS_X509_CertParseBuff(int32_t format, const BSL_Buffer *encode, HITLS_X509_Cert **cert)
{
(void)format;
(void)encode;
(void)cert;
return BSL_MALLOC_FAIL;
}
static int32_t test;
static int32_t STUB_BSL_LIST_AddElement(BslList *pList, void *pData, BslListPosition enPosition)
{
if (marked <= test) { // It takes 53 triggers to prevent cert from entering p12
marked++;
return (int32_t)BSL_LIST_AddElementInt(pList, pData, enPosition);
}
return BSL_MALLOC_FAIL;
}
#ifdef HITLS_CRYPTO_PROVIDER
#define MAX_TRIGGERS 103
#else
#define MAX_TRIGGERS 51
#endif
/**
* For test parse authSafe in incorrect condition.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_AUTHSAFE_INVALID_TC001(Hex *buff)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)buff;
SKIP_TEST();
#else
FuncStubInfo tmpRpInfo = {0};
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
char *pwd = "123456";
uint32_t pwdlen = strlen(pwd);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_PKCS12_ParseAsn1AddList, STUB_HITLS_PKCS12_ParseAsn1AddList) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_PKCS12_ParseContentInfo, STUB_HITLS_PKCS12_ParseContentInfo) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_AddListItemDefault, STUB_HITLS_X509_AddListItemDefault1) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_AddListItemDefault, STUB_HITLS_X509_AddListItemDefault2) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_AddListItemDefault, STUB_HITLS_X509_AddListItemDefault2) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, CRYPT_EAL_ProviderDecodeBuffKey, STUB_CRYPT_EAL_ProviderDecodeBuffKey) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_X509_CertParseBuff, STUB_HITLS_X509_CertParseBuff) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_PKCS12_BagNew, STUB_HITLS_PKCS12_BagNew) == 0);
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12), HITLS_PKI_SUCCESS);
STUB_Reset(&tmpRpInfo);
STUB_Init();
test = MAX_TRIGGERS; // It takes fixed triggers to parse cert from entering p12
marked = 0;
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, BSL_LIST_AddElement, STUB_BSL_LIST_AddElement) == 0);
HITLS_PKCS12_Free(p12);
p12 = NULL;
for (int i = MAX_TRIGGERS; i > 0; i--) {
p12 = HITLS_PKCS12_New();
marked = 0;
test--;
ASSERT_NE(HITLS_PKCS12_ParseAuthSafeData((BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, p12),
HITLS_PKI_SUCCESS);
HITLS_PKCS12_Free(p12);
p12 = NULL;
}
EXIT:
HITLS_PKCS12_Free(p12);
STUB_Reset(&tmpRpInfo);
#endif
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/pkcs12/test_suite_sdv_pkcs12.c | C | unknown | 85,886 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "bsl_sal.h"
#include "securec.h"
#include "hitls_pki_pkcs12.h"
#include "hitls_pki_errno.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "sal_file.h"
#include "bsl_init.h"
#include "hitls_pkcs12_local.h"
#include "hitls_crl_local.h"
#include "hitls_cert_type.h"
#include "hitls_cert_local.h"
#include "bsl_types.h"
#include "crypt_errno.h"
#include "stub_replace.h"
#include "bsl_list_internal.h"
#include "crypt_eal_rand.h"
/* END_HEADER */
static int32_t SetCertBag(HITLS_PKCS12 *p12, HITLS_X509_Cert *cert)
{
HITLS_PKCS12_Bag *certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, cert); // new a cert Bag
ASSERT_NE(certBag, NULL);
char *name = "I am a x509CertBag";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer = {0};
buffer.data = (uint8_t *)name;
buffer.dataLen = nameLen;
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_CERTBAG, certBag, 0), 0);
EXIT:
HITLS_PKCS12_BagFree(certBag);
return HITLS_PKI_SUCCESS;
}
static int32_t SetEntityCertBag(HITLS_PKCS12 *p12, HITLS_X509_Cert *cert)
{
HITLS_PKCS12_Bag *certBag = HITLS_PKCS12_BagNew(BSL_CID_CERTBAG, BSL_CID_X509CERTIFICATE, cert); // new a cert Bag
ASSERT_NE(certBag, NULL);
char *name = "I am a entity cert bag";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer = {0};
buffer.data = (uint8_t *)name;
buffer.dataLen = nameLen;
ASSERT_EQ(HITLS_PKCS12_BagCtrl(certBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_CERTBAG, certBag, 0), 0);
EXIT:
HITLS_PKCS12_BagFree(certBag);
return HITLS_PKI_SUCCESS;
}
static int32_t SetEntityKeyBag(HITLS_PKCS12 *p12, CRYPT_EAL_PkeyCtx *cert)
{
HITLS_PKCS12_Bag *pkeyBag = HITLS_PKCS12_BagNew(BSL_CID_PKCS8SHROUDEDKEYBAG, 0, cert); // new a cert Bag
ASSERT_NE(pkeyBag, NULL);
char *name = "I am a p8 encrypted bag";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer = {0};
buffer.data = (uint8_t *)name;
buffer.dataLen = nameLen;
ASSERT_EQ(HITLS_PKCS12_BagCtrl(pkeyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_SET_ENTITY_KEYBAG, pkeyBag, 0), 0);
EXIT:
HITLS_PKCS12_BagFree(pkeyBag);
return HITLS_PKI_SUCCESS;
}
static int32_t NewAndSetKeyBag(HITLS_PKCS12 *p12, int algId)
{
CRYPT_EAL_PkeyCtx *key = CRYPT_EAL_ProviderPkeyNewCtx(p12->libCtx, algId, 0, p12->attrName);
ASSERT_NE(key, NULL);
if (algId == BSL_CID_RSA) {
CRYPT_EAL_PkeyPara para = {.id = CRYPT_PKEY_RSA};
uint8_t e[] = {1, 0, 1};
para.para.rsaPara.e = e;
para.para.rsaPara.eLen = 3;
para.para.rsaPara.bits = 1024;
ASSERT_TRUE(CRYPT_EAL_PkeySetPara(key, ¶) == CRYPT_SUCCESS);
}
ASSERT_EQ(CRYPT_EAL_PkeyGen(key), 0);
HITLS_PKCS12_Bag *keyBag = HITLS_PKCS12_BagNew(BSL_CID_KEYBAG, 0, key); // new a key Bag
ASSERT_NE(keyBag, NULL);
char *name = "I am a keyBag";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer = {0};
buffer.data = (uint8_t *)name;
buffer.dataLen = nameLen;
ASSERT_EQ(HITLS_PKCS12_BagCtrl(keyBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_KEYBAG, keyBag, 0), 0);
EXIT:
CRYPT_EAL_PkeyFreeCtx(key);
HITLS_PKCS12_BagFree(keyBag);
return HITLS_PKI_SUCCESS;
}
static int32_t NewAndSetSecretBag(HITLS_PKCS12 *p12)
{
char *secret = "this is the secret.";
BSL_Buffer buffer = {0};
buffer.data = (uint8_t *)secret;
buffer.dataLen = strlen(secret);
// new a secret Bag
HITLS_PKCS12_Bag *secretBag = HITLS_PKCS12_BagNew(BSL_CID_SECRETBAG, BSL_CID_PKCS7_SIMPLEDATA, &buffer);
ASSERT_NE(secretBag, NULL);
char *name = "I am an attribute of secretBag";
uint32_t nameLen = strlen(name);
BSL_Buffer buffer2 = {0};
buffer2.data = (uint8_t *)name;
buffer2.dataLen = nameLen;
ASSERT_EQ(HITLS_PKCS12_BagCtrl(secretBag, HITLS_PKCS12_BAG_ADD_ATTR, &buffer2, BSL_CID_FRIENDLYNAME), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_ADD_SECRETBAG, secretBag, 0), 0);
EXIT:
HITLS_PKCS12_BagFree(secretBag);
return HITLS_PKI_SUCCESS;
}
/**
* For test generating a .p12 inlcuding keyBag, certBag, p8KeyBag and secretBag.
*/
/* BEGIN_CASE */
void SDV_PKCS12_GEN_KEYBAGS_TC001(char *pkeyPath, char *enCertPath, char *ca1CertPath, char *otherCertPath)
{
#ifndef HITLS_PKI_PKCS12_GEN
(void)pkeyPath;
(void)enCertPath;
(void)ca1CertPath;
(void)otherCertPath;
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SHA256, CRYPT_CIPHER_AES256_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SHA256, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
#ifdef HITLS_PKI_PKCS12_PARSE
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
#endif
CRYPT_EAL_PkeyCtx *pkey = NULL;
HITLS_X509_Cert *enCert = NULL;
HITLS_X509_Cert *ca1Cert = NULL;
HITLS_X509_Cert *otherCert = NULL;
int32_t mdId = CRYPT_MD_SHA1;
BSL_Buffer output = {0};
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
HITLS_PKCS12 *p12_1 = NULL;
BSL_ASN1_List *certList = NULL;
BSL_ASN1_List *keyList = NULL;
BSL_ASN1_List *secretBags = NULL;
ASSERT_EQ(CRYPT_EAL_DecodeFileKey(BSL_FORMAT_ASN1, CRYPT_PRIKEY_PKCS8_UNENCRYPT, pkeyPath, NULL, 0, &pkey), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, enCertPath, &enCert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, ca1CertPath, &ca1Cert), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, otherCertPath, &otherCert), HITLS_PKI_SUCCESS);
// Add the entity cert to p12.
ASSERT_EQ(SetEntityCertBag(p12, enCert), HITLS_PKI_SUCCESS);
// Add the entity key to p12.
ASSERT_EQ(SetEntityKeyBag(p12, pkey), HITLS_PKI_SUCCESS);
// Add the ca cert to p12.
ASSERT_EQ(SetCertBag(p12, ca1Cert), HITLS_PKI_SUCCESS);
// Add the other cert to p12.
ASSERT_EQ(SetCertBag(p12, otherCert), HITLS_PKI_SUCCESS);
// Gen and add key bag to p12.
ASSERT_EQ(NewAndSetKeyBag(p12, BSL_CID_RSA), HITLS_PKI_SUCCESS);
ASSERT_EQ(NewAndSetKeyBag(p12, BSL_CID_ED25519), HITLS_PKI_SUCCESS);
ASSERT_EQ(NewAndSetSecretBag(p12), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12, HITLS_PKCS12_GEN_LOCALKEYID, &mdId, sizeof(CRYPT_MD_AlgId)), 0);
// Gen a p12.
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), 0);
#ifdef HITLS_PKI_PKCS12_PARSE
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, true), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_CERTBAGS, &certList, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_KEYBAGS, &keyList, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_SECRETBAGS, &secretBags, 0), 0);
ASSERT_EQ(BSL_LIST_COUNT(certList), 2);
ASSERT_EQ(BSL_LIST_COUNT(keyList), 2);
ASSERT_EQ(BSL_LIST_COUNT(secretBags), 1);
ASSERT_NE(p12_1->entityCert, NULL);
ASSERT_NE(p12_1->key, NULL);
#endif
certList = NULL;
keyList = NULL;
secretBags = NULL;
BSL_SAL_FREE(output.data);
output.dataLen = 0;
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, false, &output), 0);
HITLS_PKCS12_Free(p12_1);
p12_1 = NULL;
#ifdef HITLS_PKI_PKCS12_PARSE
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, false), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_CERTBAGS, &certList, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_KEYBAGS, &keyList, 0), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_SECRETBAGS, &secretBags, 0), 0);
ASSERT_EQ(BSL_LIST_COUNT(certList), 2);
ASSERT_EQ(BSL_LIST_COUNT(keyList), 2);
ASSERT_EQ(BSL_LIST_COUNT(secretBags), 1);
ASSERT_NE(p12_1->entityCert, NULL);
ASSERT_NE(p12_1->key, NULL);
#endif
EXIT:
CRYPT_EAL_PkeyFreeCtx(pkey);
HITLS_X509_CertFree(enCert);
HITLS_X509_CertFree(ca1Cert);
HITLS_X509_CertFree(otherCert);
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
/**
* For test ctrl of get key bags from p12.
*/
/* BEGIN_CASE */
void SDV_PKCS12_CTRL_GET_KEYBAGS_TC001(void)
{
#ifndef HITLS_PKI_PKCS12_GEN
SKIP_TEST();
#else
TestMemInit();
ASSERT_EQ(TestRandInit(), 0);
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SHA256, CRYPT_CIPHER_AES256_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SHA256, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
#ifdef HITLS_PKI_PKCS12_PARSE
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
#endif
BSL_ASN1_List *keyList = NULL;
BSL_Buffer output = {0};
HITLS_PKCS12 *p12 = HITLS_PKCS12_New();
ASSERT_NE(p12, NULL);
HITLS_PKCS12 *p12_1 = NULL;
HITLS_PKCS12_Bag *bag = NULL;
uint8_t bufferValue[20] = {0}; // 20 bytes is enough for the attr value.
uint32_t bufferValueLen = 20;
int32_t id = 0;
BSL_Buffer buffer = {.data = bufferValue, .dataLen = bufferValueLen};
char *attrName = "I am a keyBag";
CRYPT_EAL_PkeyCtx *key = NULL;
ASSERT_EQ(NewAndSetKeyBag(p12, BSL_CID_RSA), HITLS_PKI_SUCCESS);
ASSERT_EQ(NewAndSetKeyBag(p12, BSL_CID_ED25519), HITLS_PKI_SUCCESS);
ASSERT_EQ(NewAndSetSecretBag(p12), HITLS_PKI_SUCCESS);
// Gen a p12.
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), 0);
#ifdef HITLS_PKI_PKCS12_PARSE
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &output, &pwdParam, &p12_1, true), 0);
ASSERT_EQ(HITLS_PKCS12_Ctrl(p12_1, HITLS_PKCS12_GET_KEYBAGS, &keyList, 0), 0);
ASSERT_EQ(BSL_LIST_COUNT(keyList), 2);
ASSERT_NE(keyList, NULL);
bag = BSL_LIST_GET_FIRST(keyList);
while (bag != NULL) {
ASSERT_EQ(HITLS_PKCS12_BagCtrl(bag, HITLS_PKCS12_BAG_GET_VALUE, &key, 0), 0);
ASSERT_NE(key, NULL);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(bag, HITLS_PKCS12_BAG_GET_ID, &id, sizeof(int32_t)), 0);
ASSERT_EQ(id, BSL_CID_KEYBAG);
ASSERT_EQ(HITLS_PKCS12_BagCtrl(bag, HITLS_PKCS12_BAG_GET_ATTR, &buffer, BSL_CID_FRIENDLYNAME), 0);
ASSERT_COMPARE("compare key bag attr", buffer.data, buffer.dataLen, attrName, strlen(attrName));
ASSERT_EQ(HITLS_PKCS12_BagCtrl(bag, HITLS_PKCS12_BAG_GET_ATTR, &buffer, BSL_CID_LOCALKEYID),
HITLS_PKCS12_ERR_NO_SAFEBAG_ATTRIBUTES);
bag = BSL_LIST_GET_NEXT(keyList);
CRYPT_EAL_PkeyFreeCtx(key);
key = NULL;
}
#endif
EXIT:
HITLS_PKCS12_Free(p12);
HITLS_PKCS12_Free(p12_1);
BSL_SAL_Free(output.data);
#endif
}
/* END_CASE */
/**
* For test parse multi bags in p12, including certBag, keyBag, secretBag, p8ShroudedKeyBag.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_MULBAG_TC001(Hex *mulBag, int hasMac)
{
#ifndef HITLS_PKI_PKCS12_PARSE
(void)mulBag;
(void)hasMac;
SKIP_TEST();
#else
char *pwd = "123456";
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
HITLS_PKCS12 *p12 = NULL;
BSL_Buffer buffer = {.data = (uint8_t *)mulBag->x, .dataLen = mulBag->len};
ASSERT_EQ(HITLS_PKCS12_ParseBuff(BSL_FORMAT_ASN1, &buffer, &pwdParam, &p12, hasMac == 1), 0);
ASSERT_NE(BSL_LIST_COUNT(p12->certList), 0);
ASSERT_NE(BSL_LIST_COUNT(p12->keyList), 0);
ASSERT_NE(BSL_LIST_COUNT(p12->secretBags), 0);
ASSERT_NE(p12->key, NULL);
EXIT:
HITLS_PKCS12_Free(p12);
#endif
}
/* END_CASE */
#if defined(HITLS_CRYPTO_PROVIDER) && defined(HITLS_PKI_PKCS12_PARSE) && defined(HITLS_PKI_PKCS12_GEN)
#define HITLS_P12_LIB_NAME "libprovider_load_p12.so"
#define HITLS_P12_PROVIDER_ATTR "provider?=p12Load"
#define HITLS_P12_PROVIDER_PATH "../../testcode/testdata/provider/path1"
static CRYPT_EAL_LibCtx *P12_ProviderLoadWithDefault(void)
{
CRYPT_EAL_LibCtx *libCtx = CRYPT_EAL_LibCtxNew();
ASSERT_TRUE(libCtx != NULL);
ASSERT_EQ(CRYPT_EAL_ProviderSetLoadPath(libCtx, HITLS_P12_PROVIDER_PATH), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_ProviderLoad(libCtx, 0, HITLS_P12_LIB_NAME, NULL, NULL), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_ProviderLoad(libCtx, 0, "default", NULL, NULL), CRYPT_SUCCESS);
ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(libCtx, CRYPT_RAND_SHA256, HITLS_P12_PROVIDER_ATTR, NULL, 0, NULL),
CRYPT_SUCCESS);
return libCtx;
EXIT:
CRYPT_EAL_LibCtxFree(libCtx);
return NULL;
}
static int32_t STUB_HITLS_PKCS12_CalMac(HITLS_PKCS12 *p12, BSL_Buffer *pwd, BSL_Buffer *initData, BSL_Buffer *output)
{
(void)p12;
(void)pwd;
(void)initData;
uint8_t *temp = BSL_SAL_Malloc(32);
ASSERT_TRUE(temp != NULL);
uint8_t outBuf[32] = {0xe3, 0x69, 0x5a, 0x9a, 0xfe, 0x27, 0x53, 0x4c,
0x95, 0x17, 0x67, 0xa2, 0x3f, 0x83, 0x9f, 0x94,
0x55, 0xb7, 0xf9, 0x00, 0x4b, 0xbc, 0x28, 0xbb,
0x9d, 0x59, 0x73, 0xbb, 0x91, 0xd0, 0x7c, 0x53};
memcpy(temp, outBuf, 32);
output->data = temp;
output->dataLen = 32;
EXIT:
return CRYPT_SUCCESS;
}
#endif
/**
* For test parse p12 with provider.
*/
/* BEGIN_CASE */
void SDV_PKCS12_PARSE_WITH_PROVIDER_TC001(Hex *mulBag)
{
#if !defined(HITLS_PKI_PKCS12_PARSE) || !defined(HITLS_CRYPTO_PROVIDER)
(void)mulBag;
SKIP_TEST();
#else
CRYPT_EAL_LibCtx *libCtx = NULL;
libCtx = P12_ProviderLoadWithDefault();
ASSERT_TRUE(libCtx != NULL);
FuncStubInfo tmpRpInfo = {0};
char *pwd = "123456";
BSL_Buffer encPwd = {.data = (uint8_t *)pwd, .dataLen = strlen(pwd)};
HITLS_PKCS12_PwdParam pwdParam = {.encPwd = &encPwd, .macPwd = &encPwd};
HITLS_PKCS12 *p12 = NULL;
BSL_Buffer buffer = {.data = (uint8_t *)mulBag->x, .dataLen = mulBag->len};
ASSERT_EQ(HITLS_PKCS12_ProviderParseBuff(libCtx, HITLS_P12_PROVIDER_ATTR, "ASN1", &buffer, &pwdParam, &p12, true),
HITLS_PKCS12_ERR_VERIFY_FAIL); // mac has been installed.
STUB_Init();
ASSERT_TRUE(STUB_Replace(&tmpRpInfo, HITLS_PKCS12_CalMac, STUB_HITLS_PKCS12_CalMac) == 0);
ASSERT_EQ(HITLS_PKCS12_ProviderParseBuff(libCtx, HITLS_P12_PROVIDER_ATTR, "ASN1", &buffer, &pwdParam, &p12, true),
HITLS_PKI_SUCCESS);
ASSERT_NE(p12, NULL);
ASSERT_NE(BSL_LIST_COUNT(p12->certList), 0);
ASSERT_NE(BSL_LIST_COUNT(p12->keyList), 0);
ASSERT_NE(BSL_LIST_COUNT(p12->secretBags), 0);
ASSERT_NE(p12->key, NULL);
EXIT:
HITLS_PKCS12_Free(p12);
CRYPT_EAL_RandDeinitEx(libCtx);
CRYPT_EAL_LibCtxFree(libCtx);
STUB_Reset(&tmpRpInfo);
#endif
}
/* END_CASE */
/**
* For test encode p12 with provider.
*/
/* BEGIN_CASE */
void SDV_PKCS12_ENCODE_WITH_PROVIDER_TC001(void)
{
#if !defined(HITLS_PKI_PKCS12_GEN) || !defined(HITLS_CRYPTO_PROVIDER)
SKIP_TEST();
#else
CRYPT_EAL_RandDeinit();
CRYPT_EAL_LibCtx *libCtx = NULL;
libCtx = P12_ProviderLoadWithDefault();
ASSERT_TRUE(libCtx != NULL);
BSL_Buffer output = {0};
char *pwd = "123456";
CRYPT_Pbkdf2Param pbParam = {BSL_CID_PBES2, BSL_CID_PBKDF2, CRYPT_MAC_HMAC_SM3, CRYPT_CIPHER_AES256_CBC,
16, (uint8_t *)pwd, strlen(pwd), 2048};
CRYPT_EncodeParam encParam = {CRYPT_DERIVE_PBKDF2, &pbParam};
HITLS_PKCS12_KdfParam macParam = {8, 2048, BSL_CID_SHA256, (uint8_t *)pwd, strlen(pwd)};
HITLS_PKCS12_MacParam paramTest = {.para = &macParam, .algId = BSL_CID_PKCS12KDF};
HITLS_PKCS12_EncodeParam encodeParam = {encParam, paramTest};
HITLS_PKCS12 *p12 = HITLS_PKCS12_ProviderNew(libCtx, HITLS_P12_PROVIDER_ATTR);
ASSERT_EQ(NewAndSetKeyBag(p12, BSL_CID_RSA), HITLS_PKI_SUCCESS);
ASSERT_EQ(HITLS_PKCS12_GenBuff(BSL_FORMAT_ASN1, p12, &encodeParam, true, &output), HITLS_PKI_SUCCESS);
EXIT:
BSL_SAL_Free(output.data);
HITLS_PKCS12_Free(p12);
CRYPT_EAL_RandDeinitEx(libCtx);
CRYPT_EAL_LibCtxFree(libCtx);
#endif
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/pkcs12/test_suite_sdv_pkcs12_util.c | C | unknown | 17,489 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "bsl_sal.h"
#include "securec.h"
#include "bsl_types.h"
#include "bsl_log.h"
#include "bsl_init.h"
#include "bsl_list.h"
#include "hitls_pki_x509.h"
#include "hitls_pki_errno.h"
#include "hitls_x509_verify.h"
#include "hitls_cert_local.h"
#include "hitls_crl_local.h"
#include "bsl_list_internal.h"
#include "sal_atomic.h"
#include "hitls_x509_verify.h"
#include "crypt_eal_md.h"
#include "crypt_errno.h"
#include "bsl_uio.h"
#include "hitls_pki_utils.h"
#include "bsl_asn1.h"
/* END_HEADER */
void HITLS_X509_FreeStoreCtxMock(HITLS_X509_StoreCtx *ctx)
{
if (ctx == NULL) {
return;
}
int ret;
(void)BSL_SAL_AtomicDownReferences(&ctx->references, &ret);
if (ret > 0) {
return;
}
if (ctx->store != NULL) {
BSL_LIST_FREE(ctx->store, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
if (ctx->crl != NULL) {
BSL_LIST_FREE(ctx->crl, (BSL_LIST_PFUNC_FREE)HITLS_X509_CrlFree);
}
BSL_SAL_ReferencesFree(&ctx->references);
BSL_SAL_Free(ctx);
}
HITLS_X509_StoreCtx *HITLS_X509_NewStoreCtxMock(void)
{
HITLS_X509_StoreCtx *ctx = (HITLS_X509_StoreCtx *)BSL_SAL_Malloc(sizeof(HITLS_X509_StoreCtx));
if (ctx == NULL) {
return NULL;
}
(void)memset_s(ctx, sizeof(HITLS_X509_StoreCtx), 0, sizeof(HITLS_X509_StoreCtx));
ctx->store = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
if (ctx->store == NULL) {
BSL_SAL_Free(ctx);
return NULL;
}
ctx->crl = BSL_LIST_New(sizeof(HITLS_X509_Crl *));
if (ctx->crl == NULL) {
BSL_SAL_FREE(ctx->store);
BSL_SAL_Free(ctx);
return NULL;
}
ctx->verifyParam.maxDepth = 20;
ctx->verifyParam.securityBits = 128;
ctx->verifyParam.flags |= HITLS_X509_VFY_FLAG_CRL_ALL;
ctx->verifyParam.flags |= HITLS_X509_VFY_FLAG_SECBITS;
BSL_SAL_ReferencesInit(&(ctx->references));
return ctx;
}
static int32_t HITLS_BuildChain(BslList *list, int type,
char *path1, char *path2, char *path3, char *path4, char *path5)
{
int32_t ret;
char *path[] = {path1, path2, path3, path4, path5};
for (size_t i = 0; i < sizeof(path) / sizeof(path[0]); i++) {
if (path[i] == NULL) {
continue;
}
if (type == 0) { // cert
HITLS_X509_Cert *cert = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, path[i], &cert);
if (ret != HITLS_PKI_SUCCESS) {
return ret;
}
ret = BSL_LIST_AddElement(list, cert, BSL_LIST_POS_END);
if (ret != BSL_SUCCESS) {
return ret;
}
} else { // crl
HITLS_X509_Crl *crl = NULL;
ret = HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path[i], &crl);
if (ret != HITLS_PKI_SUCCESS) {
return ret;
}
ret = BSL_LIST_AddElement(list, crl, BSL_LIST_POS_END);
if (ret != BSL_SUCCESS) {
return ret;
}
}
}
return ret;
}
/* BEGIN_CASE */
void SDV_X509_STORE_VFY_PARAM_EXR_FUNC_TC001(char *path1, char *path2, char *path3, int secBits, int exp)
{
int ret;
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = NULL;
storeCtx = HITLS_X509_NewStoreCtxMock();
ASSERT_NE(storeCtx, NULL);
storeCtx->verifyParam.securityBits = secBits;
BslList *chain = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
ASSERT_NE(chain, NULL);
ret = HITLS_BuildChain(chain, 0, path1, path2, path3, NULL, NULL);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_VerifyParamAndExt(storeCtx, chain);
ASSERT_EQ(ret, exp);
EXIT:
HITLS_X509_FreeStoreCtxMock(storeCtx);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_VFY_CRL_FUNC_TC001(int type, int expResult, char *path1, char *path2, char *path3,
char *crl1, char *crl2)
{
int ret;
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = NULL;
storeCtx = HITLS_X509_NewStoreCtxMock();
ASSERT_NE(storeCtx, NULL);
if (type == 1) {
storeCtx->verifyParam.flags ^= HITLS_X509_VFY_FLAG_CRL_ALL;
storeCtx->verifyParam.flags |= HITLS_X509_VFY_FLAG_CRL_DEV;
}
BslList *chain = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
ASSERT_NE(chain, NULL);
ret = HITLS_BuildChain(chain, 0, path1, path2, path3, NULL, NULL);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_BuildChain(storeCtx->crl, 1, crl1, crl2, NULL, NULL, NULL);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_VerifyCrl(storeCtx, chain);
ASSERT_EQ(ret, expResult);
EXIT:
HITLS_X509_FreeStoreCtxMock(storeCtx);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_CTRL_FUNC_TC001(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
int32_t val = 20;
int32_t ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_PARAM_DEPTH, &val, sizeof(int32_t));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(store->verifyParam.maxDepth, val);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_SECBITS, &val, sizeof(int32_t));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(store->verifyParam.securityBits, val);
ASSERT_EQ(store->verifyParam.flags, HITLS_X509_VFY_FLAG_SECBITS);
int64_t timeval = 55;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(store->verifyParam.time, timeval);
ASSERT_EQ(store->verifyParam.flags & HITLS_X509_VFY_FLAG_TIME, HITLS_X509_VFY_FLAG_TIME);
timeval = HITLS_X509_VFY_FLAG_TIME;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_CLR_PARAM_FLAGS, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(store->verifyParam.flags & HITLS_X509_VFY_FLAG_TIME, 0);
ASSERT_EQ(store->verifyParam.flags, HITLS_X509_VFY_FLAG_SECBITS);
int ref;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_REF_UP, &ref, sizeof(int));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ref, 2);
HITLS_X509_StoreCtxFree(store);
EXIT:
HITLS_X509_StoreCtxFree(store);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_CTRL_CERT_FUNC_TC002(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
HITLS_X509_Cert *cert = NULL;
int32_t ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/asn1/rsa2048ssa-pss.crt", &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_DEEP_COPY_SET_CA, cert, sizeof(HITLS_X509_Cert));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(cert->references.count, 2);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 1);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_DEEP_COPY_SET_CA, cert, sizeof(HITLS_X509_Cert));
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
HITLS_X509_Crl *crl = NULL;
ret = HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, "../testdata/cert/asn1/ca-empty-rsa-sha256-v2.der", &crl);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_CRL, crl, sizeof(HITLS_X509_Crl));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(crl->references.count, 2);
ASSERT_EQ(BSL_LIST_COUNT(store->crl), 1);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_CRL, crl, sizeof(HITLS_X509_Crl));
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(cert);
HITLS_X509_CrlFree(crl);
}
/* END_CASE */
static int32_t HITLS_AddCertToStoreTest(char *path, HITLS_X509_StoreCtx *store, HITLS_X509_Cert **cert)
{
int32_t ret = HITLS_X509_CertParseFile(BSL_FORMAT_UNKNOWN, path, cert);
if (ret != HITLS_PKI_SUCCESS) {
return ret;
}
return HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_DEEP_COPY_SET_CA, *cert, sizeof(HITLS_X509_Cert));
}
static int32_t HITLS_AddCrlToStoreTest(char *path, HITLS_X509_StoreCtx *store, HITLS_X509_Crl **crl)
{
int32_t ret = HITLS_X509_CrlParseFile(BSL_FORMAT_ASN1, path, crl);
if (ret != HITLS_PKI_SUCCESS) {
return ret;
}
return HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_CRL, *crl, sizeof(HITLS_X509_Crl));
}
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC001(char *rootPath, char *caPath, char *cert, char *crlPath)
{
TestMemInit();
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
ASSERT_EQ(HITLS_AddCertToStoreTest(rootPath, store, &root), HITLS_PKI_SUCCESS);
HITLS_X509_Cert *ca = NULL;
ASSERT_EQ(HITLS_AddCertToStoreTest(caPath, store, &ca), HITLS_PKI_SUCCESS);
HITLS_X509_Cert *entity = NULL;
ASSERT_TRUE(HITLS_AddCertToStoreTest(cert, store, &entity) != HITLS_PKI_SUCCESS);
HITLS_X509_Crl *crl = NULL;
ASSERT_EQ(HITLS_AddCrlToStoreTest(crlPath, store, &crl), HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->crl), 1);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 2);
HITLS_X509_List *chain = NULL;
ASSERT_TRUE(HITLS_X509_CertChainBuild(store, false, entity, &chain) == HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(chain), 2);
int64_t timeval = time(NULL);
ASSERT_EQ(HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval)), 0);
int64_t flag = HITLS_X509_VFY_FLAG_CRL_ALL;
ASSERT_EQ(HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_CLR_PARAM_FLAGS, &flag, sizeof(flag)), 0);
ASSERT_EQ(HITLS_X509_CertVerify(store, chain), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
HITLS_X509_CrlFree(crl);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC002(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *ca = NULL;
int32_t ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-pss-v3/inter.der", store, &ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *entity = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-pss-v3/end.der", store, &entity);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 1);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, entity, &chain);
ASSERT_TRUE(ret == HITLS_PKI_SUCCESS);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
HITLS_X509_Cert *root = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-pss-v3/ca.der", store, &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertChainBuild(store, false, entity, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(chain), 2);
int64_t timeval = time(NULL);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
static int32_t X509_AddCertToChainTest(HITLS_X509_List *chain, HITLS_X509_Cert *cert)
{
int ref;
int32_t ret = HITLS_X509_CertCtrl(cert, HITLS_X509_REF_UP, &ref, sizeof(int));
if (ret != HITLS_PKI_SUCCESS) {
return ret;
}
ret = BSL_LIST_AddElement(chain, cert, BSL_LIST_POS_END);
if (ret != HITLS_PKI_SUCCESS) {
HITLS_X509_CertFree(cert);
}
return ret;
}
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC003(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *ca = NULL;
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/chain/rsa-pss-v3/ca.der", &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-pss-v3/inter.der", store, &ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *entity = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/chain/rsa-pss-v3/end.der", &entity);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 1);
HITLS_X509_List *chain = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
ASSERT_TRUE(chain != NULL);
ret = X509_AddCertToChainTest(chain, entity);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = X509_AddCertToChainTest(chain, ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC004(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-pss-v3/ca.der", store, &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 1);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, root, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_TRUE(chain != NULL);
ASSERT_EQ(BSL_LIST_COUNT(chain), 1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC005(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/chain/rsa-pss-v3/ca.der", &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 0);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, root, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_TRUE(chain != NULL);
ASSERT_EQ(BSL_LIST_COUNT(chain), 1);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC006(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/chain/rsa-pss-v3/ca.der", &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 0);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, root, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_TRUE(chain != NULL);
ASSERT_EQ(BSL_LIST_COUNT(chain), 1);
int64_t timeval = 5555;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC007(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/rootca.der", store, &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *ca = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/ca.der", store, &ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *entity = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/cert.der", store, &entity);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 2);
int32_t depth = 2;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_PARAM_DEPTH, &depth, sizeof(depth));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, entity, &chain);
ASSERT_TRUE(ret == HITLS_PKI_SUCCESS);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
chain = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
ASSERT_TRUE(chain != NULL);
ret = X509_AddCertToChainTest(chain, entity);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
int64_t timeval = time(NULL);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC008(char *rootPath, char *caPath, char *cert, char *rootcrlpath, char *cacrlpath, int flag, int except)
{
TestMemInit();
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *root = NULL;
int32_t ret = HITLS_AddCertToStoreTest(rootPath, store, &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *ca = NULL;
ret = HITLS_AddCertToStoreTest(caPath, store, &ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_Cert *entity = NULL;
ret = HITLS_AddCertToStoreTest(cert, store, &entity);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 2);
HITLS_X509_Crl *rootcrl = NULL;
if (strlen(rootcrlpath) != 0) {
ret = HITLS_AddCrlToStoreTest(rootcrlpath, store, &rootcrl);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
}
HITLS_X509_Crl *cacrl = NULL;
ret = HITLS_AddCrlToStoreTest(cacrlpath, store, &cacrl);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
if (strlen(rootcrlpath) == 0) {
ASSERT_EQ(BSL_LIST_COUNT(store->crl), 1);
} else {
ASSERT_EQ(BSL_LIST_COUNT(store->crl), 2);
}
int32_t depth = 3;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_PARAM_DEPTH, &depth, sizeof(depth));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, false, entity, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
int64_t setFlag = (int64_t)flag;
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_PARAM_FLAGS, &setFlag, sizeof(int64_t));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
int64_t timeval = time(NULL);
ret = HITLS_X509_StoreCtxCtrl(store, HITLS_X509_STORECTX_SET_TIME, &timeval, sizeof(timeval));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret == except);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
HITLS_X509_CrlFree(rootcrl);
HITLS_X509_CrlFree(cacrl);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_FUNC_TC009(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_List *chain = BSL_LIST_New(sizeof(HITLS_X509_Cert *));
int32_t ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
HITLS_X509_Cert *root = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, "../testdata/cert/chain/rsa-pss-v3/ca.der", &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = X509_AddCertToChainTest(chain, root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = BSL_LIST_AddElementInt(chain, NULL, BSL_LIST_POS_BEGIN);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_CertVerify(store, chain);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_BUILD_CERT_CHAIN_WITH_ROOT_FUNC_TC001(void)
{
HITLS_X509_StoreCtx *store = HITLS_X509_StoreCtxNew();
ASSERT_TRUE(store != NULL);
HITLS_X509_Cert *entity = NULL;
int32_t ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/cert.der", store, &entity);
ASSERT_TRUE(ret != HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 0);
HITLS_X509_Cert *ca = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/ca.der", store, &ca);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 1);
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(store, true, entity, &chain);
ASSERT_EQ(ret, HITLS_X509_ERR_ISSUE_CERT_NOT_FOUND);
HITLS_X509_Cert *root = NULL;
ret = HITLS_AddCertToStoreTest("../testdata/cert/chain/rsa-v3/rootca.der", store, &root);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(store->store), 2);
ret = HITLS_X509_CertChainBuild(store, true, entity, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_EQ(BSL_LIST_COUNT(chain), 3);
EXIT:
HITLS_X509_StoreCtxFree(store);
HITLS_X509_CertFree(root);
HITLS_X509_CertFree(ca);
HITLS_X509_CertFree(entity);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_SM2_CERT_USERID_FUNC_TC001(char *caCertPath, char *interCertPath, char *entityCertPath,
int isUseDefaultUserId)
{
TestMemInit();
TestRandInit();
HITLS_X509_Cert *entityCert = NULL;
HITLS_X509_Cert *interCert = NULL;
HITLS_X509_Cert *caCert = NULL;
HITLS_X509_List *chain = NULL;
char sm2DefaultUserid[] = "1234567812345678";
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
ASSERT_EQ(HITLS_AddCertToStoreTest(caCertPath, storeCtx, &caCert), 0);
ASSERT_EQ(HITLS_AddCertToStoreTest(interCertPath, storeCtx, &interCert), 0);
ASSERT_EQ(HITLS_X509_CertParseFile(BSL_FORMAT_UNKNOWN, entityCertPath, &entityCert), 0);
ASSERT_EQ(BSL_LIST_COUNT(storeCtx->store), 2);
if (isUseDefaultUserId != 0) {
ASSERT_EQ(HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_SET_VFY_SM2_USERID, sm2DefaultUserid,
strlen(sm2DefaultUserid)), 0);
}
ASSERT_EQ(HITLS_X509_CertChainBuild(storeCtx, false, entityCert, &chain), 0);
ASSERT_EQ(HITLS_X509_CertVerify(storeCtx, chain), HITLS_PKI_SUCCESS);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
HITLS_X509_CertFree(entityCert);
HITLS_X509_CertFree(interCert);
HITLS_X509_CertFree(caCert);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_LOAD_CA_PATH_FUNC_TC001(void)
{
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
// Test adding additional CA path
const char *testPath1 = "/usr/local/ssl/certs";
int32_t ret =
HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)testPath1, strlen(testPath1));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ret = HITLS_X509_StoreCtxCtrl(NULL, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)testPath1, strlen(testPath1));
ASSERT_EQ(ret, HITLS_X509_ERR_INVALID_PARAM);
ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH,
NULL, strlen(testPath1));
ASSERT_EQ(ret, HITLS_X509_ERR_INVALID_PARAM);
ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH,
(void *)testPath1, 0);
ASSERT_EQ(ret, HITLS_X509_ERR_INVALID_PARAM);
ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH,
(void *)testPath1, 4097);
ASSERT_EQ(ret, HITLS_X509_ERR_INVALID_PARAM);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_LOAD_CA_PATH_CHAIN_BUILD_TC001(void)
{
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
// Add additional CA paths
const char *caPath = "../testdata/tls/certificate/pem/rsa_sha256";
int32_t ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)caPath, strlen(caPath));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Load the certificate to be verified
const char *certToVerify = "../testdata/tls/certificate/pem/rsa_sha256/client.pem";
HITLS_X509_Cert *cert = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_PEM, certToVerify, &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Build certificate chain with on-demand CA loading from multiple paths
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(storeCtx, true, cert, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(chain, NULL);
uint32_t chainLength = BSL_LIST_COUNT(chain);
ASSERT_TRUE(chainLength >= 1);
// Verify the certificate chain
ret = HITLS_X509_CertVerify(storeCtx, chain);
printf("Certificate verification result: %d\n", ret);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
HITLS_X509_CertFree(cert);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_LOAD_CA_PATH_CHAIN_BUILD_TC002(void)
{
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
// Add additional CA paths
const char *caPath = "../testdata/tls/certificate/pem/ed25519";
int32_t ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)caPath, strlen(caPath));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Load the certificate to be verified
const char *certToVerify = "../testdata/tls/certificate/pem/rsa_sha256/client.pem";
HITLS_X509_Cert *cert = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_PEM, certToVerify, &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Build certificate chain with on-demand CA loading from multiple paths
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(storeCtx, true, cert, &chain);
ASSERT_EQ(ret, HITLS_X509_ERR_ISSUE_CERT_NOT_FOUND);
ASSERT_EQ(chain, NULL);
HITLS_X509_CertFree(cert);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_LOAD_CA_PATH_CHAIN_BUILD_TC003(void)
{
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
// Add additional CA paths
const char *caPath = "../testdata/tls/certificate/pem/test_dir";
int32_t ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)caPath, strlen(caPath));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Load the certificate to be verified
const char *certToVerify = "../testdata/tls/certificate/pem/rsa_sha256/client.pem";
HITLS_X509_Cert *cert = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_PEM, certToVerify, &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Build certificate chain with on-demand CA loading from multiple paths
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(storeCtx, true, cert, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(chain, NULL);
HITLS_X509_CertFree(cert);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_X509_STORE_LOAD_CA_PATH_CHAIN_BUILD_TC004(void)
{
TestMemInit();
HITLS_X509_StoreCtx *storeCtx = HITLS_X509_StoreCtxNew();
ASSERT_NE(storeCtx, NULL);
// Add additional CA paths
const char *caPath = "../testdata/tls/certificate/pem/test_dir";
int32_t ret = HITLS_X509_StoreCtxCtrl(storeCtx, HITLS_X509_STORECTX_ADD_CA_PATH, (void *)caPath, strlen(caPath));
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Load the certificate to be verified
const char *certToVerify = "../testdata/tls/certificate/pem/ecdsa_sha256/client.pem";
HITLS_X509_Cert *cert = NULL;
ret = HITLS_X509_CertParseFile(BSL_FORMAT_PEM, certToVerify, &cert);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
// Build certificate chain with on-demand CA loading from multiple paths
HITLS_X509_List *chain = NULL;
ret = HITLS_X509_CertChainBuild(storeCtx, true, cert, &chain);
ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
ASSERT_NE(chain, NULL);
HITLS_X509_CertFree(cert);
BSL_LIST_FREE(chain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
EXIT:
HITLS_X509_StoreCtxFree(storeCtx);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/pki/verify/test_suite_sdv_x509_vfy.c | C | unknown | 30,479 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <unistd.h>
#include <semaphore.h>
#include "hitls_build.h"
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
#include "test.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10088
bool SkipTlsTest(int connType, int version)
{
switch (version) {
#ifdef HITLS_TLS_PROTO_TLS13
case TLS1_3:
break;
#endif
#ifdef HITLS_TLS_PROTO_TLS12
case TLS1_2:
break;
#endif
#ifdef HITLS_TLS_PROTO_DTLS12
case DTLS1_2:
break;
#endif
#ifdef HITLS_TLS_PROTO_TLCP11
case TLCP1_1:
break;
#endif
#ifdef HITLS_TLS_PROTO_DTLCP11
case DTLCP1_1:
break;
#endif
#ifdef HITLS_TLS_PROTO_ALL
case TLS_ALL:
break;
#endif
default:
return true;
}
switch (connType) {
#ifdef HITLS_BSL_UIO_TCP
case TCP:
break;
#endif
#ifdef HITLS_BSL_UIO_UDP
case UDP:
break;
#endif
default:
return true;
}
return false;
}
/* BEGIN_CASE */
void SDV_TLS_BASE_CONNECT_TC01(int connType, int version)
{
if (SkipTlsTest(connType, version)) {
SKIP_TEST();
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
if (version == TLCP1_1 || version == DTLCP1_1) {
serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
} else {
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
}
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
// Configure link information on the server.
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure link information on the client.
clientRes = HLT_ProcessTlsConnect(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/ciphersuite/test_suite_sdv_hlt_base_connect.c | C | unknown | 3,611 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10086
int32_t g_testSecurityLevel = 0;
void SetCert(HLT_Ctx_Config *ctxConfig, char *cert)
{
if (strncmp(cert, "RSA", strlen("RSA")) == 0) {
HLT_SetCertPath(ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3,
"NULL", "NULL");
} else if (strncmp(cert, "ECDSA", strlen("ECDSA")) == 0) {
HLT_SetCertPath(ctxConfig, ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH, "NULL", "NULL");
}
}
void SetGMCert(HLT_Ctx_Config *serverCtxConfig, HLT_Ctx_Config *clientCtxConfig, char *cert)
{
if (strncmp(cert, "SM2", strlen("SM2")) == 0) {
HLT_SetCertPath(serverCtxConfig, SM2_VERIFY_PATH, SM2_CHAIN_PATH, SM2_SERVER_ENC_CERT_PATH, SM2_SERVER_ENC_KEY_PATH,
SM2_SERVER_SIGN_CERT_PATH, SM2_SERVER_SIGN_KEY_PATH);
HLT_SetCertPath(clientCtxConfig, SM2_VERIFY_PATH, SM2_CHAIN_PATH, SM2_CLIENT_ENC_CERT_PATH, SM2_CLIENT_ENC_KEY_PATH,
SM2_CLIENT_SIGN_CERT_PATH, SM2_CLIENT_SIGN_KEY_PATH);
}
}
char *HITLS_TLS13_Ciphersuite[] = {
"HITLS_AES_128_GCM_SHA256",
"HITLS_AES_256_GCM_SHA384",
"HITLS_CHACHA20_POLY1305_SHA256",
"HITLS_AES_128_CCM_SHA256",
"HITLS_AES_128_CCM_8_SHA256",
};
// RSA Authentication
char *HITLS_RSA_Ciphersuite[] = {
"HITLS_RSA_WITH_AES_128_CBC_SHA",
"HITLS_RSA_WITH_AES_256_CBC_SHA",
"HITLS_RSA_WITH_AES_128_CBC_SHA256",
"HITLS_RSA_WITH_AES_256_CBC_SHA256",
"HITLS_RSA_WITH_AES_128_GCM_SHA256",
"HITLS_RSA_WITH_AES_256_GCM_SHA384",
"HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
"HITLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
"HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
"HITLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
"HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
"HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_DHE_RSA_WITH_AES_128_CBC_SHA",
"HITLS_DHE_RSA_WITH_AES_256_CBC_SHA",
"HITLS_DHE_RSA_WITH_AES_128_CBC_SHA256",
"HITLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"HITLS_DHE_RSA_WITH_AES_128_CCM",
"HITLS_DHE_RSA_WITH_AES_256_CCM",
"HITLS_RSA_WITH_AES_256_CCM",
"HITLS_RSA_WITH_AES_256_CCM_8",
"HITLS_RSA_WITH_AES_128_CCM",
"HITLS_RSA_WITH_AES_128_CCM_8",
};
// ECDSA Authentication
char *HITLS_ECDSA_Ciphersuite[] = {
"HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
"HITLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
"HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
"HITLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
"HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
"HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
"HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_ECDHE_ECDSA_WITH_AES_128_CCM",
"HITLS_ECDHE_ECDSA_WITH_AES_256_CCM",
};
char *HITLS_ANON_Ciphersuite[] = {
"HITLS_DH_ANON_WITH_AES_128_CBC_SHA",
"HITLS_DH_ANON_WITH_AES_256_CBC_SHA",
"HITLS_DH_ANON_WITH_AES_128_CBC_SHA256",
"HITLS_DH_ANON_WITH_AES_256_CBC_SHA256",
"HITLS_DH_ANON_WITH_AES_128_GCM_SHA256",
"HITLS_DH_ANON_WITH_AES_256_GCM_SHA384",
"HITLS_ECDH_ANON_WITH_AES_128_CBC_SHA",
"HITLS_ECDH_ANON_WITH_AES_256_CBC_SHA",
};
// PSK Authentication
char *HITLS_PSK_Ciphersuite[] = {
"HITLS_PSK_WITH_AES_128_CBC_SHA",
"HITLS_PSK_WITH_AES_256_CBC_SHA",
"HITLS_PSK_WITH_AES_128_GCM_SHA256",
"HITLS_PSK_WITH_AES_256_GCM_SHA384",
"HITLS_PSK_WITH_AES_128_CBC_SHA256",
"HITLS_PSK_WITH_AES_256_CBC_SHA384",
"HITLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
"HITLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
"HITLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256",
"HITLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384",
"HITLS_PSK_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_RSA_PSK_WITH_AES_128_CBC_SHA",
"HITLS_RSA_PSK_WITH_AES_256_CBC_SHA",
"HITLS_RSA_PSK_WITH_AES_128_GCM_SHA256",
"HITLS_RSA_PSK_WITH_AES_256_GCM_SHA384",
"HITLS_RSA_PSK_WITH_AES_128_CBC_SHA256",
"HITLS_RSA_PSK_WITH_AES_256_CBC_SHA384",
"HITLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_DHE_PSK_WITH_AES_128_CBC_SHA",
"HITLS_DHE_PSK_WITH_AES_256_CBC_SHA",
"HITLS_DHE_PSK_WITH_AES_128_GCM_SHA256",
"HITLS_DHE_PSK_WITH_AES_256_GCM_SHA384",
"HITLS_DHE_PSK_WITH_AES_128_CBC_SHA256",
"HITLS_DHE_PSK_WITH_AES_256_CBC_SHA384",
"HITLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
"HITLS_DHE_PSK_WITH_AES_128_CCM",
"HITLS_DHE_PSK_WITH_AES_256_CCM",
"HITLS_PSK_WITH_AES_256_CCM",
"HITLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256",
"HITLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384",
"HITLS_ECDHE_PSK_WITH_AES_128_CCM_SHA256",
};
char *HITLS_GM_Ciphersuite[] = {
"HITLS_ECDHE_SM4_CBC_SM3",
"HITLS_ECC_SM4_CBC_SM3",
"HITLS_ECDHE_SM4_GCM_SM3",
"HITLS_ECC_SM4_GCM_SM3",
};
static void CONNECT(int version, int connType, char *Ciphersuite, int hasPsk, char *cert)
{
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
if (version == TLCP1_1 || version == DTLCP1_1) {
serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
} else {
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
}
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
uint8_t psk[] = "12121212121212";
if (hasPsk) {
memcpy_s(serverCtxConfig->psk, PSK_MAX_LEN, psk, sizeof(psk));
memcpy_s(clientCtxConfig->psk, PSK_MAX_LEN, psk, sizeof(psk));
}
serverCtxConfig->securitylevel = g_testSecurityLevel;
clientCtxConfig->securitylevel = g_testSecurityLevel;
if (version == TLCP1_1 || version == DTLCP1_1) {
SetGMCert(serverCtxConfig, clientCtxConfig, cert);
} else {
SetCert(serverCtxConfig, cert);
SetCert(clientCtxConfig, cert);
}
HLT_SetCipherSuites(serverCtxConfig, Ciphersuite);
HLT_SetCipherSuites(clientCtxConfig, Ciphersuite);
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/* BEGIN_CASE */
void SDV_TLS_TLS13_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_TLS13_Ciphersuite) / sizeof(HITLS_TLS13_Ciphersuite[0]); i++) {
CONNECT(TLS1_3, TCP, HITLS_TLS13_Ciphersuite[i], 0, "RSA");
}
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_RSA_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_RSA_Ciphersuite) / sizeof(HITLS_RSA_Ciphersuite[0]); i++) {
SUB_PROC_BEGIN(continue);
CONNECT(TLS1_2, TCP, HITLS_RSA_Ciphersuite[i], 0, "RSA");
if (IsEnableSctpAuth()) {
CONNECT(DTLS1_2, SCTP, HITLS_RSA_Ciphersuite[i], 0, "RSA");
}
CONNECT(DTLS1_2, UDP, HITLS_RSA_Ciphersuite[i], 0, "RSA");
SUB_PROC_END();
}
SUB_PROC_WAIT(sizeof(HITLS_RSA_Ciphersuite) / sizeof(HITLS_RSA_Ciphersuite[0]));
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_ECDSA_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_ECDSA_Ciphersuite) / sizeof(HITLS_ECDSA_Ciphersuite[0]); i++) {
SUB_PROC_BEGIN(continue);
CONNECT(TLS1_2, TCP, HITLS_ECDSA_Ciphersuite[i], 0, "ECDSA");
if (IsEnableSctpAuth()) {
CONNECT(DTLS1_2, SCTP, HITLS_ECDSA_Ciphersuite[i], 0, "ECDSA");
}
CONNECT(DTLS1_2, UDP, HITLS_ECDSA_Ciphersuite[i], 0, "ECDSA");
SUB_PROC_END();
}
SUB_PROC_WAIT(sizeof(HITLS_ECDSA_Ciphersuite) / sizeof(HITLS_ECDSA_Ciphersuite[0]));
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_PSK_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_PSK_Ciphersuite) / sizeof(HITLS_PSK_Ciphersuite[0]); i++)
{
SUB_PROC_BEGIN(continue);
CONNECT(TLS1_2, TCP, HITLS_PSK_Ciphersuite[i], 1, "RSA");
if (IsEnableSctpAuth()) {
CONNECT(DTLS1_2, SCTP, HITLS_PSK_Ciphersuite[i], 1, "RSA");
}
CONNECT(DTLS1_2, UDP, HITLS_PSK_Ciphersuite[i], 1, "RSA");
SUB_PROC_END();
}
SUB_PROC_WAIT(sizeof(HITLS_PSK_Ciphersuite) / sizeof(HITLS_PSK_Ciphersuite[0]));
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_ANON_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_ANON_Ciphersuite) / sizeof(HITLS_ANON_Ciphersuite[0]); i++) {
SUB_PROC_BEGIN(continue);
CONNECT(TLS1_2, TCP, HITLS_ANON_Ciphersuite[i], 0, "RSA");
if (IsEnableSctpAuth()) {
CONNECT(DTLS1_2, SCTP, HITLS_ANON_Ciphersuite[i], 0, "RSA");
}
CONNECT(DTLS1_2, UDP, HITLS_ANON_Ciphersuite[i], 0, "RSA");
SUB_PROC_END();
}
SUB_PROC_WAIT(sizeof(HITLS_ANON_Ciphersuite) / sizeof(HITLS_ANON_Ciphersuite[0]));
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_GM_CIPHER_SUITE(void)
{
for (uint16_t i = 0; i < sizeof(HITLS_GM_Ciphersuite) / sizeof(HITLS_GM_Ciphersuite[0]); i++) {
SUB_PROC_BEGIN(continue);
CONNECT(TLCP1_1, TCP, HITLS_GM_Ciphersuite[i], 0, "SM2");
if (IsEnableSctpAuth()) {
CONNECT(DTLCP1_1, SCTP, HITLS_GM_Ciphersuite[i], 0, "SM2");
}
SUB_PROC_END();
}
SUB_PROC_WAIT(sizeof(HITLS_GM_Ciphersuite) / sizeof(HITLS_GM_Ciphersuite[0]));
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/ciphersuite/test_suite_sdv_hlt_ciphersuite.c | C | unknown | 11,199 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10086
int32_t g_testSecurityLevel = 0;
void SetCert(HLT_Ctx_Config *ctxConfig, char *cert)
{
if (strncmp(cert, "ECDSA-384", strlen("ECDSA-384")) == 0) {
HLT_SetCertPath(ctxConfig, ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(cert, "ECDSA-512", strlen("ECDSA-512")) == 0) {
HLT_SetCertPath(ctxConfig, ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(cert, "ECDSA", strlen("ECDSA")) == 0) {
HLT_SetCertPath(ctxConfig, ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(cert, "RSAE", strlen("RSAE")) == 0) {
HLT_SetCertPath(ctxConfig, RSAPSS_RSAE_CA_PATH, RSAPSS_RSAE_CHAIN_PATH, RSAPSS_RSAE_EE_PATH,
RSAPSS_RSAE_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(cert, "RSAPSS", strlen("RSAPSS")) == 0) {
HLT_SetCertPath(ctxConfig, RSAPSS_SHA256_CA_PATH, RSAPSS_SHA256_CHAIN_PATH, RSAPSS_SHA256_EE_PATH,
RSAPSS_SHA256_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(cert, "RSA", strlen("RSA")) == 0) {
HLT_SetCertPath(ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3,
"NULL", "NULL");
} else if (strncmp(cert, "ED25519", strlen("ED25519")) == 0) {
HLT_SetCertPath(ctxConfig, ED25519_SHA512_CA_PATH, ED25519_SHA512_CHAIN_PATH, ED25519_SHA512_EE_PATH,
ED25519_SHA512_PRIV_PATH, "NULL", "NULL");
}
}
void SetGMCert(HLT_Ctx_Config *serverCtxConfig, HLT_Ctx_Config *clientCtxConfig, char *cert)
{
if (strncmp(cert, "SM2", strlen("SM2")) == 0) {
HLT_SetCertPath(serverCtxConfig, SM2_VERIFY_PATH, SM2_CHAIN_PATH, SM2_SERVER_ENC_CERT_PATH, SM2_SERVER_ENC_KEY_PATH,
SM2_SERVER_SIGN_CERT_PATH, SM2_SERVER_SIGN_KEY_PATH);
HLT_SetCertPath(clientCtxConfig, SM2_VERIFY_PATH, SM2_CHAIN_PATH, SM2_CLIENT_ENC_CERT_PATH, SM2_CLIENT_ENC_KEY_PATH,
SM2_CLIENT_SIGN_CERT_PATH, SM2_CLIENT_SIGN_KEY_PATH);
}
}
char *HITLS_Groups[] = {
"HITLS_EC_GROUP_BRAINPOOLP256R1",
"HITLS_EC_GROUP_BRAINPOOLP384R1",
"HITLS_EC_GROUP_BRAINPOOLP512R1",
"HITLS_EC_GROUP_SECP256R1",
"HITLS_EC_GROUP_SECP384R1",
"HITLS_EC_GROUP_SECP521R1",
"HITLS_EC_GROUP_CURVE25519",
"HITLS_FF_DHE_2048",
"HITLS_FF_DHE_3072",
"HITLS_FF_DHE_4096",
"HITLS_FF_DHE_6144",
"HITLS_FF_DHE_8192",
};
char *HITLS_Signatures[] = {
"CERT_SIG_SCHEME_RSA_PKCS1_SHA1",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA224",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA384",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA512",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256",
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA384",
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA512",
"CERT_SIG_SCHEME_ECDSA_SHA1",
"CERT_SIG_SCHEME_ECDSA_SHA224",
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
};
static void CONNECT(int version, int connType, char *Ciphersuite, char *groups, char *signature, int hasPsk, char *cert)
{
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
if (version == TLCP1_1 || version == DTLCP1_1) {
serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
} else {
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
}
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
uint8_t psk[] = "12121212121212";
if (hasPsk) {
memcpy_s(serverCtxConfig->psk, PSK_MAX_LEN, psk, sizeof(psk));
memcpy_s(clientCtxConfig->psk, PSK_MAX_LEN, psk, sizeof(psk));
}
serverCtxConfig->securitylevel = g_testSecurityLevel;
clientCtxConfig->securitylevel = g_testSecurityLevel;
if (version == TLCP1_1 || version == DTLCP1_1) {
SetGMCert(serverCtxConfig, clientCtxConfig, cert);
} else {
SetCert(serverCtxConfig, cert);
SetCert(clientCtxConfig, cert);
}
HLT_SetCipherSuites(serverCtxConfig, Ciphersuite);
HLT_SetCipherSuites(clientCtxConfig, Ciphersuite);
HLT_SetGroups(clientCtxConfig, groups);
HLT_SetGroups(serverCtxConfig, groups);
HLT_SetSignature(clientCtxConfig, signature);
HLT_SetSignature(serverCtxConfig, signature);
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
static void CONNECT_V12(char *Ciphersuite, char *groups, char *signature, int hasPsk, char *cert)
{
CONNECT(TLS1_2, TCP, Ciphersuite, groups, signature, hasPsk, cert);
if (IsEnableSctpAuth()) {
CONNECT(DTLS1_2, SCTP, Ciphersuite, groups, signature, hasPsk, cert);
}
CONNECT(DTLS1_2, UDP, Ciphersuite, groups, signature, hasPsk, cert);
}
/* BEGIN_CASE */
void SDV_TLS_13_GROUP(void)
{
for (uint16_t i = 3; i < sizeof(HITLS_Groups) / sizeof(HITLS_Groups[0]); i++) {
CONNECT(TLS1_3, TCP, "HITLS_AES_256_GCM_SHA384", HITLS_Groups[i], NULL, 0, "RSA");
}
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_12_GROUP(char *groups)
{
CONNECT_V12("HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", groups, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", 0, "RSA");
}
/* END_CASE */
/*
tls12 dtls12
"CERT_SIG_SCHEME_ECDSA_SHA1",
"CERT_SIG_SCHEME_ECDSA_SHA224",
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
*/
/* BEGIN_CASE */
void SDV_TLS_ECDSA_SIGNATURE(char *signature)
{
CONNECT_V12("HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "HITLS_EC_GROUP_SECP256R1", signature, 0, "ECDSA");
}
/* END_CASE */
/*
tls12 dtls12
"CERT_SIG_SCHEME_RSA_PKCS1_SHA1",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA224",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA384",
"CERT_SIG_SCHEME_RSA_PKCS1_SHA512",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
*/
/* BEGIN_CASE */
void SDV_TLS_RSA_SIGNATURE(char *signature)
{
CONNECT_V12("HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "HITLS_EC_GROUP_SECP256R1", signature, 0, "RSAE");
}
/* END_CASE */
/*
tls12 dtls12 tls13
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256",
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA384",
"CERT_SIG_SCHEME_RSA_PSS_PSS_SHA512",
*/
/* BEGIN_CASE */
void SDV_TLS_RSAPSS_SIGNATURE(char *signature)
{
CONNECT(TLS1_3, TCP, "HITLS_AES_256_GCM_SHA384", "HITLS_EC_GROUP_SECP256R1", signature, 0, "RSAPSS");
CONNECT_V12("HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "HITLS_EC_GROUP_SECP256R1", signature, 0, "RSAPSS");
}
/* END_CASE */
/*
tls13
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
*/
/* BEGIN_CASE */
void SDV_TLS13_RSA_SIGNATURE(char *signature)
{
CONNECT(TLS1_3, TCP, "HITLS_AES_256_GCM_SHA384", "HITLS_EC_GROUP_SECP256R1", signature, 0, "RSA");
}
/* END_CASE */
/*
tls13
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
*/
/* BEGIN_CASE */
void SDV_TLS13_ECDSA_SIGNATURE(char *signature, char *cert)
{
CONNECT(TLS1_3, TCP, "HITLS_AES_256_GCM_SHA384", "HITLS_EC_GROUP_SECP256R1", signature, 0, cert);
}
/* END_CASE */
/*
tls13
"CERT_SIG_SCHEME_ED25519"
*/
/* BEGIN_CASE */
void SDV_TLS13_EDDSA_SIGNATURE(char *signature)
{
CONNECT(TLS1_3, TCP, "HITLS_AES_256_GCM_SHA384", "HITLS_EC_GROUP_CURVE25519", signature, 0, "ED25519");
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/ciphersuite/test_suite_sdv_hlt_group_signature.c | C | unknown | 9,720 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <unistd.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10088
/* BEGIN_CASE */
void SDV_TLS_TLCP_CIPHER_SUITE_TC01(char *cipherSuiteType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
// Configure link information on the server.
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCipherSuites(serverCtxConfig, cipherSuiteType);
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->needCheckKeyUsage = true;
// The server listens on the TLS link.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLCP1_1, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure link information on the client.
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCipherSuites(clientCtxConfig, cipherSuiteType);
// Set up a TLCP link on the client.
clientRes = HLT_ProcessTlsConnect(localProcess, TLCP1_1, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(localProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/ciphersuite/test_suite_sdv_hlt_tlcp_ciphersuite.c | C | unknown | 2,713 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "stub_crypt.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "rec_wrapper.h"
#include "process.h"
#include "pthread.h"
#include "unistd.h"
#include "rec_header.h"
#include "bsl_log.h"
#include "cert_callback.h"
#define BUF_SIZE_DTO_TEST 18432
int32_t g_uiPort = 18887;
#define PARSEMSGHEADER_LEN 13
#define ILLEGAL_VALUE 0xFF
#define HASH_EXDATA_LEN_ERROR 23
#define SIGNATURE_ALGORITHMS 0x04, 0x03
#define READ_BUF_SIZE (18 * 1024)
#define TEMP_DATA_LEN 1024
#define REC_DTLS_RECORD_HEADER_LEN 13
#define BUF_TOOLONG_LEN ((1 << 14) + 1)
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo, int uioType)
{
testInfo->client = FRAME_CreateTLCPLink(testInfo->config, uioType, true);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateTLCPLink(testInfo->config, uioType, false);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo, int uioType)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewDTLCPConfig();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark(testInfo, uioType);
}
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewDTLCPConfig();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_SM4_CBC_SM3,HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo, BSL_UIO_UDP);
}
int32_t SendHelloReqWithIndex(HITLS_Ctx *ctx, uint8_t index)
{
int32_t ret;
uint8_t buf[DTLS_HS_MSG_HEADER_SIZE] = {0u};
buf[5] = index;
size_t len = DTLS_HS_MSG_HEADER_SIZE;
ret = REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
return ret;
}
int32_t ConstructAnEmptyCertMsg(FRAME_LinkObj *link)
{
FRAME_Msg frameMsg = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(link->io);
uint8_t *buffer = ioUserData->recMsg.msg;
uint32_t len = ioUserData->recMsg.len;
if (len == 0) {
return HITLS_MEMCPY_FAIL;
}
uint32_t parseLen = 0;
if (ParserTotalRecord(link, &frameMsg, buffer, len, &parseLen) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
CERT_Item *tmpCert = frameMsg.body.handshakeMsg.body.certificate.cert;
frameMsg.body.handshakeMsg.body.certificate.cert = NULL;
frameMsg.bodyLen = 15;
if (PackFrameMsg(&frameMsg) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
ioUserData->recMsg.len = 0;
if (FRAME_TransportRecMsg(link->io, frameMsg.buffer, frameMsg.len) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_SUCCESS;
}
static int32_t GetDisorderClientFinished1(FRAME_LinkObj *client, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t GetDisorderServerFinished(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t AppWrite(HITLS_Ctx *ctx)
{
int32_t ret;
uint8_t writeBuf[] = "GET HTTP 1.0";
uint32_t len = strlen((char *)writeBuf);
do {
uint32_t writeLen;
ret = HITLS_Write(ctx, writeBuf, len, &writeLen);
} while (ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY || ret == HITLS_REC_NORMAL_IO_BUSY);
return ret;
}
static int32_t GetDisorderServerFinish_AppData(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t ccs[TEMP_DATA_LEN] = {0};
uint32_t ccsLen = sizeof(ccs);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, ccs, ccsLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
ccsLen = readLen;
uint8_t finished[TEMP_DATA_LEN] = {0};
uint32_t finishedLen = sizeof(finished);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, finished, finishedLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
finishedLen = readLen;
uint8_t app[TEMP_DATA_LEN] = {0};
uint32_t appLen = sizeof(finished);
ret = AppWrite(server->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(server->io, app, appLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
appLen = readLen;
if (memcpy_s(&data[offset], len - offset, ccs, ccsLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += ccsLen;
if (memcpy_s(&data[offset], len - offset, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
if (memcpy_s(&data[offset], len - offset, finished, finishedLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += finishedLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark1(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewDTLCPConfig();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t groups[] = {HITLS_EC_GROUP_SM2};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_SM2_SM3};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
HITLS_CFG_SetClientVerifySupport(testInfo->config, testInfo->isSupportClientVerify);
HITLS_CFG_SetNoClientCertSupport(testInfo->config, false);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo->config, true);
return StatusPark(testInfo, BSL_UIO_UDP);
}
static int32_t GetRepeatsApp(FRAME_LinkObj *obj, uint8_t *data, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t app[TEMP_DATA_LEN] = {0};
uint32_t appLen = sizeof(app);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app, appLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
appLen = readLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN - offset, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t GetDisorderApp(FRAME_LinkObj *obj, uint8_t *data, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t app1[TEMP_DATA_LEN] = {0};
uint32_t app1Len = sizeof(app1);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app1, app1Len, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
app1Len = readLen;
uint8_t app2[TEMP_DATA_LEN] = {0};
uint32_t app2Len = sizeof(app2);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app2, app2Len, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
app2Len = readLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN, app2, app2Len) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += app2Len;
if (memcpy_s(&data[offset], TEMP_DATA_LEN - offset, app1, app1Len) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += app1Len;
*usedLen = offset;
return HITLS_SUCCESS;
} | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtlcp/test_suite_sdv_frame_dtlcp_consistency.base.c | C | unknown | 12,719 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_sdv_frame_dtlcp_consistency */
/* END_HEADER */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_FINISH_TC001
* @spec -
* @titleThe client receives a FINISH message and the CCS message is out of order.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and CCS message are out of order. That is, the client processes the FINISH message and then processes the CCS message. After the processing, the client continues to establish a connection. Expected result 2 is displayed.
* @expect 1: The initialization is successful.
* 2: The client waits to receive the FINISH message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_FINISH_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerFinished(testInfo.server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
ASSERT_EQ(testInfo.client->ssl->state, CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC002
* @spec -
* @titleThe server receives a FINISH message and the CCS message is out of order.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and CCS message are out of order.
That is, the server processes the FINISH message and then processes the CCS message. After the processing, the server continues to
establish a connection. Expected result 2 is displayed.
* @expect 1: The initialization is successful.
* 2: The server is waiting to receive the FINISH message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_FINISH_TC002(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = TRY_SEND_CLIENT_KEY_EXCHANGE;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderClientFinished1(testInfo.client, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Accept(testInfo.server->ssl);
ASSERT_EQ(testInfo.server->ssl->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(testInfo.server->ssl->hsCtx->state, TRY_SEND_CHANGE_CIPHER_SPEC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_FINISH_TC003
* @spec -
* @titleThe client receives a FINISH message and the APP message is out of order.
* @precon nan
* @brief 1. Initialize the client and server using the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and APP message are out of order. That is, the client processes the APP message first, processes the FINISH message, and then continues to establish a connection. Expected result 2.
* @expect 1: Initialization succeeded.
* 2: The connection between the client and server is successfully established.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_FINISH_TC003(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerFinish_AppData(testInfo.server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_DISORDER_TC001
* @spec -
* @titleThe server receives out-of-order APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLCP. Expected result 2.
* 3. Construct an app message whose SN is 2 and send it to the server. When the server invokes HiTLS_Read, expected result 3.
* 4. Construct an app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 4.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLCP connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_DISORDER_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderApp(testInfo.client, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
uint8_t app1Data[MAX_RECORD_LENTH] = {0};
uint32_t app1Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, app1Data, MAX_RECORD_LENTH, &app1Len) == HITLS_SUCCESS);
uint8_t app2Data[MAX_RECORD_LENTH] = {0};
uint32_t app2Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, app2Data, MAX_RECORD_LENTH, &app2Len) == HITLS_SUCCESS);
ASSERT_EQ(app1Len, app2Len);
ASSERT_EQ(memcmp(app1Data, app2Data, app2Len), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_DISORDER_TC002
* @spec -
* @titleThe client receives out-of-order APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection request using DTLCP. Expected result 2.
* 3. Construct an app message whose sequence number is 2 and send it to the client. When the client invokes HiTLS_Read, expected result 3.
* 4. Construct an app message whose sequence number is 1 and send it to the client. When the client invokes HiTLS_Read, expected result 4.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLCP connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_DISORDER_TC002(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderApp(testInfo.server, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
uint8_t app1Data[MAX_RECORD_LENTH] = {0};
uint32_t app1Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, app1Data, MAX_RECORD_LENTH, &app1Len) == HITLS_SUCCESS);
uint8_t app2Data[MAX_RECORD_LENTH] = {0};
uint32_t app2Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, app2Data, MAX_RECORD_LENTH, &app2Len) == HITLS_SUCCESS);
ASSERT_EQ(app1Len, app2Len);
ASSERT_EQ(memcmp(app1Data, app2Data, app1Len), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_APPDATA_TC001
* @spec -
* @title The server receives duplicate APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLCP. Expected result 2.
* 3. Construct an app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 3.
* 4. Construct the app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 4.
* 5. The server constructs data and sends it to the client. When the client invokes HiTLS_Read, expected result 5.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLCP connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* 5: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_APPDATA_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetRepeatsApp(testInfo.client, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_TRUE(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.server->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_APPDATA_TC002
* @spec -
* @titleThe client receives duplicate APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLCP. Expected result 2.
* 3. Construct an app message whose sequence number is 1 and send the message to the client. When the client invokes HiTLS_Read, expected result 3.
* 4. Construct an app message with the sequence number being 1 and send it to the client. When the client invokes HiTLS_Read, expected result 4.
* 5. The server constructs data and sends it to the client. When the client invokes HiTLS_Read, expected result 5.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLCP connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* 5: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_APPDATA_TC002(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetRepeatsApp(testInfo.server, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_TRUE(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.server->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_CLIENT_HELLO_TC001
* @spec -
* @title The server receives a Client Hello packet after the connection is established.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request. Expected result 2.
* 3. Construct a Client Hello packet and send it to the server. The server invokes HiTLS_Read to receive the packet. Expected result 3.
* 4. The client invokes HiTLS_Write to send data to the server, and the server invokes HiTLS_Read to read data. (Expected result 4)
* @expect 1: Initialization succeeded.
* 2: The connection between the client and server is successfully established.
* 3: The HiTLS_Read returns an error code, indicating that the receiving buffer is empty.
* 4: The data read by the server is consistent with the data sent by the client.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_CLIENT_HELLO_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo), HITLS_SUCCESS);
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(REC_Write(testInfo.client->ssl, REC_TYPE_HANDSHAKE,
&sendBuf[REC_DTLS_RECORD_HEADER_LEN], sendLen - REC_DTLS_RECORD_HEADER_LEN) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_EQ(HITLS_Write(testInfo.client->ssl, writeData, writeLen, &sendNum), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.client->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC8422_ECPOINT_TC001
* @spec -
* @titleThe client receives an abnormal dot format field.
* @precon nan
* @brief 1. The client and server use the default initialization. Expected result 1.
* 2. The client initiates a connection request. When the client wants to read the Server Hello message,
* Modify the Elliptic curves point formats field, change the group supported by the field to 0x01, and send the field to the client. Expected result 2.
* @expect 1. Initialization succeeded.
* 2. The client sends a FATAL ALERT with the description of ALERT_ELLEGAL_PARAMETER.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC8422_ECPOINT_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t Gdata[] = { 0x01 };
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->pointFormats.exState = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgInteger(HS_EX_TYPE_POINT_FORMATS, &(serverMsg->pointFormats.exType)) == HITLS_SUCCESS);
serverMsg->pointFormats.exLen.state = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgArray8(Gdata, sizeof(Gdata)/sizeof(uint8_t),
&(serverMsg->pointFormats.exData), &(serverMsg->pointFormats.exDataLen)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNSUPPORT_POINT_FORMAT);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC8422_EXTENSION_MISS_TC001
* @spec -
* @title The server receives a Client Hello packet that does not carry the group or dot format.
* @precon nan
* @brief 1. Configure the HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 cipher suite on the client and initialize the cipher suite on the server by default. Expected result 1.
* 2. When the client initiates a connection request and the server is about to read ClientHello,
* Delete the supported_groups and ec_point_formats fields from the packet. Expected result 2.
* @expect 1. Initialization succeeded.
* 2. The server sends a Server Hello packet with the HITLS_ECDHE_SM4_CBC_SM3 algorithm suite.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC8422_EXTENSION_MISS_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->supportedGroups.exState = MISSING_FIELD;
clientMsg->pointFormats.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_HANDSHAKE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_HANDSHAKE);
ASSERT_TRUE(frameMsg.body.hsMsg.type.data == SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_EQ(serverMsg->cipherSuite.data, HITLS_ECDHE_SM4_CBC_SM3);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* when receive alert between finish and ccs, dtlcp should cache it*/
/* BEGIN_CASE */
void UT_DTLCP_RFC6347_RECV_ALERT_AFTER_CCS_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_UDP, true);
FRAME_LinkObj *server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_UDP, false);
client->needStopBeforeRecvCCS = true;
server->needStopBeforeRecvCCS = true;
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
// client receive ccs, wait to receive finish
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t alertdata[2] = {0x02, 0x0a};
ASSERT_EQ(REC_Write(serverTlsCtx, REC_TYPE_ALERT, alertdata, sizeof(alertdata)), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
// client cache the alert, wait to receive finish
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
// server send finish, handshake success
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
// client receive finish, handshake success
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
// client read cached alert
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLCP_CONSISTENCY_RFC6347_TC001
* @spec -
* @title The client receives a Hello Request message which msg seq is not 0.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. After the client finished handshake, the client receives a Hello Request message. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client igore the message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLCP_CONSISTENCY_RFC6347_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLCPConfig();
tlsConfig->isSupportRenegotiation = true;
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_UDP, true);
FRAME_LinkObj *server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_UDP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t buf[DTLS_HS_MSG_HEADER_SIZE] = {0u};
buf[5] = 1;
size_t len = DTLS_HS_MSG_HEADER_SIZE;
REC_Write(serverTlsCtx, REC_TYPE_HANDSHAKE, buf, len);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readData, MAX_RECORD_LENTH, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtlcp/test_suite_sdv_frame_dtlcp_consistency.c | C | unknown | 29,185 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "stub_crypt.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "rec_wrapper.h"
#include "process.h"
#include "pthread.h"
#include "unistd.h"
#include "rec_header.h"
#include "bsl_log.h"
#include "cert_callback.h"
#include "bsl_uio.h"
#include "uio_abstraction.h"
/* END_HEADER */
#define BUF_SIZE_DTO_TEST 18432
void Hello(void *ssl)
{
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_TlsWrite(ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
EXIT:
return;
}
/**
* @test SDV_TLS_DTLCP_HANDSHAKE_FUNC_TC001
* @spec -
* @title Check whether DTLCP over SCTP can handshake successfully.
* @precon nan
* @brief
* 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. A DTLCP over SCTP connection is established between the client and server. Expected result 2.
* 3. Construct a app message and send it to the client. Check the server ret. Expected result 3.
* @expect
* 1. The initialization is successful.
* 2. The connection is set up successfully.
* 3. The server ret is HITLS_SUCCESS.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_DTLCP_HANDSHAKE_FUNC_TC001(int connType)
{
if (connType == SCTP && !IsEnableSctpAuth()) {
return;
}
bool certverifyflag = true;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, 16790, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverCtxConfig != NULL);
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(localProcess, DTLCP1_1, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientCtxConfig != NULL);
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLCP1_1, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_EQ(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")), HITLS_SUCCESS);
uint8_t readBuf[1024] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/*
* @
* @test SDV_TLS_DTLCP_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001
* @Specifications-
* @Title1. Construct a scenario where renegotiation messages and application messages are sent at the same time.
* It is expected that the server processes renegotiation messages first.
* @preconan
* @short
* @ Previous Level 1
* @autotrue
@ */
/* BEGIN_CASE */
void SDV_TLS_DTLCP_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001()
{
int version = TLS1_2;
int connType = TCP;
int32_t serverConfigId = 0;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HITLS_Session *session = NULL;
TLS_TYPE local = HITLS;
TLS_TYPE remote = HITLS;
localProcess = HLT_InitLocalProcess(local);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(remote);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_SetRenegotiationSupport(clientCtxConfig, true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetRenegotiationSupport(serverCtxConfig, true);
HLT_SetClientRenegotiateSupport(serverCtxConfig, true);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 1666;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HITLS_Renegotiate(clientSsl) == HITLS_SUCCESS);
const char *writeBuf = "Hello world";
pthread_t thrd;
ASSERT_TRUE(pthread_create(&thrd, NULL, (void *)Hello, clientSsl) == 0);
sleep(2);
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
pthread_join(thrd, NULL);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
ClearWrapper();
HLT_CleanFrameHandle();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtlcp/test_suite_sdv_hlt_dtlcp_consistency.c | C | unknown | 7,869 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "stub_crypt.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "rec_wrapper.h"
#include "process.h"
#include "pthread.h"
#include "unistd.h"
#include "rec_header.h"
#include "bsl_log.h"
#include "cert_callback.h"
#define BUF_SIZE_DTO_TEST 18432
int32_t g_uiPort = 18887;
#define PARSEMSGHEADER_LEN 13
#define ILLEGAL_VALUE 0xFF
#define HASH_EXDATA_LEN_ERROR 23
#define SIGNATURE_ALGORITHMS 0x04, 0x03
#define READ_BUF_SIZE (18 * 1024)
#define TEMP_DATA_LEN 1024
#define REC_DTLS_RECORD_HEADER_LEN 13
#define BUF_TOOLONG_LEN ((1 << 14) + 1)
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo, int uioType)
{
int ret;
testInfo->client = FRAME_CreateLink(testInfo->config, uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state);
if (ret != HITLS_SUCCESS) {
return ret;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo, int uioType)
{
FRAME_Init();
// FRAME_RegCryptMethod(); // stub all crypto functions
testInfo->config = HITLS_CFG_NewDTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
HITLS_CFG_SetDtlsCookieExchangeSupport(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo, uioType);
}
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo, int uioType)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewDTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo, uioType);
}
int32_t SendHelloReqWithIndex(HITLS_Ctx *ctx, uint8_t index)
{
uint8_t buf[DTLS_HS_MSG_HEADER_SIZE] = {0u};
buf[5] = index;
size_t len = DTLS_HS_MSG_HEADER_SIZE;
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
int32_t ConstructAnEmptyCertMsg(FRAME_LinkObj *link)
{
FRAME_Msg frameMsg = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(link->io);
uint8_t *buffer = ioUserData->recMsg.msg;
uint32_t len = ioUserData->recMsg.len;
if (len == 0) {
return HITLS_MEMCPY_FAIL;
}
uint32_t parseLen = 0;
if (ParserTotalRecord(link, &frameMsg, buffer, len, &parseLen) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
CERT_Item *tmpCert = frameMsg.body.handshakeMsg.body.certificate.cert;
frameMsg.body.handshakeMsg.body.certificate.cert = NULL;
frameMsg.bodyLen = 15;
if (PackFrameMsg(&frameMsg) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
ioUserData->recMsg.len = 0;
if (FRAME_TransportRecMsg(link->io, frameMsg.buffer, frameMsg.len) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_SUCCESS;
}
static int32_t GetDisorderClientFinished(FRAME_LinkObj *client, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t GetDisorderServerFinished(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t AppWrite(HITLS_Ctx *ctx)
{
int32_t ret;
uint8_t writeBuf[] = "GET HTTP 1.0";
uint32_t len = strlen((char *)writeBuf);
do {
uint32_t writeLen;
ret = HITLS_Write(ctx, writeBuf, len, &writeLen);
} while (ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY || ret == HITLS_REC_NORMAL_IO_BUSY);
return ret;
}
static int32_t GetDisorderClientFinished_AppData(FRAME_LinkObj *client, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t app[TEMP_DATA_LEN] = {0};
uint32_t appLen = sizeof(app);
uint8_t finished[TEMP_DATA_LEN] = {0};
uint32_t finishedLen = sizeof(finished);
(void)HITLS_Connect(client->ssl);
ret = FRAME_TransportSendMsg(client->io, finished, finishedLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
finishedLen = readLen;
appLen=finishedLen;
if (memcpy_s(app, appLen, finished, finishedLen) != EOK) {
return HITLS_INTERNAL_EXCEPTION;
}
app[0] = 23;
if (memcpy_s(&data[offset], len - offset, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
if (memcpy_s(&data[offset], len - offset, finished, finishedLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += finishedLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t GetDisorderServerFinish_AppData(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t ccs[TEMP_DATA_LEN] = {0};
uint32_t ccsLen = sizeof(ccs);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, ccs, ccsLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
ccsLen = readLen;
uint8_t finished[TEMP_DATA_LEN] = {0};
uint32_t finishedLen = sizeof(finished);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, finished, finishedLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
finishedLen = readLen;
uint8_t app[TEMP_DATA_LEN] = {0};
uint32_t appLen = sizeof(finished);
ret = AppWrite(server->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(server->io, app, appLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
appLen = readLen;
if (memcpy_s(&data[offset], len - offset, ccs, ccsLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += ccsLen;
if (memcpy_s(&data[offset], len - offset, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
if (memcpy_s(&data[offset], len - offset, finished, finishedLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += finishedLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark1(HandshakeTestInfo *testInfo, int uioType)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewDTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
HITLS_CFG_SetClientVerifySupport(testInfo->config, testInfo->isSupportClientVerify);
HITLS_CFG_SetNoClientCertSupport(testInfo->config, false);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo->config, true);
return StatusPark(testInfo, uioType);
}
static int32_t GetRepeatsApp(FRAME_LinkObj *obj, uint8_t *data, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t app[TEMP_DATA_LEN] = {0};
uint32_t appLen = sizeof(app);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app, appLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
appLen = readLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN - offset, app, appLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += appLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
static int32_t GetDisorderApp(FRAME_LinkObj *obj, uint8_t *data, uint32_t *usedLen)
{
int32_t ret;
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t app1[TEMP_DATA_LEN] = {0};
uint32_t app1Len = sizeof(app1);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app1, app1Len, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
app1Len = readLen;
uint8_t app2[TEMP_DATA_LEN] = {0};
uint32_t app2Len = sizeof(app2);
ret = AppWrite(obj->ssl);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_TransportSendMsg(obj->io, app2, app2Len, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
app2Len = readLen;
if (memcpy_s(&data[offset], TEMP_DATA_LEN, app2, app2Len) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += app2Len;
if (memcpy_s(&data[offset], TEMP_DATA_LEN - offset, app1, app1Len) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += app1Len;
*usedLen = offset;
return HITLS_SUCCESS;
} | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtls12/test_suite_sdv_frame_dtls12_consistency.base.c | C | unknown | 13,558 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "hs_cookie.h"
/* INCLUDE_BASE test_suite_sdv_frame_dtls12_consistency */
/* END_HEADER */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001
* @spec -
* @titleThe client and server receive the client Hello message after the connection establishment is complete.
* @precon nan
* @brief
* 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. A DTLS over SCTP connection is established between the client and server. Expected result 2.
* 3. Construct a Client Hello message and send it to the client. Check the client status. Expected result 3.
* 4. Construct a Client Hello message and send it to the server. Check the server status. Expected result 4.
* @expect
* 1. The initialization is successful.
* 2. The connection is set up successfully.
* 3. The client status is CM_STATE_TRANSPORTING.
* 4. The server is in the CM_STATE_TRANSPORTING state.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = HS_STATE_BUTT;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
uint8_t data[1024] = {0};
uint32_t dataSize = 0;
ASSERT_TRUE(testInfo.server->ssl != NULL);
HITLS_Read(testInfo.server->ssl, data, sizeof(data), &dataSize);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(dataSize == 0);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
dataSize = 0 ;
ASSERT_TRUE(testInfo.client->ssl != NULL);
HITLS_Read(testInfo.client->ssl, data, sizeof(data), &dataSize);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(dataSize == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC001
* @spec -
* @title Check whether the seq number of the record layer complies with the RFC specifications during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a connection establishment request. When the client sends a CLIENT_HELLO message, the client checks the sequence number at the Reocrd layer. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The sequence number is 0.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == recvLen);
ASSERT_TRUE(frameMsg.body.hsMsg.sequence.data == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC002
* @spec -
* @title Check whether the sequence number of the record layer during the handshake complies with the RFC specification.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The server continuously establishes a connection. After receiving the client Hello message, the server sends a Server Hello message and checks the sequence number at the Record layer. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The sequence number is 0.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC002(int uioType)
{
RegDefaultMemCallback();
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == recvLen);
ASSERT_TRUE(frameMsg.body.hsMsg.sequence.data == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC003
* @spec -
* @title Check whether the sequence number at the record layer complies with the RFC specification during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client sends a FINISH message during continuous connection establishment, check whether the sequence number in the record header is. Expected result 2.
3. When the server sends a FINISH message during continuous connection establishment, check the sequence number in the message. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The sequence number is 0.
3. The sequence number is 0.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SEQ_NUMBER_TC003(int uioType)
{
RegDefaultMemCallback();
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_RECV_FINISH;
testInfo.isClient = false;
ASSERT_EQ(DefaultCfgStatusPark(&testInfo, uioType), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == PARSEMSGHEADER_LEN);
ASSERT_TRUE(frameMsg.body.hsMsg.sequence.data == 0);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
FrameUioUserData *ioUserData_1 = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf_1 = ioUserData_1->recMsg.msg;
uint32_t recvLen_1 = ioUserData_1->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen_1 = 0;
FRAME_Msg frameMsg_1 = {0};
FRAME_Type frameType_1 = {0};
frameType_1.versionType = HITLS_VERSION_DTLS12;
frameType_1.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType_1.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType_1, recvBuf_1, recvLen_1, &frameMsg_1, &parseLen_1) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen_1 == PARSEMSGHEADER_LEN);
ASSERT_TRUE(frameMsg_1.body.hsMsg.sequence.data == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType_1, &frameMsg_1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC001
* @spec -
* @titleThe client sends a Client Certificate message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS connection creation request. When the client needs to send a Client Certificate message, the two fields are modified as follows:
Certificates Length is 2 ^ 14 + 1
Certificates are changed to 2 ^ 14 + 1 byte buffer.
After the modification is complete, send the modification to the server. Expected result 2.
3. When the server receives the Client Certificate message, check the value returned by the HITLS_Accept interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC002
* @spec -
* @title The server sends a Server Certificate message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS connection creation request. When the server needs to send a Server Certificate message, the server modifies the following two fields:
Certificates Length is 2 ^ 14 + 1
Certificates are changed to 2 ^ 14 + 1 byte buffer.
After the modification is complete, send the modification to the server. Expected result 2.
3. When the client receives the Server Certificate message, check the value returned by the HITLS_Connect interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Connect interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC002(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC003
* @spec -
* @titleThe client sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS connection establishment request and sends a Change Cipher Spec message, the client modifies one field as follows:
Length is 2 ^ 14 + 1. After the modification is complete, send the modification to the server. Expected result 2.
3. When the server receives the Change Cipher Spec message, check the value returned by the HITLS_Accept interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_TOOLONG_TC003(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.ccsMsg.extra.data);
frameMsg.body.ccsMsg.extra.data = certDataTemp;
frameMsg.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE_VERIFY);
bool isCcsRecv = testInfo.server->ssl->method.isRecvCCS(testInfo.server->ssl);
ASSERT_TRUE(isCcsRecv == false);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC001
* @spec -
* @title The server receives a Client Hello message with a length of zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request, constructs a Client Hello message with zero length, and sends the message to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT message. The level is ALERT_ LEVEL_FATAL, and the description is ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extensionState = MISSING_FIELD;
clientMsg->version.state = MISSING_FIELD;
clientMsg->randomValue.state = MISSING_FIELD;
clientMsg->sessionIdSize.state = MISSING_FIELD;
clientMsg->sessionId.state = MISSING_FIELD;
clientMsg->cookiedLen.state = MISSING_FIELD;
clientMsg->cookie.state = MISSING_FIELD;
clientMsg->cipherSuitesSize.state = MISSING_FIELD;
clientMsg->cipherSuites.state = MISSING_FIELD;
clientMsg->compressionMethodsLen.state = MISSING_FIELD;
clientMsg->compressionMethods.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC002
* @spec -
* @titleThe client receives a Server Hello message with a length of zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After sending a Client Hello message, the client constructs a zero-length Server Hello message and sends it to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_Level_FATAL and description of ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC002(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->version.state = MISSING_FIELD;
serverMsg->randomValue.state = MISSING_FIELD;
serverMsg->sessionIdSize.state = MISSING_FIELD;
serverMsg->sessionId.state = MISSING_FIELD;
serverMsg->cipherSuite.state = MISSING_FIELD;
serverMsg->compressionMethod.state = MISSING_FIELD;
serverMsg->extensionLen.state = MISSING_FIELD;
serverMsg->pointFormats.exState = MISSING_FIELD;
serverMsg->extendedMasterSecret.exState = MISSING_FIELD;
serverMsg->secRenego.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC003
* @spec -
* @titleThe client receives a Certificate message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After receiving the Server Hello message, the client constructs a zero-length Certificate message and sends it to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT message. The level is ALERT_ LEVEL_FATAL and the description is ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC003(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_CertificateMsg *certifiMsg = &frameMsg.body.hsMsg.body.certificate;
certifiMsg->certsLen.state = MISSING_FIELD;
certifiMsg->certItem->state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC004
* @spec -
* @titleThe client receives a Server Key Exchange message whose length is zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After receiving the Certificate message, the client constructs a Server Key Exchange message with zero length and sends the message to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_Level_FATAL and description of ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC004(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerKeyExchangeMsg *serverKeyExMsg = &frameMsg.body.hsMsg.body.serverKeyExchange;
serverKeyExMsg->keyEx.ecdh.curveType.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.namedcurve.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.pubKeySize.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.pubKey.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signAlgorithm.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signSize.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signData.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC005
* @spec -
* @titleThe server receives a Client Key Exchange message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After the server sends a Server Hello Done message, the server constructs a Client Key Exchange message with zero length and sends the message to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message. The level is ALERT_Level_FATAL and the description is ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC005(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientKeyExchangeMsg *clientKeyExMsg = &frameMsg.body.hsMsg.body.clientKeyExchange;
clientKeyExMsg->pubKey.state = MISSING_FIELD;
clientKeyExMsg->pubKeySize.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC006
* @spec -
* @title The server receives a Change Cipher Spec message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After receiving the Client Key Exchange message, the server constructs a Change Cipher Spec message with zero length and sends it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server receives the message, which is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC006(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
frameType1.versionType = HITLS_VERSION_DTLS12;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType1.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
FRAME_CcsMsg *CcsMidMsg = &frameMsg1.body.ccsMsg;
CcsMidMsg->ccsType.state = MISSING_FIELD;
CcsMidMsg->extra.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC007
* @spec -
* @titleThe client receives a Change Cipher Spec message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS over SCTP connection request. After the client sends a Finish message, it constructs a Change Cipher Spec message with zero length and sends the message to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client receives the message, which is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_MSGLENGTH_ZERO_TC007(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
frameType1.versionType = HITLS_VERSION_DTLS12;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType1.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
FRAME_CcsMsg *CcsMidMsg = &frameMsg1.body.ccsMsg;
CcsMidMsg->ccsType.state = MISSING_FIELD;
CcsMidMsg->extra.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_COMPRESSED_TC001
* @spec -
* @titleThe server receives a Client Hello message in which the compression field is set to 1.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server expects to receive the Client Hello packet, the server constructs the Client Hello packet with the compressed field value being 1. Check the behavior of the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_COMPRESSED_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
*(clientMsg->compressionMethods.data) = 1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_INVALID_COMPRESSION_METHOD);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.handshakeType = SERVER_HELLO;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_COMPRESSED_TC002
* @spec -
* @titleThe client receives a Server Hello message in which the compressed field value is 1.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. After sending the Client Hello packet, the client constructs a Server Hello packet with the compressed field value being 1. Check the behavior of the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_ILLEGAL_PARAMETER.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_COMPRESSED_TC002(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->compressionMethod.data = 1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_COMPRESSION_METHOD_ERR);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_CIPHER_TC001
* @spec -
* @title Check whether the cipher suite selected on the server complies with the RFC.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection application, the server modifies the algorithm suite field in the Client Hello packet when the server expects to receive the Client Hello packet,
Change the value to 0x00b6, 0x00b7, 0xffff, or 0xc030, and send the modification to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the Server Hello message, and the algorithm suite field is 0xc030.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_CIPHER_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint16_t suite[] = {0x00B6, 0x00B7, ILLEGAL_VALUE, HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384};
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(FRAME_ModifyMsgArray16(suite, sizeof(suite)/sizeof(uint16_t),
&(clientMsg->cipherSuites), &(clientMsg->cipherSuitesSize)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.handshakeType = SERVER_HELLO;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.cipherSuite.data, HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_CIPHER_TC002
* @spec -
* @titleHow to handle unexpected cipher suites received by the client?
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection request, the client constructs a Server Hello packet after sending the Client Hello message,
Change the value of the cipher suite field to 0xff and send the modified value to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_ILLEGAL_PARAMETER.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_CIPHER_TC002(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->cipherSuite.data = ILLEGAL_VALUE;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC001
* @spec -
* @title The server receives a Client Hello packet without the Signature Algorithms field.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection request and the server expects to receive the Client Hello packet,
Delete the signature field from the Client Hello packet and send the packet to the server after the packet is modified. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the Server Hello message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.body.hsMsg.type.data == CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.signatureAlgorithms.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_HANDSHAKE);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC002
* @spec -
* @titleThe client receives the Server Hello packet carrying the Signature Algorithms field.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection application, the client constructs a Server Hello packet after sending the Client Hello message,
Add the Signature Algorithms field and set its value to 0x0403. Modify the field and send it to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_UNSUPPORTED_EXTENSION.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC002(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exState = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgInteger(HS_EX_TYPE_SIGNATURE_ALGORITHMS,
&(serverMsg->secRenego.exType)) == HITLS_SUCCESS);
serverMsg->secRenego.exLen.state = INITIAL_FIELD;
uint8_t Signature[] = {SIGNATURE_ALGORITHMS};
ASSERT_TRUE(FRAME_ModifyMsgArray8(Signature, sizeof(Signature),
&(serverMsg->secRenego.exData), &(serverMsg->secRenego.exDataLen)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_UNSUPPORTED_EXTENSION);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNSUPPORTED_EXTENSION);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC003
* @spec -
* @titleThe server receives a client Hello message with abnormal signature hash fields.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a connection request, constructs an abnormal Client Hello packet, that is, the signature hash field is not in pairs, and sends the packet to the server.
* @expect 1. The initialization is successful.
* 2. After the server receives the Client Hello message, the HiTLS_ACCEPT interface returns a failure message and the server sends an ALERT message. The ALERT level is ALERT_ LEVEL_FATAL and the description is ALERT_DECODE_ERROR.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC003(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->signatureAlgorithms.exDataLen.data = HASH_EXDATA_LEN_ERROR ;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.handshakeType = SERVER_HELLO;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC004
* @spec -
* @title The server receives the Client Hello packet carrying the Signature Algorithms field.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection request, after the client sends the Client Hello message,
Modify the signature algorithm field by adding an invalid field value and observe the client behavior. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_HANDSHAKE_FAILURE.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC004(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
uint16_t Signature[] = {ILLEGAL_VALUE};
ASSERT_TRUE(FRAME_ModifyMsgArray16(Signature, sizeof(Signature)/sizeof(uint16_t),
&(clientMsg->signatureAlgorithms.exData), &(clientMsg->signatureAlgorithms.exDataLen)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_HANDSHAKE_FAILURE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC005
* @spec -
* @titleThe client receives the Server Hello packet carrying the Signature Algorithms field.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client initiates a DTLS over SCTP connection application, the client constructs a Server Hello packet after sending the Client Hello message,
Modify the signature algorithm field by adding an invalid field value and observe the client behavior. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT message. The ALERT level is ALERT_LEVEL_FATAL and the description is ALERT_UNSUPPORTED_EXTENSION.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC005(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exState = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgInteger(HS_EX_TYPE_SIGNATURE_ALGORITHMS,
&(serverMsg->secRenego.exType)) == HITLS_SUCCESS);
serverMsg->secRenego.exLen.state = INITIAL_FIELD;
uint8_t Signature[] = {ILLEGAL_VALUE};
ASSERT_TRUE(FRAME_ModifyMsgArray8(Signature, sizeof(Signature),
&(serverMsg->secRenego.exData), &(serverMsg->secRenego.exDataLen)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_UNSUPPORTED_EXTENSION);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNSUPPORTED_EXTENSION);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC007
* @spec -
* @titleThe client sends a Client Hello message in which the signature field is removed.
* @precon nan
* @brief 1. Use the default initialization mode on the client and server. Expected result 1.
* 2. The client initiates a connection establishment request, deletes the signature field in the client Hello message, and sends the message to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server continues to establish a connection. The connection is successfully established.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_SIGNATURE_TC007(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->signatureAlgorithms.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.handshakeType = SERVER_HELLO;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_HANDSHAKE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_CERTIFICATE_TC003
* @spec -
* @title The server receives an unexpected Client Certificate message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a DTLS connection application. After sending the Server Hello Done message, the server constructs a Client Certificate message and sends it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT message. The ALERT level is ALERT_ LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_CERTIFICATE_TC003(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_VERSION_TC001
* @spec -
* @titleThe server receives the client hello message of DTLS1.0.
* @precon nan
* @brief 1. Retain the default configuration on the client and server, and enable peer verification on the server. Expected result 1.
* 2. When the server is in the TRY_RECV_CLIENT_HELLO state, change the negotiated version number field in the client hello message to DTLS1.0. Then, check the server behavior.
* @expect 1. The initialization is successful.
* 2. The server sends a FATAL ALERT, and the description of the ALERT is ALERT_PROTOCOL_VERSION.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_VERSION_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->version.data = HITLS_VERSION_DTLS10;
clientMsg->version.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.handshakeType = SERVER_HELLO;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_PROTOCOL_VERSION);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC001
* @spec -
* @titleThe client receives a Hello Request message during startup.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and enable peer verification on the server. Expected result 1.
* 2. When the client starts, the client receives a Hello Request message. Expected result 2.
3. Continue to complete connection establishment and send and receive messages. (Expected result 3)
* @expect 1. The initialization is successful.
* 2. The client can process the packet normally.
3. The message is sent and received successfully.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TLS_IDLE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
FRAME_Init();
testInfo.config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.client = FRAME_CreateLink(testInfo.config, uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.server->ssl, 0) == HITLS_SUCCESS);
testInfo.server->ssl->hsCtx->nextSendSeq++;
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_TRANSPORTING);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint32_t sendNum;
ASSERT_EQ(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC002
* @spec -
* @titleThe client receives a Hello Request message after sending a Client Hello message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 11.
* 2. After the client sends a Client Hello message, the client receives a Hello Request message. Expected result 2.
3. Continue to establish a connection and send and receive messages. (Expected result 3)
* @expect 1. The initialization is successful.
* 2. The client can process the packet normally.
3. The message is sent and received successfully.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC002(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, uioType) == HITLS_SUCCESS);
CONN_Deinit(testInfo.client->ssl);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.server->ssl, 0) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_SERVER_HELLO);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC003
* @spec -
* @titleThe server receives a Hello Request message after sending a Server Hello Done message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 11.
* 2. After sending a Server Hello Done message, the server receives a Hello Request message. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message with the description of ALERT_UNEXPECTED_MESSAGE.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC003(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_SEND_SERVER_HELLO_DONE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, uioType) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.client->ssl, 1) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_ALERT;
frameType.transportType = uioType;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC004
* @spec -
* @titleThe client receives a Hello Request message after sending a FINISH message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 11.
* 2. After the client sends a FINISH message, the client receives a Hello Request message. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client can process the packet normally.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC004(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, uioType) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.server->ssl, 4) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_NEW_SESSION_TICKET);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC005
* @spec -
* @titleThe server receives a Hello Request message after sending a FINISH message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 11.
* 2. After the server sends a FINISH message, the server receives a Hello Request message. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server can process the packet normally.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC005(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, uioType) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.client->ssl, 1) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(testInfo.server);
ASSERT_EQ(serverTlsCtx->state, CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC006
* @spec -
* @titleThe client receives the Hello Request message after the connection establishment is complete.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 11.
* 2. After the connection is established on the client, the client receives a Hello Request message. Expected result 2.
3. The server writes a message and the client receives the message. (Expected result 3)
* @expect 1. The initialization is successful.
* 2. The server can process the packet normally.
3. The client receives the message correctly.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC006(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = HS_STATE_BUTT;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo, uioType) == HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.server->ssl, 1) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
readLen = MAX_RECORD_LENTH;
uint32_t sendNum;
ASSERT_EQ(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC007
* @spec -
* @titleThe server receives a Hello Request message during startup.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server starts, the Hello Request message is received. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT. The description is ALERT_UNEXPECTED_MESSAGE.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC5246_HELLO_REQUEST_TC007(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TLS_IDLE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
HandshakeTestInfo testInfo1 = {0};
testInfo1.state = TLS_IDLE;
FRAME_Init();
testInfo.config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.client = FRAME_CreateLink(testInfo.config, uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, uioType);
ASSERT_TRUE(testInfo.server != NULL);
testInfo1.server = FRAME_CreateLink(testInfo.config, uioType);
ASSERT_TRUE(testInfo1.server != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo1.server) == HITLS_SUCCESS);
ASSERT_TRUE(SendHelloReqWithIndex(testInfo.client->ssl, 0) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_ALERTED);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_CFG_FreeConfig(testInfo1.config);
FRAME_FreeLink(testInfo1.server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC001
* @spec -
* @titleThe client receives a FINISH message and the CCS message is out of order.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and CCS message are out of order. That is, the client processes the FINISH message and then processes the CCS message. After the processing, the client continues to establish a connection. Expected result 2 is displayed.
* @expect 1: The initialization is successful.
* 2: The client waits to receive the FINISH message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerFinished(testInfo.server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
ASSERT_EQ(testInfo.client->ssl->state, CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC002
* @spec -
* @titleThe server receives a FINISH message and the CCS message is out of order.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and CCS message are out of order. That is, the server processes the FINISH message and then processes the CCS message. After the processing, the server continues to establish a connection. Expected result 2 is displayed.
* @expect 1: The initialization is successful.
* 2: The server is waiting to receive the FINISH message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC002(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = TRY_SEND_CLIENT_KEY_EXCHANGE;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderClientFinished(testInfo.client, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Accept(testInfo.server->ssl);
ASSERT_EQ(testInfo.server->ssl->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(testInfo.server->ssl->hsCtx->state, TRY_SEND_CHANGE_CIPHER_SPEC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC003
* @spec -
* @titleThe client receives a FINISH message and the APP message is out of order.
* @precon nan
* @brief 1. Initialize the client and server using the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and APP message are out of order. That is, the client processes the APP message first, processes the FINISH message, and then continues to establish a connection. Expected result 2.
* @expect 1: Initialization succeeded.
* 2: The connection between the client and server is successfully established.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC003(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerFinish_AppData(testInfo.server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC004
* @spec -
* @titleThe server receives a FINISH message and the APP message is out of order.
* @precon nan
* @brief 1. Initialize the client and server using the default configuration. Expected result 1.
* 2. The client initiates a connection request and constructs the scenario where the FINISH message and APP message are out of order. That is, the server processes the APP message first, processes the FINISH message, and then continues to establish a connection. Expected result 2 is displayed.
* 3. After the connection is established, the client sends data to the server, and the server receives the data. (Expected result 3)
* @expect 1: Initialization succeeded.
* 2: The connection between the client and server is successfully established.
* 3: Data received by the server is consistent with data sent by the client.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_FINISH_TC004(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = TRY_SEND_FINISH;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, uioType) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderClientFinished_AppData(testInfo.client, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
(void)HITLS_Accept(testInfo.server->ssl);
ASSERT_EQ(testInfo.server->ssl->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
(void)HITLS_Accept(testInfo.server->ssl);
ASSERT_EQ(testInfo.server->ssl->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
FRAME_RegCryptMethod();
if (uioType == BSL_UIO_UDP) {
// anti-replay
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
} else {
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
}
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
(void)HITLS_Connect(testInfo.client->ssl);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint32_t sendNum;
ASSERT_EQ(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_DISORDER_TC001
* @spec -
* @titleThe server receives out-of-order APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLS. Expected result 2.
* 3. Construct an app message whose SN is 2 and send it to the server. When the server invokes HiTLS_Read, expected result 3.
* 4. Construct an app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 4.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLS connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_DISORDER_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo, uioType), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderApp(testInfo.client, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
uint8_t app1Data[MAX_RECORD_LENTH] = {0};
uint32_t app1Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, app1Data, MAX_RECORD_LENTH, &app1Len) == HITLS_SUCCESS);
uint8_t app2Data[MAX_RECORD_LENTH] = {0};
uint32_t app2Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, app2Data, MAX_RECORD_LENTH, &app2Len) == HITLS_SUCCESS);
ASSERT_EQ(app1Len, app2Len);
ASSERT_EQ(memcmp(app1Data, app2Data, app2Len), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_DISORDER_TC002
* @spec -
* @titleThe client receives out-of-order APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection request using DTLS. Expected result 2.
* 3. Construct an app message whose sequence number is 2 and send it to the client. When the client invokes HiTLS_Read, expected result 3.
* 4. Construct an app message whose sequence number is 1 and send it to the client. When the client invokes HiTLS_Read, expected result 4.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLS connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_DISORDER_TC002(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo, uioType), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderApp(testInfo.server, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
uint8_t app1Data[MAX_RECORD_LENTH] = {0};
uint32_t app1Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, app1Data, MAX_RECORD_LENTH, &app1Len) == HITLS_SUCCESS);
uint8_t app2Data[MAX_RECORD_LENTH] = {0};
uint32_t app2Len = MAX_RECORD_LENTH;
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, app2Data, MAX_RECORD_LENTH, &app2Len) == HITLS_SUCCESS);
ASSERT_EQ(app1Len, app2Len);
ASSERT_EQ(memcmp(app1Data, app2Data, app1Len), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_APPDATA_TC001
* @spec -
* @title The server receives duplicate APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLS. Expected result 2.
* 3. Construct an app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 3.
* 4. Construct the app message whose SN is 1 and send it to the server. When the server invokes HiTLS_Read, expected result 4.
* 5. The server constructs data and sends it to the client. When the client invokes HiTLS_Read, expected result 5.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLS connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* 5: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_APPDATA_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo, uioType), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetRepeatsApp(testInfo.client, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
if (uioType == BSL_UIO_UDP) {
// anti-replay
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
} else {
ASSERT_TRUE(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
}
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_TRUE(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.server->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_APPDATA_TC002
* @spec -
* @titleThe client receives duplicate APP messages.
* @precon nan
* @brief 1. Initialize the configuration on the client and server. Expected result 1.
* 2. Initiate a connection application by using DTLS. Expected result 2.
* 3. Construct an app message whose sequence number is 1 and send the message to the client. When the client invokes HiTLS_Read, expected result 3.
* 4. Construct an app message with the sequence number being 1 and send it to the client. When the client invokes HiTLS_Read, expected result 4.
* 5. The server constructs data and sends it to the client. When the client invokes HiTLS_Read, expected result 5.
* @expect 1: Initializing the configuration succeeded.
* 2: The DTLS connection is successfully created.
* 3: The interface returns a success response.
* 4: The interface returns a success response.
* 5: The interface returns a success response.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_APPDATA_TC002(int uioType)
{
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo, uioType), HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetRepeatsApp(testInfo.server, data, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, data, MAX_RECORD_LENTH, &len), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_TRUE(HITLS_Write(testInfo.server->ssl, writeData, writeLen, &sendNum) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.server->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_CLIENT_HELLO_TC001
* @spec -
* @title The server receives a Client Hello packet after the connection is established.
* @precon nan
* @brief 1. Initialize the client and server based on the default configuration. Expected result 1.
* 2. The client initiates a connection request. Expected result 2.
* 3. Construct a Client Hello packet and send it to the server. The server invokes HiTLS_Read to receive the packet. Expected result 3.
* 4. The client invokes HiTLS_Write to send data to the server, and the server invokes HiTLS_Read to read data. (Expected result 4)
* @expect 1: Initialization succeeded.
* 2: The connection between the client and server is successfully established.
* 3: The HiTLS_Read returns an error code, indicating that the receiving buffer is empty.
* 4: The data read by the server is consistent with the data sent by the client.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_CLIENT_HELLO_TC001(int uioType)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isClient = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_EQ(DefaultCfgStatusPark1(&testInfo, uioType), HITLS_SUCCESS);
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(REC_Write(testInfo.client->ssl, REC_TYPE_HANDSHAKE,
&sendBuf[REC_DTLS_RECORD_HEADER_LEN], sendLen - REC_DTLS_RECORD_HEADER_LEN) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, data, MAX_RECORD_LENTH, &len), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t writeData[] = {"abcd1234"};
uint32_t writeLen = strlen("abcd1234");
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
uint8_t tmpData[MAX_RECORD_LENTH];
uint32_t tmpLen;
uint32_t sendNum;
ASSERT_EQ(HITLS_Write(testInfo.client->ssl, writeData, writeLen, &sendNum), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.client->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, tmpData, tmpLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, readData, MAX_RECORD_LENTH, &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, writeLen);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
static bool cookie_generate_success = true;
static int32_t UT_CookieGenerateCb(HITLS_Ctx *ctx, uint8_t *cookie, uint32_t *cookie_len)
{
(void)ctx;
(void)cookie;
if (cookie_generate_success) {
*cookie_len = DTLS_COOKIE_LEN;
}
return cookie_generate_success;
}
static bool cookie_valid = true;
static int32_t UT_CookieVerifyCb(HITLS_Ctx *ctx, const uint8_t *cookie, uint32_t cookie_len)
{
(void)ctx;
(void)cookie;
(void)cookie_len;
return cookie_valid;
}
int32_t HS_CheckCookie_Stub(TLS_Ctx *ctx, const ClientHelloMsg *clientHello, bool *isCookieValid)
{
*isCookieValid = false;
/* If the client does not send the cookie, the verification is not required */
if (clientHello->cookie == NULL) {
return HITLS_SUCCESS;
}
if (ctx->globalConfig->appVerifyCookieCb == NULL) {
return HITLS_UNREGISTERED_CALLBACK;
}
HITLS_AppVerifyCookieCb cookieCb = ctx->globalConfig->appVerifyCookieCb;
int32_t isValid = cookieCb(ctx, clientHello->cookie, clientHello->cookieLen);
if (isValid != HITLS_COOKIE_VERIFY_ERROR) {
*isCookieValid = true;
}
return HITLS_SUCCESS;
}
int32_t HS_CalcCookie_Stub(TLS_Ctx *ctx, const ClientHelloMsg *clientHello, uint8_t *cookie, uint32_t *cookieLen)
{
(void)clientHello;
if (ctx->globalConfig->appGenCookieCb == NULL) {
return HITLS_UNREGISTERED_CALLBACK;
}
int32_t returnVal = ctx->globalConfig->appGenCookieCb(ctx, cookie, cookieLen);
if (returnVal == HITLS_COOKIE_GENERATE_ERROR) {
return HITLS_MSG_HANDLE_COOKIE_ERR;
}
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC001
* @spec -
* @title The server doesn't set appGenCookieCb or appVerifyCookieCb.
* @precon nan
* @brief 1. Configure option isSupportDtlsCookieExchange is on. Leave appGenCookieCb or appVerifyCookieCb blank.
Check whether server and client can handshake successfully. Expected result 1.
* @expect 1. The link fails to be set up.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC001(int setGenerateCb, int setVerifyCb)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
STUB_Init();
FuncStubInfo stubInfo = {0};
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
serverTlsCtx->config.tlsConfig.isSupportDtlsCookieExchange = true;
if (setGenerateCb) {
serverTlsCtx->globalConfig->appGenCookieCb = UT_CookieGenerateCb;
STUB_Replace(&stubInfo, HS_CheckCookie, HS_CheckCookie_Stub);
}
if (setVerifyCb) {
serverTlsCtx->globalConfig->appVerifyCookieCb = UT_CookieVerifyCb;
STUB_Replace(&stubInfo, HS_CalcCookie, HS_CalcCookie_Stub);
}
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_UNREGISTERED_CALLBACK);
EXIT:
STUB_Reset(&stubInfo);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC002
* @spec -
* @title The server fails to generate cookie.
* @precon nan
* @brief 1. Configure option isSupportDtlsCookieExchange is on. Configure appGenCookieCb and appVerifyCookieCb.
appGenCookieCb always return false.
Check whether server and client can handshake successfully. Expected result 1.
* @expect 1. The link fails to be set up.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
serverTlsCtx->config.tlsConfig.isSupportDtlsCookieExchange = true;
serverTlsCtx->globalConfig->appGenCookieCb = UT_CookieGenerateCb;
serverTlsCtx->globalConfig->appVerifyCookieCb = UT_CookieVerifyCb;
cookie_generate_success = false;
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_MSG_HANDLE_COOKIE_ERR);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC003
* @spec -
* @title The server receives a Client Hello packet with invalid cookie when isSupportDtlsCookieExchange is on.
* @precon nan
* @brief 1. Configure option isSupportDtlsCookieExchange is on. Configure appGenCookieCb and appVerifyCookieCb.
appGenCookieCb always return true and appVerifyCookieCb always return false.
Check whether server and client can handshake successfully. Expected result 1.
* @expect 1: The link fails to be set up.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
serverTlsCtx->config.tlsConfig.isSupportDtlsCookieExchange = true;
serverTlsCtx->globalConfig->appGenCookieCb = UT_CookieGenerateCb;
serverTlsCtx->globalConfig->appVerifyCookieCb = UT_CookieVerifyCb;
cookie_generate_success = true;
cookie_valid = false;
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_INTERNAL_EXCEPTION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC003
* @spec -
* @title The server receives a Client Hello packet with valid cookie when isSupportDtlsCookieExchange is on.
* @precon nan
* @brief 1. Configure option isSupportDtlsCookieExchange is on. Configure appGenCookieCb and appVerifyCookieCb.
appGenCookieCb always return true and appVerifyCookieCb always return true.
Check whether server and client can handshake successfully. Expected result 1.
* @expect 1: The link is set up successfully.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_HELLO_VERIFY_REQ_TC004(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
serverTlsCtx->config.tlsConfig.isSupportDtlsCookieExchange = true;
serverTlsCtx->globalConfig->appGenCookieCb = UT_CookieGenerateCb;
serverTlsCtx->globalConfig->appVerifyCookieCb = UT_CookieVerifyCb;
cookie_generate_success = true;
cookie_valid = true;
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC8422_ECPOINT_TC001
* @spec -
* @titleThe client receives an abnormal dot format field.
* @precon nan
* @brief 1. The client and server use the default initialization. Expected result 1.
* 2. The client initiates a connection request. When the client wants to read the Server Hello message,
* Modify the Elliptic curves point formats field, change the group supported by the field to 0x01, and send the field to the client. Expected result 2.
* @expect 1. Initialization succeeded.
* 2. The client sends a FATAL ALERT with the description of ALERT_ELLEGAL_PARAMETER.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC8422_ECPOINT_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t Gdata[] = { 0x01 };
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->pointFormats.exState = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgInteger(HS_EX_TYPE_POINT_FORMATS, &(serverMsg->pointFormats.exType)) == HITLS_SUCCESS);
serverMsg->pointFormats.exLen.state = INITIAL_FIELD;
ASSERT_TRUE(FRAME_ModifyMsgArray8(Gdata, sizeof(Gdata)/sizeof(uint8_t),
&(serverMsg->pointFormats.exData), &(serverMsg->pointFormats.exDataLen)) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNSUPPORT_POINT_FORMAT);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC8422_EXTENSION_MISS_TC001
* @spec -
* @title The server receives a Client Hello packet that does not carry the group or dot format.
* @precon nan
* @brief 1. Configure the HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 cipher suite on the client and initialize the cipher suite on the server by default. Expected result 1.
* 2. When the client initiates a connection request and the server is about to read ClientHello,
* Delete the supported_groups and ec_point_formats fields from the packet. Expected result 2.
* @expect 1. Initialization succeeded.
* 2. The server sends a Server Hello packet with the HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 algorithm suite.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC8422_EXTENSION_MISS_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo, BSL_UIO_UDP) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_DTLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_UDP;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->supportedGroups.exState = MISSING_FIELD;
clientMsg->pointFormats.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
CONN_Deinit(testInfo.server->ssl);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_HANDSHAKE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_HANDSHAKE);
ASSERT_TRUE(frameMsg.body.hsMsg.type.data == SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_EQ(serverMsg->cipherSuite.data, HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* when receive alert between finish and ccs, dtls12 should cache it*/
/* BEGIN_CASE */
void UT_DTLS_RFC6347_RECV_ALERT_AFTER_CCS_TC001(int uioType)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLSConfig();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, uioType);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, uioType);
client->needStopBeforeRecvCCS = true;
server->needStopBeforeRecvCCS = true;
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
// client receive ccs, wait to receive finish
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t alertdata[2] = {0x02, 0x0a};
ASSERT_EQ(REC_Write(serverTlsCtx, REC_TYPE_ALERT, alertdata, sizeof(alertdata)), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
// client cache the alert, wait to receive finish
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
// server send finish, handshake success
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
// client receive finish, handshake success
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
// client read cached alert
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RFC6347_TC001
* @spec -
* @title The client receives a Hello Request message which msg seq is not 0.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. After the client finished handshake, the client receives a Hello Request message. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client igore the message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RFC6347_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
tlsConfig->isSupportRenegotiation = true;
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t buf[DTLS_HS_MSG_HEADER_SIZE] = {0u};
buf[5] = 1;
size_t len = DTLS_HS_MSG_HEADER_SIZE;
REC_Write(serverTlsCtx, REC_TYPE_HANDSHAKE, buf, len);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
uint8_t readData[MAX_RECORD_LENTH] = {0};
uint32_t readLen = MAX_RECORD_LENTH;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readData, MAX_RECORD_LENTH, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_DTLS_CONSISTENCY_RETRANSMIT_TC001
* @spec -
* @title When testing the behavior of the client preparing to receive messages, querying the remaining timeout time,
selecting whether to sleep, and then retransmitting the message
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. Stop the client in the TRY1 RECV_SERVER HELLO or TRY1 RECV_FINISH state. Expected result 2.
* 3. Obtain the remaining timeout period. Expected result 3.
* 4. Choose sleep mode and then resend the message. Expected result 4.
* 5. Do not sleep, and then retransmit the message. Expected result 5.
* @expect 1. The initialization is successful.
* 2. return success.
* 3. return success.
* 4. expecting the message to be successfully retransmit.
* 5. expecting to return HITLS_MSG_HANDLE_DTLS_RETRANSMIT_NOT_TIMEOUT.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_DTLS_CONSISTENCY_RETRANSMIT_TC001(int isNeedSleep, int clientState)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
tlsConfig->isSupportRenegotiation = true;
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, clientState) == HITLS_SUCCESS);
uint64_t timeout = 0;
int32_t ret = HITLS_DtlsGetTimeout(client->ssl, &timeout);
ASSERT_TRUE(ret == HITLS_SUCCESS);
if (isNeedSleep) {
sleep(2); // the sleep time is greater than the timeout period, the retransmission operation is triggered
ret = HITLS_DtlsProcessTimeout(client->ssl);
// When there are multiple messages, retransmission will be blocked, so it will be busy.
// If there is only one message, it will be successfully sent.
if (ret == HITLS_REC_NORMAL_IO_BUSY) {
ret = FRAME_TrasferMsgBetweenLink(client, server);
}
ASSERT_EQ(ret, HITLS_SUCCESS);
} else {
ret = HITLS_DtlsProcessTimeout(client->ssl);
ASSERT_TRUE(ret == HITLS_MSG_HANDLE_DTLS_RETRANSMIT_NOT_TIMEOUT);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtls12/test_suite_sdv_frame_dtls12_consistency.c | C | unknown | 138,478 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "stub_crypt.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "rec_wrapper.h"
#include "process.h"
#include "pthread.h"
#include "unistd.h"
#include "rec_header.h"
#include "bsl_log.h"
#include "cert_callback.h"
#include "bsl_uio.h"
#include "uio_abstraction.h"
#include "record.h"
/* END_HEADER */
#define REC_DTLS_RECORD_HEADER_LEN 13
#define BUF_SIZE_DTO_TEST 18432
void Hello(void *ssl)
{
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_TlsWrite(ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
EXIT:
return;
}
/*
* @
* @test SDV_TLS_DTLS_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001
* @Specifications-
* @Title1. Construct a scenario where renegotiation messages and application messages are sent at the same time. It is expected that the server processes renegotiation messages first.
* @preconan
* @short
* @ Previous Level 1
* @autotrue
@ */
/* BEGIN_CASE */
void SDV_TLS_DTLS_CONSISTENCY_RFC5246_UNEXPETED_REORD_TYPE_TC001()
{
int version = TLS1_2;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HITLS_Session *session = NULL;
TLS_TYPE local = HITLS;
TLS_TYPE remote = HITLS;
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(local);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(remote);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_SetRenegotiationSupport(clientCtxConfig, true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetRenegotiationSupport(serverCtxConfig, true);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
HLT_SetClientRenegotiateSupport(serverCtxConfig, true);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 1666;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HITLS_Renegotiate(clientSsl) == HITLS_SUCCESS);
const char *writeBuf = "Hello world";
pthread_t thrd;
ASSERT_TRUE(pthread_create(&thrd, NULL, (void *)Hello, clientSsl) == 0);
sleep(2);
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
pthread_join(thrd, NULL);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
ClearWrapper();
HLT_CleanFrameHandle();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_CertificateParse001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_DTLS12;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_DTLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO_DONE);
int *t = (int*)user;
if (*t == 0) {
// Change the sequence number to 20
data[5] = 20;
}
(*t)++;
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static uint32_t DtlsTimerCallBack(HITLS_Ctx *ctx, uint32_t us)
{
(void)ctx;
(void)us;
return 1000000; // timeout value is 1 second.
}
/* @
* @test SDV_TLS_DTLS_CONSISTENCY_RFC6347_MTU_TC001
* @title Multiple timeout retransmissions result in a decrease in MTU
* @precon nan
* @brief 1. Establish a link using UDP with dtls12 and construct timeout retransmission three times.
Expected result 1 is obtained.
* @expect 1. MTU reduced from 1472 to 548
@ */
/* BEGIN_CASE */
void SDV_TLS_DTLS_CONSISTENCY_RFC6347_MTU_TC001()
{
int32_t port = 18888;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, UDP, port, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
int32_t user = 0;
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO_DONE,
REC_TYPE_HANDSHAKE,
false,
&user,
Test_CertificateParse001
};
RegisterWrapper(wrapper);
HLT_Tls_Res *tlsRes = HLT_ProcessTlsInit(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(tlsRes != NULL);
HITLS_SetDtlsTimerCb(tlsRes->ssl, DtlsTimerCallBack);
(void)HLT_TlsAccept(tlsRes->ssl);
HITLS_Ctx *ctx = tlsRes->ssl;
HITLS_SetNoQueryMtu(ctx, false);
ASSERT_EQ(ctx->config.pmtu, 1472);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(ctx->config.pmtu, 548);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
int32_t STUB_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num)
{
static int32_t count = 0;
if (data[0] == CERTIFICATE && count == 0) {
int32_t hsFragmentLength = BSL_ByteToUint24(&data[DTLS_HS_FRAGMENT_LEN_ADDR]);
int32_t fragmentLength = hsFragmentLength + DTLS_HS_MSG_HEADER_SIZE + REC_DTLS_RECORD_HEADER_LEN;
// On the third retransmission, the message length became 548
ASSERT_EQ(fragmentLength, 548);
count++;
}
return ctx->recCtx->recWrite(ctx, recordType, data, num);
EXIT:
return -1;
}
static void Test_Timeout001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)data;
(void)len;
(void)bufSize;
(void)user;
sleep(1);
ASSERT_EQ(HITLS_DtlsProcessTimeout(ctx), HITLS_SUCCESS);
sleep(2);
ASSERT_EQ(HITLS_DtlsProcessTimeout(ctx), HITLS_SUCCESS);
FuncStubInfo stubInfo = {0};
STUB_Replace(&stubInfo, REC_Write, STUB_REC_Write);
sleep(4);
ASSERT_EQ(HITLS_DtlsProcessTimeout(ctx), HITLS_SUCCESS);
EXIT:
STUB_Reset(&stubInfo);
return;
}
/* @
* @test SDV_TLS_DTLS_CONSISTENCY_RFC6347_MTU_TC002
* @title Multiple timeout retransmissions result in a decrease in MTU
* @precon nan
* @brief 1. Establish a link using UDP with dtls12 and construct timeout retransmission three times.
Expected result 1 is obtained.
* @expect 1. MTU reduced from 1472 to 548. On the third retransmission, the message length became 548
@ */
/* BEGIN_CASE */
void SDV_TLS_DTLS_CONSISTENCY_RFC6347_MTU_TC002()
{
HLT_ConfigTimeOut("10");
int32_t port = 18888;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, UDP, port, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
RecWrapper wrapper = {TRY_RECV_CLIENT_KEY_EXCHANGE, REC_TYPE_HANDSHAKE, true, NULL, Test_Timeout001};
RegisterWrapper(wrapper);
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
uint8_t readData[REC_MAX_PLAIN_LENGTH] = {0};
uint32_t readLen = REC_MAX_PLAIN_LENGTH;
ASSERT_EQ(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")), 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readData, readLen, &readLen), 0);
ASSERT_EQ(readLen, strlen("Hello World"));
ASSERT_EQ(memcmp("Hello World", readData, readLen), 0);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/dtls12/test_suite_sdv_hlt_dtls12_consistency.c | C | unknown | 11,386 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <unistd.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "session_type.h"
#include "hitls_type.h"
#include "pack.h"
#include "send_process.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "uio_base.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "cert.h"
#include "app.h"
#include "hlt.h"
#include "alert.h"
#include "securec.h"
#include "record.h"
#include "rec_write.h"
#include "rec_read.h"
#include "rec_wrapper.h"
#include "hitls_crypt_init.h"
#include "conn_init.h"
#include "cert_callback.h"
#include "change_cipher_spec.h"
#include "common_func.h"
#include "crypt_default.h"
#include "stub_crypt.h"
#ifdef HITLS_TLS_FEATURE_PROVIDER
#include "hitls_crypt.h"
#endif
#define PORT 11111
#define TEMP_DATA_LEN 1024 /* Length of a single message. */
#define MAX_BUF_LEN (20 * 1024)
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
#define ALERT_BODY_LEN 2u
#define REC_CONN_SEQ_SIZE 8u /* SN size */
#define GetEpochSeq(epoch, seq) (((uint64_t)(epoch) << 48) | (seq))
#define BUF_TOOLONG_LEN ((1 << 14) + 1)
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; /* session set to the client, used for session recovery. */
} ResumeTestInfo;
typedef struct {
int connectExpect; // Expected connect result
int acceptExpect; // Expected accept result
ALERT_Level expectLevel; // Expected alert level
ALERT_Description expectDescription; // Expected alert description of the tested end
} TestExpect;
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isServerExtendMasterSecret;
bool isSupportRenegotiation; /* Renegotiation support flag */
bool needStopBeforeRecvCCS; /* CCS test, so that the TRY_RECV_FINISH stops before the CCS message is received. */
} HandshakeTestInfo;
uint16_t GetCipherSuite(const char *cipherSuite)
{
if (strcmp(cipherSuite, "HITLS_ECDHE_SM4_CBC_SM3") == 0) {
return HITLS_ECDHE_SM4_CBC_SM3;
}
if (strcmp(cipherSuite, "HITLS_ECC_SM4_CBC_SM3") == 0) {
return HITLS_ECC_SM4_CBC_SM3;
}
return 0;
}
int32_t RandBytes(uint8_t *randNum, uint32_t randLen)
{
srand(time(0));
const int maxNum = 256u;
for (uint32_t i = 0; i < randLen; i++) {
randNum[i] = (uint8_t)(rand() % maxNum);
}
return HITLS_SUCCESS;
}
int32_t GenerateEccPremasterSecret(TLS_Ctx *ctx);
int32_t RecordDecryptPrepare(
TLS_Ctx *ctx, uint16_t version, REC_Type recordType, REC_TextInput *cryptMsg);
int32_t RecConnDecrypt(
TLS_Ctx *ctx, RecConnState *state, const REC_TextInput *cryptMsg, uint8_t *data, uint32_t *dataLen);
int32_t STUB_GenerateEccPremasterSecret(TLS_Ctx *ctx)
{
uint32_t offset;
HS_Ctx *hsCtx = ctx->hsCtx;
KeyExchCtx *kxCtx = hsCtx->kxCtx;
uint8_t *premasterSecret = kxCtx->keyExchParam.ecc.preMasterSecret;
/* The first two bytes are the latest version supported by the client.*/
/* Change the version number and construct an exception. */
BSL_Uint16ToByte(0x0505, premasterSecret);
offset = sizeof(uint16_t);
/* 46 bytes secure random number */
#ifdef HITLS_TLS_FEATURE_PROVIDER
return HITLS_CRYPT_RandbytesEx(NULL, &premasterSecret[offset], MASTER_SECRET_LEN - offset);
#else
return CRYPT_DEFAULT_RandomBytes(&premasterSecret[offset], MASTER_SECRET_LEN - offset);
#endif
}
int32_t STUB_TlsRecordRead(TLS_Ctx *ctx, REC_Type recordType, uint8_t *data, uint32_t *readLen, uint32_t num)
{
int32_t ret;
(void)recordType;
(void)readLen;
RecConnState *state = ctx->recCtx->readStates.currentState;
uint16_t version = ctx->negotiatedInfo.version;
REC_TextInput encryptedMsg = {0};
ret = RecordDecryptPrepare(ctx, version, recordType, &encryptedMsg);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint32_t dataLen = num;
ASSERT_EQ(encryptedMsg.textLen, num);
ret = RecConnDecrypt(ctx, state, &encryptedMsg, data, &dataLen);
EXIT:
return ret;
}
int32_t StatusGMPark(HandshakeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateTLCPLink(testInfo->config, BSL_UIO_TCP, true);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateTLCPLink(testInfo->config, BSL_UIO_TCP, false);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLCPConfig();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusGMPark(testInfo);
}
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLCPConfig();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusGMPark(testInfo);
}
void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
HITLS_KeyExchAlgo keyExType)
{
frametype->versionType = versionType;
frametype->recordType = recordType;
frametype->handshakeType = handshakeType;
frametype->keyExType = keyExType;
frametype->transportType = BSL_UIO_TCP;
}
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tlcp/test_suite_sdv_frame_tlcp_consistency.base.c | C | unknown | 7,359 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_sdv_frame_tlcp_consistency */
/* END_HEADER */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC001
* @title After initialization, the server receives a CCS message after sending the serverhellodone message and return
* an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. During the handshake, after sending the Server Hello Done message, the server
* constructs a CCS message and sends it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC001(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC002
* @title After initialization, the client receives a CCS message after sending a client hello message and returns an
* alert message
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. During the handshake, after sending the client hello message, constructs a CCS message and sends it to the
* client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC002(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC003
* @title During connection establishment, the client receives the serverhello message after sending the CCS message and
* expects to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. During the handshake, after client sending the CCS, constructs a serverhello message and sends it to the
* client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC003(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
testInfo.client->ssl->hsCtx->state = TRY_RECV_FINISH;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_EQ(FRAME_GetDefaultMsg(&frameType, &frameMsg), HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC004
* @title After initialization, construct an app message and send it to the client. The expected alert is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. Durintg the handshake, When the client is in the TRY_RECV_SERVER_HELLO state, construct an APP data message
* and send it to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC004(void)
{
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HandshakeTestInfo testInfo = { 0 };
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateTLCPLink(testInfo.config, BSL_UIO_TCP, true);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateTLCPLink(testInfo.config, BSL_UIO_TCP, false);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_IDLE);
uint8_t appdata[] = {0x17, 0x01, 0x01, 0x00, 0x02, 0x01, 0x01};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len),
EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_ALERTING);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_CM_LINK_FATAL_ALERTED);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC005
* @title After the connection is established, the client receives the serverhello message after receiving the app data.
* The client is expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. The client initiates a TLS conncetion request. After the handshake succeeds, constructs a serverhello
* message and sends it to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC005(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLCP_DTLCP11, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
/* Reassembly */
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_BAD_RECORD_MAC);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_EQ(FRAME_ParseMsgHeader(&frameType, sndBuf, sndLen, &frameMsg, &parseLen), 0);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC006
* @title After the link is established, renegotiation is not enabled. The server receives the client keyexchange message
* and is expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. After the client initiates a TLS link request. After the handshake succeeds, construct a clientkeyexchange
* message and send it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC006(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
uint8_t data[5] = {0x10, 0x00, 0x00, 0x01, 0x01};
ASSERT_EQ(REC_Write(client->ssl, REC_TYPE_HANDSHAKE, data, sizeof(data)), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC007
* @title After initialization, construct an app message and send it to the server. Expected alert is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. When the client initiates a TLS link application request in the RECV_CLIENT_HELLO message on the server,
* construct an APP message and send it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_RECORDTYPE_TC007(void)
{
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HandshakeTestInfo testInfo = { 0 };
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateTLCPLink(testInfo.config, BSL_UIO_TCP, true);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateTLCPLink(testInfo.config, BSL_UIO_TCP, false);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0x17, 0x01, 0x01, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len),
EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC01
* @spec Record layer protocols include: handshake, alarm, and password specification change.
* To support protocol extensions, the record layer protocol may support other record types.
* Any new record types should be deassigned in addition to the Content Type values assigned for the types
* described above.
* If an unrecognized record type is received, ignore it.
* @title There are only four types of record layers.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. When the client is in TRY_RECV_SERVER_HELLO state,receives the serverhello message whose recordType is 99,
* Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC01(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_SM4_CBC_SM3, HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.msg[0] = 0x99u;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC02
* @spec Record layer protocols include: handshake, alarm, and password specification change.
* To support protocol extensions, the record layer protocol may support other record types.
* Any new record types should be de-assigned in addition to the Content Type values assigned for the types
* described above.
* If an unrecognized record type is received, ignore it.
* @title There are only four types of record layers.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. After the connection is established, the client receives abnormal messages (recordType: 99) after receiving
* the app data. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC02(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_SM4_CBC_SM3, HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t dataBuf[] = "Hello World!";
uint8_t readBuf[READ_BUF_SIZE];
uint32_t readbytes;
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, dataBuf, sizeof(dataBuf), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.msg[0] = 0x99u;
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readbytes), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC03
* @spec Record layer protocols include: handshake, alarm, and password specification change.
* To support protocol extensions, the record layer protocol may support other record types.
* Any new record types should be deassigned in addition to the Content Type values assigned for the types
* described above.
* If an unrecognized record type is received, ignore it.
* @title There are only four types of record layers.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. When the server is in TRY_RECV_CLIENT_HELLO state, the server receives the client hello message whose
* recordType is 99. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC03(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_SM4_CBC_SM3, HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.msg[0] = 0x99u;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC04
* @spec Record layer protocols include: handshake, alarm, and password specification change.
* To support protocol extensions, the record layer protocol may support other record types.
* Any new record types should be deassigned in addition to the Content Type values assigned for the types
* described above.
* If an unrecognized record type is received, ignore it.
* @title There are only four types of record layers.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1.
* 2. After the connection is established, the server receives abnormal messages (recordType: 99) after receiving
* the app data. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT_UNEXPECTED_MESSAGE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNKNOW_RECORDTYPE_TC04(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_SM4_CBC_SM3, HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t dataBuf[] = "Hello World!";
uint8_t readBuf[READ_BUF_SIZE];
uint32_t readbytes;
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(client->ssl, dataBuf, sizeof(dataBuf), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.msg[0] = 0x99u;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readbytes), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC001
* @title An unexpected message is received when the client is in the TRY_RECV_CERTIFICATIONATE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_CERTIFICATIONATE state, construct a Server Hello message and send it to
* the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT. The level is ALERT_LEVEL_FATAL and
* the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC002
* @title An unexpected message is received when the client is in the TRY_RECV_SERVER_KEY_EXCHANGE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_SERVER_KEY_EXCHANGE state, construct a Server Hello message and send it
* to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is ALERT_Level_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC002(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE), HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC003
* @title An unexpected message is received when the client is in the TRY_RECV_SERVER_HELLO_DONE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_SERVER_HELLO_DONE state, construct a Server Hello message and send it to
* the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is ALERT_LEVEL_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC003(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO_DONE), HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC004
* @title An unexpected message is received when the client is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_FINISH state, construct a Server Hello message and send it to the client
Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is ALERT_LEVEL_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg,
MAX_RECORD_LENTH,
ioUserData->recMsg.msg + REC_TLS_RECORD_HEADER_LEN,
ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN) == EOK);
sndMsg.len = ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN;
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
REC_Write(server->ssl, REC_TYPE_HANDSHAKE, sndMsg.msg, sndMsg.len);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC005
* @title An unexpected message is received when the client is in the TRY_RECV_CERTIFICATE_REQUEST state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_CERTIFICATE_REQUEST state, construct a Server Hello message and send it
to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is ALERT_LEVEL_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC005(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_REQUEST), HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC006
* @title An unexpected message is received when the client is in the TRY_RECV_NEW_SESSION_TICKET state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in the TRY_RECV_NEW_SESSION_TICKET state, construct a Server Hello message and send it
* to the client. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is ALERT_LEVEL_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC006(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
client->ssl->hsCtx->state = TRY_RECV_NEW_SESSION_TICKET;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC007
* @title An unexpected message is received when the server is in the TRY_RECV_CLIENT_HELLO state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the server is in the TRY_RECV_CLIENT_HELLO state, construct a CLIENT_KEY_EXCHANGE message and send it
* to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC007(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
server->ssl->hsCtx->state = TRY_RECV_CLIENT_HELLO;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC008
* @title An unexpected message is received when the server is in the TRY_RECV_CERTIFICATIONATE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server is in the TRY_RECV_CERTIFICATIONATE state, construct a CLIENT_KEY_EXCHANGE message and send
* it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC008(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC009
* @title An unexpected message is received when the server is in the TRY_RECV_CLIENT_KEY_EXCHANGE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server is in the TRY_RECV_CLIENT_KEY_EXCHANGE state, construct a SERVER_HELLO message and send it
* to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the SERVER_HELLO message, the server sends an ALERT message. The level is ALERT_Level_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC009(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
server->ssl->hsCtx->state = TRY_RECV_CLIENT_KEY_EXCHANGE;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC010
* @title An unexpected message is received when the server is in the TRY_RECV_CERTIFICATIONATE_VERIFY state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server is in the TRY_RECV_CERTIFICATIONATE_VERIFY state, construct a CLIENT_KEY_EXCHANGE message
* and send it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC010(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE_VERIFY;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC011
* @title An unexpected message is received when the server is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the server is in the TRY_RECV_FINISH state, construct a CLIENT_KEY_EXCHANGE message and send it to the
server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC011(int version)
{
FRAME_Init();
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
server->ssl->hsCtx->state = TRY_RECV_FINISH;
uint32_t parseLen = 0;
frameType.versionType = version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC012
* @title An unexpected message is received when the client is in the TRY_RECV_SERVER_HELLO state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client is in the TRY_RECV_SERVER_HELLO state, construct a Server Hello Done message and send it to
* the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello Done message, the client sends an ALERT. The level is ALERT_Level_FATAL
* and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_UNEXPECT_HANDSHAKEMSG_TC012(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
config->isSupportExtendMasterSecret = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = true;
config->isSupportRenegotiation = true;
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FRAME_Msg parsedSHdone = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, version, REC_TYPE_HANDSHAKE, SERVER_HELLO_DONE, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &parsedSHdone) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSHdone, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &parsedSHdone);
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &parsedSHdone, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedSHdone.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedSHdone.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSHdone);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC001
* @title The client sends a Client Certificate message with the length of 2 ^ 14 + 1 bytes.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a connection creation request. When the client needs to send the Client Certificate
* message, the two fields are modified as follows:
* Certificates Length is 2 ^ 14 + 1
* Certificates are changed to 2 ^ 14 + 1 bytes buffer.
* After the modification is complete, send the message to the server. Expected result 2.
* 3. When the server receives the Client Certificate message, check the value returned by the HITLS_Accept
* interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
* 3. The value returned by the HITLS_Accept interface is HITLS_REC_RECORD_OVERFLOW.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC002
* @title The server sends a Server Certificate message with the length of 2 ^ 14 + 1 bytes.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The client initiates a connection creation request. When the server needs to send a Server Certificate
* message, the two fields are modified as follows:
* Certificates Length is 2 ^ 14 + 1
* Certificates are changed to 2 ^ 14 + 1 bytes buffer.
* After the modification is complete, send the modification to the server. Expected result 2.
* 3. When the client receives the Server Certificate message, check the value returned by the HITLS_Connect
* interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
* 3. The value returned by the HITLS_Connect interface is HITLS_REC_RECORD_OVERFLOW.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC002(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC003
* @title The client sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 bytes.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a link creation request. When the client needs to send a Change Cipher Spec message,
* modify one field as follows: Length is 2 ^ 14 + 1. After the modification, the modification is sent to the
* server. Expected result 2 is obtained.
* 3. When the server receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
* interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the server.
* 3. The value returned by the HITLS_Accept interface is HITLS_REC_RECORD_OVERFLOW.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC003(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.ccsMsg.extra.data);
frameMsg.body.ccsMsg.extra.data = certDataTemp;
frameMsg.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE_VERIFY);
bool isCcsRecv = testInfo.server->ssl->method.isRecvCCS(testInfo.server->ssl);
ASSERT_TRUE(isCcsRecv == false);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC004
* @title The server sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 bytes.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. The server initiates a link creation request. When the server needs to send a Change Cipher Spec message,
* modify one field as follows: Length is 2 ^ 14 + 1. After the modification, the modification is sent to the
* server. Expected result 2.
* 3. When the client receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
* interface. Expected result 3.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
* 3. The value returned by the HITLS_Accept interface is HITLS_REC_RECORD_OVERFLOW.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_MSGLENGTH_TOOLONG_TC004(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_FINISH;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
frameType1.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg1.body.ccsMsg.extra.data);
frameMsg1.body.ccsMsg.extra.data = certDataTemp;
frameMsg1.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg1.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType1, &frameMsg1);
memset_s(&frameMsg1, sizeof(frameMsg1), 0, sizeof(frameMsg1));
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_RECORD_OVERFLOW);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CIPHERTEXT_TOOLONG_TC001
* @title A too long cipher text app message is sent by client or server.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. When the client is in transporting state, the server sends a message whose ciphertext length is 2 ^ 14 +
* 2048. Expected result 2.
* 3. When the server is in transporting state, the client sends a message whose ciphertext length is 2 ^ 14 +
* 2048. Expected result 3.
* 4. When the server is in transporting state, the client sends a message whose ciphertext length is 2 ^ 14 +
* 2049. Expected result 4.
* 5. When the client is in transporting state, the server sends a message whose ciphertext length is 2 ^ 14 +
* 2049. Expected result 5.
* @expect 1. The initialization is successful.
* 2. The message can be decrypted.
* 3. The message can be decrypted.
* 4. The server send an alert message.
* 5. The client send an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CIPHERTEXT_TOOLONG_TC001(int isClient, int ptLen, int ctLen)
{
FRAME_Init();
STUB_Init();
FuncStubInfo stubInfo = {0};
HITLS_Config *config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
FRAME_LinkObj *server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
FRAME_LinkObj *sender = NULL;
FRAME_LinkObj *receiver = NULL;
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
if (isClient) {
sender = client;
receiver = server;
} else {
sender = server;
receiver = client;
}
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
uint8_t readBuf[MAX_BUF_LEN] = {0};
uint32_t readLen = 0;
uint8_t sendBuf[MAX_BUF_LEN] = {0};
(void)memset_s(sendBuf + 5, REC_MAX_CIPHER_TEXT_LEN + 1, 9, REC_MAX_CIPHER_TEXT_LEN + 1);
RecBufFree(sender->ssl->recCtx->outBuf);
sender->ssl->recCtx->outBuf = RecBufNew(MAX_BUF_LEN);
RecBufFree(receiver->ssl->recCtx->inBuf);
receiver->ssl->recCtx->inBuf = RecBufNew(MAX_BUF_LEN);
ASSERT_EQ(TlsRecordWrite(sender->ssl, REC_TYPE_APP, sendBuf, ptLen), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(sender, receiver), HITLS_SUCCESS);
STUB_Replace(&stubInfo, TlsRecordRead, STUB_TlsRecordRead);
int32_t ret = ctLen > 18432 ? HITLS_REC_RECORD_OVERFLOW : HITLS_SUCCESS;
ASSERT_EQ(TlsRecordRead(receiver->ssl, REC_TYPE_APP, readBuf, &readLen, ctLen), ret);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(sender);
FRAME_FreeLink(receiver);
STUB_Reset(&stubInfo);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_NONZERO_MESSAGELEN_TC001
* @title Test the scenario where the message parameter is unacceptable.
* @precon nan
* @spec 1. After receiving the servehellodone message from the server, the client verifies whether the server
* certificate is valid and the servehello message from the server. Indicates whether the message parameter
* is acceptable. If acceptable, the client continues the handshake process. Otherwise, a HandShakeFailure
* critical alarm is sent.
* @brief 1. After receiving the servehellodone message from the server, the client verifies whether the server
* certificate is valid and the servehello message from the server. Expected result 1.
* @expect 1. During the first connection setup, the client receives a servehellodone message from the server. The
* message length is not 0. The expected handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_NONZERO_MESSAGELEN_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
int32_t ret;
ret = FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO_DONE);
ASSERT_EQ(ret, HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO_DONE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloDoneMsg *serverHelloDone = &frameMsg.body.hsMsg.body.serverHelloDone;
uint8_t extra[1] = {0};
FRAME_ModifyMsgArray8(extra, 1, &serverHelloDone->extra, NULL);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_PARSE_EXCESSIVE_MESSAGE_SIZE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_SEQ_NUM_TC001
* @title Check whether the sequence number of the read/write status in the FINISH message sent by the server/client is
1.
* @precon nan
* @brief 1. Configure the client/server to stay in the TRY_SEND_FINISH state. Expected result 1.
* 2. Connect the server and client once and send the message. Expected result 2.
3. Obtain the messages sent by the server or client. Expected result 3.
4. Parse the message sent by the local end into the hs_msg structure. Expected result 4.
5. Check the sequence number in the sent message. Expected result 5.
6. Enable the local end to transmit data to the peer end. Expected result 6.
7. Obtain the messages received by the peer end. Expected result 7.
8. Parse the message received by the peer end into the hs_msg structure. Expected result 8.
9. Check the sequence number in the received message. Expected result 9.
* @expect 1. The initialization is successful.
* 2. If the server is successfully connected, the client returns NORMAL_RECV_BUF_EMPTY.
3. The sending length is not null.
4. The parsing is successful and the message header length is fixed to 5 bytes.
5. The serial number is 1.
6. The transmission is successful.
7. The received length is not empty.
8. The parsing succeeds and the message header length is fixed to 5 bytes.
9. The serial number is 1.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_SEQ_NUM_TC001(int isClient)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_SEND_FINISH;
testInfo.isClient = isClient;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
if (isClient) {
ASSERT_TRUE(HITLS_Connect(testInfo.client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
} else {
ASSERT_TRUE(HITLS_Accept(testInfo.server->ssl) == HITLS_SUCCESS);
}
HITLS_Ctx *localSsl = isClient ? testInfo.client->ssl : testInfo.server->ssl;
ASSERT_TRUE(localSsl->recCtx->writeStates.currentState->seq == 1);
if (isClient) {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
}
HITLS_Ctx *remoteSsl = isClient ? testInfo.server->ssl : testInfo.client->ssl;
if (!isClient) {
ASSERT_TRUE(HITLS_Connect(testInfo.client->ssl) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(HITLS_Accept(testInfo.server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
}
ASSERT_TRUE(remoteSsl->recCtx->readStates.currentState->seq == 1);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_SEQ_NUM_TC002
* @title Check whether the sequence number of the read/write sequence in the APP message sent by the server/client is
2.
* @precon nan
* @brief 1. Configure the status of the client/server to the successful handshake. Expected result 1.
* 2. Check the server/client connection status. Expected result 2.
3. Randomly generate 32-byte data. Expected result 3.
4. Write app data. Expected result 4.
5. Obtain data from the I/O sent by the local end and parse the header and content. Expected result 5.
6. Check the sequence number in the sent message. Expected result 6.
7. Perform I/O data transmission from the local end to the peer end. Expected result 7.
8. Obtain data from the received I/O from the peer end and parse the header and content. Expected result 8.
9. Check the sequence number in the received message. Expected result 9.
* @expect 1. The initialization is successful.
* 2. The link status is Transferring.
3. The generation is successful.
4. The writing is successful.
5. The parsing is successful and the value of RecordType is REC_TYPE_APP.
6. The SN is 2.
7. The transmission is successful.
8. The parsing is successful and the value of RecordType is REC_TYPE_APP.
9. The SN is 2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_SEQ_NUM_TC002(int isClient)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = HS_STATE_BUTT;
testInfo.isClient = isClient;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_TRANSPORTING);
uint8_t transportData[REC_CONN_SEQ_SIZE * 4] = {0};
uint32_t transportDataLen = sizeof(transportData) / sizeof(uint8_t);
ASSERT_EQ(RandBytes(transportData, transportDataLen), HITLS_SUCCESS);
HITLS_Ctx *localSsl = isClient ? testInfo.client->ssl : testInfo.server->ssl;
uint32_t writeLen;
ASSERT_EQ(APP_Write(localSsl, transportData, transportDataLen, &writeLen), HITLS_SUCCESS);
ASSERT_EQ(localSsl->recCtx->writeStates.currentState->seq , 2);
if (isClient) {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
}
HITLS_Ctx *remoteSsl = isClient ? testInfo.server->ssl : testInfo.client->ssl;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(APP_Read(remoteSsl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_EQ(remoteSsl->recCtx->readStates.currentState->seq , 2);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tlcp/test_suite_sdv_frame_tlcp_consistency_1.c | C | unknown | 91,392 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_sdv_frame_tlcp_consistency */
/* END_HEADER */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_RESUME_TC003
* @title Enable the session restoration function at both ends. If the session ID is obtained after the link is
successfully established, a fatal alert is sent. The session ID fails to be used to restore the session.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server.
* Enable the session restoration function at both ends. Expected result 1.
* 2. Obtaine the session ID and a fatal alert is sent. The session ID fails to be used to restore the session.
* Expected result 2.
* @expect 1. The initialization is successful.
* 2. Expected handshake failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_RESUME_TC003()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
HITLS_SetSession(client->ssl, clientSession);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_FINISH), HITLS_SUCCESS);
client->ssl->method.sendAlert(client->ssl, ALERT_LEVEL_FATAL, ALERT_DECRYPT_ERROR);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
HITLS_SetSession(client->ssl, clientSession);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_RESUME_TC004
* @title Set the client and server support session recovery. After the first connection is established, the session ID
is obtained. Create two connection. The client and server are the same as those in the last session. Use the
same session ID to restore the session. If the session on one link fails, check whether the data communication
on the other link is blocked. It is expected that the link is not blocked.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server.
* Enable the session restoration function at both ends. Expected result 1.
* 2. Use the default configuration items to configure two new client and server.
* The client and server are the same as those in the last session. Expected result 1.
* 3. Use the obtained session ID to restore one session and send a alert. Expected result 2.
* 4. Use the obtained session ID to restore another session. Expected result 3.
* @expect 1. The initialization is successful.
* 2. Expected handshake failure.
* 3. Restore the session successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_RESUME_TC004()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_LinkObj *clientResume = NULL;
FRAME_LinkObj *serverResume = NULL;
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(client->ssl, clientSession);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_FINISH), HITLS_SUCCESS);
clientResume = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
serverResume = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(clientResume->ssl, clientSession);
ASSERT_EQ(FRAME_CreateConnection(clientResume, serverResume, false, TRY_SEND_FINISH), HITLS_SUCCESS);
client->ssl->method.sendAlert(client->ssl, ALERT_LEVEL_FATAL, ALERT_DECRYPT_ERROR);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ASSERT_EQ(FRAME_CreateConnection(clientResume, serverResume, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(clientResume->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(clientResume);
FRAME_FreeLink(serverResume);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_RESUME_TC005
* @title Set the client and server support session recovery. After the first connection is established, the session ID
* is obtained. Apply for two links. The client and server are the same as those in the last session. Use the same
* session ID to restore the session. If the session on one link times out, check whether the data communication
* on the other link is blocked. If the data communication on the other link is not blocked, the data
* communication on the other link is not blocked.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server.
* Enable the session restoration function at both ends. Expected result 1.
* 2. Use the default configuration items to configure two new client and server.
* The client and server are the same as those in the last session. Expected result 1.
* 3. Use the obtained session ID to restore one session and sleep to cause a session to time out.
* Expected result 2.
* 4. Use the obtained session ID to restore another session. Expected result 3.
* @expect 1. The initialization is successful.
* 2. Establish the connection but restore the session failed.
* 3. Establish the connection and restore the session successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_RESUME_TC005()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_LinkObj *clientResume = NULL;
FRAME_LinkObj *serverResume = NULL;
config = HITLS_CFG_NewTLCPConfig();
const uint64_t timeout = 5u;
HITLS_CFG_SetSessionTimeout(config, timeout);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(client->ssl, clientSession);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_FINISH), HITLS_SUCCESS);
clientResume = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
serverResume = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(clientResume->ssl, clientSession);
sleep(timeout);
ASSERT_EQ(FRAME_CreateConnection(clientResume, serverResume, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(clientResume->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
FRAME_FreeLink(clientResume);
FRAME_FreeLink(serverResume);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_RESUME_TC006
* @title Enable the session recovery function at both ends. The link is successfully established. The setting of the
* session_id expires. The session fails to be restored.
* @precon nan
* @brief 1. Set the client and server support session recovery. Establishe the first connection. Expected result 1.
* 2. Set the session_id expired, restore the session. Expected result 2.
* @expect 1. The expected handshake is successful.
* 2. The session is not recovered.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_RESUME_TC006()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
const uint64_t timeout = 5u;
HITLS_CFG_SetSessionTimeout(config, timeout);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(client->ssl, clientSession);
sleep(timeout);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_RESUME_TC007
* @title When a link is established for the first time, the clienthello message on the client contains the session_id
* field that is not empty and is in the connection state. If the session ID on the server is not found in the
* cache, the first connection setup process is triggered.
* @precon nan
* @brief 1. Create the TLCP links on the client and server again, set the obtained session as the session on the
* client, and check whether the session is reused. Expected result 1.
* @expect 1. The expected handshake is successful, but the session is not recovered.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_RESUME_TC007()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
HITLS_SetSession(client->ssl, clientSession);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tlcp/test_suite_sdv_frame_tlcp_consistency_2.c | C | unknown | 12,979 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_sdv_frame_tlcp_consistency */
/* END_HEADER */
static void Test_MisSessionId(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
if (*(bool *)user) {
return;
}
*(bool *)user = true;
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_EQ(parseLen, *len);
frameMsg.body.hsMsg.body.serverHello.sessionId.state = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_SESSIONID_MISS_TC001
* @title During session recovery, the server deletes session_id after sending the server hello message. The expected
* session recovery fails and the connection is interrupted.
* @precon nan
* @brief 1. The client hello and server hello messages are followed by authentication and key exchange. Including
* server certificate, server key exchange, client certificate, and client key exchange. Expected result 1.
* @expect 1. The expected handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_SESSIONID_MISS_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
bool isModify = false;
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
HITLS_SetSession(client->ssl, clientSession);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&isModify,
Test_MisSessionId
};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
static void Test_DiffServerKeyEx(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
if (*(bool *)user) {
return;
}
*(bool *)user = true;
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_KEY_EXCHANGE);
ASSERT_EQ(parseLen, *len);
frameType.keyExType = HITLS_KEY_EXCH_ECC;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_KEY_EXCHANGE_TC001
* @title After the client sends a certificate, the key exchange message sent by the client is different from the
* negotiated key exchange algorithm. As a result, the link fails to be established.
* @precon nan
* @brief 1. After the client sends a certificate, the key exchange message sent by the client must be consistent with
* the negotiated key exchange algorithm. Expected result 1.
* @expect 1. The expected handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_KEY_EXCHANGE_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
bool isModify = false;
RecWrapper wrapper = {
TRY_SEND_SERVER_KEY_EXCHANGE,
REC_TYPE_HANDSHAKE,
false,
&isModify,
Test_DiffServerKeyEx
};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CLIENTKXCH_VERSIONERR_TC001
* @title Test when the value of Client_Version on the server does not match that on the client.
* @precon nan
* @spec 1. "Client_Version: version number supported by the client. The server checks whether the value matches the
* value sent in the hello message from the client. random 46-byte random number"
* @brief 1. The server checks whether the Client_Version matches the value sent in the hello message from the client.
* Expected result 1.
* @expect 1. If the Client_Version of PreMasterSecret in the ClientKeyExChange message received by the server is
* different from the ClientHello message, the server reports an alarm indicating that the decryption fails
* and the handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CLIENTKXCH_VERSIONERR_TC001(char *cipherSuite)
{
FRAME_Init();
STUB_Init();
RegDefaultMemCallback();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite(cipherSuite);
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
FuncStubInfo stubInfo = {0};
STUB_Replace(&stubInfo, GenerateEccPremasterSecret, STUB_GenerateEccPremasterSecret);
int32_t ret = FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&stubInfo);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC001
* @title Violation of the rule that the signature certificate is before the encryption certificate is after, and
* check the result.
* @precon nan
* @spec 1. Server certificate: signature certificate is placed before the encryption certificate.
* @brief 1. Signature certificate before encryption certificate. Expected result 1.
* @expect 1. After the signature certificate encrypts the certificate after the server sends the certificate,
* the client reports an error indicating that the certificate verification fails and the handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2/ca.der",
"sm2/inter.der",
"sm2/sign.der",
"sm2/enc.der",
"sm2/sign.key.der",
"sm2/enc.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret;
ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_KEYUSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void TEST_SendUnexpectCertificateVerifyMsg(void *msg, void *data)
{
FRAME_Type *frameType = (FRAME_Type *)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_Msg newFrameMsg = {0};
HS_MsgType hsTypeTmp = frameType->handshakeType;
REC_Type recTypeTmp = frameType->recordType;
frameType->handshakeType = CERTIFICATE_VERIFY;
FRAME_Init();
FRAME_GetDefaultMsg(frameType, &newFrameMsg);
HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT); // recovery callback
frameType->handshakeType = hsTypeTmp;
frameType->recordType = recTypeTmp;
FRAME_CleanMsg(frameType, frameMsg);
frameType->recordType = REC_TYPE_HANDSHAKE;
frameType->handshakeType = CERTIFICATE_VERIFY;
frameType->keyExType = HITLS_KEY_EXCH_ECDHE;
if (memcpy_s(msg, sizeof(FRAME_Msg), &newFrameMsg, sizeof(newFrameMsg)) != EOK) {
Print("TEST_SendUnexpectCertificateMsg memcpy_s Error!");
}
}
static void TEST_UnexpectMsg(HLT_FrameHandle *frameHandle, TestExpect *testExpect, bool isSupportClientVerify)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
ALERT_Info alertInfo = {0};
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverConfig != NULL);
if (isSupportClientVerify) {
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, isSupportClientVerify) == 0);
}
HLT_Ctx_Config *clientConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(clientConfig, isSupportClientVerify) == 0);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLCP1_1, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Client Initialization
clientRes = HLT_ProcessTlsInit(localProcess, TLCP1_1, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(frameHandle != NULL);
frameHandle->ctx = clientRes->ssl;
HLT_SetFrameHandle(frameHandle);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), testExpect->connectExpect);
HLT_CleanFrameHandle();
ALERT_GetInfo(clientRes->ssl, &alertInfo);
ASSERT_TRUE(alertInfo.level == testExpect->expectLevel);
ASSERT_EQ(alertInfo.description, testExpect->expectDescription);
ASSERT_EQ(HLT_RpcGetTlsAcceptResult(serverRes->acceptId), testExpect->acceptExpect);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC002
* @title The server receives the certificate verify message when receiving the certificate.
* @precon nan
* @brief 1. Configure dual-end verification. Expected result 1.
* 2. Set the client callback type to certificate and replace it with certificate verify. Expected result 2.
* @expect 1. Expected success.
* 2. Expected server to return alert.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC002()
{
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
frameHandle.frameCallBack = TEST_SendUnexpectCertificateVerifyMsg;
frameHandle.expectHsType = CERTIFICATE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, true);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC003
* @title Dual-end verification. The server receives the CERTIFICATION_VERIFY message when expecting to receive the
* CLIENT_KEY_EXCHANGE message.
* @precon nan
* @brief 1. Configure unidirectional authentication. Expected result 1.
* 2. Set the client callback mode to send certificate verify. Expected result 2.
* @expect 1. Expected success.
* 2. Expected server to return alert.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC003()
{
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
frameHandle.frameCallBack = TEST_SendUnexpectCertificateVerifyMsg;
frameHandle.expectHsType = CLIENT_KEY_EXCHANGE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, true);
}
/* END_CASE */
// Replace the message to be sent with the CERTIFICATE.
static void TEST_SendUnexpectCertificateMsg(void *msg, void *data)
{
FRAME_Type *frameType = (FRAME_Type *)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_Msg newFrameMsg = {0};
HS_MsgType hsTypeTmp = frameType->handshakeType;
frameType->handshakeType = CERTIFICATE;
// Callback for changing the certificate algorithm, which is used to generate negotiation handshake messages.
FRAME_Init();
FRAME_GetDefaultMsg(frameType, &newFrameMsg);
HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT); // recovery callback
// Release the original msg.
frameType->handshakeType = hsTypeTmp;
FRAME_CleanMsg(frameType, frameMsg);
// Change message.
frameType->recordType = REC_TYPE_HANDSHAKE;
frameType->handshakeType = CERTIFICATE;
frameType->keyExType = HITLS_KEY_EXCH_ECDHE;
frameType->transportType = BSL_UIO_TCP;
if (memcpy_s(msg, sizeof(FRAME_Msg), &newFrameMsg, sizeof(newFrameMsg)) != EOK) {
Print("TEST_SendUnexpectCertificateMsg memcpy_s Error!");
}
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC004
* @title Single-end verification, indicating that the server receives the CERTIFICATION message.
* @precon nan
* @brief 1. Configure unidirectional authentication. Expected result 1.
* 2. Set the client severhello done callback mode to send certificate verify. Expected result 2.
* @expect 1. Expected success.
* 2. Expected server to return alert.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC004()
{
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
frameHandle.frameCallBack = TEST_SendUnexpectCertificateMsg;
frameHandle.expectHsType = CLIENT_KEY_EXCHANGE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, false);
}
/* END_CASE */
static void Test_ErrCertVerify(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
if (*(bool *)user) {
return;
}
*(bool *)user = true;
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
frameType.transportType = BSL_UIO_TCP;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
frameMsg.transportType = BSL_UIO_TCP;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_VERIFY);
ASSERT_EQ(parseLen, *len);
frameMsg.body.hsMsg.body.certificateVerify.sign.data[0]++;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC005
* @title Bidirectional verification. After the client sends a certificate, the certificate verify message contains
* an incorrect digital signature or does not contain a digital signature. As a result, the link fails to be
* established.
* @precon nan
* @brief 1. Start a handshake. Expected result 1
* 2. Modify the certificate verify message. Expected result 2
* @expect 1. Return success
* 2. Handshake fails
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC005()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
bool isModify = false;
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_VERIFY,
REC_TYPE_HANDSHAKE,
false,
&isModify,
Test_ErrCertVerify
};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC006
* @title Bidirectional verification. After the client sends a certificate, the certificate verify message contains
* an incorrect digital signature or does not contain a digital signature. As a result, the link fails to be
* established.
* @precon nan
* @brief 1. Start a handshake and set the ciphersuite to HITLS_ECC_SM4_CBC_SM3, Expected result 1
* 2. Modify the certificate verify message. Expected result 2
* @expect 1. Return success
* 2. Handshake fails
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CERTFICATE_TC006()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
bool isModify = false;
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_VERIFY,
REC_TYPE_HANDSHAKE,
false,
&isModify,
Test_ErrCertVerify
};
RegisterWrapper(wrapper);
uint16_t cipherSuite = HITLS_ECC_SM4_CBC_SM3;
HITLS_CFG_SetCipherSuites(config, &cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
config = HITLS_CFG_NewTLCPConfig();
client = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(config, BSL_UIO_TCP, false);
ASSERT_NE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
ClearWrapper();
}
/* END_CASE */
static int32_t SendCcs(HITLS_Ctx *ctx, uint8_t *data, uint8_t len)
{
/** Write records. */
return REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, data, len);
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CCS_TC006
* @title If an implementation detects a change_cipher_spec record received before the first ClientHello
* message or after the peer's Finished message, it MUST be treated as an unexpected record type
* @precon nan
* @brief 1. Establish a connection. Expected result 1
* 2. Send a CCS message to client. Expected result 2
* 3. Send a CCS message to server. Expected result 3
* @expect 1. Return success
* 2. client send ALERT_UNEXPECTED_MESSAGE alert
* 3. server send ALERT_UNEXPECTED_MESSAGE alert
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CCS_TC006(int isClient)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t data = 1;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
if (isClient != 0) {
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
} else {
memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE);
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_Finish_Len_TooLong(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
ASSERT_EQ(frameMsg.body.hsMsg.body.finished.verifyData.size, 12); // in RFC5246, length of verifyData is always 12.
frameMsg.body.hsMsg.body.finished.verifyData.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.finished.verifyData.data[0] = 0x00;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Finish_Len_TooLong_client(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
ASSERT_EQ(frameMsg.body.hsMsg.body.finished.verifyData.size, 12); // in RFC5246, length of verifyData is always 12.
if (ctx->isClient==true) {
frameMsg.body.hsMsg.body.finished.verifyData.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.finished.verifyData.data[0] += 1;
}
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_ERROR_FINISH_001
* @title An unexpected message is received when the server is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_ERROR_FINISH_001(void)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLCP_DTLCP11;
testInfo.uioType = BSL_UIO_TCP;
RecWrapper wrapper = {
TRY_SEND_FINISH,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Finish_Len_TooLong,
};
RegisterWrapper(wrapper);
testInfo.config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateTLCPLink(testInfo.config, testInfo.uioType, true);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateTLCPLink(testInfo.config, testInfo.uioType, false);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_ERROR_FINISH_002
* @title An unexpected message is received when the server is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_ERROR_FINISH_002(void)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLCP_DTLCP11;
testInfo.uioType = BSL_UIO_TCP;
RecWrapper wrapper = {
TRY_SEND_FINISH,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Finish_Len_TooLong_client,
};
RegisterWrapper(wrapper);
testInfo.config = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateTLCPLink(testInfo.config, testInfo.uioType, true);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateTLCPLink(testInfo.config, testInfo.uioType, false);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static int32_t GetDisorderServerCertAndKeyExchMsg(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t tmpData[READ_BUF_SIZE] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Accept(server->ssl);
int32_t ret = FRAME_TransportSendMsg(server->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_DISORDER_TC001
* @title Configure dual-ended verification and construct abnormal scenarios.
* @precon nan
* @spec The server should send a server certificate message to the client, which always follows the server hello
* message. If the selected cipher suite uses the RSA, ECC, or ECDHE algorithm, the message contains the
* signature certificate and encryption certificate of the server.
* @brief 1. Configure dual-end verification and construct an abnormal scenario. After the serverhello message is sent,
* the sequence of the certificate message and serverkeyexchange message is changed. Expected result 1.
* @expect 1. Client returns an unexpected alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_DISORDER_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_CERTIFICATE), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(client->ssl->hsCtx->state, TRY_RECV_CERTIFICATE);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerCertAndKeyExchMsg(server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(ioUserData->recMsg.len != 0);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_DISORDER_TC002
* @title During the handshake, the client receives the CCS when the client is receiving the clientkeyexchange.
* @precon nan
* @brief 1. Configure the single-end authentication. After the server sends the serverhellodone message, the client
* stops in the try send client key exchange state. Expected result 1.
* 2. Construct an unexpected CCS message and send it to the server. Expected result 2.
* @expect 1. The initialization succeeds.
* 2. The connection fails to be established and the server returns an unexpected message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_DISORDER_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECC_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE) == HITLS_SUCCESS);
uint32_t sendLen = 6;
uint8_t sendBuf[6] = {0x14, 0x01, 0x01, 0x00, 0x01, 0x01};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(server->ssl->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_DISORDER_TC003
* @title During the handshake, the clientkeyexchange message is received when the client status is receiving the
* certificate.
* @precon nan
* @brief 1. Configure dual-end authentication. After the server sends a serverhellodone message, the client stops in
* the try send certificate state. Expected result 1.
* 2. Construct an unexpected clientkeyexchange message and send the message to the server. Expected result 2.
* @expect 1. The initialization succeeds.
* 2. The connection fails to be established and the server returns an unexpected message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_DISORDER_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t STUB_APP_Write_Fatal(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t *writeLen)
{
(void)data;
(void)dataLen;
(void)writeLen;
ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE);
return HITLS_INTERNAL_EXCEPTION;
}
/* @
* @test UT_TLS_TLCP_CONSISTENCY_FATAL_ALERT_TC003
* @title Session processing in the case of critical alarms.
* @precon nan
* @spec When a critical alarm is sent or received, both parties should immediately close the connection and discard
* the session ID and key of the incorrect connection. The connection closed by the critical alarm cannot be
* reused.
* @brief 1. Sending a fatal alert. Expected result 1.
* 2. The server sends a fatal alert, the session information is used for the next connection.
* Expected result 2.
* @expect 1. The server fails to send data and receive data.
* 2. The connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_FATAL_ALERT_TC003(char *cipherSuite, int isResume)
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite(cipherSuite);
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
FuncStubInfo tmpRpInfo = { 0 };
uint8_t readBuf[READ_BUF_SIZE] = {0};
HITLS_Session *Newsession = NULL;
HITLS_Session *serverSession = NULL;
uint32_t readLen = 0;
uint8_t data[] = "Hello World";
STUB_Replace(&tmpRpInfo, APP_Write, STUB_APP_Write_Fatal);
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, data, sizeof(data), &writeLen), HITLS_INTERNAL_EXCEPTION);
STUB_Reset(&tmpRpInfo);
ASSERT_TRUE(server->ssl->state == CM_STATE_ALERTED);
if (isResume == 1) {
serverSession = HITLS_GetDupSession(server->ssl);
ASSERT_TRUE(serverSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, serverSession), 0);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
Newsession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(memcmp(serverSession->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) != 0);
} else {
ASSERT_TRUE(HITLS_Write(server->ssl, data, sizeof(data), &writeLen) == HITLS_CM_LINK_FATAL_ALERTED);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_FATAL_ALERTED);
}
ASSERT_TRUE(HITLS_Close(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_CLOSED);
EXIT:
HITLS_SESS_Free(Newsession);
HITLS_SESS_Free(serverSession);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC001
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a connection between the client and server. Expected result 1.
* 2. The client closes the link, obtains the message sent by the client, and checks whether the message is a
* close_notify message. Expected result 2.
* 3. The server obtains the received message and checks whether the message is a close_notify message.
* Expected result 3.
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server receives the close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC002
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a link between the client and server. Expected result 1.
* 2. The client closes the link, obtains the message sent by the client, and checks whether the message is a
* close_notify message. Expected result 2.
* 3. The server processes the message received by the server, obtains the message to be sent after processing,
* and checks whether the message is a close_notify message. Expected result 3.
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server sends a close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(server->ssl != NULL);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_IO_BUSY);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_IO_BUSY);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_CM_LINK_FATAL_ALERTED);
FRAME_Msg serverframeMsg1 = {0};
uint8_t *serverbuffer1 = serverioUserData->sndMsg.msg;
uint32_t serverreadLen1 = serverioUserData->sndMsg.len;
uint32_t serverparseLen1 = 0;
ret = ParserTotalRecord(server, &serverframeMsg1, serverbuffer1, serverreadLen1, &serverparseLen1);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg1.type == REC_TYPE_ALERT && serverframeMsg1.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg1.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg1.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC003
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a connection between the client and server. Expected result 1.
* 2. The server closes the link, obtains the message sent by the server,
* and checks whether the message is a close_notify message. Expected result 2.
* 3. Obtain the received message and check whether the message is a close_notify message. Expected result 3.
* @expect 1. The link is successfully established.
* 2. The server sends a close_notify message.
* 3. The client receives the close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC004
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a link between the client and server. Expected result 1.
* 2. The server closes the link, obtains the message sent by the server,
* and checks whether the message is a close_notify message. Expected result 2.
* 3. The client processes the received message, obtains the message to be sent after processing,
* and checks whether the message is a close_notify message. Expected result 3.
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server sends a close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_CLOSE_NOTIFY_TC004(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite("HITLS_ECDHE_SM4_CBC_SM3");
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
clientioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(client->ssl != NULL);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_IO_BUSY);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_CM_LINK_FATAL_ALERTED);
FRAME_Msg clientframeMsg1 = {0};
uint8_t *clientbuffer1 = clientioUserData->sndMsg.msg;
uint32_t clientreadLen1 = clientioUserData->sndMsg.len;
uint32_t clientparseLen1 = 0;
ret = ParserTotalRecord(client, &clientframeMsg1, clientbuffer1, clientreadLen1, &clientparseLen1);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg1.type == REC_TYPE_ALERT);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_WARNING);
ASSERT_EQ(alert.description, ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_AMEND_APPDATA_TC001
* @title Modify the app message received by the client, modify the encrypted data, inject the message,
* and observe the response from the client.
* @precon nan
* @spec "AEADEncrypted = AEAD-Encrypt(write_ key, nonce, plaintext, additional data) To decrypt and verify,
* the cryptographic algorithm uses the key, random number, additional_data and AEADEncrypted values are used
* as inputs. The output is plaintext or an error indicating a decryption failure. There is no additional
* integrity check. I.e.: TLSCompressed. fragment= AEAD-Decrypt (write_key, nonce,AEADEncrypted,
* additional_data) A fatal bad_record_mac warning should be generated if decryption fails. See Appendix A for
* the GCM Authenticated Encryption Mode."
* @brief 1. Set up a link, read and write data, modify the app message received by the client, modify the encrypted
* data, and perform message injection. Observe the response from the client. Expected result 1.
* 2. Set up a link, read and write data, modify the app message received by the server, modify the encrypted
* data, and perform message injection. Observe the response of the server. Expected result 2.
* @expect 1. The bad_record_mac alert is sent.
* 2. The bad_record_mac alert is sent.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_AMEND_APPDATA_TC001(char *cipherSuite, int isClient)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite(cipherSuite);
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
FRAME_LinkObj *client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
FRAME_LinkObj *recver = isClient ? client : server;
FRAME_LinkObj *sender = isClient ? server : client;
uint8_t data[] = "Hello World";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
uint32_t writeLen = 0;
ASSERT_EQ(HITLS_Write(sender->ssl, data, sizeof(data), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(sender, recver) == HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(recver->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
frameType.recordType = REC_TYPE_APP;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_AppMsg *appMsg = &frameMsg.body.appMsg;
uint8_t appData[] = "123";
appMsg->appData.state = ASSIGNED_FIELD;
ASSERT_EQ(memcpy_s(appMsg->appData.data, appMsg->appData.size, "123", sizeof(appData)), 0);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(recver->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_EQ(HITLS_Read(recver->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(recver->ssl, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
ASSERT_TRUE(HITLS_Close(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_CLOSED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_FRAME_FUNC_TLCP_CERT_MISMATCH_TC001
* @title After receiving the servehellodone message from the server, the client checks whether the server certificate
* is valid and whether the parameters in the servehello message are acceptable.
* @precon nan
* @spec After receiving the servehellodone message from the server, the client verifies whether the server
* certificate is valid and whether the server's servehello message parameters are acceptable. If acceptable,
* the client continues the handshake process. Otherwise, a HandShakeFailure fatal alarm is sent.
* @brief 1. Set the algorithm suite to ECDHE_SM4_CBC_SM3 on the client and server, and load the RSA certificate to
* the server. Expected result 1.
* 2. Set the algorithm suite to ECC_SM4_CBC_SM3 on the client and server, and load the RSA certificate to
* the server. Expected result 2.
* @expect 1. The server fails to negotiate the cipher suite (the certificate and cipher suite do not match).
* 2. The server fails to negotiate the cipher suite (the certificate and cipher suite do not match).
@ */
/* BEGIN_CASE */
void UT_FRAME_FUNC_TLCP_CERT_MISMATCH_TC001(char *cipherSuite)
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2/ca.der",
"sm2/inter.der",
"rsa_sha256/server.der",
"sm2/sign.der",
"rsa_sha256/server.key.der",
"sm2/sign.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t toSetCipherSuite = GetCipherSuite(cipherSuite);
HITLS_CFG_SetCipherSuites(tlsConfig, &toSetCipherSuite, 1);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC001
* @title The encryption certificate does not have a keyusage extension, and check the result.
* @precon nan
* @brief 1. Use the default configuration on the client and server,Server setting encryption certificate without
* keyusage extension. Expected result 1.
* 2. Start a handshake. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_BAD_CERTIFICATE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2_cert/root.der",
"sm2_cert/intCa.der",
"sm2_cert/server_enc_no_keyusage.der",
"sm2_cert/server_sign.der",
"sm2_cert/server_enc_no_keyusage.key.der",
"sm2_cert/server_sign.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_EXP_CERT);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC002
* @title The signing certificate does not have a keyusage extension, and check the result.
* @precon nan
* @brief 1. Use the default configuration on the client and server,Server setting signing certificate without
* keyusage extension. Expected result 1.
* 2. Start a handshake. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_BAD_CERTIFICATE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2_cert/root.der",
"sm2_cert/intCa.der",
"sm2_cert/server_enc.der",
"sm2_cert/server_sign_no_keyusage.der",
"sm2_cert/server_enc.key.der",
"sm2_cert/server_sign_no_keyusage.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_KEYUSAGE);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC003
* @title The signature certificate has an incorrect keyusage extension, and check the result.
* @precon nan
* @brief 1. Use the default configuration on the client and server,The server's signature certificate contains an
* incorrect keyusage extension. Expected result 1.
* 2. Start a handshake. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_BAD_CERTIFICATE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2_cert/root.der",
"sm2_cert/intCa.der",
"sm2_cert/server_enc.der",
"sm2_cert/client_sign_err_keyusage.der",
"sm2_cert/server_enc.key.der",
"sm2_cert/client_sign_err_keyusage.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_KEYUSAGE);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC004
* @title The encryption certificate has an incorrect keyusage extension, and check the result.
* @precon nan
* @brief 1. Use the default configuration on the client and server,The server's encryption certificate contains an
* incorrect keyusage extension. Expected result 1.
* 2. Start a handshake. Expected result 2.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_BAD_CERTIFICATE message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLCP_CONSISTENCY_KEYUSAGE_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"sm2_cert/root.der",
"sm2_cert/intCa.der",
"sm2_cert/client_enc_err_keyusage.der",
"sm2_cert/server_sign.der",
"sm2_cert/client_enc_err_keyusage.key.der",
"sm2_cert/server_sign.key.der",
};
tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_EXP_CERT);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tlcp/test_suite_sdv_frame_tlcp_consistency_3.c | C | unknown | 69,427 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "process.h"
#include "session_type.h"
#include "hitls_type.h"
#include "send_process.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "uio_base.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "app.h"
#include "hlt.h"
#include "alert.h"
#include "securec.h"
#include "record.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "cert_callback.h"
#include "change_cipher_spec.h"
#include "common_func.h"
#include "crypt_util_rand.h"
/* END_HEADER */
static uint32_t g_uiPort = 16888;
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
#define REC_CONN_SEQ_SIZE 8u /* SN size */
#define PORT 11111
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isServerExtendMasterSecret;
bool isSupportRenegotiation; /* Renegotiation support flag */
bool needStopBeforeRecvCCS; /* For CCS test, stop at TRY_RECV_FINISH stage before CCS message is received. */
} HandshakeTestInfo;
int32_t GetSessionCacheMode(HLT_Ctx_Config* config)
{
return config->setSessionCache;
}
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC001
* @title Modify the resume flag on the client. Resumption fails
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Modify the resume flag and resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. Resumption fails
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC001(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
int32_t cachemode = 0;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
cachemode = GetSessionCacheMode(clientCtxConfig);
ASSERT_EQ(cachemode , HITLS_SESS_CACHE_SERVER);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
SESS_Disable(session);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == false);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt == 2)
{
HITLS_Session *Newsession = NULL;
Newsession = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(memcmp(session->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) != 0);
HITLS_SESS_Free(Newsession);
} else {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC002
* @title During session resumption, set none cipher suite. The resumption fails
* @precon nan
* @brief 1. Establish a connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. During session resumption, do not set the cipher suite and resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. Failed to resume the session.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC002(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
uint16_t sess_Ciphersuite;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL ) {
HITLS_SESS_GetCipherSuite(session, &sess_Ciphersuite);
ASSERT_TRUE(HITLS_SESS_SetCipherSuite(session, 0) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_EQ(HLT_TlsConnect(clientSsl), HITLS_MSG_HANDLE_ILLEGAL_CIPHER_SUITE);
} else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
}
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt == 1) {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC003
* @title Session resume succeed
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. The client carries the session ID for first connection establishment and resumes the session.
The server sends the same session ID in the hello message. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. The session is resumed successfully
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC003(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_EQ(HLT_TlsConnect(clientSsl), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_TRUE(isReused == 1);
} else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
}
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt == 1) {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC004
* @title Use same session to resume two connections
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Use same session to resume two different connections at the same time. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. The session is resumed successfully on both connections at the same time
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC004(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HLT_FD sockFd2 = {0};
int count = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
void *clientConfig2 = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(clientConfig2 != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *clientCtxConfig2 = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig2, clientCtxConfig2) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (session != NULL) {
DataChannelParam channelParam2;
channelParam2.port = PORT;
channelParam2.type = connType;
channelParam2.isBlock = true;
sockFd2 = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam2);
ASSERT_TRUE((sockFd2.srcFd > 0) && (sockFd2.peerFd > 0));
remoteProcess->connType = connType;
localProcess->connType = connType;
remoteProcess->connFd = sockFd2.peerFd;
localProcess->connFd = sockFd2.srcFd;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig2);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
HITLS_Session *Newsession = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(Newsession != NULL);
ASSERT_TRUE(memcmp(session->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) == 0);
HITLS_SESS_Free(Newsession);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
HITLS_SESS_Free(session);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
if (count == 2) {
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_TRUE(isReused == 1);
}
count++;
} while (count <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC005
* @title Multiple connections can be established using the same session
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Use same session to resume three connections. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. The sessions are all resumed successfully
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC005(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
int32_t cachemode = 0;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
cachemode = GetSessionCacheMode(clientCtxConfig);
ASSERT_EQ(cachemode , HITLS_SESS_CACHE_SERVER);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt != 1) {
HITLS_Session *Newsession = NULL;
Newsession = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(memcmp(session->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) == 0);
HITLS_SESS_Free(Newsession);
} else {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt <= 4);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC006
* @title Modify the session ID on the client. Resumption fails
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Modify the session ID and resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. Resumption fails
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC006(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
int32_t cachemode = 0;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
cachemode = GetSessionCacheMode(clientCtxConfig);
ASSERT_EQ(cachemode , HITLS_SESS_CACHE_SERVER);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
session->sessionId[0] -= 1;
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt == 2) {
HITLS_Session *Newsession = NULL;
Newsession = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(memcmp(session->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) != 0);
HITLS_SESS_Free(Newsession);
} else {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC007
* @title Modify the session cipher suite on the client. Resumption fails
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Modify the session cipher suite and resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. Resumption fails
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC007(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
int32_t cachemode = 0;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
uint16_t sess_Ciphersuite;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
cachemode = GetSessionCacheMode(clientCtxConfig);
ASSERT_EQ(cachemode , HITLS_SESS_CACHE_SERVER);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (cnt == 1) {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
} else {
HITLS_SESS_GetCipherSuite(session, &sess_Ciphersuite);
if(sess_Ciphersuite == HITLS_ECC_SM4_CBC_SM3) {
ASSERT_TRUE(HITLS_SESS_SetCipherSuite(session, HITLS_ECDHE_SM4_CBC_SM3) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(HITLS_SESS_SetCipherSuite(session, HITLS_ECC_SM4_CBC_SM3) == HITLS_SUCCESS);
}
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == HITLS_MSG_HANDLE_ILLEGAL_CIPHER_SUITE);
}
cnt++;
} while (cnt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC008
* @title Modify the session master key on the client. Resumption fails
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Modify the session master key and resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. Resumption fails
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC008(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
int32_t cachemode = 0;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
cachemode = GetSessionCacheMode(clientCtxConfig);
ASSERT_EQ(cachemode , HITLS_SESS_CACHE_SERVER);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (session != NULL) {
session->masterKey[0] -= 1;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (cnt == 1) {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
} else {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_EQ(HLT_TlsConnect(clientSsl), HITLS_REC_BAD_RECORD_MAC);
}
cnt++;
} while (cnt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_TRANSPORT_FUNC_TC01(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLCP1_1, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLCP1_1, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
uint8_t writeBuf[READ_BUF_SIZE] = {0};
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, writeBuf, 16384) == 0);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == 16384);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC009
* @title set the session cache mode on the client server. try to Resumption
* @precon nan
* @brief 1. Configure the session cache mode establish the connection. Expected result 1
2. Perform the first handshake, obtain and save the session. Expected result 2
3. Try resume the session. Expected result 3
* @expect 1. Return success
2. The handshake is complete and obtain the session successfully
3. HITLS_SESS_CACHE_NO and HITLS_SESS_CACHE_CLIENT resumption fails, otherwise successful
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP_CONSISTENCY_RESUME_FUNC_TC009(int mode)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int cnt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(TLCP1_1);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, TLCP1_1, false, serverCtxConfig->providerPath,
serverCtxConfig->providerNames, serverCtxConfig->providerLibFmts, serverCtxConfig->providerCnt,
serverCtxConfig->attrName);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, TLCP1_1, false);
#endif
HLT_SetSessionCacheMode(clientCtxConfig, mode);
HLT_SetSessionCacheMode(serverCtxConfig, mode);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do{
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = TCP;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = TCP;
localProcess->connType = TCP;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = TCP;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = TCP;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_EQ(HLT_TlsConnect(clientSsl) , 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt == 2) {
if (mode == HITLS_SESS_CACHE_NO || mode == HITLS_SESS_CACHE_CLIENT){
uint8_t isReused = -1;
HITLS_IsSessionReused(clientSsl, &isReused);
ASSERT_TRUE(isReused == 0);
} else {
uint8_t isReused = -1;
HITLS_IsSessionReused(clientSsl, &isReused);
ASSERT_TRUE(isReused == 1);
}
} else {
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
}cnt++;
}while(cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
#define ALERT_BODY_LEN 2u /* Alert data length */
static int32_t SendAlert(HITLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
uint8_t data[ALERT_BODY_LEN];
/** Obtain the alert level. */
data[0] = level;
data[1] = description;
/** Write records. */
return REC_Write(ctx, REC_TYPE_ALERT, data, ALERT_BODY_LEN);
}
/* BEGIN_CASE */
void SDV_TLS_TLCP1_1_LEVEL_UNKNOWN_ALERT_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportClientVerify = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CLIENT_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(SendAlert(client->ssl, ALERT_LEVEL_UNKNOWN, ALERT_NO_RENEGOTIATION) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
HITLS_Ctx *Ctx = FRAME_GetTlsCtx(server);
ALERT_Info alert = { 0 };
ALERT_GetInfo(Ctx, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_TLCP1_1_LEVEL_UNKNOWN_ALERT_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportClientVerify = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(SendAlert(server->ssl, ALERT_LEVEL_UNKNOWN, ALERT_DECODE_ERROR) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
HITLS_Ctx *Ctx = FRAME_GetTlsCtx(client);
ALERT_Info alert = { 0 };
ALERT_GetInfo(Ctx, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test SDV_TLS_TLCP1_1_TRANSPORT_FUNC_TC01
* @title decrypt the app data with length 16384.
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Perform the a handshake. Expected result 2
3. Send the app data with length 16384. Expected result 3
* @expect 1. Return success
2. The handshake is complete
3. The app data is decrypted successfully
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP1_1_TRANSPORT_FUNC_TC01(void)
{
CRYPT_RandRegist(TestSimpleRand);
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLCP1_1, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLCP1_1, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
uint8_t writeBuf[READ_BUF_SIZE] = {0};
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, writeBuf, 16384) == 0);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == 16384);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
static void TEST_Client_SessionidLength_TooLong(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLCP_DTLCP11;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLCP_DTLCP11;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->sessionIdSize.data = 33;
BSL_SAL_Free(clientMsg->sessionId.data);
clientMsg->sessionId.data = BSL_SAL_Calloc(33, sizeof(uint8_t));
clientMsg->sessionId.size = 0;
clientMsg->sessionId.state = ASSIGNED_FIELD;
const uint8_t sessionId_temp[33] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
ASSERT_TRUE(memcpy_s(clientMsg->sessionId.data, sizeof(sessionId_temp) / sizeof(uint8_t),
sessionId_temp, sizeof(sessionId_temp) / sizeof(uint8_t)) == 0);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test SDV_TLS_TLCP1_1_RESUME_FAILED_TC001
* @title test wrong session id length client hello.
* @precon nan
* @brief 1. Establish the connection. Expected result 1
2. Client send a client hello with wrong session id length. Expected result 2
* @expect 1. Return success
2. Server return HITLS_PARSE_INVALID_MSG_LEN error
@ */
/* BEGIN_CASE */
void SDV_TLS_TLCP1_1_RESUME_FAILED_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 8888, false);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientConfig != NULL);
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
TEST_Client_SessionidLength_TooLong
};
RegisterWrapper(wrapper);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLCP1_1, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLCP1_1, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes) , HITLS_PARSE_INVALID_MSG_LEN);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tlcp/test_suite_sdv_hlt_tlcp_consistency.c | C | unknown | 55,410 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "cert_callback.h"
#include "change_cipher_spec.h"
#include "common_func.h"
#include "uio_base.h"
#include "hs.h"
#include "stub_crypt.h"
/* END_HEADER */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC001
* @title Verify that the server receives a 0-length Client Hello message and the expected alert is returned.
* @precon nan
* @brief 1. Create a config and client link, and construct a 0-length client hello message.
* Expected result 1 is obtained.
* 2. The server invokes the HITLS_Accept interface. (Expected result 2)
* @expect 1. A success message is returned.
* 2. A failure message is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC001(void)
{
// Create a config and client link, and construct a 0-length client hello message.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extensionState = MISSING_FIELD;
clientMsg->version.state = MISSING_FIELD;
clientMsg->randomValue.state = MISSING_FIELD;
clientMsg->sessionIdSize.state = MISSING_FIELD;
clientMsg->sessionId.state = MISSING_FIELD;
clientMsg->cookiedLen.state = MISSING_FIELD;
clientMsg->cookie.state = MISSING_FIELD;
clientMsg->cipherSuitesSize.state = MISSING_FIELD;
clientMsg->cipherSuites.state = MISSING_FIELD;
clientMsg->compressionMethodsLen.state = MISSING_FIELD;
clientMsg->compressionMethods.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
// The server invokes the HITLS_Accept interface.
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC002
* @title Verify that the client receives a serverhello message with a length of 0 and the expected alert is returned.
* @precon nan
* @brief 1. Create a config and server link, and construct a 0-length server hello message.
* Expected result 1 is obtained.
* 2. The client invokes the HITLS_Connect interface. (Expected result 2)
* @expect 1. A success message is returned.
* 2. A failure message is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC002(void)
{
// Create a config and server link, and construct a 0-length server hello message.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->version.state = MISSING_FIELD;
serverMsg->randomValue.state = MISSING_FIELD;
serverMsg->sessionIdSize.state = MISSING_FIELD;
serverMsg->sessionId.state = MISSING_FIELD;
serverMsg->cipherSuite.state = MISSING_FIELD;
serverMsg->compressionMethod.state = MISSING_FIELD;
serverMsg->extensionLen.state = MISSING_FIELD;
serverMsg->pointFormats.exState = MISSING_FIELD;
serverMsg->extendedMasterSecret.exState = MISSING_FIELD;
serverMsg->secRenego.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* The client invokes the HITLS_Connect interface. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC003
* @title The client receives a Certificate message with a length of zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained
* 2. Construct a zero-length Certificate message and send it to the client. Expected result 2 is obtained
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_Level_FATAL and description of ALERT_DECODE_ERROR
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC003(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Construct a zero-length Certificate message and send it to the client. */
FRAME_CertificateMsg *certifiMsg = &frameMsg.body.hsMsg.body.certificate;
certifiMsg->certsLen.state = MISSING_FIELD;
certifiMsg->certItem->state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC004
* @title The client receives a Server Key Exchange message whose length is 0.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained
* 2. Construct a zero-length Server Key Exchange message and send it to the client.
* Expected result 2 is obtained
* @expect 1. The initialization is successful
* 2. The client sends an ALERT message. The level is ALERT_LEVEL_FATAL and the description is ALERT_DECODE_ERROR
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC004(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Construct a zero-length Server Key Exchange message and send it to the client. */
FRAME_ServerKeyExchangeMsg *serverKeyExMsg = &frameMsg.body.hsMsg.body.serverKeyExchange;
serverKeyExMsg->keyEx.ecdh.curveType.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.namedcurve.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.pubKeySize.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.pubKey.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signAlgorithm.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signSize.state = MISSING_FIELD;
serverKeyExMsg->keyEx.ecdh.signData.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC005
* @title The server receives a Client Key Exchange message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a zero-length Client Key Exchange message and send it to the server.
* Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message. The level is ALERT_LEVEL_FATAL and the description is ALERT_DECODE_ERROR
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC005(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Construct a zero-length Client Key Exchange message and send it to the server.
FRAME_ClientKeyExchangeMsg *clientKeyExMsg = &frameMsg.body.hsMsg.body.clientKeyExchange;
clientKeyExMsg->pubKey.state = MISSING_FIELD;
clientKeyExMsg->pubKeySize.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC006
* @title The server receives a Change Cipher Spec message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained
* 2. Construct a Change Cipher Spec message with zero length and send it to the server.
* Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC006(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CERTIFICATE_VERIFY;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg1 = { 0 };
FRAME_Type frameType1 = { 0 };
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType1.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
// Construct a Change Cipher Spec message with zero length and send it to the server.
FRAME_CcsMsg *CcsMidMsg = &frameMsg1.body.ccsMsg;
CcsMidMsg->ccsType.state = MISSING_FIELD;
CcsMidMsg->extra.state = MISSING_FIELD;
CcsMidMsg->extra.size = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType1, &frameMsg1);
memset_s(&frameMsg1, sizeof(frameMsg1), 0, sizeof(frameMsg1));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC007
* @title The client receives a Change Cipher Spec message with a length of 0 and the data is encrypted.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained
* 2. Construct a Change Cipher Spec message with zero length and send it to the client.
* Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC007(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_NEW_SESSION_TICKET;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg1 = { 0 };
FRAME_Type frameType1 = { 0 };
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType1.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
// Construct a Change Cipher Spec message with zero length and send it to the client.
FRAME_CcsMsg *CcsMidMsg = &frameMsg1.body.ccsMsg;
CcsMidMsg->ccsType.state = MISSING_FIELD;
CcsMidMsg->extra.state = MISSING_FIELD;
CcsMidMsg->extra.size = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType1, &frameMsg1);
memset_s(&frameMsg1, sizeof(frameMsg1), 0, sizeof(frameMsg1));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_AlertMsg *alertMsg = &frameMsg1.body.alertMsg;
ASSERT_EQ(alertMsg->alertLevel.data , 0);
ASSERT_EQ(alertMsg->alertDescription.data , ALERT_CLOSE_NOTIFY);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC008
* @title The client receives a SERVER_HELLO_DONE message whose length is 0 and expects to return an alert message
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained
* 2. Construct a SERVER_HELLO_DONE message with a zero length, and send the message to the client.
* Expected result 2 is obtained
* @expect 1. The initialization is successful
* 2. The client sends an ALERT message
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC008(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO_DONE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO_DONE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Construct a SERVER_HELLO_DONE message with a zero length, and send the message to the client.
FRAME_ServerHelloDoneMsg *serverHelloDone = &frameMsg.body.hsMsg.body.serverHelloDone;
serverHelloDone->extra.state = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC001
* @title An unexpected message is received when the client is in the TRY_RECV_CERTIFICATIONATE
* state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message.
* The level is ALERT_LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC001(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FRAME_Msg parsedSHdone = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_HELLO_DONE, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &parsedSHdone) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSHdone, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &parsedSHdone);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &parsedSHdone, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedSHdone.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedSHdone.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSHdone);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC002
* @title An unexpected message is received when the client is in the TRY_RECV_CERTIFICATIONATE state
* during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message.
* The level is ALERT_LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC002(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC003
* @title An unexpected message is received when the client is in the TRY_RECV_SERVER_KEY_EXCHANGE state
* during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message.
* The level is ALERT_LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC003(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_KEY_EXCHANGE;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC004
* @title An unexpected message is received when the client is in the TRY_RECV_SERVER_HELLO_DONE state
* during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message. The level is
* ALERT_LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC004(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO_DONE;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC005
* @title An unexpected message is received when the client is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message.
* The level is ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC005(void)
{
// Use the default configuration items to configure the client and server.
// Construct a Server Hello message and send it to the client.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg parsedAlert = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
testInfo.client->ssl->hsCtx->state = TRY_RECV_FINISH;
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf, sndLen, &parsedAlert, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC006
* @title An unexpected message is received when the client is in the TRY_RECV_CERTIFICATE_REQUEST
* state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT.
* The level is ALERT_LEVEL_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC006(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CERTIFICATE_REQUEST;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC007
* @title An unexpected message is received when the client is in the TRY_RECV_NEW_SESSION_TICKET state
* during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a Server Hello message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the Server Hello message, the client sends an ALERT message.
* The level is ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC007(void)
{
// Use the default configuration items to configure the client and server.
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// Construct a Server Hello message and send it to the client.
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.client->ssl->hsCtx->state = TRY_RECV_NEW_SESSION_TICKET;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC008
* @title An unexpected message is received when the server is in the TRY_RECV_CLIENT_HELLO state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT.
* The level is ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC008(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Construct a CLIENT_KEY_EXCHANGE message and send it to the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.server->ssl->hsCtx->state = TRY_RECV_CLIENT_HELLO;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC009
* @title An unexpected message is received when the server is in the TRY_RECV_CERTIFICATIONATE
* state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC009(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Construct a CLIENT_KEY_EXCHANGE message and send it to the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC010
* @title An unexpected message is received when the server is in TRY_RECV_CLIENT_KEY_EXCHANGE state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a SERVER_HELLO message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the SERVER_HELLO message, the server sends an ALERT. The level is ALERT_Level_FATAL and the
* description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC010(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Construct a SERVER_HELLO message and send it to the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.server->ssl->hsCtx->state = TRY_RECV_CLIENT_KEY_EXCHANGE;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC011
* @title An unexpected message is received when the server is in the TRY_RECV_CERTIFICATIONATE_VERIFY state during the
* handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
* ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC011(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Construct a CLIENT_KEY_EXCHANGE message and send it to the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE_VERIFY;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC012
* @title An unexpected message is received when the server is in the TRY_RECV_FINISH state during the handshake.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a CLIENT_KEY_EXCHANGE message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CLIENT_KEY_EXCHANGE message, the server sends an ALERT message. The level is
ALERT_Level_FATAL and the description is ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_TC012(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Construct a CLIENT_KEY_EXCHANGE message and send it to the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
testInfo.server->ssl->hsCtx->state = TRY_RECV_FINISH;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_AEAD_EXPLICIT_IV_LENGTH_TC001
* @title The client and server establish a connection. Check whether the sequence number in the APP message is
* contained in the record-layer message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. A TLS over TCP link is established between the client and server. Expected result 2 is obtained.
* 3. Randomly generate a 32-bit transmission data. Expected result 3 is obtained.
* 4. Randomly generate a serial number. Expected result 4 is obtained.
* 5. Write app data to the server.
* 6. Data transmission at the record layer.
* 7. Check the changes before and after the sequence number is sent.
* 8. Record layer data receiving.
* @expect 1. The initialization is successful.
* 2. The link is set up successfully.
* 3. The generation is successful.
* 4. The generation is successful.
* 5. The writing is successful.
* 6. Transmission is successful.
* 7. After the sending, the seqNum is increased by 1.
* 8. The data length and data content are verified successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_AEAD_EXPLICIT_IV_LENGTH_TC001()
{
/* Use the default configuration items to configure the client and server. */
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HandshakeTestInfo testInfo = { 0 };
testInfo.state = HS_STATE_BUTT;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* A TLS over TCP link is established between the client and server. */
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_TRANSPORTING);
/* Randomly generate a 32-bit transmission data. */
uint8_t transportData[REC_CONN_SEQ_SIZE * 4] = {0};
uint32_t transportDataLen = sizeof(transportData) / sizeof(uint8_t);
ASSERT_EQ(RandBytes(transportData, transportDataLen), HITLS_SUCCESS);
/* Randomly generate a serial number. */
uint8_t randSeq[REC_CONN_SEQ_SIZE] = {0};
ASSERT_EQ(RandBytes(randSeq, REC_CONN_SEQ_SIZE), HITLS_SUCCESS);
REC_Ctx *recCtx = (REC_Ctx *)testInfo.server->ssl->recCtx;
recCtx->writeStates.currentState->seq = BSL_ByteToUint64(randSeq);
uint64_t sequenceNumber = recCtx->writeStates.currentState->seq;
uint8_t seq[REC_CONN_SEQ_SIZE] = {0};
BSL_Uint64ToByte(sequenceNumber, seq);
/* Write app data to the server. */
uint32_t writeLen;
int32_t ret = APP_Write(testInfo.server->ssl, transportData, transportDataLen, &writeLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
/* Data transmission at the record layer. */
uint8_t tmpData[MAX_RECORD_LENTH] = {0};
uint32_t tmpLen;
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.server->io, tmpData, MAX_RECORD_LENTH, &tmpLen) == HITLS_SUCCESS);
/* Check the changes before and after the sequence number is sent. */
recCtx = (REC_Ctx *)testInfo.server->ssl->recCtx;
uint64_t sequenceNumberAfterSend = recCtx->writeStates.currentState->seq;
ASSERT_EQ(sequenceNumberAfterSend, sequenceNumber + 1);
/* Record layer data receiving. */
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmpData, tmpLen) == HITLS_SUCCESS);
const int32_t AEAD_TAG_LEN = 16u;
ASSERT_EQ(tmpLen, REC_TLS_RECORD_HEADER_LEN + REC_CONN_SEQ_SIZE + transportDataLen + AEAD_TAG_LEN);
ASSERT_TRUE(memcmp(tmpData + REC_TLS_RECORD_HEADER_LEN, seq, REC_CONN_SEQ_SIZE) == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_READ_PENDING_STATE_TC001
* @title Observe the changes in the status of the record layer before and after the client receives the CCS message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client to stop the client in the TRY_RECV_FINISH
state. Expected result 1 is obtained.
* 2. Record the states at the record layer that the client reads. Expected result 2 is obtained.
* 3. Reconnect the client. Expected result 3 is obtained.
* 4. Check the states at the record layer after the client is reconnected. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
* 2. The outdatedState field is empty.
* 3. The return value for reconnection is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* 4. OutdatedState is the previous currentState.
* currentState is the previous pendingState.
* The pendingState field is empty.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_READ_PENDING_STATE_TC001()
{
/* Use the default configuration items to configure the client to stop the client in the TRY_RECV_FINISH state. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = true;
testInfo.state = TRY_RECV_FINISH;
testInfo.needStopBeforeRecvCCS = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Record the states at the record layer that the client reads. */
RecConnStates *readStates = (RecConnStates *)&(testInfo.client->ssl->recCtx->readStates);
RecConnState *oldOutdatedState = readStates->outdatedState;
RecConnState *oldCurrentState = readStates->currentState;
RecConnState *oldPendingState = readStates->pendingState;
ASSERT_TRUE(oldOutdatedState == NULL);
// Reconnect the client.
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
/* Check the states at the record layer after the client is reconnected. */
RecConnState *newOutdatedState = readStates->outdatedState;
RecConnState *newCurrentState = readStates->currentState;
RecConnState *newPendingState = readStates->pendingState;
ASSERT_TRUE(newOutdatedState == oldCurrentState);
ASSERT_TRUE(newCurrentState == oldPendingState);
ASSERT_TRUE(newPendingState == NULL);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_READ_PENDING_STATE_TC002
* @title Check the status change of the record layer before and after the server receives the CCS message.
* @precon nan
* @brief 1. Use the default configuration items to set the server to the TRY_RECV_FINISH state. Expected result 1 is
obtained.
* 2. Record the states of the record layer that the client reads. Expected result 2 is obtained.
* 3. Reconnect the server. Expected result 3 is obtained.
* 4. Check the states at the record layer after the client is reconnected. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
* 2. OutdatedState is empty.
* 3. The return value for reconnection is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* 4. OutdatedState is the previous currentState.
* currentState is the previous pendingState.
* The pendingState field is empty.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_READ_PENDING_STATE_TC002()
{
/* Use the default configuration items to set the server to the TRY_RECV_FINISH state. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = false;
testInfo.state = TRY_RECV_FINISH;
testInfo.needStopBeforeRecvCCS = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Record the states of the record layer that the client reads. */
RecConnStates *readStates = (RecConnStates *)&(testInfo.server->ssl->recCtx->readStates);
RecConnState *oldOutdatedState = readStates->outdatedState;
RecConnState *oldCurrentState = readStates->currentState;
RecConnState *oldPendingState = readStates->pendingState;
ASSERT_TRUE(oldOutdatedState == NULL);
// Reconnect the server.
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
/* Check the states at the record layer after the client is reconnected. */
RecConnState *newOutdatedState = readStates->outdatedState;
RecConnState *newCurrentState = readStates->currentState;
RecConnState *newPendingState = readStates->pendingState;
ASSERT_TRUE(newOutdatedState == oldCurrentState);
ASSERT_TRUE(newCurrentState == oldPendingState);
ASSERT_TRUE(newPendingState == NULL);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_WRITE_PENDING_STATE_TC001
* @title Observe the change of the write record layer status before and after the client sends the CCS message.
* @precon nan
* @brief 1. Use the default configuration items to set the server to the TRY_RECV_FINISH state. Expected result 1 is
obtained.
* 2. Record the states of the client write record layer. Expected result 2 is obtained.
* 3. Reconnect the server. Expected result 3 is obtained.
* 4. Check the states of the write record layer after the client is reconnected. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
* 2. The outdatedState field is empty.
* 3. The return value for reconnection is HITLS_REC_NORMAL_IO_BUSY.
* 4. OutdatedState is the previous currentState.
* currentState is the previous pendingState.
* The pendingState field is empty.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_WRITE_PENDING_STATE_TC001()
{
/* Use the default configuration items to set the server to the TRY_RECV_FINISH state. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = true;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
testInfo.needStopBeforeRecvCCS = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Record the states of the client write record layer. */
RecConnStates *writeStates = (RecConnStates *)&(testInfo.client->ssl->recCtx->writeStates);
RecConnState *oldOutdatedState = writeStates->outdatedState;
RecConnState *oldCurrentState = writeStates->currentState;
RecConnState *oldPendingState = writeStates->pendingState;
ASSERT_TRUE(oldOutdatedState == NULL);
// Reconnect the server.
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
/* Check the states of the write record layer after the client is reconnected. */
RecConnState *newOutdatedState = writeStates->outdatedState;
RecConnState *newCurrentState = writeStates->currentState;
RecConnState *newPendingState = writeStates->pendingState;
ASSERT_TRUE(newOutdatedState == oldCurrentState);
ASSERT_TRUE(newCurrentState == oldPendingState);
ASSERT_TRUE(newPendingState == NULL);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_WRITE_PENDING_STATE_TC002
* @title Observe the change of the write record layer status before and after the server sends the CCS message.
* @precon nan
* @brief 1. Use the default configuration items to configure the server to stop the server in the TRY_RECV_FINISH
state. Expected result 1 is obtained.
* 2. Record the states at the write record layer on the client. Expected result 2 is obtained.
* 3. Reconnect the server. Expected result 3 is obtained.
* 4. Check the states of the write record layer after the client is reconnected. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
* 2. The outdatedState field is empty.
* 3. The return value for reconnection is HITLS_REC_NORMAL_IO_BUSY.
* 4. OutdatedState is the previous currentState.
* currentState is the previous pendingState.
* The pendingState field is empty.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_WRITE_PENDING_STATE_TC002()
{
/* Use the default configuration items to configure the server to stop the server in the TRY_RECV_FINISH state. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = false;
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
testInfo.needStopBeforeRecvCCS = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Record the states at the write record layer on the client. */
RecConnStates *writeStates = (RecConnStates *)&(testInfo.server->ssl->recCtx->writeStates);
RecConnState *oldOutdatedState = writeStates->outdatedState;
RecConnState *oldCurrentState = writeStates->currentState;
RecConnState *oldPendingState = writeStates->pendingState;
ASSERT_TRUE(oldOutdatedState == NULL);
// Reconnect the server.
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
/* Check the states of the write record layer after the client is reconnected. */
RecConnState *newOutdatedState = writeStates->outdatedState;
RecConnState *newCurrentState = writeStates->currentState;
RecConnState *newPendingState = writeStates->pendingState;
ASSERT_TRUE(newOutdatedState == oldCurrentState);
ASSERT_TRUE(newCurrentState == oldPendingState);
ASSERT_TRUE(newPendingState == NULL);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RENEGOTIATION_MASTEKEY_TC001
* @title Check whether the master key changes before and after the client initiates renegotiation.
* @precon nan
* @brief 1. Use the default configuration items to configure the server so that the connection is successfully
established. Expected result 1 is obtained.
* 2. Simulate link establishment and check the TLS_Ctx and CM_State on both ends. Expected result 2 is obtained.
* 3. Obtain the current session from the client. Expected result 3 is obtained.
* 4. Obtain the masterKey from the session ID of the client. Expected result 4 is obtained.
* 5. The server sends a Hello Request message. Expected result 5 is obtained.
* 6. Reconnect the client and retransmit data on the server. Expected result 6 is obtained.
* 7. Obtain the new masterKey based on the client session ID and compare it with the old masterKey. Expected
result 7 is obtained.
* @expect 1. The initialization is successful.
* 2. The link is set up successfully. TLS_Ctx is not empty and CM_State is Transporting.
* 3. The session is not empty.
* 4. Obtained successfully.
* 5. The message is sent successfully.
* 6. The connection is successful.
* 7. The data is obtained successfully and the comparison is consistent.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RENEGOTIATION_MASTEKEY_TC001()
{
/* Use the default configuration items to configure the server so that the connection is successfully established.
*/
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = true;
testInfo.isSupportRenegotiation = true;
testInfo.state = HS_STATE_BUTT;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Simulate link establishment and check the TLS_Ctx and CM_State on both ends. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
/* Obtain the current session from the client. */
HITLS_Session *session = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(session != NULL);
/* Obtain the masterKey from the session ID of the client. */
uint8_t masterkey1[MAX_MASTER_KEY_SIZE] = {0};
uint32_t masterkey1Len = MAX_MASTER_KEY_SIZE;
ASSERT_TRUE(HITLS_SESS_GetMasterKey(session, masterkey1, &masterkey1Len) == HITLS_SUCCESS);
/* The server sends a Hello Request message. */
ASSERT_TRUE(HITLS_Renegotiate(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
/* Reconnect the client and retransmit data on the server. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_RENEGOTIATION);
ASSERT_EQ(FRAME_CreateRenegotiationState(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_EQ(testInfo.client->ssl->state, CM_STATE_TRANSPORTING);
/* Obtain the new masterKey based on the client session ID and compare it with the old masterKey. */
uint8_t masterkey2[MAX_MASTER_KEY_SIZE] = {0};
uint32_t masterkey2Len = MAX_MASTER_KEY_SIZE;
HITLS_Session *session2 = HITLS_GetDupSession(testInfo.client->ssl);
uint32_t masterkeylen = HITLS_SESS_GetMasterKeyLen(session2);
ASSERT_TRUE(HITLS_SESS_GetMasterKey(session2, masterkey2, &masterkey2Len) == HITLS_SUCCESS);
ASSERT_TRUE(memcmp(masterkey1, masterkey2, masterkeylen) != 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
HITLS_SESS_Free(session);
HITLS_SESS_Free(session2);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SEQ_NUM_TC001
* @title Check whether the sequence number in the record layer in the FINISH message sent by the server or client is 0.
* @precon nan
* @brief 1. Configure the client/server to stay in the TRY_SEND_FINISH state. Expected result 1 is obtained.
* 2. Connect the server and client once and send the message. Expected result 2 is obtained.
* 3. Obtain the messages sent by the server or client. Expected result 3 is obtained.
* 4. Parse the message sent by the local end into the hs_msg structure. Expected result 4 is obtained.
* 5. Check the sequence number in the sent message. Expected result 5 is obtained.
* 6. Enable the local end to transmit data to the peer end. Expected result 6 is obtained.
* 7. Obtain the messages received by the peer end. Expected result 7 is obtained.
* 8. Parse the message received by the peer end into the hs_msg structure. Expected result 8 is obtained.
* 9. Check the sequence number in the received message. Expected result 5 is obtained.
* @expect 1. The initialization is successful.
* 2. If the server is successfully connected, the client returns NORMAL_RECV_BUF_EMPTY.
* 3. The sending length is not null.
* 4. The parsing is successful, and the message header length is fixed to 5 bytes (TLS1.2).
* 5. The serial number is zero.
* 6. The transmission is successful.
* 7. The received length is not empty.
* 8. The parsing succeeds and the message header length is fixed to 5 bytes (TLS1.2).
* 9. The serial number is zero.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SEQ_NUM_TC001(int isClient)
{
/* Configure the client/server to stay in the TRY_SEND_FINISH state. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_SEND_FINISH;
testInfo.isClient = isClient;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Connect the server and client once and send the message. */
if (isClient) {
ASSERT_TRUE(HITLS_Connect(testInfo.client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
} else {
ASSERT_TRUE(HITLS_Accept(testInfo.server->ssl) == HITLS_SUCCESS);
}
/* Obtain the messages sent by the server or client. */
BSL_UIO *sendIo = isClient ? testInfo.client->io : testInfo.server->io;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(sendIo);
uint8_t *sendBuf = ioUserData->sndMsg.msg;
uint32_t sendLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sendLen != 0);
/* Parse the message sent by the local end into the hs_msg structure. */
uint32_t parseLen = 0;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, sendBuf, sendLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == REC_TLS_RECORD_HEADER_LEN);
/* Check the sequence number in the sent message. */
uint8_t seqBuf[REC_CONN_SEQ_SIZE] = {0};
ASSERT_TRUE(memcpy_s(seqBuf, sendLen, sendBuf + REC_TLS_RECORD_HEADER_LEN, REC_CONN_SEQ_SIZE) == 0);
uint64_t seq = BSL_ByteToUint64(seqBuf);
ASSERT_EQ(seq, 0u);
/* Enable the local end to transmit data to the peer end. */
if (isClient) {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
}
/* Obtain the messages received by the peer end. */
BSL_UIO *recvIo = isClient ? testInfo.server->io : testInfo.client->io;
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(recvIo);
uint8_t *recvBuf = ioUserData2->recMsg.msg;
uint32_t recvLen = ioUserData2->recMsg.len;
ASSERT_TRUE(recvLen != 0);
/* Parse the message received by the peer end into the hs_msg structure. */
uint32_t parseLen2 = 0;
FRAME_Msg frameMsg2 = { 0 };
FRAME_Type frameType2 = { 0 };
frameType2.versionType = HITLS_VERSION_TLS12;
frameType2.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType2, recvBuf, recvLen, &frameMsg2, &parseLen2) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen2 == REC_TLS_RECORD_HEADER_LEN);
/* Check the sequence number in the received message. */
ASSERT_TRUE(memset_s(seqBuf, REC_CONN_SEQ_SIZE, 0, REC_CONN_SEQ_SIZE) == 0);
ASSERT_TRUE(memcpy_s(seqBuf, recvLen, recvBuf + REC_TLS_RECORD_HEADER_LEN, REC_CONN_SEQ_SIZE) == 0);
uint64_t seq2 = BSL_ByteToUint64(seqBuf);
ASSERT_EQ(seq2, 0u);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType2, &frameMsg2);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SEQ_NUM_TC002
* @title Check whether the sequence number in the record layer in the FINISH message sent by the server/client is 0.
* @precon nan
* @brief 1. Configure the client/server to the status that the handshake is successful. Expected result 1 is obtained.
* 2. Check the server/client connection status. Expected result 2 is obtained.
* 3. Randomly generate 32 bytes of data to be transmitted. Expected result 3 is obtained.
* 4. Write app data. Expected result 4 is obtained.
5. Obtain data from the I/O sent by the local end and parse the header and content. Expected result 5 is
obtained.
6. Check the sequence number in the sent message. Expected result 6 is obtained.
7. Perform I/O data transmission from the local end to the peer end. Expected result 7 is obtained.
8. Obtain data from the received I/O from the peer end and parse the header and content. Expected result 8 is
obtained.
9. Check the sequence number in the received message. Expected result 9 is obtained.
* @expect 1. The initialization is successful.
* 2. The link status is Transferring.
3. The generation is successful.
4. The writing is successful.
5. The parsing is successful, and the value of RecordType is REC_TYPE_APP.
6. The SN is 1.
7. The transmission is successful.
8. The parsing is successful, and the value of RecordType is REC_TYPE_APP.
9. The serial number is 1.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SEQ_NUM_TC002(int isClient)
{
/* Configure the client/server to the status that the handshake is successful. */
HandshakeTestInfo testInfo = { 0 };
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = HS_STATE_BUTT;
testInfo.isClient = isClient;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Check the server/client connection status. */
ASSERT_TRUE(testInfo.client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(testInfo.server->ssl->state == CM_STATE_TRANSPORTING);
/* Randomly generate 32 bytes of data to be transmitted. */
uint8_t transportData[REC_CONN_SEQ_SIZE * 4] = {0};
uint32_t transportDataLen = sizeof(transportData) / sizeof(uint8_t);
ASSERT_EQ(RandBytes(transportData, transportDataLen), HITLS_SUCCESS);
/* Write app data. */
HITLS_Ctx *localSsl = isClient ? testInfo.client->ssl : testInfo.server->ssl;
uint32_t writeLen;
ASSERT_EQ(APP_Write(localSsl, transportData, transportDataLen, &writeLen), HITLS_SUCCESS);
/* Obtain data from the I/O sent by the local end and parse the header and content. */
BSL_UIO *sendIo = isClient ? testInfo.client->io : testInfo.server->io;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(sendIo);
uint8_t *sendBuf = ioUserData->sndMsg.msg;
uint32_t sendLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sendLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, sendBuf, sendLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == REC_TLS_RECORD_HEADER_LEN);
ASSERT_TRUE(FRAME_ParseMsgBody(&frameType, sendBuf + parseLen, sendLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameType.recordType, REC_TYPE_APP);
/* Check the sequence number in the sent message. */
uint8_t seqBuf[REC_CONN_SEQ_SIZE] = {0};
ASSERT_TRUE(memcpy_s(seqBuf, sendLen, sendBuf + REC_TLS_RECORD_HEADER_LEN, REC_CONN_SEQ_SIZE) == 0);
uint64_t seq = BSL_ByteToUint64(seqBuf);
ASSERT_EQ(seq, 1u);
/* Perform I/O data transmission from the local end to the peer end. */
if (isClient) {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
}
/* Obtain data from the received I/O from the peer end and parse the header and content. */
BSL_UIO *recvIo = isClient ? testInfo.server->io : testInfo.client->io;
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(recvIo);
uint8_t *recvBuf = ioUserData2->recMsg.msg;
uint32_t recvLen = ioUserData2->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen2 = 0;
FRAME_Msg frameMsg2 = { 0 };
FRAME_Type frameType2 = { 0 };
frameType2.versionType = HITLS_VERSION_TLS12;
frameType2.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType2, recvBuf, recvLen, &frameMsg2, &parseLen2) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen2 == REC_TLS_RECORD_HEADER_LEN);
ASSERT_TRUE(FRAME_ParseMsgBody(&frameType2, recvBuf + parseLen2, recvLen, &frameMsg2, &parseLen2) == HITLS_SUCCESS);
ASSERT_EQ(frameType.recordType, REC_TYPE_APP);
/* Check the sequence number in the received message. */
ASSERT_TRUE(memset_s(seqBuf, REC_CONN_SEQ_SIZE, 0, REC_CONN_SEQ_SIZE) == 0);
ASSERT_TRUE(memcpy_s(seqBuf, recvLen, recvBuf + REC_TLS_RECORD_HEADER_LEN, REC_CONN_SEQ_SIZE) == 0);
uint64_t seq2 = BSL_ByteToUint64(seqBuf);
ASSERT_EQ(seq2, 1u);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType2, &frameMsg2);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC001
* @title After initialization, the server receives a CCS message after sending the serverhellodone message and expects
to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
Expected result 1 is obtained.
* 2. The client initiates a TLS link application. After sending the Server Hello Done message, the server
constructs a CCS message and sends it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC001(void)
{
/* Use the default configuration on the client and server, and disable peer verification on the server. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* The client initiates a TLS link application. After sending the Server Hello Done message, the server constructs a
* CCS message and sends it to the server. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC002
* @title After initialization, the client receives a CCS message after sending a client hello message and expects to
return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
Expected result 1 is obtained.
* 2. The client initiates a TLS link application. After sending the client hello message, the client constructs
a CCS message and sends it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC002(void)
{
/* Use the default configuration on the client and server, and disable peer verification on the server. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* The client initiates a TLS link application. After sending the client hello message, the client constructs a CCS
* message and sends it to the client. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC003
* @title During link establishment, the client receives the serverhello message after sending the CCS and expects to
return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
Expected result 1 is obtained.
* 2. The client initiates a TLS link application. After sending the CCS, the client constructs a serverhello
message and sends it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC003(void)
{
/* Use the default configuration on the client and server, and disable peer verification on the server. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_SEND_CHANGE_CIPHER_SPEC;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = false;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
testInfo.client->ssl->hsCtx->state = TRY_RECV_FINISH;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_EQ(FRAME_GetDefaultMsg(&frameType, &frameMsg), HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* The client initiates a TLS link application. After sending the CCS, the client constructs a serverhello message
* and sends it to the client. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC004
* @title After initialization, construct an app message and send it to the client. The expected alert is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable the peer end verification function on
the server. Expected result 1 is obtained.
* 2. When the client initiates a TLS link application request, construct an APP message and send it to the
client in the RECV_SERVER_HELLO message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC004(void)
{
/* Use the default configuration on the client and server, and disable the peer end verification function on the
* server. */
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = true;
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == 0);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len),
EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
/* When the client initiates a TLS link application request, construct an APP message and send it to the client in
* the RECV_SERVER_HELLO message. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC005
* @title After the link is set up, the client receives the serverhello message when receiving the app data. The client
is expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
Expected result 1 is obtained.
* 2. The client initiates a TLS link request. After the handshake succeeds, construct a serverhello message and
send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC005(void)
{
/* Use the default configuration on the client and server, and disable peer verification on the server. */
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
/* The client initiates a TLS link request. After the handshake succeeds, construct a serverhello message and send
* it to the client. */
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_BAD_RECORD_MAC);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_EQ(FRAME_ParseMsgHeader(&frameType, sndBuf, sndLen, &frameMsg, &parseLen), 0);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC006
* @title After the link is established, renegotiation is not enabled. The server receives a client hello message and is
expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server. Expected result 1 is obtained.
* 2. After the client initiates a TLS link request and handshakes successfully, construct a client hello message
and send it to the server. Expected result 2 is displayed.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC006(void)
{
/* Use the default configuration on the client and server. */
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
/* After the client initiates a TLS link request and handshakes successfully, construct a client hello message and
* send it to the server. */
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_INVALID_PROTOCOL_VERSION);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_EQ(FRAME_ParseMsgHeader(&frameType, sndBuf, sndLen, &frameMsg, &parseLen), 0);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC007
* @title After initialization, construct an app message and send it to the server. The expected alert is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable the peer end verification function on
the server. Expected result 1 is obtained.
* 2. When the client initiates a TLS link application request, construct an APP message and send it to the
server in the RECV_CLIENT_HELLO message on the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_UNEXPECT_RECODETYPE_TC007(void)
{
/* Use the default configuration on the client and server, and disable the peer end verification function on the
* server. Expected result 1 is obtained. */
FRAME_Msg parsedAlert = { 0 };
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = false;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == 0);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len),
EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
/* When the client initiates a TLS link application request, construct an APP message and send it to the server in
* the RECV_CLIENT_HELLO message on the server. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf, sndLen, &parsedAlert, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(parsedAlert.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_RECV_APPDATA_TC001
* @title Before the first handshake, no app message is received.
* @precon nan
* @brief During the first handshake, the client/server receives the app message when expecting to receive the finish
message. The expected handshake fails and the handshake is interrupted.
* @expect 1. Return a failure message and interrupt the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_RECV_APPDATA_TC001(int isClient)
{
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
HandshakeTestInfo testInfo = { 0 };
testInfo.isClient = isClient;
testInfo.state = TRY_RECV_FINISH;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == 0);
FrameUioUserData *ioUserData =
isClient ? BSL_UIO_GetUserData(testInfo.client->io) : BSL_UIO_GetUserData(testInfo.server->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
/* application data record header construction
17 - type is 0x17 (application data)
03 03 - protocol version is "3,3" (TLS 1.2)
00 02 - 2 bytes of application data follows */
uint8_t appdataRecordHeader[] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, MAX_RECORD_LENTH, appdataRecordHeader, sizeof(appdataRecordHeader)), EOK);
ASSERT_EQ(memcpy_s(data + sizeof(appdataRecordHeader), MAX_RECORD_LENTH - sizeof(appdataRecordHeader),
ioUserData->recMsg.msg, ioUserData->recMsg.len),
EOK);
uint32_t constructLen = ioUserData->recMsg.len + sizeof(appdataRecordHeader);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, constructLen), EOK);
ioUserData->recMsg.len = constructLen;
/* During the first handshake, the client/server receives the app message when expecting to receive the finish
* message. */
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, isClient, HS_STATE_BUTT),
HITLS_REC_BAD_RECORD_MAC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC001
* @title Send a hello request when the link status is CM_STATE_IDLE.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a HelloRequest message and send it to the client. The client invokes the HITLS_Connect interface
to receive the message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the HelloRequest message, the client ignores the message and stays in the
TRY_RECV_SERVER_HELLO state after sending the ClientHello message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC001(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TLS_IDLE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
FRAME_Init();
/* Use the configuration items to configure the client and server. */
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
// Construct a HelloRequest message and send it to the client.
ASSERT_TRUE(SendHelloReq(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state == TRY_RECV_SERVER_HELLO);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC002
* @title The server sends a Hello Request message after the client sends the client hello message.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a HelloRequest message and send it to the client. The client invokes the HITLS_Connect interface
to receive the message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the HelloRequest message, the client ignores the message and stays in the
TRY_RECV_SERVER_HELLO state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC002(void)
{
/* Use the configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_SEND_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
/* Construct a HelloRequest message and send it to the client. */
ASSERT_TRUE(SendHelloReq(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_SERVER_HELLO);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC003
* @title The server sends a Hello Request message when preparing to send CCS messages.
* @precon nan
* @brief 1. Use configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client invokes HITLS_Connect in the Try_SEND_FINISH phase. Expected result 2 is obtained.
* 3. Construct a HelloRequest message and send it to the client. The client invokes HITLS_Connect to receive the
message. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends a FINISH message, changes the status to TRY_RECV_NEW_SESSION_TICKET, and returns
HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* 3. After receiving the HelloRequest message, the client ignores the message and stays in the
TRY_RECV_NEW_SESSION_TICKET state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC003(void)
{
/* Use configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_SEND_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
/* The client invokes HITLS_Connect in the Try_SEND_FINISH phase. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
/* Construct a HelloRequest message and send it to the client. The client invokes HITLS_Connect to receive the
* message. */
ASSERT_TRUE(SendHelloReq(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_HANDSHAKING);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_NEW_SESSION_TICKET);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC004
* @title The server sends a hello request when the link status is CM_STATE_TRANSPORTING.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The server sends a hello request to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT_NO_RENEGOTIATION message successfully, but the client can continue sending and
receiving data.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
/* Use the configuration items to configure the client and server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
/* The server sends a hello request to the client. */
ASSERT_TRUE(SendHelloReq(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTING);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, data, sizeof(data), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(readLen == sizeof(data) && memcmp(data, readBuf, readLen) == 0);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC005
* @title The server sends a hello request when the link status is CM_STATE_RENEGOTIATION.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server to support renegotiation. Expected result 1
is displayed.
* 2. After the link is established, the client sends a Hello Request message. The client receives the
renegotiation request message and is in the renegotiation state. At this time, the server sends a Hello Request message
again. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The expected message is sent successfully but is ignored.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC005(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
/* Use the configuration items to configure the client and server to support renegotiation. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
/* After the link is established, the client sends a Hello Request message. The client receives the renegotiation
* request message and is in the renegotiation state. At this time, the server sends a Hello Request message again.
*/
ASSERT_TRUE(SendHelloReq(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_RENEGOTIATION);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_RENEGOTIATION);
ASSERT_TRUE(client->ssl->hsCtx->state = TRY_SEND_CLIENT_HELLO);
ASSERT_TRUE(SendHelloReq(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(client->ssl->hsCtx->state = TRY_RECV_SERVER_HELLO);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC006
* @title Enable the client and server to support renegotiation. After the connection between the client and server is
established, the client and server send a Hello Request message.
* @precon nan
* @brief 1. Configure the client and server to support renegotiation. Expected result 1 is displayed.
* 2. After the link is established, the server sends the Hello Request message successfully.
* @expect 1. The initialization is successful.
* 2. The client enters the renegotiation state and sends client hello.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC006(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
/* Configure the client and server to support renegotiation. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
/* After the link is established, the server sends the Hello Request message successfully. */
ASSERT_TRUE(SendHelloReq(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_RENEGOTIATION);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_RENEGOTIATION);
ASSERT_TRUE(client->ssl->hsCtx->state = TRY_SEND_CLIENT_HELLO);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC007
* @title The server receives a Hello Request message after sending the server hello done message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. After sending a Server Hello Done message, the server receives a Hello Request message. Expected result 2
is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends an ALERT. The level is ALERT_LEVEL_FATAL, and the description is
ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC007(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg parsedAlert = { 0 };
testInfo.state = TRY_SEND_SERVER_HELLO_DONE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
/* After sending a Server Hello Done message, the server receives a Hello Request message. */
ASSERT_TRUE(SendHelloReq(testInfo.client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf, sndLen, &parsedAlert, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC008
* @title The server sends a Hello Request message after sending the finish message. The renegotiation is successful.
The server sends a Hello Request message again. The renegotiation is successful.
* @precon nan
* @brief 1. Configure the client and server to support renegotiation. Expected result 1 is displayed.
* 2. Send a Hello Request message after the server sends a finish message. Expected result 2 is obtained.
* 3. The server sends a Hello Request message again. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The renegotiation succeeds.
* 3. The client ignores the Hello Request message and continues the renegotiation.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HELLO_REQUEST_TC008(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
FRAME_Init();
/* Configure the client and server to support renegotiation. */
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.config->isSupportRenegotiation = true;
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state) ==
HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_TRANSPORTING);
/* Send a Hello Request message after the server sends a finish message. */
ASSERT_TRUE(SendHelloReq(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_RENEGOTIATION);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state = TRY_SEND_CLIENT_HELLO);
/* The server sends a Hello Request message again. */
ASSERT_TRUE(SendHelloReq(testInfo.server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state = TRY_RECV_SERVER_HELLO);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_VERSION_TC001
* @title Check the TLS protocol version carried in the clientHello message.
* @precon nan
* @brief 1. Use configuration items to configure the client and server. Set the maximum version number of the client to
TLS1.2 and the minimum version number to TLS1.1. Expected result 1 is obtained.
* 2. Obtain and parse the client Hello message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The protocol version carried in the client Hello message is TLS1.2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_VERSION_TC001(void)
{
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
FRAME_Init();
/* Use configuration items to configure the client and server. Set the maximum version number of the client to
* TLS1.2 and the minimum version number to TLS1.1. */
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.config->minVersion = HITLS_VERSION_TLS11;
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.config->minVersion = HITLS_VERSION_TLS12;
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state) ==
HITLS_SUCCESS);
/* Obtain and parse the client Hello message. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->version.data == HITLS_VERSION_TLS12);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_NOT_SUPPORT_SERVER_VERSION_TC001
* @title The client does not support the version selected by the server.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. After receiving the Server Hello message, the client changes the TLS version field in the serverhello
message to DTLS1.2 and sends the message to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message. The level is ALERT_ LEVEL_FATAL and the description is
ALERT_PROTOCOL_VERSION.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_NOT_SUPPORT_SERVER_VERSION_TC001(void)
{
/* Use the configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* After receiving the Server Hello message, the client changes the TLS version field in the serverhello message to
* DTLS1.2 and sends the message to the client. */
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->version.data = HITLS_VERSION_DTLS12;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_PROTOCOL_VERSION);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_CHOSE_VERSION_TC001
* @title Check the TLS protocol version carried in the serverHello message.
* @brief 1. Use the configuration items to configure the client and server. Set the maximum version number of the
client to TLS1.3 and the minimum version number to TLS1.1,
* Set the maximum version number of the server to TLS1.2 and the minimum version number to TLS1.1. Expected
result 1 is obtained.
* 2. Obtain and parse the server Hello message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The protocol version carried in the server Hello message is TLS1.2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_CHOSE_VERSION_TC001(void)
{
/* Use the configuration items to configure the client and server.
Set the maximum version number of the client to TLS1.3 and the minimum version number to TLS1.1;
Set the maximum version number of the server to TLS1.2 and the minimum version number to TLS1.1. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
testInfo.config->maxVersion = HITLS_VERSION_TLS13;
testInfo.config->minVersion = HITLS_VERSION_TLS11;
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.config->maxVersion = HITLS_VERSION_TLS12;
testInfo.config->minVersion = HITLS_VERSION_TLS11;
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state) ==
HITLS_SUCCESS);
/* Obtain and parse the server Hello message. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->version.data == HITLS_VERSION_TLS12);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_DEFAULT_SIGNATURE_EXTENSION_TC001
* @title HITLS If no signature algorithm is specified, select the default algorithm and check whether the extension is
carried.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Check whether the client Clienet Hello message carries the signature algorithm extension.
* @expect 1. The initialization is successful.
* 2. Expected carrying expansion
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_DEFAULT_SIGNATURE_EXTENSION_TC001(void)
{
/* Use the configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Check whether the client Clienet Hello message carries the signature algorithm extension. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->signatureAlgorithms.exState == INITIAL_FIELD);
ASSERT_TRUE(&frameMsg.body.handshakeMsg.body.clientHello.extension.flag.haveSignatureAlgorithms);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_WITHOUT_SIGNATURE_TC001
* @title Default processing logic of the signature algorithm
* @precon nan
* @brief 1. Use configuration items to configure the client and server, and set the client and service cipher suite to
HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256. Expected result 1 is obtained.
* 2. Modify the client hello message so that the message does not carry the signature algorithm extension. Then,
the link is established. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The signature algorithm in the server key exchange message sent by the server is
CERT_SIG_SCHEME_ECDSA_SHA1.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_WITHOUT_SIGNATURE_TC001(void)
{
/* Use configuration items to configure the client and server, and set the client and service cipher suite to
* HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
ASSERT_EQ(HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t)),
HITLS_SUCCESS);
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
ASSERT_TRUE(StatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Modify the client hello message so that the message does not carry the signature algorithm extension. Then, the
* link is established. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->signatureAlgorithms.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
testInfo.state = TRY_RECV_SERVER_KEY_EXCHANGE;
testInfo.isClient = true;
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state),
HITLS_SUCCESS);
ASSERT_EQ(testInfo.client->ssl->hsCtx->state, TRY_RECV_SERVER_KEY_EXCHANGE);
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf2 = ioUserData2->recMsg.msg;
uint32_t recvLen2 = ioUserData2->recMsg.len;
ASSERT_TRUE(recvLen != 0);
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf2, recvLen2, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerKeyExchangeMsg *serverMsg = &frameMsg.body.hsMsg.body.serverKeyExchange;
ASSERT_EQ(serverMsg->keyEx.ecdh.signAlgorithm.data, CERT_SIG_SCHEME_ECDSA_SHA1);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_WITHOUT_SIGNATURE_TC002
* @title Default processing logic of the signature algorithm
* @precon nan
* @brief 1. Use the configuration items to configure the client and server, and set the client and service cipher suite
to HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA. Expected result 1 is obtained.
* 2. Modify the client hello message so that the message does not carry the signature algorithm extension. Then,
the link is established. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The signature algorithm in the server keyexchange message sent by the server is
CERT_SIG_SCHEME_RSA_PKCS1_SHA1.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_HELLO_WITHOUT_SIGNATURE_TC002(void)
{
/* Use the configuration items to configure the client and server, and set the client and service cipher suite to
* HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA};
ASSERT_EQ(HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t)),
HITLS_SUCCESS);
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
ASSERT_TRUE(StatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Modify the client hello message so that the message does not carry the signature algorithm extension. Then, the
* link is established. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->signatureAlgorithms.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
testInfo.state = TRY_RECV_SERVER_KEY_EXCHANGE;
testInfo.isClient = true;
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state),
HITLS_SUCCESS);
ASSERT_EQ(testInfo.client->ssl->hsCtx->state, TRY_RECV_SERVER_KEY_EXCHANGE);
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf2 = ioUserData2->recMsg.msg;
uint32_t recvLen2 = ioUserData2->recMsg.len;
ASSERT_TRUE(recvLen != 0);
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf2, recvLen2, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerKeyExchangeMsg *serverMsg = &frameMsg.body.hsMsg.body.serverKeyExchange;
ASSERT_EQ(serverMsg->keyEx.ecdh.signAlgorithm.data, CERT_SIG_SCHEME_RSA_PKCS1_SHA1);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC001
* @title server can receive any version field in the recordheader of the client hello.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Change the version field in the recod header of the client hello message to 0x03ff.
* @expect 1. The initialization is successful.
* 2. The server can process the message normally and enter the next state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC001(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo.config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
ASSERT_TRUE(StatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen != 5);
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == 5);
/* Change the version field in the recod header of the client hello message to 0x03ff. */
frameMsg.recVersion.data = 0x0300;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, recvBuf, recvLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(testInfo.server->ssl->hsCtx->state, TRY_SEND_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC002
* @title server can receive any version field in the recordheader of the client hello.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Change the version field in the recod header of the client hello message to 0x03ff.
* @expect 1. The initialization is successful.
* 2. The server can process the message normally and enter the next state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC002(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo.config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
ASSERT_TRUE(StatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen != 5);
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parseLen == 5);
/* Change the version field in the recod header of the client hello message to 0x03ff. */
frameMsg.recVersion.data = 0x03ff;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, recvBuf, recvLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(testInfo.server->ssl->hsCtx->state, TRY_SEND_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC003
* @title server can receive any version field in the recordheader of the client hello message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Change the version field in the recod header of the client hello message to 0x0399.
* @expect 1. The initialization is successful.
* 2. The server can process the message normally and enter the next state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECODE_VERSION_TC003(void)
{
/* Use the default configuration items to configure the client and server. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo.config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo.config->isSupportExtendMasterSecret = testInfo.isSupportExtendMasterSecret;
testInfo.config->isSupportClientVerify = testInfo.isSupportClientVerify;
testInfo.config->isSupportNoClientCert = testInfo.isSupportNoClientCert;
ASSERT_TRUE(StatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// The record length must be greater than or equal to 5 bytes.
ASSERT_TRUE(parseLen >= 5);
ASSERT_TRUE(FRAME_ParseTLSRecordHeader(recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// The length of the record header must be 5 bytes.
ASSERT_TRUE(parseLen == 5);
/* Change the version field in the recod header of the client hello message to 0x0399. */
frameMsg.recVersion.data = 0x0399;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, recvBuf, recvLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(testInfo.server->ssl->hsCtx->state, TRY_SEND_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* Define a new structure,
Construct the SH message with the signatureAlgorithms extension added,
Actually, the SH message should not contain the signatureAlgorithms.
*/
typedef struct {
FRAME_Integer version; /* Version number */
FRAME_Array8 randomValue; /* Random number */
FRAME_Integer sessionIdSize; /* session ID length */
FRAME_Array8 sessionId; /* session ID */
FRAME_Integer cipherSuite; /* Cipher suite */
FRAME_Integer compressionMethod; /* Compression method */
FRAME_Integer extensionLen; /* Total length of the extension. */
FRAME_HsExtArray8 pointFormats; /* dot format */
FRAME_HsExtArray8 extendedMasterSecret; /* extended master key */
FRAME_HsExtArray8 secRenego; /* security renegotiation */
FRAME_HsExtArray8 sessionTicket; /* sessionTicket */
FRAME_HsExtArray16 signatureAlgorithms; /* algorithm signature */
} FRAME_ServerHelloMsg_WithSignatureAlgorithms;
void SetServerHelloMsgWithSignatureAlgorithms(FRAME_ServerHelloMsg_WithSignatureAlgorithms *destMsg,
const FRAME_ServerHelloMsg *serverMsg)
{
destMsg->version = serverMsg->version;
destMsg->randomValue = serverMsg->randomValue;
destMsg->sessionIdSize = serverMsg->sessionIdSize;
destMsg->sessionId = serverMsg->sessionId;
destMsg->cipherSuite = serverMsg->cipherSuite;
destMsg->compressionMethod = serverMsg->compressionMethod;
destMsg->extensionLen = serverMsg->extensionLen;
destMsg->pointFormats = serverMsg->pointFormats;
destMsg->extendedMasterSecret = serverMsg->extendedMasterSecret;
destMsg->secRenego = serverMsg->secRenego;
destMsg->sessionTicket = serverMsg->sessionTicket;
}
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_HELLO_ADD_SIGNATURE_TC001
* @title The server attempts to send the signatureAlgorithms extension.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server, and obtain the clientHello and serverHello
respectively. Expected result 1 is obtained.
* 2. Define a new structure, load the serverHello into the new structure, add the signatureAlgorithms extension
from the clientHello, and send the extension to the client.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message. The level is ALERT_ LEVEL_FATAL and the description is
ALERT_UNSUPPORTED_EXTENSION.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_HELLO_ADD_SIGNATURE_TC001(void)
{
/* Use the configuration items to configure the client and server, and obtain the clientHello and serverHello
* respectively. */
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData_c = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvSHbuf = ioUserData_c->recMsg.msg;
uint32_t recvSHbufLen = ioUserData_c->recMsg.len;
ASSERT_TRUE(recvSHbufLen != 0);
uint32_t parsedSHlen = 0;
FRAME_Msg parsedSH = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvSHbuf, recvSHbufLen, &parsedSH, &parsedSHlen) == HITLS_SUCCESS);
HandshakeTestInfo testInfo2 = { 0 };
testInfo2.state = TRY_RECV_CLIENT_HELLO;
testInfo2.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo2) == HITLS_SUCCESS);
FrameUioUserData *ioUserData_s = BSL_UIO_GetUserData(testInfo2.server->io);
uint8_t *recvCHbuf = ioUserData_s->recMsg.msg;
uint32_t recvCHbufLen = ioUserData_s->recMsg.len;
ASSERT_TRUE(recvCHbufLen != 0);
/* Define a new structure, load the serverHello into the new structure, add the signatureAlgorithms extension from
* the clientHello, and send the extension to the client. */
uint32_t parsedCHlen = 0;
FRAME_Msg parsedCH = { 0 };
FRAME_Type frameType2 = { 0 };
SetFrameType(&frameType2, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType2, recvCHbuf, recvCHbufLen, &parsedCH, &parsedCHlen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *chMsg = &parsedCH.body.hsMsg.body.clientHello;
ASSERT_TRUE(chMsg->signatureAlgorithms.exState == INITIAL_FIELD);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
FRAME_ServerHelloMsg_WithSignatureAlgorithms shWithSigAlgExt;
SetServerHelloMsgWithSignatureAlgorithms(&shWithSigAlgExt, shMsg);
uint32_t sigAlgNum = chMsg->signatureAlgorithms.exData.size;
uint16_t *chSigAlgData = chMsg->signatureAlgorithms.exData.data;
uint32_t chSigAlgDataSize = sigAlgNum * sizeof(uint16_t);
memcpy_s(&shWithSigAlgExt.signatureAlgorithms, sizeof(FRAME_HsExtArray16), &(chMsg->signatureAlgorithms),
sizeof(FRAME_HsExtArray16));
shWithSigAlgExt.signatureAlgorithms.exData.data = calloc(sigAlgNum, sizeof(uint16_t));
memcpy_s(shWithSigAlgExt.signatureAlgorithms.exData.data, chSigAlgDataSize, chSigAlgData, chSigAlgDataSize);
memcpy_s(&parsedSH.body.hsMsg.body.serverHello, sizeof(FRAME_ServerHelloMsg_WithSignatureAlgorithms),
&shWithSigAlgExt, sizeof(FRAME_ServerHelloMsg_WithSignatureAlgorithms));
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData_c->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_UNSUPPORTED_EXTENSION);
ioUserData_c = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf_c = ioUserData_c->sndMsg.msg;
uint32_t sndBufLen_c = ioUserData_c->sndMsg.len;
ASSERT_TRUE(sndBufLen_c != 0);
uint32_t parsedAlertLen = 0;
FRAME_Msg parsedAlert = { 0 };
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf_c, sndBufLen_c, &parsedAlert, &parsedAlertLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNSUPPORTED_EXTENSION);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
FRAME_CleanMsg(&frameType2, &parsedCH);
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo2.client);
FRAME_FreeLink(testInfo2.server);
HITLS_CFG_FreeConfig(testInfo2.config);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
void Test_RenegoWrapperFunc(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.body.clientHello.secRenego.exState, INITIAL_FIELD);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
void Test_RenegoRemoveExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
ASSERT_EQ(parseLen, *len);
frameMsg.body.hsMsg.body.clientHello.secRenego.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
int32_t g_writeRet;
uint32_t g_writeLen;
bool g_isUseWriteLen;
uint8_t g_writeBuf[REC_TLS_RECORD_HEADER_LEN + REC_MAX_CIPHER_TEXT_LEN];
int32_t STUB_MethodWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
{
(void)uio;
if (memcpy_s(g_writeBuf, sizeof(g_writeBuf), buf, len) != EOK) {
return BSL_MEMCPY_FAIL;
}
*writeLen = len;
if (g_isUseWriteLen) {
*writeLen = g_writeLen;
}
return g_writeRet;
}
int32_t g_readRet;
uint32_t g_readLen;
uint8_t g_readBuf[REC_TLS_RECORD_HEADER_LEN + REC_MAX_CIPHER_TEXT_LEN + 1];
int32_t STUB_MethodRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen)
{
(void)uio;
if (g_readLen != 0 && memcpy_s(buf, len, g_readBuf, g_readLen) != EOK) {
return BSL_MEMCPY_FAIL;
}
*readLen = g_readLen;
return g_readRet;
}
int32_t g_ctrlRet;
BSL_UIO_CtrlParameter g_ctrlCmd;
int32_t STUB_MethodCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *param)
{
(void)larg;
(void)uio;
(void)param;
if ((int32_t)g_ctrlCmd == cmd) {
return g_ctrlRet;
}
return BSL_SUCCESS;
}
HITLS_Config *g_tlsConfig = NULL;
HITLS_Ctx *g_tlsCtx = NULL;
BSL_UIO *g_uio = NULL;
int32_t TlsCtxNew(BSL_UIO_TransportType type)
{
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
BSL_UIO *uio = NULL;
const BSL_UIO_Method *ori = NULL;
switch (type) {
case BSL_UIO_TCP:
#ifdef HITLS_BSL_UIO_TCP
ori = BSL_UIO_TcpMethod();
#endif
break;
default:
#ifdef HITLS_BSL_UIO_SCTP
ori = BSL_UIO_SctpMethod();
#endif
break;
}
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
BSL_UIO_Method method = { 0 };
memcpy(&method, ori, sizeof(method));
method.uioWrite = STUB_MethodWrite;
method.uioRead = STUB_MethodRead;
method.uioCtrl = STUB_MethodCtrl;
uio = BSL_UIO_New(&method);
ASSERT_TRUE(uio != NULL);
BSL_UIO_SetInit(uio, 1);
ASSERT_TRUE(HITLS_SetUio(ctx, uio) == HITLS_SUCCESS);
/* Default value of stub function */
g_writeRet = HITLS_SUCCESS;
g_writeLen = 0;
g_isUseWriteLen = false;
g_readLen = 0;
g_readRet = HITLS_SUCCESS;
g_tlsConfig = config;
g_tlsCtx = ctx;
g_uio = uio;
return HITLS_SUCCESS;
EXIT:
BSL_UIO_Free(uio);
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(config);
return HITLS_INTERNAL_EXCEPTION;
}
void TlsCtxFree(void)
{
BSL_UIO_Free(g_uio);
HITLS_Free(g_tlsCtx);
HITLS_CFG_FreeConfig(g_tlsConfig);
g_uio = NULL;
g_tlsCtx = NULL;
g_tlsConfig = NULL;
}
#define BUFFER_SIZE 128
#define UT_AEAD_NONCE_SIZE 12u /* AEAD nonce is fixed to 12. */
#define UT_AEAD_TAG_LENGTH 16
ALERT_Level g_alertLevel;
ALERT_Description g_alertDescription;
void STUB_SendAlert(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
(void)ctx;
g_alertLevel = level;
g_alertDescription = description;
return;
}
typedef struct {
REC_Type type;
uint16_t version;
uint64_t epochSeq;
uint16_t bodyLen;
uint8_t *body;
} RecordMsg;
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; /* Set the session to the client for session recovery. */
} ResumeTestInfo;
static uint8_t *g_sessionId;
static uint32_t g_sessionIdSize;
int32_t NewConfig(ResumeTestInfo *testInfo)
{
/* Construct the configuration. */
switch (testInfo->version) {
case HITLS_VERSION_DTLS12:
testInfo->config = HITLS_CFG_NewDTLS12Config();
break;
case HITLS_VERSION_TLS13:
testInfo->config = HITLS_CFG_NewTLS13Config();
break;
case HITLS_VERSION_TLS12:
testInfo->config = HITLS_CFG_NewTLS12Config();
break;
default:
break;
}
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetClientVerifySupport(testInfo->config, true);
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo->config, true);
HITLS_CFG_SetNoClientCertSupport(testInfo->config, true);
HITLS_CFG_SetRenegotiationSupport(testInfo->config, true);
HITLS_CFG_SetPskServerCallback(testInfo->config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo->config, (HITLS_PskClientCb)ExampleClientCb);
return HITLS_SUCCESS;
}
static void FreeLink(ResumeTestInfo *testInfo)
{
/* Release resources. */
FRAME_FreeLink(testInfo->client);
testInfo->client = NULL;
FRAME_FreeLink(testInfo->server);
testInfo->server = NULL;
}
int32_t GetSessionId(ResumeTestInfo *testInfo)
{
FRAME_Type frameType = { 0 };
FRAME_Msg recvframeMsg = { 0 };
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo->client->io);
uint8_t *recMsg = ioUserData->recMsg.msg;
uint32_t recMsgLen = ioUserData->recMsg.len;
frameType.handshakeType = SERVER_HELLO;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.versionType = testInfo->version;
uint32_t parseLen = 0;
int32_t ret = FRAME_ParseMsg(&frameType, recMsg, recMsgLen, &recvframeMsg, &parseLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* Save the sessionId in the serverhello. */
FRAME_ServerHelloMsg *serverHello = &recvframeMsg.body.hsMsg.body.serverHello;
g_sessionIdSize = serverHello->sessionIdSize.data;
g_sessionId = BSL_SAL_Dump(serverHello->sessionId.data, g_sessionIdSize);
FRAME_CleanMsg(&frameType, &recvframeMsg);
return HITLS_SUCCESS;
}
int32_t FirstHandshake(ResumeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
int32_t ret = 0;
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, true, TRY_RECV_SERVER_HELLO);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* Obtain the session ID for the first connection setup. */
ret = GetSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* User data transmission */
uint8_t data[] = "Hello World";
uint32_t writeLen;
ret = HITLS_Write(testInfo->server->ssl, data, sizeof(data), &writeLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ret = FRAME_TrasferMsgBetweenLink(testInfo->server, testInfo->client);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = HITLS_Read(testInfo->client->ssl, readBuf, READ_BUF_SIZE, &readLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
testInfo->clientSession = HITLS_GetDupSession(testInfo->client->ssl);
FreeLink(testInfo);
return HITLS_SUCCESS;
}
int32_t CmpClientHelloSessionId(ResumeTestInfo *testInfo)
{
FRAME_Type frameType = { 0 };
/* Obtain the client hello message received by the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo->server->io);
uint8_t *recMsg = ioUserData->recMsg.msg;
uint32_t recMsgLen = ioUserData->recMsg.len;
uint32_t parseLen = 0;
FRAME_Msg frameMsg = { 0 };
frameType.versionType = testInfo->version;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
int32_t ret = FRAME_ParseMsg(&frameType, recMsg, recMsgLen, &frameMsg, &parseLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* Compare the sessionId in the client hello with the saved sessionId. */
FRAME_ClientHelloMsg *clienHello = &frameMsg.body.hsMsg.body.clientHello;
if (clienHello->sessionIdSize.data != g_sessionIdSize) {
return HITLS_INTERNAL_EXCEPTION;
}
if (memcmp(clienHello->sessionId.data, g_sessionId, g_sessionIdSize) != 0) {
return HITLS_INTERNAL_EXCEPTION;
}
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(testInfo->server->ssl);
HITLS_Accept(testInfo->server->ssl);
FRAME_TrasferMsgBetweenLink(testInfo->server, testInfo->client);
return HITLS_SUCCESS;
}
int32_t CmpSessionId(ResumeTestInfo *testInfo)
{
FRAME_Type frameType = { 0 };
FRAME_Msg recvframeMsg = { 0 };
/* Obtain the server hello message received by the client. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo->client->io);
uint8_t *recMsg = ioUserData->recMsg.msg;
uint32_t recMsgLen = ioUserData->recMsg.len;
frameType.handshakeType = SERVER_HELLO;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.versionType = testInfo->version;
/* Parse the server hello message. */
uint32_t parseLen = 0;
int32_t ret = FRAME_ParseMsg(&frameType, recMsg, recMsgLen, &recvframeMsg, &parseLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* Check whether the received serverhello message is consistent with the saved one. */
FRAME_ServerHelloMsg *serverHello = &recvframeMsg.body.hsMsg.body.serverHello;
if (serverHello->sessionIdSize.data != g_sessionIdSize) {
FRAME_CleanMsg(&frameType, &recvframeMsg);
return HITLS_INTERNAL_EXCEPTION;
}
if (memcmp(serverHello->sessionId.data, g_sessionId, g_sessionIdSize) != 0) {
FRAME_CleanMsg(&frameType, &recvframeMsg);
return HITLS_INTERNAL_EXCEPTION;
}
FRAME_CleanMsg(&frameType, &recvframeMsg);
return HITLS_SUCCESS;
}
int32_t TryResumeBySessionId(ResumeTestInfo *testInfo)
{
int32_t ret;
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, false, TRY_RECV_CLIENT_HELLO);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = CmpClientHelloSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = CmpSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* User data transmission */
uint8_t data[] = "Hello World";
uint32_t writeLen;
ret = HITLS_Write(testInfo->server->ssl, data, sizeof(data), &writeLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ret = FRAME_TrasferMsgBetweenLink(testInfo->server, testInfo->client);
if (ret != HITLS_SUCCESS) {
return ret;
}
return HITLS_Read(testInfo->client->ssl, readBuf, READ_BUF_SIZE, &readLen);
}
int32_t TryResumeByTheUsingSessionId(ResumeTestInfo *testInfo)
{
int32_t ret;
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, false, TRY_RECV_CLIENT_HELLO);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = CmpClientHelloSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = CmpSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID;
}
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MULTILINK_RESUME_ALERT_TC001
* @title Test the scenario where the session recovers.
* @precon nan
* @brief 1. First handshake between the client and server. Save the session ID. Expected result 1 is obtained.
2. Obtain the client session and set the session to the client that performs the next handshake. Expected
result 2 is obtained.
3. Perform the second handshake between the client and server. Expected result 3 is obtained.
4. Use the same session ID on the client and server to restore the session. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The session is restored successfully.
4. The session fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MULTILINK_RESUME_ALERT_TC001(int uioType, int version)
{
g_sessionId = NULL;
g_sessionIdSize = 0;
FRAME_Init();
ResumeTestInfo testInfo01 = { 0 };
testInfo01.version = (uint16_t)version;
testInfo01.uioType = (BSL_UIO_TransportType)uioType;
/* First handshake between the client and server. Save the session ID. */
ASSERT_EQ(NewConfig(&testInfo01), HITLS_SUCCESS);
HITLS_CFG_SetSessionTicketSupport(testInfo01.config, false);
ASSERT_EQ(FirstHandshake(&testInfo01), HITLS_SUCCESS);
/* Obtain the client session and set the session to the client that performs the next handshake. */
ASSERT_TRUE(testInfo01.clientSession != NULL);
ASSERT_EQ(TryResumeBySessionId(&testInfo01), HITLS_SUCCESS);
ResumeTestInfo testInfo02 = { 0 };
testInfo02.version = (uint16_t)version;
testInfo02.uioType = (BSL_UIO_TransportType)uioType;
/* Perform the second handshake between the client and server. */
ASSERT_EQ(NewConfig(&testInfo02), HITLS_SUCCESS);
HITLS_CFG_SetSessionTicketSupport(testInfo02.config, false);
/* Use the same session ID on the client and server to restore the session. */
testInfo02.clientSession = testInfo01.clientSession;
ASSERT_EQ(TryResumeByTheUsingSessionId(&testInfo02), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
ASSERT_TRUE(testInfo02.clientSession != NULL);
EXIT:
FreeLink(&testInfo01);
FreeLink(&testInfo02);
BSL_SAL_FREE(g_sessionId);
HITLS_CFG_FreeConfig(testInfo01.config);
FRAME_FreeLink(testInfo01.client);
FRAME_FreeLink(testInfo01.server);
HITLS_SESS_Free(testInfo01.clientSession);
HITLS_CFG_FreeConfig(testInfo02.config);
FRAME_FreeLink(testInfo02.client);
FRAME_FreeLink(testInfo02.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC001
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a connection between the client and server. Expected result 1 is obtained.
* 2. The client closes the link, obtains the message sent by the client, and checks whether the message is a
close_notify message. (Expected result 2)
* 3. The server obtains the received message and checks whether the message is a close_notify message. (Expected
result 3)
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server receives the close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
/* Establish a connection between the client and server. */
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_KEY_EXCHANGE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
/* The client closes the link, obtains the message sent by the client, and checks whether the message is a
* close_notify message. */
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
/* The server obtains the received message and checks whether the message is a close_notify message. */
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = { 0 };
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC002
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a connection between the client and server. Expected result 1 is obtained.
* 2. Close the link on the client, obtain the message sent by the client, and check whether the message is a
close_notify message. (Expected result 2)
* 3. The server processes the message received by the server, obtains the message to be sent after processing,
and checks whether the message is a close_notify message. (Expected result 3)
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server sends a close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
/* Establish a connection between the client and server. */
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_KEY_EXCHANGE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
/* Close the link on the client, obtain the message sent by the client, and check whether the message is a
* close_notify message. */
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
/* The server processes the message received by the server, obtains the message to be sent after processing, and
* checks whether the message is a close_notify message. */
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = { 0 };
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(server->ssl != NULL);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_IO_BUSY);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_CM_LINK_FATAL_ALERTED);
FRAME_Msg serverframeMsg1 = { 0 };
uint8_t *serverbuffer1 = serverioUserData->sndMsg.msg;
uint32_t serverreadLen1 = serverioUserData->sndMsg.len;
uint32_t serverparseLen1 = 0;
ret = ParserTotalRecord(server, &serverframeMsg1, serverbuffer1, serverreadLen1, &serverparseLen1);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg1.type == REC_TYPE_ALERT && serverframeMsg1.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg1.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg1.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC003
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a connection between the client and server. Expected result 1 is obtained.
* 2. The server closes the link, obtains the message sent by the server, and checks whether the message is a
close_notify message. (Expected result 2)
* 3. Obtain the received message and check whether the message is a close_notify message. (Expected result 3)
* @expect 1. The link is successfully established.
* 2. The server sends a close_notify message.
* 3. The client receives the close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC003(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
/* Establish a connection between the client and server. */
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(server, client, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
/* The server closes the link, obtains the message sent by the server, and checks whether the message is a
* close_notify message. */
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = { 0 };
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
/* Obtain the received message and check whether the message is a close_notify message. */
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC004
* @title Close the link and check whether the close_notify alarm is sent.
* @precon nan
* @brief 1. Establish a link between the client and server. Expected result 1 is obtained.
* 2. The server closes the link, obtains the message sent by the server, and checks whether the message is a
close_notify message. (Expected result 2)
* 3. The client processes the received message, obtains the message to be sent after processing, and checks
whether the message is a close_notify message. (Expected result 3)
* @expect 1. The link is successfully established.
* 2. The client sends a close_notify message.
* 3. The server sends a close_notify message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CLOSE_NOTIFY_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
/* Establish a link between the client and server. */
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CLIENT_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_KEY_EXCHANGE);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_KEY_EXCHANGE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
/* The server closes the link, obtains the message sent by the server, and checks whether the message is a
* close_notify message. */
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = { 0 };
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
clientioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
/* The client processes the received message, obtains the message to be sent after processing, and checks whether
* the message is a close_notify message. */
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(client->ssl != NULL);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_IO_BUSY);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_IO_BUSY);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_IO_BUSY);
clientioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_CM_LINK_FATAL_ALERTED);
FRAME_Msg clientframeMsg1 = { 0 };
uint8_t *clientbuffer1 = clientioUserData->sndMsg.msg;
uint32_t clientreadLen1 = clientioUserData->sndMsg.len;
uint32_t clientparseLen1 = 0;
ret = ParserTotalRecord(client, &clientframeMsg1, clientbuffer1, clientreadLen1, &clientparseLen1);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg1.type == REC_TYPE_ALERT);
ASSERT_TRUE(clientframeMsg1.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CM_CLOSE_SEND_ALERT_TC001
* @title Disable the link abnormally and check whether the two ends send an alert.
* @precon nan
* @brief 1. Establish a link between the client and server, and the client is disabling the link. Expected result 1 is
obtained.
* 2. Enable the client to send a critical alarm. Obtain the received message from the server and check whether
the message is an alert message. Expected result 2 is obtained.
* @expect 1. The link is successfully established and the client is in the closed state.
* 2. Check the alert message on the server.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CM_CLOSE_SEND_ALERT_TC001(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Establish a link between the client and server, and the client is disabling the link. */
FRAME_Msg parsedSHdone = { 0 };
FRAME_Type frameType = { 0 };
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_HELLO_DONE, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &parsedSHdone) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSHdone, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &parsedSHdone);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &parsedSHdone, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedSHdone.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedSHdone.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
// Enable the client to send a critical alarm. Obtain the received message from the server and check whether the
// message is an alert message.
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(testInfo.server->io);
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(testInfo.server, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_FATAL &&
clientframeMsg.body.alertMsg.description == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSHdone);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CM_CLOSE_SEND_ALERT_TC002
* @title Abnormally close the link and check whether the two ends send an alert.
* @precon nan
* @brief 1. Establish a link between the client and server. The client is in the link disabling state. Expected result
1 is obtained.
* 2. Enable the client to send a critical alarm, obtain the received message from the server, and check whether
the message is an alert message. Expected result 2 is obtained.
* @expect 1. The link is successfully established and the client is in the closed state.
* 2. Check the alert message on the server.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CM_CLOSE_SEND_ALERT_TC002(void)
{
HandshakeTestInfo testInfo = { 0 };
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
/* Establish a link between the client and server. The client is in the link disabling state. */
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
// Enable the client to send a critical alarm, obtain the received message from the server, and check whether the
// message is an alert message.
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client) == HITLS_SUCCESS);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(testInfo.client->io);
FRAME_Msg clientframeMsg = { 0 };
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(testInfo.client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_FATAL &&
clientframeMsg.body.alertMsg.description == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_NO_CERTIFICATE_RESERVED_ALERT_TC001
* @title The client receives the NO_CERTIFICATIONATE_RESERVED alarm.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client stops receiving server hello, the server sends the NO_CERTIFICATIONATE_RESERVED alarm and
observe the response from the client.
* @expect 1. The initialization is successful.
* 2. The client ignores the message. The client should not receive the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_NO_CERTIFICATE_RESERVED_ALERT_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
FRAME_Msg sndframeMsg = { 0 };
/* Use the default configuration items to configure the client and server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
/* When the client stops receiving server hello, the server sends the NO_CERTIFICATIONATE_RESERVED alarm and observe
* the response from the client. */
recvframeMsg.type = REC_TYPE_ALERT;
recvframeMsg.version = HITLS_VERSION_TLS12;
recvframeMsg.bodyLen = ALERT_BODY_LEN;
recvframeMsg.epochSeq = GetEpochSeq(0, 5);
recvframeMsg.body.alertMsg.level = ALERT_LEVEL_WARNING;
recvframeMsg.body.alertMsg.description = ALERT_NO_CERTIFICATE_RESERVED;
ASSERT_TRUE(PackFrameMsg(&recvframeMsg) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, recvframeMsg.buffer, recvframeMsg.len) == HITLS_SUCCESS);
ioUserData->sndMsg.len = 0;
ASSERT_TRUE(HITLS_Connect(clientTlsCtx) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
CleanRecordBody(&recvframeMsg);
CleanRecordBody(&sndframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_NO_CERTIFICATE_RESERVED_ALERT_TC002
* @title The client receives the NO_CERTIFICATIONATE_RESERVED alarm.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the server stops receiving the client keyexchange, the client sends the no_certificate_RESERVED alarm
and observe the message returned by the server.
* @expect 1. The initialization is successful.
* 2. The server ignores the message. The message cannot be received.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_NO_CERTIFICATE_RESERVED_ALERT_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
FRAME_Msg sndframeMsg = { 0 };
/* Use the default configuration items to configure the client and server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* When the server stops receiving the client keyexchange, the client sends the no_certificate_RESERVED alarm and
* observe the message returned by the server. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
recvframeMsg.type = REC_TYPE_ALERT;
recvframeMsg.version = HITLS_VERSION_TLS12;
recvframeMsg.bodyLen = ALERT_BODY_LEN;
recvframeMsg.epochSeq = GetEpochSeq(0, 5);
recvframeMsg.body.alertMsg.level = ALERT_LEVEL_WARNING;
recvframeMsg.body.alertMsg.description = ALERT_NO_CERTIFICATE_RESERVED;
ASSERT_TRUE(PackFrameMsg(&recvframeMsg) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, recvframeMsg.buffer, recvframeMsg.len) == HITLS_SUCCESS);
ioUserData->sndMsg.len = 0;
ASSERT_TRUE(HITLS_Accept(serverTlsCtx) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
CleanRecordBody(&recvframeMsg);
CleanRecordBody(&sndframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_EXPORT_RESTRICTION_RESERVED_ALERT_TC001
* @title The client receives an ERROR_RESTRICTION_RESERVED alarm.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client stops receiving server hello, the server sends the export_restriction_RESERVED alarm.
* @expect 1. The initialization is successful.
* 2. The client ignores the message. The client should not receive the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_EXPORT_RESTRICTION_RESERVED_ALERT_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
FRAME_Msg sndframeMsg = { 0 };
/* Use the default configuration items to configure the client and server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
/* When the client stops receiving server hello, the server sends the export_restriction_RESERVED alarm. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
recvframeMsg.type = REC_TYPE_ALERT;
recvframeMsg.version = HITLS_VERSION_TLS12;
recvframeMsg.bodyLen = ALERT_BODY_LEN;
recvframeMsg.epochSeq = GetEpochSeq(0, 5);
recvframeMsg.body.alertMsg.level = ALERT_LEVEL_WARNING;
recvframeMsg.body.alertMsg.description = ALERT_EXPORT_RESTRICTION_RESERVED;
ASSERT_TRUE(PackFrameMsg(&recvframeMsg) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, recvframeMsg.buffer, recvframeMsg.len) == HITLS_SUCCESS);
ioUserData->sndMsg.len = 0;
ASSERT_TRUE(HITLS_Connect(clientTlsCtx) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
CleanRecordBody(&recvframeMsg);
CleanRecordBody(&sndframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_EXPORT_RESTRICTION_RESERVED_ALERT_TC002
* @title The client receives an ERROR_RESTRICTION_RESERVED alarm.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the server receives the client keyexchange, the client sends the export_restriction_RESERVED alarm,
Check the message returned by the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server ignores the message. The message cannot be received.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_EXPORT_RESTRICTION_RESERVED_ALERT_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = { 0 };
FRAME_Msg sndframeMsg = { 0 };
/* Use the default configuration items to configure the client and server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* When the server receives the client keyexchange, the client sends the export_restriction_RESERVED alarm, check
* the message returned by the server. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
recvframeMsg.type = REC_TYPE_ALERT;
recvframeMsg.version = HITLS_VERSION_TLS12;
recvframeMsg.bodyLen = ALERT_BODY_LEN;
recvframeMsg.epochSeq = GetEpochSeq(0, 5);
recvframeMsg.body.alertMsg.level = ALERT_LEVEL_WARNING;
recvframeMsg.body.alertMsg.description = ALERT_EXPORT_RESTRICTION_RESERVED;
ASSERT_TRUE(PackFrameMsg(&recvframeMsg) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, recvframeMsg.buffer, recvframeMsg.len) == HITLS_SUCCESS);
ioUserData->sndMsg.len = 0;
ASSERT_TRUE(HITLS_Accept(serverTlsCtx) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
CleanRecordBody(&recvframeMsg);
CleanRecordBody(&sndframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS1_2_RFC5246_SERVER_CHOSE_VERSION_TC002
* @spec -
* @title Check the TLS protocol version carried in the serverHello message.
* @brief 1. Use the configuration items to configure the client and server. Change the record version in client hello
* to 0x0305 Expected result 1 is obtained.
* 2. Obtain and parse the server Hello message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The protocol version carried in the server Hello message is TLS1.2.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS1_2_RFC5246_SERVER_CHOSE_VERSION_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
recvBuf[2] = 0x05;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->version.data == HITLS_VERSION_TLS12);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
FRAME_CleanMsg(&frameType, &frameMsg);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_RENEGOTIATION_RECV_APP_TC001
* @spec -
* @title The client receives app after renegotiate request
* @precon nan
* @brief 1. The client receives app after sending renegotiate request client hello
* @expect 1.clien read app message success
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_RENEGOTIATION_RECV_APP_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetRenegotiationSupport(tlsConfig, true);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(clientTlsCtx) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_RENEGOTIATION);
uint8_t data[] = "Hello World";
uint32_t len;
HITLS_Write(server->ssl, data, sizeof(data), &len);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(readLen == sizeof(data) && memcmp(data, readBuf, readLen) == 0);
EXIT:
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_SEND_DATA_BEWTEEN_CCS_AND_FINISH
* @spec -
* @title The client receives a alert bewteen ccs and finish
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Server send a alert message bewteen ccs and finish. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. Client return HITLS_REC_ERR_DATA_BETWEEN_CCS_AND_FINISHED
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_SEND_DATA_BEWTEEN_CCS_AND_FINISH(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_FINISH), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->sndMsg.len = 0;
server->ssl->method.sendAlert(server->ssl, ALERT_LEVEL_FATAL, ALERT_CERTIFICATE_EXPIRED);
ALERT_Flush(server->ssl);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_ERR_DATA_BETWEEN_CCS_AND_FINISHED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_Fragmented_Msg_FUNC_TC001
* @spec -
* @title The client receives a Fragmented_Msg during handshake
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Server send a Fragmented_Msg during handshake. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. Client return HITLS_REC_ERR_DATA_BETWEEN_CCS_AND_FINISHED
* @prior Level 1
* @auto TRUE
@ */
// To test whether fragmented messages can be received correctly. Test REC_TlsReadNbytes
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_Fragmented_Msg_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
int32_t ret = HITLS_Connect(client->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(client->ssl->hsCtx->state == TRY_RECV_SERVER_HELLO);
ret = HITLS_Accept(server->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(server->ssl->hsCtx->state == TRY_RECV_CLIENT_HELLO);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t dataLen = MAX_RECORD_LENTH;
ASSERT_EQ(memcpy_s(data, MAX_RECORD_LENTH, ioUserData->sndMsg.msg, ioUserData->sndMsg.len), 0);
dataLen = ioUserData->sndMsg.len;
uint32_t msglength = BSL_ByteToUint16(&data[3]);
uint32_t msgLen = (msglength - 1) / 2;
uint32_t len = 5 + msgLen;
uint8_t recorddata1[] = {0x16, 0x03, 0x03, 0x00, 0x46};
BSL_Uint16ToByte((uint16_t)msgLen, &recorddata1[3]);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg, MAX_RECORD_LENTH, data, len), 0);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg, MAX_RECORD_LENTH, recorddata1, sizeof(recorddata1)), 0);
uint8_t recorddata2[] = {0x16, 0x03, 0x03, 0x00, 0x53};
msgLen = dataLen - len;
BSL_Uint16ToByte((uint16_t)msgLen, &recorddata2[3]);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg + len, MAX_RECORD_LENTH - len, recorddata2, sizeof(recorddata2)), 0);
ioUserData->sndMsg.len = len + 5;
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg + ioUserData->sndMsg.len, MAX_RECORD_LENTH - len, data + len, dataLen - len), 0);
ioUserData->sndMsg.len += dataLen - len;
ret = HITLS_Connect(client->ssl);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ret = HITLS_Accept(server->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_IO_BUSY);
ret = HITLS_Accept(server->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC001
* @spec -
* @title There is no alert during the handshake. When the handshake is halfway through, call close to break the chain,
* and then call hitls_read to read the plaintext app message. It is expected that the read message failed.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Stop the handshake state at TRY_RECV_SERVER_HELLO, expected result 2.
* 3. Call the hitls_close interface to break the chain, expected result 3.
* 4. Construct plaintext app message, call hitls_read to read, expected result 4.
* @expect 1. The initialization is successful.
* 2. Return successful.
* 3. Return successful.
* 4. Reading message failed.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t data[] = {0x17, 0x03, 0x03, 0x00, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, sizeof(data));
ioUserData->recMsg.len = sizeof(data);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_CLOSED);
ASSERT_TRUE(readLen == 0);
EXIT:
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC002
* @spec -
* @title A fatal alert occurred during the handshake process. Call close to break the chain,
* and then call hitls_read to read the plaintext app message. It is expected that the read message failed.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Certificate and algorithm set do not match, establish connection, expected result 2.
* 3. Call the hitls_close interface to break the chain, expected result 3.
* 4. Construct plaintext app message, call hitls_read to read, expected result 4.
* @expect 1. The initialization is successful.
* 2. Send a fatal alert, ALERT_BAD_CERTIFICATE.
* 3. Return successful.
* 4. Reading message failed.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuits[] = {HITLS_RSA_WITH_AES_128_CBC_SHA256};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
FRAME_CertInfo certInfo = {
"rsa_sha512/root.der",
"rsa_sha512/intca.der",
"rsa_sha512/usageKeyEncipher.der",
0,
"rsa_sha512/usageKeyEncipher.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
clientTlsCtx->config.tlsConfig.needCheckKeyUsage = true;
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_KEYUSAGE);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(client->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_UNSUPPORTED_CERTIFICATE);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t data[] = {0x17, 0x03, 0x03, 0x00, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, sizeof(data));
ioUserData->recMsg.len = sizeof(data);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_CLOSED);
ASSERT_TRUE(readLen == 0);
EXIT:
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
int32_t STUB_HS_DoHandshake_Warning(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t plainLen)
{
(void)recordType;
(void)data;
(void)plainLen;
ctx->method.sendAlert(ctx, ALERT_LEVEL_WARNING, ALERT_NO_CERTIFICATE_RESERVED);
return HITLS_INTERNAL_EXCEPTION;
}
static int32_t UioWriteException(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
{
(void)uio;
(void)buf;
(void)len;
(void)writeLen;
return BSL_UIO_IO_EXCEPTION;
}
/* @
* @test UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC003
* @spec -
* @title During the handshake process, a warning alert occurred, and the construction of the alert failed to send. The
* status then changed to alerting, call close to break the chain, and then call hitls_read to read the plaintext app
* message. It is expected that the read message failed.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Suspend the sending of warning alerts, expected result 2.
* 3. Call the hitls_close interface to break the chain, expected result 3.
* 4. Construct plaintext app message, call hitls_read to read, expected result 4.
* @expect 1. The initialization is successful.
* 2. The status then changed to alerting.
* 3. Return successful.
* 4. Reading message failed.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_READ_AFTER_CLOSE_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetRenegotiationSupport(tlsConfig, true);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
FuncStubInfo tmpRpInfo = { 0 };
STUB_Init();
STUB_Replace(&tmpRpInfo, HS_DoHandshake, STUB_HS_DoHandshake_Warning);
BSL_UIO *uio = HITLS_GetReadUio(clientTlsCtx);
BSL_UIO_Method *method = (BSL_UIO_Method *)BSL_UIO_GetMethod(uio);
ASSERT_EQ(BSL_UIO_SetMethod(method, BSL_UIO_WRITE_CB, UioWriteException), BSL_SUCCESS);
ASSERT_TRUE(HITLS_Connect(clientTlsCtx) == HITLS_REC_ERR_IO_EXCEPTION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTING);
ASSERT_EQ(BSL_UIO_SetMethod(method, BSL_UIO_WRITE_CB, FRAME_Write), BSL_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t data[] = {0x17, 0x03, 0x03, 0x00, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, sizeof(data));
ioUserData->recMsg.len = sizeof(data);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_CLOSED);
ASSERT_TRUE(readLen == 0);
EXIT:
STUB_Reset(&tmpRpInfo);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_CLIENT_HELLO_ENCRYPT_THEN_MAC_TC001
* @spec -
* @title Check the encrypt then mac extension carried in the clientHello message.
* @precon nan
* @brief 1. Use configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Obtain and parse the client Hello message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The encrypt then mac extension carried in the client Hello message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_CLIENT_HELLO_ENCRYPT_THEN_MAC_TC001(void)
{
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_RECV_CLIENT_HELLO;
FRAME_Init();
/* Use configuration items to configure the client and server. */
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
ASSERT_EQ(testInfo.client->ssl->config.tlsConfig.isEncryptThenMac, 1);
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(testInfo.server->ssl->config.tlsConfig.isEncryptThenMac, 1);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, testInfo.state) ==
HITLS_SUCCESS);
/* Obtain and parse the client Hello message. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_EQ(clientMsg->encryptThenMac.exType.data, HS_EX_TYPE_ENCRYPT_THEN_MAC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_CLIENT_PSK_FUNC_TC001
* @spec -
* @title Client configured with PSK ciphersuite, without PSK callback, clienthello failed to be sent.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Set PSK ciphersuite, expected result 2
* 3. Call HITLS_Connect to start handshake, expected result 3
* @expect 1. The initialization is successful.
* 2. Return success.
* 3. Clienthello send failed, return HITLS_PACK_CLIENT_CIPHER_SUITE_ERR
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_CLIENT_PSK_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t cipherSuits[] = {HITLS_RSA_PSK_WITH_AES_128_CBC_SHA};
HITLS_CFG_SetCipherSuites(config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_PACK_CLIENT_CIPHER_SUITE_ERR);
ASSERT_TRUE(client->ssl->hsCtx->state == TRY_SEND_CLIENT_HELLO);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS1_2_RFC5246_CLIENT_PSK_FUNC_TC002
* @spec -
* @title Client configured with PSK ciphersuite, with PSK callback, server use default config and with PSK callback
* configured. Handshake will success.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1
* 2. Set PSK ciphersuite to client and set psk callback to both client and server, expected result 2
* 3. Start handshake, expected result 3
* @expect 1. The initialization is successful.
* 2. Return success.
* 3. Handshake success.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS1_2_RFC5246_CLIENT_PSK_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *c_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(s_config != NULL);
ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(c_config, ExampleClientCb) == 0);
ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(s_config, ExampleServerCb) == 0);
uint16_t cipherSuits[] = {HITLS_RSA_PSK_WITH_AES_128_CBC_SHA};
HITLS_CFG_SetCipherSuites(c_config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc5246.c | C | unknown | 247,867 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
#include <stdio.h>
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "cert.h"
#include "securec.h"
#include "frame_msg.h"
#include "alert.h"
#include "bsl_list.h"
#include "app_ctx.h"
#include "hs_kx.c"
#include "common_func.h"
#include "stub_crypt.h"
/* END_HEADER */
#define g_uiPort 45678
#define PREMASTERSECRETLEN 1534
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_SEND_CERTFICATE_TC001 rfc 5246 table row 51
* @title If the server has sent a CertificateRequest message, the client must send a certificate message.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, and enable the dual-end verification. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP link request. After the server receives the CertificateRequest message, expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client is in the TRY_SEND_CLIENT_KEY_EXCHANGE state, and the server is in the TRY_RECV_CERTIFICATIONATE state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_HANDSHAKE_SEND_CERTFICATE_TC001(void)
{
/* Use the default configuration items to configure the client and server, and enable the dual-end verification. */
HandshakeTestInfo testInfo = { 0 };
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
testInfo.state = TRY_SEND_SERVER_HELLO_DONE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
testInfo.isSupportNoClientCert = false;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == HITLS_SUCCESS);
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO_DONE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
// The client initiates a TLS over TCP link request.
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(testInfo.client->ssl->hsCtx->state, TRY_SEND_CLIENT_KEY_EXCHANGE);
uint32_t readLen = 0;
uint8_t tmp1[MAX_RECORD_LENTH] = {0};
uint32_t tmp1Len = sizeof(tmp1);
ASSERT_TRUE(FRAME_TransportSendMsg(testInfo.client->io, tmp1, tmp1Len, &readLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t tmp2[MAX_RECORD_LENTH] = {0};
uint32_t tmp2Len = sizeof(tmp2);
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, tmp2, tmp2Len) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SIGNATION_NOT_SUITABLE_CERT_TC002
* @title During dual-end verification, the server signature algorithm does not match the certificate signature algorithm. As a result, the link fails to be established.
* @precon nan
* @brief 1. Configure dual-end verification. Set the signature algorithm to RSA_PKCS1_SHA256, cipher suite to RSA, certificate to RSA, and certificate signature to ECDSA_SHA256 on the server. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SIGNATION_NOT_SUITABLE_CERT_TC002(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// 1. Configure dual-end verification. Set the signature algorithm to RSA_PKCS1_SHA256, cipher suite to RSA, certificate to RSA, and certificate signature to ECDSA_SHA256 on the server.
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
uint32_t signAlgsSize = sizeof(signAlgs) / sizeof(uint16_t);
HITLS_CFG_SetSignature(config, signAlgs, signAlgsSize);
HITLS_CFG_SetClientVerifySupport(config, true);
uint16_t cipherSuites[] = {HITLS_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t));
FRAME_CertInfo certInfoServer = {
"ecdsa/ca-nist521.der",
"rsa_sha/inter-3072.der",
"rsa_sha/end-sha256.der",
0,
"rsa_sha/end-sha256.key.der",
0,
};
FRAME_CertInfo certInfoClient = {
"rsa_sha/ca-3072.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC003
* @title During dual-end verification, the intermediate certificate is incorrectly configured on the client. As a result, the verification on the server fails.
* @precon nan
* @brief 1. Configure dual-ended authentication. Configure a correct terminal certificate and an incorrect intermediate certificate on the client. Configure a correct certificate chain on the server. Check whether the server fails the authentication. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC003(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
// 1. Configure dual-ended authentication. Configure a correct terminal certificate and an incorrect intermediate certificate on the client. Configure a correct certificate chain on the server. Check whether the server fails the authentication.
FRAME_CertInfo certInfoClient = {
"ecdsa/ca-nist521.der",
"rsa_sha/inter-3072.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_SIGNATION_FAIL_TC001
* @title Link setup failed because the signature algorithm does not match.
* @precon nan
* @brief 1. Set the client signature algorithm to ECDSA_SECP256R1_SHA256 and the server certificate signature algorithm to RSA_SHA256. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_SIGNATION_FAIL_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
uint32_t signAlgsSize = sizeof(signAlgs) / sizeof(uint16_t);
FRAME_CertInfo certInfo = {
"rsa_sha/ca-3072.der:rsa_sha/inter-3072.der",
"rsa_sha/inter-3072.der",
"rsa_sha/end-sha256.der",
0,
"rsa_sha/end-sha256.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
// 1. Set the client signature algorithm to ECDSA_SECP256R1_SHA256 and the server certificate signature algorithm to RSA_SHA256.
ASSERT_TRUE(HITLS_SetSigalgsList(client->ssl, signAlgs, signAlgsSize) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_SIGNATION_NOT_SUITABLE_CERT_TC001
* @title The certificate signature algorithm does not match the signature algorithm set on the client. As a result, the link fails to be established.
* @precon nan
* @brief 1. Set the signature algorithm to RSA_PKCS1_SHA256 on the client, cipher suite to RSA, certificate to RSA,
server signature algorithm to RSA_PKCS1_SHA256, and certificate signature algorithm to ECDSA_SHA256.
Expected Certificate Verification Failure (Failed to Select a Certificate on the Server)
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_SIGNATION_NOT_SUITABLE_CERT_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// 1. Set the signature algorithm to RSA_PKCS1_SHA256 on the client, cipher suite to RSA, certificate to RSA,
// server signature algorithm to RSA_PKCS1_SHA256, and certificate signature algorithm to ECDSA_SHA256.
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
uint32_t signAlgsSize = sizeof(signAlgs) / sizeof(uint16_t);
HITLS_CFG_SetSignature(config, signAlgs, signAlgsSize);
HITLS_CFG_SetClientVerifySupport(config, true);
uint16_t cipherSuites[] = {HITLS_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t));
FRAME_CertInfo certInfoClient = {
"ecdsa/ca-nist521.der",
"rsa_sha/inter-3072.der",
"rsa_sha/end-sha256.der",
0,
"rsa_sha/end-sha256.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha/ca-3072.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC004
* @title During dual-end authentication, the client certificate is out of order, and the link fails to be established.
* @precon nan
* @brief 1. Configure dual-end verification. Configure the first certificate on the client as an intermediate
certificate and the second certificate as a terminal certificate. Configure a correct certificate chain
on the server. Check whether the verification fails on the server. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC004(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Change the certificate sequence. The intermediate certificate is used first, and then the device certificate is used.
// 1. Configure dual-end authentication. Configure the first certificate on the client as an intermediate
// certificate, and the second certificate as a terminal certificate. Configure a correct certificate chain on the server.
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
struct FrameCertItem_ *tmp = frameMsg.body.hsMsg.body.certificate.certItem->next;
frameMsg.body.hsMsg.body.certificate.certItem->next = tmp->next; // a->c
tmp->next = frameMsg.body.hsMsg.body.certificate.certItem; // b -> a
frameMsg.body.hsMsg.body.certificate.certItem = tmp;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_PARSE_VERIFY_SIGN_FAIL);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t STUB_SAL_CERT_KeyDecrypt_Fail(const HITLS_CipherParameters *cipher, const uint8_t *in, uint32_t inLen,
uint8_t *out, uint32_t *outLen)
{
(void)cipher;
(void)in;
(void)inLen;
(void)out;
(void)outLen;
return HITLS_CRYPT_ERR_DECRYPT;
}
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_DECRYPT_FAIL_TC005
* @title After the premasterkey to be received by the server is modified, the connection fails to be established.
* @precon nan
* @brief 1. Configure the RSA cipher suite. When the server receives the premasterkey, change the value of the
premasterkey and construct a decryption failure scenario. It is expected that CCS messages are sent normally
and the link fails to be established. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_DECRYPT_FAIL_TC005(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
// 1.Configure the RSA cipher suite. Change the value of the premasterkey when the server receives the premasterkey.
// Construct a decryption failure scenario. The CCS message is expected to be sent normally.
uint16_t cipherSuits[] = {HITLS_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
STUB_Init();
FuncStubInfo tmpStubInfo;
STUB_Replace(&tmpStubInfo, SAL_CERT_KeyDecrypt, STUB_SAL_CERT_KeyDecrypt_Fail);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
STUB_Reset(&tmpStubInfo);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC003
* @title The certificate carries the correct keyuage extension, and the link is successfully established.
* @precon nan
* @brief 1. Configure the server certificate with the keyuage extension and support digitalSignature. The link is successfully established. Expected result 1 is obtained.
* @expect 1. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC003(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
// 1. Set the server certificate with the keyuage extension and support digitalSignature.
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RENEGOTIATION_UNSUPPORTED_TC001
* @title Invoke the hitls_connect/hitls_accept interface to initiate renegotiation. The peer end does not support renegotiation.
* @precon nan
* @brief 1. Invoke the hitls_renegotiate interface to initiate renegotiation. Expected result 1 is obtained.
2. Invoke the hitls_connect/hitls_accept interface to initiate renegotiation. The peer end does not support renegotiation. Expected result 2 is obtained.
* @expect 1. The link enters the renegotiation state.
2. The peer end returns a warning alert.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RENEGOTIATION_UNSUPPORTED_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
HITLS_SetRenegotiationSupport(client->ssl, true);
// 1. Invoke the hitls_renegotiate interface to initiate renegotiation.
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
// Invoke the hitls_connect/hitls_accept interface to initiate renegotiation. The peer end does not support renegotiation.
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(client->ssl->state, CM_STATE_RENEGOTIATION);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
// Send a warning alert and ALERT_NO_RENEGOTIATION message. After receiving the message, the peer send fatal alert.
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(client->ssl->state, CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t Stub_GenPremasterSecretFromEcdhe(TLS_Ctx *ctx, uint8_t *preMasterSecret, uint32_t *preMasterSecretLen)
{
int32_t ret = SAL_CRYPT_CalcEcdhSharedSecret(LIBCTX_FROM_CTX(ctx), ATTRIBUTE_FROM_CTX(ctx),
ctx->hsCtx->kxCtx->key, ctx->hsCtx->kxCtx->peerPubkey,
ctx->hsCtx->kxCtx->pubKeyLen, preMasterSecret, preMasterSecretLen);
*preMasterSecretLen = PREMASTERSECRETLEN;
return ret;
}
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_ECDHE_PSK_TC001
* @title After the premasterkey to be received by the server is modified, the connection fails to be established.
* @precon nan
* @brief 1. Configure the PSK cipher suite. When generating the premasterkey, change the preMasterSecretLen parameter
of GenPremasterSecretFromEcdhe to 1534 to reach the maximum value of the secret and check whether it is out
of bounds.
* @expect 1. The link success.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_ECDHE_PSK_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t cipherSuits[] = {HITLS_ECDHE_PSK_WITH_AES_128_CBC_SHA};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(config, ExampleClientCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(config, ExampleServerCb) == HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
STUB_Init();
FuncStubInfo tmpStubInfo;
STUB_Replace(&tmpStubInfo, GenPremasterSecretFromEcdhe, Stub_GenPremasterSecretFromEcdhe);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
STUB_Reset(&tmpStubInfo);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_RSA_REMASTER_TC001
* @title After the premasterkey to be received by the server is modified, the connection fails to be established.
* @precon nan
* @brief 1. Configure the RSA cipher suite. When the server receives the premasterkey, change the value of the
premasterkey and construct a decryption failure scenario. It is expected that CCS messages are sent normally
and the link fails to be established. Expected result 1 is obtained.
* @expect 1. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_RSA_REMASTER_TC001(int rsaEncryptLen)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, false);
// 1.Configure the RSA cipher suite. Change the value of the premasterkey when the server receives the premasterkey.
// Construct a decryption failure scenario. The CCS message is expected to be sent normally.
uint16_t cipherSuits[] = {HITLS_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_KEY_EXCHANGE), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_KEY_EXCHANGE;
frameType.keyExType = HITLS_KEY_EXCH_RSA;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
frameMsg.body.hsMsg.body.clientKeyExchange.pubKeySize.data = rsaEncryptLen;
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientKeyExchange.pubKey.data);
frameMsg.body.hsMsg.body.clientKeyExchange.pubKey.data = BSL_SAL_Calloc(1,rsaEncryptLen);
frameMsg.body.hsMsg.body.clientKeyExchange.pubKey.size = rsaEncryptLen;
uint32_t sendLen = MAX_RECORD_LENTH;
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, recvBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = sendLen;
HITLS_Accept(server->ssl);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC004
* @title The certificate without keyuage extension, and the link is successfully established.
* @precon nan
* @brief 1. Configure the server certificate without keyuage extension and support CheckKeyUsage. The link is successfully established. Expected result 1 is obtained.
* @expect 1. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC004(int isCheckKeyUsage)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t cipherSuits[] = {HITLS_RSA_WITH_AES_128_CBC_SHA256};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t)) == HITLS_SUCCESS);
HITLS_CFG_SetClientVerifySupport(config, true);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
if (isCheckKeyUsage) {
HITLS_SetCheckKeyUsage(client->ssl, true);
} else {
HITLS_SetCheckKeyUsage(client->ssl, false);
}
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_EXTRA_CHAIN_CERT_TC001
* @title Set the intermediate certificates separately to the extra_chain and the chain, where the correct intermediate
certificate is stored in the extra_chain and the incorrect intermediate certificate is stored in the chain, proving
that the priority of the chain is higher than that of extra_chain.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Analyze valid and invalid intermediate certificates separately. Expected result 2 is obtained.
3. Set valid intermediate certificates to extra_chain and invalid intermediate certificates to chain. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. The parsing was successful.
3. Successfully set up.
4. establish the link failed, returned HITLS_CERT_ERR_VERIFY_CERT_CHAIN, proving that the priority of the chain is higher than extra_chain.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_EXTRA_CHAIN_CERT_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
char invalidIntCaFile[] = "../testdata/tls/certificate/der/ecdsa_sha256/inter.der";
char validIntCaFile[] = "../testdata/tls/certificate/der/rsa_sha256/inter.der";
HITLS_CERT_X509 *invalidIntCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)invalidIntCaFile,
strlen(invalidIntCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
HITLS_CERT_X509 *validIntCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)validIntCaFile,
strlen(validIntCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(invalidIntCa != NULL);
ASSERT_TRUE(validIntCa != NULL);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_CFG_AddChainCert(&server->ssl->config.tlsConfig, invalidIntCa, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddExtraChainCert(&server->ssl->config.tlsConfig, validIntCa) == HITLS_SUCCESS);
HITLS_CERT_Chain *extraChainCert = HITLS_CFG_GetExtraChainCerts(&server->ssl->config.tlsConfig);
ASSERT_TRUE(extraChainCert->count == 1);
ASSERT_TRUE(extraChainCert != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_EXTRA_CHAIN_CERT_TC002
* @title Set the intermediate certificates separately to the extra_chain and store, where the correct intermediate
certificate is stored in the extra_chain and the incorrect intermediate certificate is stored in the store, proving
that the priority of extra_chain is higher than that of the store.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Analyze valid and invalid intermediate certificates separately Expected result 2 is obtained.
3. Set valid intermediate certificates to extra_chain and invalid intermediate certificates to store. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. The parsing was successful.
3. Successfully set up.
4. The link is set up successfullyl, returns HITLS_SUCCESS, proving that extra_chain has a higher priority than store.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_EXTRA_CHAIN_CERT_TC002()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
char validIntCaFile[] = "../testdata/tls/certificate/der/rsa_sha256/inter.der";
HITLS_CERT_X509 *validIntCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)validIntCaFile,
strlen(validIntCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(validIntCa != NULL);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"ecdsa_sha256/inter.der",
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_CFG_AddExtraChainCert(&server->ssl->config.tlsConfig, validIntCa) == HITLS_SUCCESS);
HITLS_CERT_Chain *extraChainCert = HITLS_CFG_GetExtraChainCerts(&server->ssl->config.tlsConfig);
ASSERT_TRUE(extraChainCert->count == 1);
ASSERT_TRUE(extraChainCert != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC001
* @title
* @precon Set up a certificate chain from the chain, but there is no certificate in the chain chain. There is a
certificate in the store, and it is expected that the connection will be successfully established.
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Load the intermediate certificate into the store. Expected result 2 is obtained.
3. Call the HITLS_CFG_BuildCertChain function to group the certificate chain, set the flag to HITLS_SBILD_CHAIN_LAGCHECK
. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. Successfully set up.
3. Return success.
4. The link is set up successfullyl, returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC001()
{
#ifdef HITLS_TLS_CONFIG_CERT_BUILD_CHAIN
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_CFG_BuildCertChain(&server->ssl->config.tlsConfig, HITLS_BUILD_CHAIN_FLAG_CHECK) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC002
* @title Set up a certificate chain from the store, but there is no certificate in the store. There is a certificate in
the chain, and it is expected that the chain will be empty after the chain is formed, resulting in a connection failure.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Set the intermediate certificate to the chain, but there are no certificates in the Cert_store and Chain_store.
Expected result 2 is obtained.
3. Call HITLS_CFG_BuildCertChain to group the certificate chain and set the flag to 0. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. Successfully set up.
3. Return success.
4. Failed to establish connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC002()
{
#ifdef HITLS_TLS_CONFIG_CERT_BUILD_CHAIN
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
char rootCaFile[] = "../testdata/tls/certificate/der/rsa_sha256/ca.der";
char intCaFile[] = "../testdata/tls/certificate/der/rsa_sha256/inter.der";
HITLS_CERT_X509 *rootCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)rootCaFile,
strlen(rootCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(rootCa != NULL);
HITLS_CERT_X509 *intCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)intCaFile,
strlen(intCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(intCa != NULL);
HITLS_CERT_Store *store = SAL_CERT_StoreNew(config_s->certMgrCtx);
ASSERT_TRUE(store != NULL);
SAL_CERT_StoreCtrl(config_s, store, CERT_STORE_CTRL_ADD_CERT_LIST, rootCa, NULL);
HITLS_CFG_SetVerifyStore(config_s, store, 0);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
0,
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_CFG_AddChainCert(&server->ssl->config.tlsConfig, intCa, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_BuildCertChain(&server->ssl->config.tlsConfig, 0) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC003
* @title Set up a certificate chain from the chain, where there are multiple certificates and unrelated certificates.
It is expected that after the chain is formed, only the certificates that make up the certificate chain will be
present, and the connection will be successfully established.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Load 2 different intermediate certificates into the chain, and there is only 1 available intermediate certificate.
Expected result 2 is obtained.
3. Call the HITLS_CFG_BuildCertChain function to group the certificate chain, set the flag to HITLS_SBILD_CHAIN_LAGCHECK
, Obtain the number of certificates in the chain after the chain is formed. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. The parsing was successful.
3. Obtaining only one certificate in the chain.
4. The link is set up successfullyl, returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC003()
{
#ifdef HITLS_TLS_CONFIG_CERT_BUILD_CHAIN
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
char intCaFile1[] = "../testdata/tls/certificate/der/rsa_sha256/inter.der";
HITLS_CERT_X509 *intCa1 = HITLS_CFG_ParseCert(config_s, (const uint8_t *)intCaFile1,
strlen(intCaFile1) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(intCa1 != NULL);
char intCaFile2[] = "../testdata/tls/certificate/der/ecdsa_sha256/inter.der";
HITLS_CERT_X509 *intCa2 = HITLS_CFG_ParseCert(config_s, (const uint8_t *)intCaFile2,
strlen(intCaFile2) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(intCa2 != NULL);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_CFG_AddChainCert(&server->ssl->config.tlsConfig, intCa1, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddChainCert(&server->ssl->config.tlsConfig, intCa2, false) == HITLS_SUCCESS);
HITLS_CERT_Chain *chainCertList = HITLS_CFG_GetChainCerts(&server->ssl->config.tlsConfig);
ASSERT_EQ(BSL_LIST_COUNT(chainCertList), 2);
ASSERT_TRUE(HITLS_CFG_BuildCertChain(&server->ssl->config.tlsConfig, HITLS_BUILD_CHAIN_FLAG_CHECK) == HITLS_SUCCESS);
chainCertList = HITLS_CFG_GetChainCerts(&server->ssl->config.tlsConfig);
ASSERT_EQ(BSL_LIST_COUNT(chainCertList), 1);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC004
* @title Set the flag to HITLS_BUILD_CHAIN_FLAG_NO_ROOT, and set both the intermediate certificate and the root
* certificate to the certificate store. It is expected that the root certificate will not appear in the certificate
* chain after the completion of the chain assembly.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. Load the root certificate and intermediate certificate into CertStore. Expected result 2 is obtained.
3. Call the HITLS_BuildCertChain function to group the certificate chain, set the flag to HITLS_BUILD_CHAIN_FLAG_NO_ROOT
, Obtain the number of certificates in the chain after the chain is formed. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. The parsing was successful.
3. Obtaining only one certificate in the chain.
4. The link is set up successfully, returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_BUILD_CERT_CHAIN_TC004()
{
#ifdef HITLS_TLS_CONFIG_CERT_BUILD_CHAIN
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetVerifyNoneSupport(config_c, false);
char intCaFile[] = "../testdata/tls/certificate/der/rsa_sha256/inter.der";
HITLS_CERT_X509 *intCa = HITLS_CFG_ParseCert(config_s, (const uint8_t *)intCaFile,
strlen(intCaFile) + 1, TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(intCa != NULL);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
0,
"rsa_sha256/server.der",
0,
"rsa_sha256/server.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
HITLS_CERT_Store *store = HITLS_CFG_GetCertStore(&server->ssl->config.tlsConfig);
ASSERT_TRUE(store != NULL);
SAL_CERT_StoreCtrl(&server->ssl->config.tlsConfig, store, CERT_STORE_CTRL_ADD_CERT_LIST, intCa, NULL);
ASSERT_TRUE(HITLS_BuildCertChain(server->ssl, HITLS_BUILD_CHAIN_FLAG_NO_ROOT) == HITLS_SUCCESS);
HITLS_CERT_Chain *chainCertList = HITLS_CFG_GetChainCerts(&server->ssl->config.tlsConfig);
ASSERT_EQ(BSL_LIST_COUNT(chainCertList), 1);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
/**
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC001
* @title Verify the certificate chain in three ways: from a file, from a directory, and from a single CA certificate.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Load the certificate chain from a file. Expected result 2 is obtained.
* 3. Continue to establish the link. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The parsing was successful.
* 3. The link is set up successfully, returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetClientVerifySupport(config_s, true);
FRAME_CertInfo certInfoClient = {
"rsa_sha256/ca.der", 0, 0, 0, 0, 0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
const char *path = "../testdata/tls/certificate/pem/rsa_sha256/cert_chain.pem";
int32_t ret = HITLS_CFG_UseCertificateChainFile(config_c, path);
ASSERT_TRUE(ret == HITLS_SUCCESS);
const char *keyPath = "../testdata/tls/certificate/pem/rsa_sha256/client.key.pem";
HITLS_CFG_LoadKeyFile(config_c, keyPath, TLS_PARSE_FORMAT_PEM);
ASSERT_TRUE(ret == HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC002
* @title Verify the certificate chain in three ways: from a file, from a directory, and from a single CA certificate.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Load the certificate chain from a directory. Expected result 2 is obtained.
* 3. Continue to establish the link. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The parsing was successful.
* 3. The link is set up successfully, returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC002()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetClientVerifySupport(config_s, true);
FRAME_CertInfo certInfoClient = {
0,
0,
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
const char *path = "../testdata/tls/certificate/pem/rsa_sha256";
int32_t ret = HITLS_CFG_LoadVerifyDir(config_c, path);
ASSERT_TRUE(ret == HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC003
* @title Verify the certificate chain in three ways: from a file, from a directory, and from a single CA certificate.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Load the certificate chain from a single CA certificate. Expected result 2 is obtained.
* 3. Continue to establish the link. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The parsing was successful.
* 3. The link is set up successfully, returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_VERIFY_CHAIN_TC003()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_s != NULL);
HITLS_CFG_SetClientVerifySupport(config_s, true);
FRAME_CertInfo certInfoClient = {
0,
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
FRAME_CertInfo certInfoServer = {
"rsa_sha256/ca.der",
"rsa_sha256/inter.der",
"rsa_sha256/client.der",
0,
"rsa_sha256/client.key.der",
0,
};
const char *caPath = "../testdata/tls/certificate/pem/rsa_sha256/ca.pem";
int32_t ret = HITLS_CFG_LoadVerifyFile(config_c, caPath);
ASSERT_TRUE(ret == HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfoClient);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfoServer);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc5246_cert.c | C | unknown | 52,685 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <semaphore.h>
#include "process.h"
#include "securec.h"
#include "hitls_error.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "bsl_sal.h"
#include "simulate_io.h"
#include "tls.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "alert.h"
#include "session_type.h"
#include "process.h"
#include "hitls_type.h"
#include "rec.h"
#include "hs_msg.h"
#include "hs_extensions.h"
#include "frame_msg.h"
#include "stub_crypt.h"
/* END_HEADER */
#define BUF_TOOLONG_LEN ((1 << 14) + 1)
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isServerExtendMasterSecret;
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo)
{
/* Construct a link. */
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/*Set up a link and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/* Construct the configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark(testInfo);
}
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC001
* @title The client sends a Client Certificate message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. The client initiates a DTLS link creation request. When the client needs to send a Client Certificate message, the two fields are modified as follows:
Certificates Length is 2 ^ 14 + 1
Certificates are changed to 2 ^ 14 + 1 byte buffer.
After the modification is complete, send the modification to the server. Expected result 2 is obtained.
3. When the server receives the Client Certificate message, check the value returned by the HITLS_Accept interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC001(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC002
* @title The server sends a Server Certificate message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. The client initiates a DTLS link creation request. When the server needs to send a Server Certificate message, the server modifies the following two fields:
Certificates Length is 2 ^ 14 + 1
Certificates are changed to 2 ^ 14 + 1 byte buffer.
After the modification is complete, send the modification to the server. Expected result 2 is obtained.
3. When the client receives the Server Certificate message, check the value returned by the HITLS_Connect interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Connect interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC002(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CERTIFICATE;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CERTIFICATE;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.certificate.certItem->cert.data);
frameMsg.body.hsMsg.body.certificate.certItem->cert.data = certDataTemp;
frameMsg.body.hsMsg.body.certificate.certItem->cert.size = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->cert.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.data = BUF_TOOLONG_LEN;
frameMsg.body.hsMsg.body.certificate.certItem->certLen.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC003
* @title The client sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. When the client initiates a DTLS link establishment request and sends a Change Cipher Spec message, the client modifies one field as follows:
Length is 2 ^ 14 + 1. After the modification is complete, send the modification to the server. Expected result 2 is obtained.
3. When the server receives the Change Cipher Spec message, check the value returned by the HITLS_Accept interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC003(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_CLIENT_KEY_EXCHANGE;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.ccsMsg.extra.data);
frameMsg.body.ccsMsg.extra.data = certDataTemp;
frameMsg.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CERTIFICATE_VERIFY);
bool isCcsRecv = testInfo.server->ssl->method.isRecvCCS(testInfo.server->ssl);
ASSERT_TRUE(isCcsRecv == false);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC004
* @title The client receives a Change Cipher Spec message with a length of zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
2. The client initiates a DTLS over SCTP link request. After sending a Finish message, the client constructs a Change Cipher Spec message with a zero length and sends the message to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
2. The client receives the message, which is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MSGLENGTH_TOOLONG_TC004(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_SEND_FINISH;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server) == HITLS_SUCCESS);
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType1, &frameMsg1) == HITLS_SUCCESS);
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg1.body.ccsMsg.extra.data);
frameMsg1.body.ccsMsg.extra.data = certDataTemp;
frameMsg1.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg1.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType1, &frameMsg1, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData1->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType1, &frameMsg1);
memset_s(&frameMsg1, sizeof(frameMsg1), 0, sizeof(frameMsg1));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_RECORD_OVERFLOW);
ASSERT_EQ(testInfo.client->ssl->hsCtx->state, TRY_RECV_NEW_SESSION_TICKET);
bool isCcsRecv = testInfo.client->ssl->method.isRecvCCS(testInfo.client->ssl);
ASSERT_TRUE(isCcsRecv == false);
EXIT:
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
int32_t StatusPark1(HandshakeTestInfo *testInfo)
{
if(testInfo->isServerExtendMasterSecret == true){
testInfo->config->isSupportExtendMasterSecret = true;
}else {
testInfo->config->isSupportExtendMasterSecret = false;
}
testInfo->config->isSupportRenegotiation = false;
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if(testInfo->isServerExtendMasterSecret == true){
testInfo->config->isSupportExtendMasterSecret = false;
}else {
testInfo->config->isSupportExtendMasterSecret = true;
}
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark1(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark1(testInfo);
}
/* @
* @test UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC001
* @title The client does not forcibly verify the master key extension. The server supports the master key extension. The extended information in client hello and server hello is carried.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, and configure the client not to forcibly verify the master key extension. The server supports this function. Expected result 1 is obtained.
2. When the client continuously sets up a link, the server receives the CLIENT_HELLO message and checks whether the extension of the master key is carried. Expected result 2 is obtained.
3. When the client receives the SERVER_HELLO message, check whether the message carries the master key extension. Expected result 2 is obtained.
4. Continue to establish the link. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
2. The master key extension is expected to be carried.
3. Expected Master Key Extension
4. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
testInfo.isServerExtendMasterSecret = true;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark1(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.body.hsMsg.body.clientHello.extendedMasterSecret.exState , INITIAL_FIELD);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark1(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf1 = ioUserData1->recMsg.msg;
uint32_t recvLen1 = ioUserData1->recMsg.len;
ASSERT_TRUE(recvLen1 != 0);
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType1, recvBuf1, recvLen1, &frameMsg1, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg1.body.hsMsg.body.serverHello.extendedMasterSecret.exState , INITIAL_FIELD);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.state = HS_STATE_BUTT;
ASSERT_TRUE(DefaultCfgStatusPark1(&testInfo) == HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC002
* @title The client supports the extension of the key for strong verification, but the server does not. The clienthello carries the extension of the master key, and the serverhello carries the extension of the master key. The link is successfully established.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, and configure the client to forcibly verify the master key extension. The server does not support the extension. Expected result 1 is displayed.
2. When the client is continuously establishing a link, the server receives the Client_Hello message and checks whether the client_Hello message carries the master key extension. Expected result 2 is obtained.
3. When the client receives the SERVER_HELLO message, check whether the message carries the master key extension. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. Expected to carry the master key extension.
3. The master key extension is expected to be carried.
4. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC002(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
FRAME_Init();
HITLS_Config *c_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(s_config != NULL);
HITLS_CFG_SetExtenedMasterSecretSupport(c_config, true);
HITLS_CFG_SetExtenedMasterSecretSupport(s_config, false);
FRAME_LinkObj *client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.body.hsMsg.body.clientHello.extendedMasterSecret.exState == INITIAL_FIELD);
HITLS_Accept(server->ssl);
FRAME_TrasferMsgBetweenLink(server, client);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf1 = ioUserData1->recMsg.msg;
uint32_t recvLen1 = ioUserData1->recMsg.len;
ASSERT_TRUE(recvLen1 != 0);
uint32_t parseLen1 = 0;
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType1, recvBuf1, recvLen1, &frameMsg1, &parseLen1) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg1.body.hsMsg.body.serverHello.extendedMasterSecret.exState == INITIAL_FIELD);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC004
* @title The client and server support master key extension. The client hello message carries master key extension and the server hello message carries master key extension. The link is successfully established.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server to support master key extension. (Expected result 1)
2. When the client continuously sets up a link, the server checks whether the client_hello message carries the master key extension when receiving the CLIENT_HELLO message. Expected result 2 is obtained.
3. When the client receives the SERVER_HELLO message, check whether the message carries the master key extension. Expected result 3 is obtained.
4. Continue to establish the link. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
2. Expected to carry the master key extension.
3. The master key extension is expected to be carried.
4. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_TC004(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
FRAME_Msg frameMsg1 = {0};
FRAME_Type frameType1 = {0};
testInfo.isSupportExtendMasterSecret = true;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.body.hsMsg.body.clientHello.extendedMasterSecret.exState == INITIAL_FIELD);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData1 = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf1 = ioUserData1->recMsg.msg;
uint32_t recvLen1 = ioUserData1->recMsg.len;
ASSERT_TRUE(recvLen1 != 0);
frameType1.versionType = HITLS_VERSION_TLS12;
frameType1.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType1, recvBuf1, recvLen1, &frameMsg1, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg1.body.hsMsg.body.serverHello.extendedMasterSecret.exState == INITIAL_FIELD);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
testInfo.state = HS_STATE_BUTT;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
FRAME_CleanMsg(&frameType1, &frameMsg1);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
int32_t StatusPark2(HandshakeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc5246_extensions.c | C | unknown | 28,510 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246_malformed_msg */
/* BEGIN_HEADER */
#include "hitls_error.h"
#include "tls.h"
#include "rec.h"
#include "hs_msg.h"
#include "hs_ctx.h"
#include "hs_extensions.h"
#include "frame_msg.h"
#include "pack_msg.h"
#include "stub_crypt.h"
/* END_HEADER */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC009
* @title record layer allows the sending of the APPdata message with the length of zero.
* @precon nan
* @brief
1. Establish a link. After the link is established, construct an Appdata message with zero length and send it to the
server. Then, send an APPdata message with data to the server. Expected result 1 is obtained.
* @expect 1. Expected success
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC009(int messageLen)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_EQ(TlsCtxNew(BSL_UIO_TCP), HITLS_SUCCESS);
ASSERT_EQ(REC_Init(g_tlsCtx), HITLS_SUCCESS);
/* 1. Establish a link. After the link is established, construct an Appdata message with zero length and send it to
* the server. Then, send an APPdata message with data to the server. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t data[REC_MAX_CIPHER_TEXT_LEN];
ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_APP, data, messageLen), HITLS_SUCCESS);
EXIT:
TlsCtxFree();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC010
* @title record layer allows the sending of the APPdata message with the length of zero.
* @precon nan
* @brief
1. Establish a link. After the link is established, construct an APPdata message with zero length and send it to the
client. Then, send an APPdata message with data to the client. Expected result 1 is obtained.
* @expect 1. Expected success
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_RECV_ZEROLENGTH_MSG_TC010(int messageLen)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_EQ(TlsCtxNew(BSL_UIO_TCP), HITLS_SUCCESS);
ASSERT_EQ(REC_Init(g_tlsCtx), HITLS_SUCCESS);
/* 1. Establish a link. After the link is established, construct an APPdata message with zero length and send it to
* the client. Then, send an APPdata message with data to the client. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t data[REC_MAX_CIPHER_TEXT_LEN];
ASSERT_EQ(REC_Write(serverTlsCtx, REC_TYPE_APP, data, messageLen), HITLS_SUCCESS);
EXIT:
TlsCtxFree();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS12_RFC5246_CONSISTENCY_MISS_CLIENT_KEYEXCHANGE_TC001
* @title During the handshake, the client receives the CCS when the client is in the TRY_RECV_FINISH state.
* @precon nan
* @brief 1. Configure the single-end authentication. After the server sends the serverhellodone message, the client
stops in the try send client key exchange state. Expected result 1
2. Construct an unexpected CCS message and send it to the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
2. The connection fails to be established and the server returns an unexpected message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_MISS_CLIENT_KEYEXCHANGE_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
/* 1. Configure the single-end authentication. After the server sends the serverhellodone message, the client
* stops in the try send client key exchange state. */
testInfo.state = TRY_SEND_CLIENT_KEY_EXCHANGE;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
testInfo.isSupportClientVerify = false;
testInfo.isSupportNoClientCert = false;
testInfo.needStopBeforeRecvCCS = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
// 2. Construct an unexpected CCS message and send it to the server.
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
// To test whether fragmented messages can be received correctly. Test REC_TlsReadNbytes
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5246_CONSISTENCY_FRAGMENTED_MSG_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
int32_t ret = HITLS_Connect(client->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(client->ssl->hsCtx->state == TRY_RECV_SERVER_HELLO);
ret = HITLS_Accept(server->ssl);
ASSERT_TRUE(ret == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(server->ssl->hsCtx->state == TRY_RECV_CLIENT_HELLO);
// Handshake messages are divided into two records, and the two records are read separately.
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t dataLen = MAX_RECORD_LENTH;
ASSERT_EQ(memcpy_s(data, MAX_RECORD_LENTH, ioUserData->sndMsg.msg, ioUserData->sndMsg.len), 0);
dataLen = ioUserData->sndMsg.len;
uint32_t msglength = BSL_ByteToUint16(&data[3]);
uint32_t msgLen = (msglength - 1) / 2;
uint32_t len = 5 + msgLen; // record + handshakemsg
// Send the first segment of packets. eg.163 = (5 + 70) + 5 +83
uint8_t recorddata1[] = {0x16, 0x03, 0x03, 0x00, 0x46};
// The last two bytes of the first five bytes of the length of bodylen are modified.
BSL_Uint16ToByte((uint16_t)msgLen, &recorddata1[3]);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg, MAX_RECORD_LENTH, data, len), 0);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg, MAX_RECORD_LENTH, recorddata1, sizeof(recorddata1)), 0);
// Send the second segment of packets. eg.163 = 5 + 70 + (5 +83)
uint8_t recorddata2[] = {0x16, 0x03, 0x03, 0x00, 0x53};
msgLen = dataLen - len;
BSL_Uint16ToByte((uint16_t)msgLen, &recorddata2[3]);
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg + len, MAX_RECORD_LENTH - len, recorddata2, sizeof(recorddata2)), 0);
ioUserData->sndMsg.len = len + 5;
ASSERT_EQ(memcpy_s(ioUserData->sndMsg.msg + ioUserData->sndMsg.len, MAX_RECORD_LENTH - len, data + len, dataLen - len), 0);
ioUserData->sndMsg.len += dataLen - len;
ret = HITLS_Connect(client->ssl);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ret = HITLS_Accept(server->ssl);
ASSERT_EQ(ret, HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc5246_malformed_msg.c | C | unknown | 10,528 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <semaphore.h>
#include <stdio.h>
#include "process.h"
#include "securec.h"
#include "hitls_error.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "bsl_sal.h"
#include "simulate_io.h"
#include "tls.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "alert.h"
#include "session_type.h"
#include "hitls_type.h"
#include "rec.h"
#include "hs_msg.h"
#include "hs_extensions.h"
#include "frame_msg.h"
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "bsl_uio.h"
#include "pack.h"
#include "send_process.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "cert_callback.h"
#include "change_cipher_spec.h"
#include "common_func.h"
#include "uio_base.h"
#include "stub_crypt.h"
#define READ_BUF_SIZE (18 * 1024)
/* END_HEADER */
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isServerExtendMasterSecret;
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
static void Test_ServerHelloHaveSecRenego(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.secRenego.exState, INITIAL_FIELD);
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark(testInfo);
}
int32_t StatusPark1(HandshakeTestInfo *testInfo)
{
if (testInfo->isServerExtendMasterSecret == true) {
testInfo->config->isSupportExtendMasterSecret = true;
} else {
testInfo->config->isSupportExtendMasterSecret = false;
}
testInfo->config->isSupportRenegotiation = false;
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->isServerExtendMasterSecret == true) {
testInfo->config->isSupportExtendMasterSecret = false;
} else {
testInfo->config->isSupportExtendMasterSecret = true;
}
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark1(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark1(testInfo);
}
void Test_RenegoWrapperFunc(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.body.clientHello.secRenego.exState, INITIAL_FIELD);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
void Test_RenegoRemoveExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
ASSERT_EQ(parseLen, *len);
frameMsg.body.hsMsg.body.clientHello.secRenego.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC001
* @titleThe client carries the renegotiation algorithm suite but does not carry the renegotiation extension.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. During continuous link setup, the server checks whether the renegotiation algorithm suite is contained and
* whether the renegotiation extension is carried when receiving the CLIENT_HELLO message.
* Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The renegotiation algorithm suite is expected to be carried, but the renegotiation extension is not
* carried.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportRenegotiation = true;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
int FlagScsv = 0;
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
for (int i = 0; i < (int)frameMsg.body.hsMsg.body.clientHello.cipherSuites.size; i++) {
if (frameMsg.body.hsMsg.body.clientHello.cipherSuites.data[i] == 255) {
FlagScsv = 1;
}
}
ASSERT_TRUE(FlagScsv == 1);
ASSERT_TRUE(frameMsg.body.hsMsg.body.clientHello.secRenego.exState == MISSING_FIELD);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC003
* @title Enable the client and server to support renegotiation. Change the length of the renegotiated_connection field
* in the server hello message to a non-zero value. Check whether the connection is successfully established.
* @precon nan
* @brief 1. The client and server support renegotiation and connection establishment. Change the length of the
* renegotiated_connection field in the server hello message to a non-zero value. Then, the connection
* is established. Expected result 1 is obtained.
* @expect 1. The connection fails to be set up and an ALERT_HANDSHAKE_FAILURE message is sent.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC003(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportRenegotiation = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->secRenego.exDataLen.data == 0u);
serverMsg->secRenego.exDataLen.data = 1u;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* The connection fails to be set up and an ALERT_HANDSHAKE_FAILURE message is sent. */
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC002
* @titleThe server does not support renegotiation. The serverhello message carries the renegotiation extension.
* @precon nan
* @brief
* 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client receives a SERVER_HELLO message during continuous link establishment, the client checks whether
* the message carries the renegotiation extension. Expected result 2 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The renegotiation extension is expected to be carried.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC002(void)
{
FRAME_Init();
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.isSupportRenegotiation = false;
testInfo.config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.body.hsMsg.body.serverHello.secRenego.exLen.data != 0);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
testInfo.state = HS_STATE_BUTT;
ASSERT_TRUE(DefaultCfgStatusPark1(&testInfo) == HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC009
* @title Renegotiation flag condition
* @precon nan
* @brief 1. The client and server support renegotiation and connection establishment. Before receiving the client hello
* message, the server disables security renegotiation. Expected result 1 is displayed.
* 2. After the server receives the client hello message, enable the security renegotiation on the server.
* Expected result 2 is displayed.
* @expect 1. The isSecureRenegotiation is false.
* 2. The isSecureRenegotiation is true.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC009(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(server->ssl->negotiatedInfo.isSecureRenegotiation == false);
FRAME_LinkObj *client1 = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server1 = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client1, server1, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(server1->ssl->negotiatedInfo.isSecureRenegotiation == true);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client1);
FRAME_FreeLink(server1);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC013
* @title Configure the client and server to support renegotiation. After the first handshake is complete, a
* renegotiation request is initiated. When the client sends a client hello message,
* Modify the message and delete the renegotiation_info extension. The server is expected to return an alert
* after receiving the message.
* @precon nan
* @brief 1. The client and server support renegotiation and connection establishment. Start renegotiation. The expected
* connection establishment is successful.
* @expect 1. When the client sends the client hello message, modify the message and remove the renegotiation_info
* extension. As a result, the expected connection establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC013(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
/* The client and server support renegotiation and connection establishment. Start renegotiation. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t verifyData[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataSize = 0;
ASSERT_TRUE(
HITLS_GetFinishVerifyData(client->ssl, verifyData, sizeof(verifyData), &verifyDataSize) == HITLS_SUCCESS);
RecWrapper wrapper = {TRY_RECV_CLIENT_HELLO, REC_TYPE_HANDSHAKE, true, NULL, Test_RenegoRemoveExtension};
RegisterWrapper(wrapper);
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) != HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC005
* @title The client and server support renegotiation and connection establishment. Start renegotiation. Check whether
* the serverhello contains the renegotiation extension. Check whether the connection is successfully
* established and verify the negotiation behavior.
* @precon nan
* @brief 1. The client and server support renegotiation and connection establishment. Start renegotiation. Expected
* result 1 is obtained.
* @expect 1. Modify the cipher suite in the client hello, add the SCSV, and check whether the connection is successfully
* set up. Expected result 2 is obtained.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC005(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
/* The client and server support renegotiation and connection establishment. Start renegotiation. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t verifyData[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataSize = 0;
ASSERT_TRUE(
HITLS_GetFinishVerifyData(client->ssl, verifyData, sizeof(verifyData), &verifyDataSize) == HITLS_SUCCESS);
RecWrapper wrapper = {TRY_RECV_CLIENT_HELLO, REC_TYPE_HANDSHAKE, true, NULL, Test_RenegoWrapperFunc};
RegisterWrapper(wrapper);
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC010
* @title Configure the client and server to support renegotiation and connection establishment. The
* renegotiated_connection field in the client hello extension received by the server is not 0.
* @precon nan
* @brief 1. Configure the client and server to support renegotiation and establish a connection. Expected result 1 is
* obtained.
* 2. Modify the client hello message received by the server. Modify the renegotiated_connection field extended
* by the (HS_EX_TYPE_RENEGOTIATION_INFO) to ensure that the length of the field is not 0.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT message. The level is ALERT_LEVEL_FATAL and the description is
* ALERT_HANDSHAKE_FAILURE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC010(void)
{
/* Configure the client and server to support renegotiation and establish a connection. */
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportRenegotiation = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusPark(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS12;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Modify the client hello message received by the server. Modify the renegotiated_connection field extended by the
* (HS_EX_TYPE_RENEGOTIATION_INFO) to ensure that the length of the field is not 0. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->secRenego.exDataLen.data == 0u);
clientMsg->secRenego.exState = INITIAL_FIELD;
clientMsg->secRenego.exType.state = INITIAL_FIELD;
clientMsg->secRenego.exType.data = 0xFF01u;
clientMsg->secRenego.exLen.state = INITIAL_FIELD;
clientMsg->secRenego.exLen.data = 2;
clientMsg->secRenego.exDataLen.state = INITIAL_FIELD;
clientMsg->secRenego.exDataLen.data = 1u;
clientMsg->secRenego.exData.state = INITIAL_FIELD;
clientMsg->secRenego.exData.size = 1;
clientMsg->secRenego.exData.data = BSL_SAL_Calloc(1, sizeof(uint8_t));
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_TRUE(HITLS_Accept(testInfo.server->ssl) == HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_HANDSHAKE_FAILURE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC011
* @title The client and server support renegotiation and connection establishment. Check whether the serverhello
* contains the renegotiation extension. Check whether the connection is successfully established and verify the
* negotiation behavior.
* @precon nan
* @brief 1. If the client and server support renegotiation and connection establishment, check the serverhello message
* that contains the renegotiation extension, and check whether the connection establishment is successful.
* (Expected result 1)
* @expect 1. Modify the cipher suite in the client hello, add the SCSV, and check whether the connection is successfully
* set up. Expected result 2 is obtained.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC011(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
/* If the client and server support renegotiation and connection establishment, check the serverhello message that
* contains the renegotiation extension, and check whether the connection establishment is successful. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* Modify the cipher suite in the client hello, add the SCSV, and check whether the connection is successfully set
* up. */
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerHelloHaveSecRenego};
RegisterWrapper(wrapper);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ClientHello_SecRenego(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_EQ(clientMsg->secRenego.exState, INITIAL_FIELD);
ASSERT_TRUE(
clientMsg->cipherSuites.data[clientMsg->cipherSuitesSize.data / 2 - 1] != TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC004
* @title The client and server support renegotiation and connection establishment. Check whether the clienthello
* contains the renegotiation extension. Check whether the connection is successfully established and verify the
* negotiation behavior.
* @precon nan
* @brief 1. Enable the client to support renegotiation and connection establishment. Initiate renegotiation and check
* whether the client hello contains secure Renegotiation extension and whether the cipher suite list contains
* the SCSV cipher suite.
* @expect 1. The client hello message contain the secure Renegotiation extension but does not contain the SCSV cipher
* suite.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
/* If the client and server support renegotiation and connection establishment, check the clienthello message that
* contains the renegotiation extension, and check whether the connection establishment is successful. */
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHello_SecRenego};
RegisterWrapper(wrapper);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyServerHello_Secrenegotiation1(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exData.data[0] = serverMsg->secRenego.exData.data[0] + 1;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_ModifyServerHello_Secrenegotiation2(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exData.data[serverMsg->secRenego.exData.size - 1] =
serverMsg->secRenego.exData.data[serverMsg->secRenego.exData.size - 1] + 1;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC007
* @title HITLS_GetRenegotiationState Interface Verification
* @precon nan
* @brief Configure the client and server to support renegotiation and connection establishment.
* Initiate renegotiation, modify the first part of renegotiated_connection in the server hello message,
* and check whether the connection is set up successfully.
* @expect
* create connection failed
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC007(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyServerHello_Secrenegotiation1};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC008
* @spec -
* @title HITLS_GetRenegotiationState Interface Verification
* @precon nan
* @brief Configure the client and server to support renegotiation and connection establishment.
* Initiate renegotiation, modify the last part of renegotiated_connection in the server hello message,
* and check whether the connection is set up successfully.
* @expect
* create connection failed
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC008(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyServerHello_Secrenegotiation2};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyServerHello_Secrenegotiation3(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC006
* @title HITLS_GetRenegotiationState Interface Verification
* @precon nan
* @brief Configure the client to support renegotiation and the server to support renegotiation and connection
* establishment. Start renegotiation, construct a server hello message that does not contain the extension,
* and check whether the connection is successfully set up.
* @expect
* The server hello message does not contain the Secrenegotiation extension, and the connection fails to be established
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC006(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyServerHello_Secrenegotiation3};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyClientHello_Secrenegotiation1(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->secRenego.exData.data[0] = clientMsg->secRenego.exData.data[0] + 1;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0014
* @title The value of client_verify_data in the client hello message does not match the actual value
* in the renegotiation state.
* @precon nan
* @brief
* Configure the client and server to support renegotiation.
* After the first handshake is complete, initiate a renegotiation request.
* When the client sends a client hello message,
* modify the value of client_verify_data in the renegotiation_info extension.
* @expect
* The server returns an alert message after receiving the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0014(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyClientHello_Secrenegotiation1};
RegisterWrapper(wrapper);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyServerHello_No_client_verify_data(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->secRenego.exData.size = 0;
serverMsg->secRenego.exDataLen.data = 0;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0015
* @title The client_verify_data and server_verify_data fields of the client hello message
are lost in the renegotiation state.
* @precon nan
* @brief
* Configure the client and server to support renegotiation.
* After the first handshake is complete, initiate a renegotiation request.
* When the server sends a server hello message,
* delete the values of client_verify_data and server_verify_data from the renegotiation_info extension.
* @expect
* The client returns an alert message after receiving the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0015(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyServerHello_No_client_verify_data};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ClientHelloHaveSecRenego(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_EQ(clientMsg->secRenego.exState, INITIAL_FIELD);
clientMsg->cipherSuites.data[clientMsg->cipherSuitesSize.data / 2 - 1] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0012
* @title In the renegotiation state, the client hello message carries the SCSV.
* @precon nan
* @brief
* Configure the client and server to support renegotiation. After the first handshake is complete,
* initiate a renegotiation request. When the client sends a client hello message, modify the cipher suite and add SCSV.
* After receiving the message, the server is expected to return an alert message.
* @expect
* The client returns an alert message after receiving the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC5746_CONSISTENCY_EXTENDED_RENEGOTIATION_FUNC_TC0012(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloHaveSecRenego};
RegisterWrapper(wrapper);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_RENEGOTIATION_FAIL);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc5746.c | C | unknown | 50,911 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
#include "securec.h"
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "conn_init.h"
/* END_HEADER */
#define g_uiPort 12121
/** @
* @test UT_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_LOSE_POINT_FUNC_TC001
* @title clienthello does not carry the point format extension.
* @precon nan
* @brief Set the ECC cipher suite. Before the server receives the client hello message, the point format extension is
* removed. It is expected that the negotiation is normal and the client can receive the server hello done
* message.
* @expect 1. The connection is set up normally and the client can receive the server hello done message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_LOSE_POINT_FUNC_TC001(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportClientVerify = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite(&testInfo) == 0);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint32_t parseLen = 0;
FRAME_ParseMsg(&frameType, ioUserData->recMsg.msg, ioUserData->recMsg.len, &frameMsg, &parseLen);
/* Set the ECC cipher suite. Before the server receives the client hello message, the point format extension is
* removed. */
frameMsg.body.hsMsg.body.clientHello.pointFormats.exState = MISSING_FIELD;
FRAME_PackMsg(&frameType, &frameMsg, ioUserData->recMsg.msg, MAX_RECORD_LENTH, &parseLen);
ioUserData->recMsg.len = parseLen;
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
// the client can receive the server hello done message.
ASSERT_TRUE(
FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO_DONE) == HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_frame_tls12_consistency_rfc8422.c | C | unknown | 3,010 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
/* END_HEADER */
static void TestFrameClientChangeCompressMethod(void *msg, void *userData)
{
(void)msg;
(void)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->compressionMethods.state = ASSIGNED_FIELD;
*clientHello->compressionMethods.data = 1;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_ERRO_COMPRESSION_FRAGMENT_TC001
* @title The record layer does not support compression.
* @precon nan
* @brief 1. When the client sends a client hello message, the compression flag is changed to 1. As a result, the
connection fails to be established.
* @expect 1. Link establishment fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_ERRO_COMPRESSION_FRAGMENT_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// When the client sends a client hello message, the compression flag is changed to 1.
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
HLT_FrameHandle frameHandle = {
.ctx = clientRes->ssl,
.frameCallBack = TestFrameClientChangeCompressMethod,
.userData = NULL,
.expectHsType = CLIENT_HELLO,
.expectReType = REC_TYPE_HANDSHAKE,
.ioState = EXP_NONE,
.pointType = POINT_SEND,
};
ASSERT_TRUE(HLT_SetFrameHandle(&frameHandle) == HITLS_SUCCESS);
int ret = HLT_TlsConnect(clientRes->ssl);
ASSERT_TRUE(ret != 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
static void TestFrameServerChangeCompressMethod(void *msg, void *userData)
{
(void)msg;
(void)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->compressionMethod.state = ASSIGNED_FIELD;
serverHello->compressionMethod.data = 1;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_ERRO_COMPRESSION_FRAGMENT_TC002
* @title The record layer does not support compression.
* @precon nan
* @brief 1. When the server sends the serverhello message, the compression flag is changed to 1, and the client is
expected to send the alert message.
* @expect 1. A failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_ERRO_COMPRESSION_FRAGMENT_TC002(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
/* When the server sends the serverhello message, the compression flag is changed to 1. */
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_FrameHandle frameHandle = {
.ctx = serverRes->ssl,
.frameCallBack = TestFrameServerChangeCompressMethod,
.userData = NULL,
.expectHsType = SERVER_HELLO,
.expectReType = REC_TYPE_HANDSHAKE,
.ioState = EXP_NONE,
.pointType = POINT_SEND,
};
ASSERT_TRUE(HLT_SetFrameHandle(&frameHandle) == HITLS_SUCCESS);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC008
* @title two-way authentication: The certificate configured on the client does not match the signature algorithm
supported by the server. As a result, the client fails to load the certificate.
* @precon nan
* @brief Set the dual-end authentication, the signature algorithm supported by the server to DSA_SHA224, and the client
certificate to RSA. The expected certificate loading failure occurs on the client.
* @expect 1. The link is set up successfully.
* 2. Link establishment failure.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC008(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
/* Set the dual-end authentication, the signature algorithm supported by the server to DSA_SHA224, and the client
* certificate to RSA. */
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
HLT_SetSignature(serverCtxConfig, "CERT_SIG_SCHEME_DSA_SHA224");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_128_GCM_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
int ret = HLT_TlsConnect(clientRes->ssl);
ASSERT_TRUE(ret != 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
int32_t SendKeyupdate_Err(HITLS_Ctx *ctx)
{
/** Initialize the message buffer. */
uint8_t buf[5] = {KEY_UPDATE, 0x00, 0x00, 0x01, 0x01};
size_t len = 5;
/** Write records. */
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
/* tls12 receive keyupdate message during transporting*/
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RECV_KEYUPDATE_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportSessionTicket=true;
clientCtxConfig->isSupportSessionTicket=true;
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, clientRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf2[READ_BUF_LEN_18K] = {0};
uint32_t readLen2= 0;
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
HITLS_Ctx *serverCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(serverCtx->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(SendKeyupdate_Err(serverRes->ssl) , HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen= 0;
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(serverRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
int32_t SendNEW_SESSION_TICKET_Err(HITLS_Ctx *ctx)
{
/** Initialize the message buffer. */
uint8_t buf[32] = {NEW_SESSION_TICKET,0,0,0x1c,0x20,0xc1,};
size_t len = 32;
/** Write records. */
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
/* tls12 receive NST message during transporting*/
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RECV_NST_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportSessionTicket=true;
clientCtxConfig->isSupportSessionTicket=true;
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, clientRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf2[READ_BUF_LEN_18K] = {0};
uint32_t readLen2= 0;
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
HITLS_Ctx *serverCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(serverCtx->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(SendNEW_SESSION_TICKET_Err(serverRes->ssl) , HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen= 0;
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(serverRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_TLS12_StateTrans_FUNC_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportExtendMasterSecret=true;
serverCtxConfig->isSupportSessionTicket=true;
clientCtxConfig->isSupportSessionTicket=true;
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, clientRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf2[READ_BUF_LEN_18K] = {0};
uint32_t readLen2= 0;
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf2, READ_BUF_LEN_18K, &readLen2) , 0);
HITLS_Ctx *serverCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(serverCtx->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(SendKeyupdate_Err(serverRes->ssl) , HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen= 0;
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf, READ_BUF_LEN_18K, &readLen) , HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(serverRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
static void MalformedCipherSuiteLenCallback_01(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data = 1000;
clientHello->cipherSuitesSize.state = ASSIGNED_FIELD;
EXIT:
return;
}
void ClientSendMalformedCipherSuiteLenMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 16384, false);
ASSERT_TRUE(remoteProcess != NULL);
// The remote server listens on the TLS connection.
HLT_Ctx_Config *serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
serverConfig->isSupportExtendMasterSecret = false;
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the TLS connection on the local client.
HLT_Ctx_Config *clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
serverConfig->isSupportExtendMasterSecret = false;
HLT_Tls_Res *clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a connection and wait until the local is complete.
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
// Wait the remote.
int ret = HLT_GetTlsAcceptResult(serverRes);
ASSERT_TRUE(ret != 0);
if (testPara->isExpectRet) {
ASSERT_EQ(ret, testPara->expectRet);
}
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(clientRes->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, testPara->expectDescription);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
/** @
* @test SDV_TLS1_2_RFC5246_MALFORMED_CIPHER_SUITE_LEN_FUN_TC001
* @spec -
* @title The length of the cipher suite in the sent ClientHello message is greater than the specific content
length_cipher suites length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_MALFORMED_CIPHER_SUITE_LEN_FUN_TC001()
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedCipherSuiteLenCallback_01;
TestPara testPara = {0};
testPara.port = g_uiPort;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedCipherSuiteLenMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void TEST_Server_check_etm_ext(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_EQ(serverMsg->encryptThenMac.exType.data, HS_EX_TYPE_ENCRYPT_THEN_MAC);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test SDV_TLS_TLS1_2_RFC5246_HANDSHAKE_FUNC_TC001
* @spec -
* @title Test link establishment when the tls1.2 client cipher suite does not match the certificate.
* @precon nan
* @brief
1. Configure the ecdhe_ecdsa cipher suite, certificate, and RSA certificate chain on the server. Expected result 1 is obtained.
2. Configure the RSA certificate and ecdsa certificate chain on the client. Expected result 2 is obtained.
3. Establish a link between the two ends. Expected result 3 is obtained.
4. Read and write data. Expected result 4 is obtained.
* @expect
1. A success message is returned.
2. A success message is returned.
3. A success message is returned.
4. Return a success message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS1_2_RFC5246_HANDSHAKE_FUNC_TC001()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
// Configure the ecdhe_ecdsa cipher suite, certificate, and RSA certificate chain on the server.
HLT_SetCertPath(serverCtxConfig,
RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, ECDSA_SHA256_EE_PATH, ECDSA_SHA256_PRIV_PATH, "NULL", "NULL");
HLT_SetClientVerifySupport(serverCtxConfig, true);
HLT_SetNoClientCertSupport(serverCtxConfig, false);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA");
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
// Configure the RSA certificate and ecdsa certificate chain on the client.
HLT_SetCertPath(clientCtxConfig,
ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
TEST_Server_check_etm_ext
};
RegisterWrapper(wrapper);
// Establish a link between the two ends.
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
// Read and write data.
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc5246.c | C | unknown | 24,590 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
/* END_HEADER */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CIPHER_SUITE_NOT_SUITABLE_CERT_TC001
* @title The public key algorithm used to verify the server terminal certificate must match the algorithm suite.
* @precon nan
* @brief 1. Create a config file, configure the server certificate as the ECDSA public key, and configure the ECDHE_RSA algorithm suite in the hello message on the client.
2. The client invokes the HITLS_Connect interface. (Expected result 1)
* @expect 1. A failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CIPHER_SUITE_NOT_SUITABLE_CERT_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
// Create a config file, configure the server certificate as the ECDSA public key, and configure the ECDHE_RSA algorithm suite in the hello message on the client.
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL); // failed in
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
// The client invokes the HITLS_Connect interface.
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
return;
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_NOTSET_CERT_TC001
* @title The certificate chain sent by the server does not contain the root certificate.
* @precon nan
* @brief If no certificate is set on the client and the server sends a complete certificate chain,
* link establishment fails.
* @expect 1. A failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_NOTSET_CERT_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
/* If no certificate is set on the client and the server sends a complete certificate chain,link establishment
* fails. */
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_WITHOUT_ROOT_CERT_TC001
* @title The certificate chain sent by the server does not contain the root certificate.
* @precon nan
* @brief Set the root certificate and send a certificate chain that does not contain the root certificate.
* The link is successfully set up.
* @expect 1. Return a success message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_SERVER_WITHOUT_ROOT_CERT_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
// Set the root certificate and send a certificate chain that does not contain the root certificate.
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_SET_ERRO_ROOT_CERT_TC001
* @title The certificate chain sent by the server does not contain the root certificate.
* @precon nan
* @brief The root certificate is incorrectly set on the client. As a result, the link fails to be established.
* @expect 1. A failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_SET_ERRO_ROOT_CERT_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// The root certificate is incorrectly set on the client.
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "rsa_sha512/otherRoot.crt", "rsa_sha512/otherInter.crt",
"rsa_sha512/otherInter2.crt", "rsa_sha512/otherInter2.key", "NULL", "NULL");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
return;
}
/* END_CASE */
/**
* Configure the certificate signature as CERT_SIG_SCHEME_RSA_PKCS1_SHA256.
* Set the algorithm suite to HITLS_DHE_DSS_WITH_AES_128_GCM_SHA256.
* The signature type in the cipher suite does not match that in the certificate,
* Expected link establishment failure, as shown in the log.
* select certificate fail
* have no suitable cert
* can not find a appropriate cipher suite
*/
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CIPHERSUITE_SIG_MATCH_CERT_SIG_TC002()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCertPath(serverCtxConfig, "rsa_sha256/ca.der:rsa_sha256/inter.der", "rsa_sha256/inter.der",
"rsa_sha256/server.der", "rsa_sha256/server.key.der", "NULL", "NULL");
HLT_SetCipherSuites(serverCtxConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "rsa_sha256/ca.der:rsa_sha256/inter.der", "NULL", "NULL", "NULL", "NULL",
"NULL");
// Set the algorithm suite to HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA.
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA");
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC001
* @title The keyusage extension of the server certificate does not contain the keyEncipherment usage.
* As a result, the link fails to be established.
* @precon nan
* @brief 1. Set the server certificate to an RSA certificate that contains the keyusage extension,
The extension does not contain the keyEncipherment usage.
The negotiation cipher suite is the RSA cipher suite. Expected result 1 is obtained.
* @expect 1. connection setup failed
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC001()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCertPath(serverCtxConfig, "rsa_sha512/root.der", "rsa_sha512/intca.der",
"rsa_sha512/usageKeyEncipher.der", "rsa_sha512/usageKeyEncipher.key.der", "NULL", "NULL");
HLT_SetCipherSuites(serverCtxConfig, "HITLS_RSA_WITH_AES_256_GCM_SHA384");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "rsa_sha512/root.der", "rsa_sha512/intca.der",
"rsa_sha512/server.der", "rsa_sha512/server.key.der", "NULL", "NULL");
clientCtxConfig->needCheckKeyUsage = true;
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC002
* @title Failed to set up the link because the keyuage extension does not match.
* @precon nan
* @brief 1. Configure the server certificate with the keyuage extension and do not support digitalSignature.
Expected result 1 is obtained.
* @expect 1. connection setup failed
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_KEYUSAGE_CERT_TC002()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCertPath(serverCtxConfig, "rsa_sha512/root.der", "rsa_sha512/intca.der",
"rsa_sha512/usagedigitalSign.der", "rsa_sha512/usagedigitalSign.key.der", "NULL", "NULL");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "rsa_sha512/root.der", "rsa_sha512/intca.der",
"rsa_sha512/server.der", "rsa_sha512/server.key.der", "NULL", "NULL");
clientCtxConfig->needCheckKeyUsage = true;
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc5246_cert.c | C | unknown | 13,772 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <semaphore.h>
#include "process.h"
#include "securec.h"
#include "hitls_error.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "bsl_sal.h"
#include "simulate_io.h"
#include "tls.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "alert.h"
#include "session_type.h"
#include "process.h"
#include "hitls_type.h"
#include "rec.h"
#include "hs_msg.h"
#include "hs_extensions.h"
#include "frame_msg.h"
/* END_HEADER */
#define PORT 19800
#define MAX_SESSION_ID_SIZE TLS_HS_MAX_SESSION_ID_SIZE
#define MIN_SESSION_ID_SIZE TLS_HS_MIN_SESSION_ID_SIZE
#define COOKIE_SIZE 32u
#define DN_SIZE 32u
#define EXTRA_DATA_SIZE 12u
#define MAX_PROTOCOL_LEN1 65536
#define READ_BUF_SIZE 18432
#define ROOT_DER "%s/ca.der:%s/inter.der"
#define INTCA_DER "%s/inter.der"
#define SERVER_DER "%s/server.der"
#define SERVER_KEY_DER "%s/server.key.der"
#define CLIENT_DER "%s/client.der"
#define CLIENT_KEY_DER "%s/client.key.der"
typedef struct {
int port;
HITLS_HandshakeState expectHsState; // Expected Local Handshake Status
bool alertRecvFlag; // Indicates whether the alert is received. The value fasle indicates the sent alert, and the value true indicates the received alert
ALERT_Description expectDescription; // Expected alert description on the test end
bool isSupportClientVerify;
bool isSupportExtendMasterSecret;
bool isSupportRenegotiation;
bool isSupportSessionTicket;
bool isSupportDhCipherSuites;
bool isSupportSni;
bool isSupportAlpn;
bool isExpectRet;
int expectRet; // Expected return value. isExpectRet needs to be enabled
const char *serverGroup; // Configure the group supported by the server. If this parameter is not specified, the default value is used
const char *serverSignature; // Configure the signature algorithm supported by the server. If this parameter is left empty, the default value is used
const char *clientGroup; // Configure the group supported by the client. If this parameter is not specified, the default value is used
const char *clientSignature; // Configure the signature algorithm supported by the client. If this parameter is left empty, the default value is used
} TestPara;
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isServerExtendMasterSecret;
} HandshakeTestInfo;
int32_t ExampleAlpnParseProtocolList2(uint8_t *out, uint32_t *outLen, uint8_t *in, uint32_t inLen)
{
if (out == NULL || outLen == NULL || in == NULL) {
return HITLS_NULL_INPUT;
}
if (inLen == 0 || inLen > MAX_PROTOCOL_LEN1) {
return HITLS_CONFIG_INVALID_LENGTH;
}
uint32_t i = 0u;
uint32_t commaNum = 0u;
uint32_t startPos = 0u;
for (i = 0u; i <= inLen; ++i) {
if (i == inLen || in[i] == ',') {
if (i == startPos) {
++startPos;
++commaNum;
continue;
}
out[startPos - commaNum] = (uint8_t)(i - startPos);
startPos = i + 1;
} else {
out[i + 1 - commaNum] = in[i];
}
}
*outLen = inLen + 1 - commaNum;
return HITLS_SUCCESS;
}
/* The local server initiates a link creation request: Ignore whether the link creation is successful. */
void ServerAccept(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
//Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, true);
ASSERT_TRUE(remoteProcess != NULL);
//The local server listens on the TLS link.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
//Configure the interface for constructing abnormal packets.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
//Set up a TLS link on the remote client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_RpcTlsConnect(remoteProcess, clientRes->sslId);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ServerSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
//Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, true);
ASSERT_TRUE(remoteProcess != NULL);
//The local server listens on the TLS link.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
serverConfig->isSupportSessionTicket = testPara->isSupportSessionTicket;
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
ASSERT_TRUE(HLT_SetRenegotiationSupport(serverConfig, testPara->isSupportRenegotiation) == 0);
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerNameCb(serverConfig, "ExampleSNICb") == 0);
ASSERT_TRUE(HLT_SetServerNameArg(serverConfig, "ExampleSNIArg") == 0);
}
if (testPara->isSupportAlpn) {
ASSERT_TRUE(HLT_SetAlpnProtosSelectCb(serverConfig, "ExampleAlpnCb", "ExampleAlpnData") == 0);
}
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(serverConfig,
RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->serverGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(serverConfig, testPara->serverGroup) == 0);
}
if (testPara->serverSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(serverConfig, testPara->serverSignature) == 0);
}
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
//Configure the interface for constructing abnormal packets.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
//Set up a TLS link on the remote client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
ASSERT_TRUE(HLT_SetRenegotiationSupport(clientConfig, testPara->isSupportRenegotiation) == 0);
clientConfig->isSupportSessionTicket = testPara->isSupportSessionTicket;
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerName(clientConfig, "testServer") == 0);
}
if (testPara->isSupportAlpn) {
static const char *alpn = "http,ftp";
uint8_t ParsedList[100] = {0};
uint32_t ParsedListLen;
ExampleAlpnParseProtocolList2(ParsedList, &ParsedListLen, (uint8_t *)alpn, (uint32_t)strlen(alpn));
ASSERT_TRUE(HLT_SetAlpnProtos(clientConfig, (const char *)ParsedList) == 0);
}
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(clientConfig,
RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->clientGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(clientConfig, testPara->clientGroup) == 0);
}
if (testPara->clientSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(clientConfig, testPara->clientSignature) == 0);
}
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
if (testPara->isExpectRet) {
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), testPara->expectRet);
} else {
ASSERT_TRUE(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId) != 0);
}
//Wait for the local end.
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
//Confirm the final status.
ASSERT_TRUE(((HITLS_Ctx *)(serverRes->ssl))->state == CM_STATE_ALERTED);
ASSERT_TRUE(((HITLS_Ctx *)(serverRes->ssl))->hsCtx != NULL);
ASSERT_EQ(((HITLS_Ctx *)(serverRes->ssl))->hsCtx->state, testPara->expectHsState);
ASSERT_TRUE(HLT_RpcTlsGetStatus(remoteProcess, clientRes->sslId) == CM_STATE_ALERTED);
if (testPara->alertRecvFlag) {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_RECV);
} else {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
}
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ((ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId),
testPara->expectDescription);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ClientSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
//Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, false);
ASSERT_TRUE(remoteProcess != NULL);
//The remote server listens on the TLS link.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(serverConfig,
RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
serverConfig->isSupportSessionTicket = testPara->isSupportSessionTicket;
ASSERT_TRUE(HLT_SetRenegotiationSupport(serverConfig, testPara->isSupportRenegotiation) == 0);
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerNameCb(serverConfig, "ExampleSNICb") == 0);
ASSERT_TRUE(HLT_SetServerNameArg(serverConfig, "ExampleSNIArg") == 0);
}
if (testPara->isSupportAlpn) {
ASSERT_TRUE(HLT_SetAlpnProtosSelectCb(serverConfig, "ExampleAlpnCb", "ExampleAlpnData") == 0);
}
if (testPara->serverGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(serverConfig, testPara->serverGroup) == 0);
}
if (testPara->serverSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(serverConfig, testPara->serverSignature) == 0);
}
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
//Configure the TLS connection on the local client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetRenegotiationSupport(clientConfig, testPara->isSupportRenegotiation) == 0);
clientConfig->isSupportSessionTicket = testPara->isSupportSessionTicket;
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerName(clientConfig, "testServer") == 0);
}
if (testPara->isSupportAlpn) {
static const char *alpn = "http,ftp";
uint8_t ParsedList[100] = {0};
uint32_t ParsedListLen;
ExampleAlpnParseProtocolList2(ParsedList, &ParsedListLen, (uint8_t *)alpn, (uint32_t)strlen(alpn));
ASSERT_TRUE(HLT_SetAlpnProtos(clientConfig, (const char *)ParsedList) == 0);
}
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(clientConfig,
RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->clientGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(clientConfig, testPara->clientGroup) == 0);
}
if (testPara->clientSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(clientConfig, testPara->clientSignature) == 0);
}
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
//Configure the interface for constructing abnormal packets.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
//Set up a link and wait until the local end is complete.
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
//Wait the remote end.
int ret = HLT_GetTlsAcceptResult(serverRes);
ASSERT_TRUE(ret != 0);
if (testPara->isExpectRet) {
ASSERT_EQ(ret, testPara->expectRet);
}
//Final status confirmation
ASSERT_EQ(HLT_RpcTlsGetStatus(remoteProcess, serverRes->sslId), CM_STATE_ALERTED);
if (testPara->alertRecvFlag) {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, serverRes->sslId), ALERT_FLAG_RECV);
} else {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, serverRes->sslId), ALERT_FLAG_SEND);
}
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, serverRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ((ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, serverRes->sslId),
testPara->expectDescription);
ASSERT_TRUE(((HITLS_Ctx *)(clientRes->ssl))->state == CM_STATE_ALERTED);
ASSERT_TRUE(((HITLS_Ctx *)(clientRes->ssl))->hsCtx != NULL);
ASSERT_EQ(((HITLS_Ctx *)(clientRes->ssl))->hsCtx->state, testPara->expectHsState);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
static int SetCertPath(HLT_Ctx_Config *ctxConfig, const char *certStr, bool isServer)
{
int ret;
char caCertPath[50] = {0};
char chainCertPath[30] = {0};
char eeCertPath[30] = {0};
char privKeyPath[30] = {0};
ret = sprintf_s(caCertPath, sizeof(caCertPath), ROOT_DER, certStr, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(chainCertPath, sizeof(chainCertPath), INTCA_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(eeCertPath, sizeof(eeCertPath), isServer ? SERVER_DER : CLIENT_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(privKeyPath, sizeof(privKeyPath), isServer ? SERVER_KEY_DER : CLIENT_KEY_DER, certStr);
ASSERT_TRUE(ret > 0);
HLT_SetCaCertPath(ctxConfig, (char *)caCertPath);
HLT_SetChainCertPath(ctxConfig, (char *)chainCertPath);
HLT_SetEeCertPath(ctxConfig, (char *)eeCertPath);
HLT_SetPrivKeyPath(ctxConfig, (char *)privKeyPath);
return 0;
EXIT:
return -1;
}
static int SetCertPath1(HLT_Ctx_Config *ctxConfig, const char *certStr, const char *certStr1, bool isServer)
{
int ret;
char caCertPath[50] = {0};
char chainCertPath[30] = {0};
char eeCertPath[30] = {0};
char privKeyPath[30] = {0};
ret = sprintf_s(caCertPath, sizeof(caCertPath), ROOT_DER, certStr1, certStr1);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(chainCertPath, sizeof(chainCertPath), INTCA_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(eeCertPath, sizeof(eeCertPath), isServer ? SERVER_DER : CLIENT_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(privKeyPath, sizeof(privKeyPath), isServer ? SERVER_KEY_DER : CLIENT_KEY_DER, certStr);
ASSERT_TRUE(ret > 0);
HLT_SetCaCertPath(ctxConfig, (char *)caCertPath);
HLT_SetChainCertPath(ctxConfig, (char *)chainCertPath);
HLT_SetEeCertPath(ctxConfig, (char *)eeCertPath);
HLT_SetPrivKeyPath(ctxConfig, (char *)privKeyPath);
return 0;
EXIT:
return -1;
}
static void GetDefaultPointFormats(FRAME_HsExtArray8 *exField)
{
exField->exState = INITIAL_FIELD;
exField->exType.state = INITIAL_FIELD;
exField->exType.data = HS_EX_TYPE_POINT_FORMATS;
uint8_t data[] = {0};
FRAME_ModifyMsgArray8(data, sizeof(data), &exField->exData, &exField->exDataLen);
exField->exLen.state = INITIAL_FIELD;
exField->exLen.data = exField->exDataLen.data + sizeof(uint8_t);
}
static void MalformedServerHelloMsgCallback001(void *msg, void *userData)
{
// ServerHello exception: Duplicate point format extension.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
GetDefaultPointFormats(&serverHello->pointFormats);
serverHello->pointFormats.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC010
* @title extension_serverhello point format extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the tested end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC010(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE; // Message type to be modified
handle.expectHsType = SERVER_HELLO; // Handshake message type to be modified
handle.frameCallBack = MalformedServerHelloMsgCallback001; // reconstruction callback
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback002(void *msg, void *userData)
{
// ServerHello exception: The extended master key extension is duplicate
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->extendedMasterSecret.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC011
* @title extension_serverHello extension master key extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the tested end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC011(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback002;
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback003(void *msg, void *userData)
{
// ServerHello exception: The extended renegotiation extension is repeated.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->secRenego.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC012
* @title extension_serverHello renegotiation extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC012(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback003;
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportRenegotiation = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback004(void *msg, void *userData)
{
// ServerHello exception: Duplicate sessionticket extension.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->sessionTicket.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC013
* @title extension_serverHello sessionticket extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC013(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback004;
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSessionTicket = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback005(void *msg, void *userData)
{
// serverHello exception: The serverName extension is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->serverName.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC014
* @title extension_serverHello servername extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message in the alerted state.
4. The status of the tested end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC014(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback005;
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback006(void *msg, void *userData)
{
// ServerHello exception: The alpn extension is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->alpn.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC014
* @title extension_serverHello Alpn extension duplicate
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the tested end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC009(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback006;
TestPara testPara = {0};
testPara.port = PORT;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportAlpn = true;
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback002(void *msg, void *userData)
{
// ClientHello exception: The format extension of the sent ClientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC002
* @title The point format extension of the ClientHello message is duplicate
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC002(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback002;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback003(void *msg, void *userData)
{
// ClientHello exception: The signature algorithm extension of the sent ClientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->signatureAlgorithms.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC003
* @title The signature algorithm extension for the clientHello message sent by the client is duplicate._Signature algorithm extension
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC003(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback003;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback004(void *msg, void *userData)
{
// ClientHello exception: The sent ClientHello message supports group extension repetition.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC004
* @title The clientHello message sent by the client supports group extension repetition._Group extension is supported.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC004(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback004;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback005(void *msg, void *userData)
{
// ClientHello exception: The extended master key extension in the sent ClientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->extendedMasterSecret.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC005
* @title Extended master key for the clientHello message that is sent repeatedly_Extended master key
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC005(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback005;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback006(void *msg, void *userData)
{
// ClientHello exception: The extended sessionticket extension of the clientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->sessionTicket.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC006
* @title The sessionticket extension for the clientHello message sent by the client is duplicate.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC006(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback006;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSessionTicket = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
// ClientHello exception: The extension servername of the clientHello message is duplicate.
static void MalformedClientHelloMsgCallback007(void *msg, void *userData)
{
// ClientHello exception: The extension servername of the clientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = DUPLICATE_FIELD;
clientHello->serverName.exLen.state = INITIAL_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t uu[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(uu, sizeof(uu)-1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007
* @title The servername extension of the clientHello message is duplicate _servername.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback007;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
// ClientHello exception: The extended alpn extension of the clientHello message is duplicate.
static void MalformedClientHelloMsgCallback008(void *msg, void *userData)
{
// ClientHello exception: The extended alpn extension of the clientHello message is duplicate.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->alpn.exState = DUPLICATE_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC008
* @title The alpn extension of the clientHello message is repeated _alpn.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC008(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback008;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportAlpn = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC001
* @title The sessionticket field is carried during the first connection setup. The extended field is not carried during the session recovery. The expected result is that the session recovery fails and the handshake is performed again.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Enable the session ticket function and initiate link establishment. Expected result 2 is obtained.
3. Configure the client not to support sessionticket during session restoration. Expected result 3 is obtained.
* @expect 1. A success message is returned.
2. The link is successfully established.
3. If the session fails to be restored, a new link is established.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC001(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = true;
clientCtxConfig->isSupportRenegotiation = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = true;
serverCtxConfig->isSupportRenegotiation = false;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
clientCtxConfig->isSupportSessionTicket = false;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(clientCtxConfig->isSupportSessionTicket == false);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_HasTicket(session) == true);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC002
* @title The sessionticket field is not carried during the first connection setup. The extended field is carried during session recovery. The expected result is that the session recovery fails and the handshake is performed again.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Disable the session ticket function and initiate link establishment. Expected result 2 is obtained.
3. Configure the client to support sessionticket during session restoration. Expected result 3 is obtained.
* @expect 1. A success message is returned.
2. The link is set up successfully.
3. The session is restored successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC002(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = false;
clientCtxConfig->isSupportRenegotiation = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = false;
serverCtxConfig->isSupportRenegotiation = false;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
clientCtxConfig->isSupportSessionTicket = true;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(clientCtxConfig->isSupportSessionTicket == true);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC003
* @title Renegotiation is carried in the first link setup message, and this extended field is not carried in the session recovery message. The expected result is that the session recovery is successful.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Enable renegotiation and initiate link establishment. Expected result 2 is obtained.
3. Configure the client not to support renegotiation during session restoration. Expected result 3 is obtained.
* @expect 1. A success message is returned.
2. The link is set up successfully.
3. The session is restored successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC003(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = false;
clientCtxConfig->isSupportRenegotiation = true;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = false;
serverCtxConfig->isSupportRenegotiation = true;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
clientCtxConfig->isSupportRenegotiation = false;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC004
* @title The first link establishment does not carry the renegotiation IE, and the session recovery IE carries the extended field. The expected result is that the session recovery is successful.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Disable renegotiation and initiate link establishment. Expected result 2 is obtained.
3. Configure the client to support renegotiation during session restoration. Expected result 3 is obtained.
* @expect 1. A success message is returned.
2. The link is set up successfully.
3. The session is restored successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_RESUME_TAKE_EXTENSION_TC004(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = false;
clientCtxConfig->isSupportRenegotiation = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = false;
serverCtxConfig->isSupportRenegotiation = false;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
clientCtxConfig->isSupportRenegotiation = true;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC001
* @title The handshake fails because different cipher suites are configured on the client and server.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Configure different cipher suites on the client and server and initiate link establishment. (Expected result 2)
* @expect 1. A success message is returned.
2. The link fails to be established.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC001(int version, int connType)
{
bool certverifyflag = false;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384");
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC003
* @title The RSA and ECDSA cipher suites are configured on the client and server, and the ECDSA certificate is configured. The handshake is successful.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Configure the RSA and ECDSA cipher suites and ECDSA certificates on the client and server, and initiate link establishment. (Expected result 2)
* @expect 1. A success message is returned.
2. Link establishment fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC003(int version, int connType)
{
bool certverifyflag = false;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_RSA_WITH_AES_128_CBC_SHA256:HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_128_CBC_SHA256:HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
void MalformedClientHellocallback001(void *msg, void *userData)
{
// ClientHello is abnormal. ClientHello modifies the algorithm suite.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
/* Modify the structure. */
uint16_t suite[] = {0x00fe, HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
ASSERT_TRUE(FRAME_ModifyMsgArray16(suite, sizeof(suite)/sizeof(uint16_t),
&(clientHello->cipherSuites), &(clientHello->cipherSuitesSize)) == HITLS_SUCCESS);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC002
* @title The client and server set incorrect and correct cipher suites. The handshake succeeds.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Configure different cipher suites on the client and server and initiate link establishment. (Expected result 2)
* @expect 1. A success message is returned.
2. A link is established normally. An error is reported during hash check.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC002(int version, int connType)
{
bool certverifyflag = false;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
DataChannelParam channelParam = {0};
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE(sockFd.srcFd > 0);
ASSERT_TRUE(sockFd.peerFd > 0);
remoteProcess->connFd = sockFd.peerFd;
remoteProcess->connType = connType;
localProcess->connFd = sockFd.srcFd;
localProcess->connType = connType;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(remoteProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetLegacyRenegotiateSupport(clientCtxConfig, true);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsInit(localProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_FrameHandle handle = {0};
handle.ctx = clientRes->ssl;
handle.userData = (void*)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHellocallback001;
ASSERT_TRUE(HLT_SetFrameHandle(&handle) == HITLS_SUCCESS);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_REC_BAD_RECORD_MAC);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
void MalformedServerHellocallback001(void *msg, void *userData)
{
// ServerHello packet: Check the serverHello algorithm suite.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
/* Determine algorithm suite */
ASSERT_EQ(serverHello->cipherSuite.data, 0x6d);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC004
* @title Set insecure and secure cipher suites on the client and server, set the ECDSA certificate, and select the secure cipher suite as expected.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client. Expected result 1 is obtained.
2. Configure insecure and secure cipher suites on the client and server, configure the ECDSA certificate, and initiate link establishment. (Expected result 2)
* @expect 1. A success message is returned.
2. The link is successfully established and the security algorithm suite is selected.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_NEGOTIATE_CIPHERSUITE_TC004(int version, int connType)
{
bool certverifyflag = false;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
DataChannelParam channelParam = {0};
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE(sockFd.srcFd > 0);
ASSERT_TRUE(sockFd.peerFd > 0);
remoteProcess->connFd = sockFd.peerFd;
remoteProcess->connType = connType;
localProcess->connFd = sockFd.srcFd;
localProcess->connType = connType;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_DH_ANON_WITH_AES_256_CBC_SHA256:HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(remoteProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_DH_ANON_WITH_AES_256_CBC_SHA256:HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsInit(localProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_SUCCESS);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_SUCCESS);
ASSERT_EQ(((HITLS_Ctx *)clientRes->ssl)->negotiatedInfo.cipherSuiteInfo.cipherSuite, HITLS_DH_ANON_WITH_AES_256_CBC_SHA256);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
//The clientHello is abnormal. The extended servername length of the clientHello message is smaller than the actual length.
static void MalformedClientHelloMsgCallback009(void *msg, void *userData)
{
//The clientHello is abnormal. The extended servername length of the clientHello message is smaller than the actual length.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t uu[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(uu, sizeof(uu)-1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data--;
clientHello->serverName.exLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007
* @title The extended length of the servername in the clientHello message is smaller than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC048(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback009;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
//The clientHello message is abnormal. The extended servername length of the clientHello message is greater than the actual length.
static void MalformedClientHelloMsgCallback010(void *msg, void *userData)
{
//The clientHello message is abnormal. The extended servername length of the clientHello message is greater than the actual length.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t uu[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(uu, sizeof(uu)-1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data++;
clientHello->serverName.exLen.data++;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007
* @title The extended length of the servername in the clientHello message is greater than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC047(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback010;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
// ClientHello exception: The extended length of the servername in the clientHello message is 0 and the content is not null.
static void MalformedClientHelloMsgCallback011(void *msg, void *userData)
{
// ClientHello exception: The length of the extended servername in the clientHello message is 0 and the content is not null.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t uu[4] = {0x00, 0x00, 0x01,0x01};
FRAME_ModifyMsgArray8(uu, sizeof(uu)-1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data = 0;
clientHello->serverName.exDataLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007
* @title The extended length of the servername in the clientHello message is 0 and the content is not null.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC046(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback011;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
//The clientHello is abnormal. The extended length of the servername in the clientHello message is 0. The content is empty.
static void MalformedClientHelloMsgCallback012(void *msg, void *userData)
{
//The clientHello is abnormal. The extended length of the servername in the clientHello message is 0. The content is empty.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = MISSING_FIELD;
clientHello->serverName.exData.state = MISSING_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
clientHello->serverName.exLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_REPEAT_EXTENSION_TC007
* @title The extended length of the servername in the clientHello message is 0. The content is empty.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC045(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback012;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportSni = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CIPHERSUITE_NOT_SUITABLE_CERT_TC003
* @title When dual-end authentication is configured, the cipher suite is set to RSA, the RSA certificate is set on the server, and the ECDSA certificate is set on the client, the link fails to be established.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, set the cipher suite to RSA, set the RSA certificate on the server, and set the ECDSA certificate on the client. Expected result 1 is obtained.
2. Initiate a link establishment request. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
2. Link establishment fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CIPHERSUITE_NOT_SUITABLE_CERT_TC003(int version, int connType)
{
bool certverifyflag = true;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath1(serverCtxConfig, "rsa_sha256", "ecdsa_sha256", true);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_DHE_RSA_WITH_AES_256_CBC_SHA256");
serverCtxConfig->isSupportClientVerify = certverifyflag;
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath1(clientCtxConfig, "ecdsa_sha256", "rsa_sha256", false);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_DHE_RSA_WITH_AES_256_CBC_SHA256");
clientCtxConfig->isSupportClientVerify = certverifyflag;
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MULTILINK_RESUME_ALERT_TC002
* @title Apply for establishing and disconnecting a link between the client and server, apply for two links, and use the session ID of the previous session to restore the session. The restoration is expected to be successful.
* @precon nan
* @brief 1. Apply for establishing and disconnecting a link between the client and server.
2. Apply for two links and use the session ID of the previous session to restore the session. The restoration is expected to be successful.
* @expect 1. The link is successfully established.
2. The restoration is successful.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MULTILINK_RESUME_ALERT_TC002(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HLT_FD sockFd2 = {0};
int cunt = 1;
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
void *clientConfig2 = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(clientConfig2 != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *clientCtxConfig2 = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig2, clientCtxConfig2) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (session != NULL) {
DataChannelParam channelParam2;
channelParam2.port = PORT;
channelParam2.type = connType;
channelParam2.isBlock = true;
sockFd2 = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam2);
ASSERT_TRUE((sockFd2.srcFd > 0) && (sockFd2.peerFd > 0));
remoteProcess->connType = connType;
localProcess->connType = connType;
remoteProcess->connFd = sockFd2.peerFd;
localProcess->connFd = sockFd2.srcFd;
int32_t serverSslId2 = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig2;
serverSslConfig2 = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig2 != NULL);
serverSslConfig2->sockFd = remoteProcess->connFd;
serverSslConfig2->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId2, serverSslConfig2) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId2);
void *clientSsl2 = HLT_TlsNewSsl(clientConfig2);
ASSERT_TRUE(clientSsl2 != NULL);
HLT_Ssl_Config *clientSslConfig2;
clientSslConfig2 = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig2 != NULL);
clientSslConfig2->sockFd = localProcess->connFd;
clientSslConfig2->connType = connType;
HLT_TlsSetSsl(clientSsl2, clientSslConfig2);
ASSERT_TRUE(HITLS_SetSession(clientSsl2, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl2) == 0);
HITLS_Session *Newsession = HITLS_GetDupSession(clientSsl2);
ASSERT_TRUE(Newsession != NULL);
ASSERT_TRUE(memcmp(session->sessionId, Newsession->sessionId, HITLS_SESSION_ID_MAX_SIZE) == 0);
HITLS_SESS_Free(Newsession);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
HITLS_SESS_Free(session);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
cunt++;
} while (cunt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc5246_extensions.c | C | unknown | 94,967 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246_malformed_msg */
/* BEGIN_HEADER */
#include "hitls_error.h"
#include "tls.h"
#include "rec.h"
#include "hs_msg.h"
#include "hs_ctx.h"
#include "hs_extensions.h"
#include "frame_msg.h"
/* END_HEADER */
// Replace the message to be sent with the CERTIFICATION_VERIFY message.
void TEST_SendUnexpectCertificateVerifyMsg(void *msg, void *data)
{
FRAME_Type *frameType = (FRAME_Type *)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_Msg newFrameMsg = {0};
HS_MsgType hsTypeTmp = frameType->handshakeType;
REC_Type recTypeTmp = frameType->recordType;
frameType->handshakeType = CERTIFICATE_VERIFY;
FRAME_Init(); // Callback for changing the certificate algorithm, which is used to generate negotiation handshake
// messages.
FRAME_GetDefaultMsg(frameType, &newFrameMsg);
HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT); // recovery callback
// Release the original msg.
frameType->handshakeType = hsTypeTmp;
frameType->recordType = recTypeTmp;
FRAME_CleanMsg(frameType, frameMsg);
// Change message.
frameType->recordType = REC_TYPE_HANDSHAKE;
frameType->handshakeType = CERTIFICATE_VERIFY;
frameType->keyExType = HITLS_KEY_EXCH_ECDHE;
if (memcpy_s(msg, sizeof(FRAME_Msg), &newFrameMsg, sizeof(newFrameMsg)) != EOK) {
Print("TEST_SendUnexpectCertificateMsg memcpy_s Error!");
}
}
static void MalformedClientHelloMsgCallback_01(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->version.state = SET_LEN_TO_ONE_BYTE;
clientHello->randomValue.state = MISSING_FIELD;
clientHello->sessionIdSize.state = MISSING_FIELD;
clientHello->sessionId.state = MISSING_FIELD;
clientHello->cookiedLen.state = MISSING_FIELD;
clientHello->cookie.state = MISSING_FIELD;
clientHello->cipherSuitesSize.state = MISSING_FIELD;
clientHello->cipherSuites.state = MISSING_FIELD;
clientHello->compressionMethodsLen.state = MISSING_FIELD;
clientHello->compressionMethods.state = MISSING_FIELD;
clientHello->extensionState = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC001
* @title version field only one byte _version
* @precon nan
* @brief
1. The server stops receiving client information. 1. ClientHello exception: The version field in the constructed
message to be sent contains only one byte and cannot be decoded. Expected result 1 is obtained.
* @expect 1. The processing result is HITLS_PARSE_INVALID_MSG_LEN.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC001(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
/* 1. The server stops receiving client information. 1. ClientHello exception: The version field in the constructed
* message to be sent contains only one byte and cannot be decoded. */
handle.frameCallBack = MalformedClientHelloMsgCallback_01;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isExpectRet = true;
testPara.expectRet = HITLS_PARSE_INVALID_MSG_LEN;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_02(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->randomValue.size = 1;
clientHello->randomValue.state = ASSIGNED_FIELD;
clientHello->sessionIdSize.state = MISSING_FIELD;
clientHello->sessionId.state = MISSING_FIELD;
clientHello->cookiedLen.state = MISSING_FIELD;
clientHello->cookie.state = MISSING_FIELD;
clientHello->cipherSuitesSize.state = MISSING_FIELD;
clientHello->cipherSuites.state = MISSING_FIELD;
clientHello->compressionMethodsLen.state = MISSING_FIELD;
clientHello->compressionMethods.state = MISSING_FIELD;
clientHello->extensionState = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC002
* @title random Less than 32 bytes_random
* @precon nan
* @brief 1. The server stops receiving client hello messages. Expected result 1 is obtained.
2. Modify the client to send the client hello message and change the random field to only one byte. Expected
result 2 is obtained.
3. The server continues to establish a link. (Expected result 3)
* @expect 1. Success
2. Success
3. Return a failure message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC002(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The server stops receiving client hello messages. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Modify the client to send the client hello message and change the random field to only one byte. */
handle.frameCallBack = MalformedClientHelloMsgCallback_02;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isExpectRet = true;
testPara.expectRet = HITLS_PARSE_INVALID_MSG_LEN;
/* 3. The server continues to establish a link. */
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_03(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t sessionId[MAX_SESSION_ID_SIZE] = {0};
ASSERT_TRUE(FRAME_ModifyMsgArray8(sessionId, MAX_SESSION_ID_SIZE, &clientHello->sessionId, NULL) == HITLS_SUCCESS);
clientHello->sessionIdSize.data = 0u;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC003
* @titleThe session ID length of the clientHello message is 0 but the content is not null. _session ID
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC003(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_03;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello message.
*/
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_04(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t sessionId[MIN_SESSION_ID_SIZE - 1] = {0};
FRAME_ModifyMsgArray8(sessionId, sizeof(sessionId), &clientHello->sessionId, &clientHello->sessionIdSize);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC004
* @title The length of the session ID in the clientHello message is smaller than the minimum length_session ID
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC004(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_04;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3.Check the status of the tested. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_05(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t sessionId[MAX_SESSION_ID_SIZE + 1] = {0};
FRAME_ModifyMsgArray8(sessionId, sizeof(sessionId), &clientHello->sessionId, &clientHello->sessionIdSize);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC005
* @title The length of the session ID in the clientHello message exceeds the maximum length_session ID
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC005(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_05;
TestPara testPara = {0};
testPara.port = PORT;
/* 3. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 4. Check the status of the tested. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_06(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data = 0;
clientHello->cipherSuites.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC006
* @title The length of the cipher suite in the clientHello message is 0 and the content is empty. _cipher suites
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC006(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_06;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_07(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t rawData[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(
rawData, sizeof(rawData) - 1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data -= 2;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC007
* @title The length of the cipher suites in the clientHello message sent is an odd number_cipher suites
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested returns an alert message, and the status is alerted.
4. The status of the test is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC007(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_07;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_08(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data = 0;
EXIT:
return;
}
/* @
* @test SDV_HITLS_TEST_DTLS_MALFORMED_CLIENT_HELLO_MSG_FUN_TC010
* @title The length of the cipher suite in the clientHello message is 0 but the content is not null. _cipher suites
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested returns an alert message, indicating that the status is alerted.
4. The status of the test is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC008(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_08;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_ILLEGAL_PARAMETER;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_09(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuites.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC009
* @title The length of the cipher suite in the clientHello message is not 0 but the content is empty. _cipher suites
length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested returns an alert message, and the status is alerted.
4. The status of the test is alerted, and the handshake status is ready to receive the serverHello message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC009(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_09;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_10(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data -= sizeof(uint16_t);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC010
* @title The length of the cipher suite in the sent clientHello message is less than the specific content
length_cipher suites length
* @precon nan
* @brief 1. The tested functions as the client, and the tested functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested. Expected result 3 is obtained.
4. Check the status of the test. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested returns an alert message, and the status is alerted.
4. The status of the test is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC010(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested functions as the client, and the tested functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_10;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_11(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data += sizeof(uint16_t);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC011
* @title The length of the cipher suite in the sent ClientHello message is greater than the specific content
length_cipher suites length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC011(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_11;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_12(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->compressionMethodsLen.data = 0;
clientHello->compressionMethods.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC012
* @title The length of the compression list of the sent ClientHello message is 0 and the content is empty.
_compression methods length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC012(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_12;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test end. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested end. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_13(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t compressionMethods[] = {0, 1};
FRAME_ModifyMsgArray8(compressionMethods, sizeof(compressionMethods),
&clientHello->compressionMethods, &clientHello->compressionMethodsLen);
clientHello->compressionMethodsLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC013
* @title The length of the compression list in the sent ClientHello message is less than the content
length_compression methods length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC013(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_13;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test end. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested end. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_14(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t compressionMethods[] = {0};
FRAME_ModifyMsgArray8(compressionMethods, sizeof(compressionMethods),
&clientHello->compressionMethods, &clientHello->compressionMethodsLen);
clientHello->compressionMethodsLen.data++;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC014
* @title The length of the compression list in the sent ClientHello message is greater than the content
length_compression methods length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC014(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_14;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test end. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested end. */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_15(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t compressionMethods[] = {1};
FRAME_ModifyMsgArray8(compressionMethods, sizeof(compressionMethods),
&clientHello->compressionMethods, &clientHello->compressionMethodsLen);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC015
* @title The compression list of clientHello messages sent by the client does not contain compression algorithm
_compression methods length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC015(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_15;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested . */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_16(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->extensionLen.state = ASSIGNED_FIELD;
clientHello->extensionLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC016
* @title The extended length of the clientHello message sent by the client is smaller than the actual message
length_Total extended length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC016(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_16;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_17(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->extensionLen.state = ASSIGNED_FIELD;
clientHello->extensionLen.data++;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC017
* @title The extended length of the clientHello message sent by the client is greater than the actual message
length_Total extended length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC017(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_17;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_18(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exLen.state = ASSIGNED_FIELD;
clientHello->pointFormats.exLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC018
* @title The extended length of the sent ClientHello message point format is smaller than the actual length_Extended
point format
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
* prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC018(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_18;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_19(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exLen.state = ASSIGNED_FIELD;
clientHello->pointFormats.exLen.data++;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC019
* @title The extended length of the sent ClientHello message point format is greater than the actual length_Extended
point format
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC019(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_19;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_20(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exDataLen.data = 0;
clientHello->pointFormats.exData.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC020
* @title The length of the sent ClientHello message is 0 and the content is empty. _ The dot format is extended
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC020(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_20;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_21(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exDataLen.data = 1;
clientHello->pointFormats.exData.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC021
* @title The length of the point format of the clientHello message is not 0 and the content is null. _ The point
format is extended
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC021(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_21;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_22(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exLen.data -= sizeof(uint16_t);
clientHello->supportedGroups.exLen.state = ASSIGNED_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC022
* @title The length of the clientHello message that supports group extension is smaller than the actual length_Group
extension is supported
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC022(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_22;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_23(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exLen.data += sizeof(uint16_t);
clientHello->supportedGroups.exLen.state = ASSIGNED_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC023
* @title The length of the clientHello message that supports group extension is greater than the actual length._Group
extension is supported
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC023(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_23;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_24(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exDataLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC024
* @title The clientHello message sent by the client supports the odd number of group lengths._Group extension is
supported
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC024(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_24;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_25(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exDataLen.data = 0;
clientHello->supportedGroups.exData.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC025
* @title The clientHello message can contain 0 characters and cannot contain any characters._Group extension is
supported.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC025(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_25;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_26(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->signatureAlgorithms.exLen.state = ASSIGNED_FIELD;
clientHello->signatureAlgorithms.exLen.data -= sizeof(uint16_t);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC026
* @title The extended signature algorithm length of the clientHello message is less than the actual
length_signature algorithm extension
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1
is obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC026(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_26;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_27(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->signatureAlgorithms.exLen.state = ASSIGNED_FIELD;
clientHello->signatureAlgorithms.exLen.data += sizeof(uint16_t);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC027
* @title The extended signature algorithm length of the clientHello message is greater than the actual
length_signature algorithm extension
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC027(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_27;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_28(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->signatureAlgorithms.exDataLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC028
* @title The signature algorithm length of the clientHello message sent by the client is an odd number_signature
algorithm extension
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC028(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_28;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_29(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->signatureAlgorithms.exDataLen.data = 0;
clientHello->signatureAlgorithms.exData.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC029
* @title The signature algorithm length of the clientHello message is 0 and the content is empty_signature algorithm
extension
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC029(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_29;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_30(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
uint8_t extendedMasterSecret[] = {0};
FRAME_ModifyMsgArray8(extendedMasterSecret, sizeof(extendedMasterSecret),
&clientHello->extendedMasterSecret.exData, NULL);
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC030
* @title The length of the extended master key in the clientHello message is not 0_Extended master
key
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC030(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_30;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_31(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->sessionTicket.exDataLen.state = ASSIGNED_FIELD;
clientHello->sessionTicket.exDataLen.data--;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC031
* @title The SessionTicket extension length of the clientHello message sent by the client is smaller than the actual
length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC031(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_31;
TestPara testPara = {0};
testPara.isSupportSessionTicket = 1;
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_32(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->sessionTicket.exDataLen.state = ASSIGNED_FIELD;
clientHello->sessionTicket.exDataLen.data++;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC032
* @title The SessionTicket length of the clientHello message is greater than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message in the alerted state.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC032(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_32;
TestPara testPara = {0};
testPara.isSupportSessionTicket = 1;
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_33(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->sessionTicket.exDataLen.data = 1;
clientHello->sessionTicket.exDataLen.state = SET_LEN_TO_ONE_BYTE;
clientHello->sessionTicket.exData.state = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC033
* @title The SessionTicket length of the clientHello message is not zero and the content is empty.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, indicating that the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC033(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_33;
TestPara testPara = {0};
testPara.isSupportSessionTicket = 1;
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_34(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t rawData[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(
rawData, sizeof(rawData) - 1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data -= 2;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC034
* @title The extended length of the servername in the clientHello message is smaller than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC034(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_34;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_35(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t rawData[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(
rawData, sizeof(rawData) - 1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data += 2;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC035
* @title The extended length of the servername in the clientHello message is greater than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC035(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_35;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_36(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
uint8_t rawData[4] = {0x00, 0x00, 0x01, 0x01};
FRAME_ModifyMsgArray8(
rawData, sizeof(rawData) - 1, &clientHello->serverName.exData, &clientHello->serverName.exDataLen);
clientHello->serverName.exLen.data = 0;
clientHello->serverName.exDataLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC036
* @title The extended length of the servername in the clientHello message is 0 and the content is not null.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC036(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_36;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_37(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->serverName.exState = INITIAL_FIELD;
clientHello->serverName.exLen.state = ASSIGNED_FIELD;
clientHello->serverName.exDataLen.state = MISSING_FIELD;
clientHello->serverName.exData.state = MISSING_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->serverName.exType);
clientHello->serverName.exLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC037
* @title The length of the servername extension in the clientHello message is 0 and the content is empty.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC037(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_37;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_38(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->alpn.exState = INITIAL_FIELD;
clientHello->alpn.exLen.state = ASSIGNED_FIELD;
clientHello->alpn.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_APP_LAYER_PROTOCOLS, &clientHello->alpn.exType);
uint8_t rawData[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(rawData, sizeof(rawData) - 1, &clientHello->alpn.exData, &clientHello->alpn.exDataLen);
clientHello->alpn.exLen.data -= 2;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC038
* @title The extended length of the servername in the clientHello message is smaller than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC038(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_38;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportALPN = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_39(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->alpn.exState = INITIAL_FIELD;
clientHello->alpn.exLen.state = ASSIGNED_FIELD;
clientHello->alpn.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_APP_LAYER_PROTOCOLS, &clientHello->alpn.exType);
uint8_t rawData[13] = {0x00, 0x00, 0x09, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x63, 0x6F, 0x6d};
FRAME_ModifyMsgArray8(rawData, sizeof(rawData) - 1, &clientHello->alpn.exData, &clientHello->alpn.exDataLen);
clientHello->alpn.exLen.data += 2;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC039
* @title The extended length of the servername in the clientHello message is greater than the actual length.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. Return a success message.
2. Return a success message.
3. Return an alert message. The status of the tested end is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC039(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_39;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportALPN = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_40(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->alpn.exState = INITIAL_FIELD;
clientHello->alpn.exLen.state = ASSIGNED_FIELD;
clientHello->alpn.exDataLen.state = INITIAL_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_APP_LAYER_PROTOCOLS, &clientHello->alpn.exType);
uint8_t rawData[4] = {0x02, 0x02, 0x01, 0x01};
FRAME_ModifyMsgArray8(rawData, sizeof(rawData) - 1, &clientHello->alpn.exData, &clientHello->alpn.exDataLen);
clientHello->alpn.exLen.data = 0;
clientHello->alpn.exDataLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC040
* @title The extended length of the servername in the clientHello message is 0 and the content is not null.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC040(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedClientHelloMsgCallback_40;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportALPN = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_41(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->alpn.exState = INITIAL_FIELD;
clientHello->alpn.exLen.state = ASSIGNED_FIELD;
clientHello->alpn.exDataLen.state = MISSING_FIELD;
clientHello->alpn.exData.state = MISSING_FIELD;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SERVER_NAME, &clientHello->alpn.exType);
clientHello->alpn.exLen.data = 0;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC041
* @title The extended length of the servername in the clientHello message is 0. The content is empty.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the tested end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns alert, and the status is alert.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC041(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
// 2. Obtain the message, modify the field content, and send the message.
handle.frameCallBack = MalformedClientHelloMsgCallback_41;
TestPara testPara = {0};
testPara.port = PORT;
// 4. Check the status of the tested end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
testPara.isSupportExtendMasterSecret = true;
testPara.isSupportALPN = true;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_42(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->extendedMasterSecret.exType.data = 0xFFFFu;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC042
* @title The sent ClientHello message contains an unrecognized extension type _ extension type
* @precon nan
* @brief 1. The tested end functions as the client and the tested end functions as the server. Expected result 1 is
obtained.
2. Capture the message, modify the field content, and send the message. Expected result 2 is obtained.
3. Check the status of the tested end. Expected result 3 is obtained. 4. Check the status of the tested end.
Expected result 4 is obtained.
* @expect 1. A success message is returned. 2. A success message is returned. 3. The tested end returns alert and the
status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC042(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback_42;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_FINISH;
testPara.expectDescription = ALERT_DECRYPT_ERROR;
testPara.isSupportExtendMasterSecret = false;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedClientHelloMsgCallback_43(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->extensionState = MISSING_FIELD;
clientHello->pointFormats.exState = MISSING_FIELD;
clientHello->supportedGroups.exState = MISSING_FIELD;
clientHello->signatureAlgorithms.exState = MISSING_FIELD;
clientHello->extendedMasterSecret.exState = MISSING_FIELD;
clientHello->secRenego.exState = MISSING_FIELD;
EXIT:
return;
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC043
* @title The sent ClientHello message does not contain any extension type.
* @precon nan
* @brief 1. The server stops receiving client hello messages. Expected result 1 is obtained.
2. Configure the client to send a client hello message without any extension type. Expected result 2 is obtained.
3. The server continues to establish a link. Expected result 3 is obtained.
* @expect 1. Success 2. Success 3. Success
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_MALFORMED_CLIENT_HELLO_MSG_FUN_TC043(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback_43;
TestPara testPara = {0};
testPara.port = PORT;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
testPara.expectDescription = ALERT_HANDSHAKE_FAILURE;
testPara.isExpectRet = true;
testPara.expectRet = HITLS_MSG_HANDLE_CIPHER_SUITE_ERR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void TEST_UnexpectMsg(HLT_FrameHandle *frameHandle, TestExpect *testExpect, bool isSupportClientVerify)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
ALERT_Info alertInfo = {0};
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfigTLCP(NULL, "SERVER", false);
ASSERT_TRUE(serverConfig != NULL);
if (isSupportClientVerify) {
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, isSupportClientVerify) == 0);
}
HLT_Ctx_Config *clientConfig = HLT_NewCtxConfigTLCP(NULL, "CLIENT", true);
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(clientConfig, isSupportClientVerify) == 0);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLCP1_1, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Client Initialization
clientRes = HLT_ProcessTlsInit(localProcess, TLCP1_1, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(frameHandle != NULL);
frameHandle->ctx = clientRes->ssl;
HLT_SetFrameHandle(frameHandle);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), testExpect->connectExpect);
HLT_CleanFrameHandle();
ALERT_GetInfo(clientRes->ssl, &alertInfo);
ASSERT_TRUE(alertInfo.level == testExpect->expectLevel);
ASSERT_EQ(alertInfo.description, testExpect->expectDescription);
ASSERT_EQ(HLT_RpcGetTlsAcceptResult(serverRes->acceptId), testExpect->acceptExpect);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC006
* @titleThe client does not send the certificate. Instead, the client sends the certificate.
* @precon nan
* @brief
1. Configure dual-end verification. Expected result 1 is obtained.
2. Set the client severhello done callback to send certificate verify.
* @expect 1. Expected success
2. Expected server to return alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC006()
{
// 1. Configure dual-end verification.
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
// 2. Set the client severhello done callback to send certificate verify.
frameHandle.frameCallBack = TEST_SendUnexpectClientKeyExchangeMsg;
frameHandle.expectHsType = CERTIFICATE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, true);
return;
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_SEND_CERTFICATE_VERITY_TC001
* @title The client does not send the certificate. Instead, the client sends the certificate.
* @precon nan
* @brief
1. Configure unidirectional authentication. Expected result 1 is obtained.
2. Set the client severhello done callback to send certificate verify.
* @expect 1. Expected success
2. Expected server to return alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CLIENT_SEND_CERTFICATE_VERITY_TC001()
{
// 1. Configure unidirectional authentication.
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
// 2. Set the client severhello done callback to send certificate verify.
frameHandle.frameCallBack = TEST_SendUnexpectCertificateMsg;
frameHandle.expectHsType = CLIENT_KEY_EXCHANGE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, false);
return;
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC007
* @title The client does not send the certificate. Instead, the client sends the certificate.
* @precon nan
* @brief
1. Configure unidirectional authentication. Expected result 1 is obtained.
2. Set the client severhello done callback to send certificate verify.
* @expect 1. Expected success
2. Expected server to return alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_CERTFICATE_VERITY_FAIL_TC007()
{
// 1. Configure unidirectional authentication.
TestExpect testExpect = {0};
testExpect.acceptExpect = HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
testExpect.expectLevel = ALERT_LEVEL_FATAL;
testExpect.expectDescription = ALERT_UNEXPECTED_MESSAGE;
testExpect.connectExpect = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
HLT_FrameHandle frameHandle = {0};
// 2. Set the client severhello done callback to send certificate verify.
frameHandle.frameCallBack = TEST_SendUnexpectCertificateVerifyMsg;
frameHandle.expectHsType = CLIENT_KEY_EXCHANGE;
frameHandle.expectReType = REC_TYPE_HANDSHAKE;
frameHandle.ioState = EXP_NONE;
frameHandle.pointType = POINT_SEND;
frameHandle.userData = NULL;
TEST_UnexpectMsg(&frameHandle, &testExpect, true);
return;
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC5246_CONSISTENCY_SUPPORT_GROUP_TC001(void)
{
FRAME_Init();
HITLS_Config *c_config = NULL;
HITLS_Config *s_config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
c_config = HITLS_CFG_NewTLS12Config();
s_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(c_config != NULL);
ASSERT_TRUE(s_config != NULL);
uint16_t cipherSuite = HITLS_ECDH_ANON_WITH_AES_128_CBC_SHA;
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(c_config, &cipherSuite, 1) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(s_config, &cipherSuite, 1) == HITLS_SUCCESS);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
client->ssl->config.tlsConfig.groupsSize = 0;
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
void ClientSendMalformedCipherSuiteLenMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess((HITLS), TCP, 16384, false);
ASSERT_TRUE(remoteProcess != NULL);
// The remote server listens on the TLS connection.
HLT_Ctx_Config *serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
serverConfig->isSupportExtendMasterSecret = false;
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the TLS connection on the local client.
HLT_Ctx_Config *clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
serverConfig->isSupportExtendMasterSecret = false;
HLT_Tls_Res *clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a connection and wait until the local is complete.
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
// Wait the remote.
int ret = HLT_GetTlsAcceptResult(serverRes);
ASSERT_TRUE(ret != 0);
if (testPara->isExpectRet) {
ASSERT_EQ(ret, testPara->expectRet);
}
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(clientRes->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, testPara->expectDescription);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
static void MalformedCipherSuiteLenCallback_01(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->cipherSuitesSize.data = 1000;
clientHello->cipherSuitesSize.state = ASSIGNED_FIELD;
EXIT:
return;
}
/** @
* @test SDV_TLS1_2_RFC5246_MALFORMED_CIPHER_SUITE_LEN_FUN_TC001
* @spec -
* @title The length of the cipher suite in the sent ClientHello message is greater than the specific content
length_cipher suites length
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server. Expected result 1 is
obtained.
2. Obtain the message, modify the field content, and send the message. (Expected result 2)
3. Check the status of the tested end. Expected result 3 is obtained.
4. Check the status of the test end. Expected result 4 is obtained.
* @expect 1. A success message is returned.
2. A success message is returned.
3. The tested end returns an alert message, and the status is alerted.
4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS1_2_RFC5246_MALFORMED_CIPHER_SUITE_LEN_FUN_TC001()
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
/* 1. The tested end functions as the client, and the tested end functions as the server. */
handle.expectHsType = CLIENT_HELLO;
/* 2. Obtain the message, modify the field content, and send the message. */
handle.frameCallBack = MalformedCipherSuiteLenCallback_01;
TestPara testPara = {0};
testPara.port = PORT;
/* 4. Check the status of the test. */
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
/* 3. Check the status of the tested */
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedCipherSuiteLenMsg(&handle, &testPara);
return;
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc5246_malformed_msg.c | C | unknown | 115,247 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <semaphore.h>
#include <unistd.h>
#include "securec.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_session.h"
#include "hitls_error.h"
#include "session.h"
#include "hlt.h"
#include "alert.h"
#include "frame_msg.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "process.h"
#include "hitls_type.h"
#include "session_type.h"
#include "cert_mgr.h"
#include "cert_mgr_ctx.h"
#include "hitls_cert_type.h"
#include "hs_extensions.h"
#include "rec_wrapper.h"
/* END_HEADER */
static uint32_t g_uiPort = 2569;
#define READ_BUF_SIZE 20
#define TEMP_DATA_LEN 1024
int32_t GetSessionCacheMode(HLT_Ctx_Config *config)
{
return config->setSessionCache;
}
static void FrameCallBack_SerrverHello_MasteKey_Add(void *msg, void *userData)
{
// ServerHello exception: The masterkey extension is added to the sent ServerHello message.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverhello = &frameMsg->body.hsMsg.body.serverHello;
serverhello->extensionLen.state = INITIAL_FIELD;
serverhello->extendedMasterSecret.exState = INITIAL_FIELD;
serverhello->extendedMasterSecret.exType.state = INITIAL_FIELD;
serverhello->extendedMasterSecret.exType.data = HS_EX_TYPE_EXTENDED_MASTER_SECRET;
serverhello->extendedMasterSecret.exLen.state = INITIAL_FIELD;
serverhello->extendedMasterSecret.exLen.data = 0u;
EXIT:
return;
}
static void FrameCallBack_SerrverHello_MasteKey_MISS(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverhello = &frameMsg->body.hsMsg.body.serverHello;
serverhello->extendedMasterSecret.exState = MISSING_FIELD;
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC006
* @title When the session is resumed, the client receives the server hello message that carries the master key
* extension.
* @precon nan
* @brief 1. The client and server do not support the extension connection establishment. Expected result 1 is
* obtained.
* 2. Disconnect the connection, save the session, and restore the session.
* 3. During session restoration, modify the server hello message to carry the master secret extension.
* Expected result 2 is obtained.
* 4. Establish a connection and observe the connection establishment status. (Expected result 3)
* @expect 1. The connection is set up successfully.
* 2. The modification is successful.
* 3. Session restoration fails and the handshake is interrupted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC006(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_SetExtenedMasterSecretSupport(clientCtxConfig, false);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// 1. The client and server do not support the extension connection establishment.
HLT_SetExtenedMasterSecretSupport(clientCtxConfig, false);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = g_uiPort;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
HLT_CleanFrameHandle();
HLT_FrameHandle handle = {0};
handle.pointType = POINT_RECV;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
// 3. During session restoration, modify the server hello message to carry the master key extension.
handle.frameCallBack = FrameCallBack_SerrverHello_MasteKey_Add;
handle.ctx = clientSsl;
ASSERT_TRUE(HLT_SetFrameHandle(&handle) == 0);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
// 4. Establish a connection and observe the connection establishment status.
ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
} else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// 2. Disconnect the connection, save the session, and restore the session.
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HLT_CleanFrameHandle();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC007
* @title When the session is resumed, the client receives a server hello message that does not carry the master key
* extension.
* @precon nan
* @brief 1. The client and server support the extension connection establishment. Expected result 1 is obtained.
* 2. Disconnect the connection, save the session, and restore the session.
* 3. During session recovery, modify the server hello command on the server to cause the master key extension
* to be lost. Expected result 2 is obtained.
* 4. Establish a connection and observe the connection setup status. (Expected result 3)
* @expect 1. The connection is set up successfully.
* 2. The modification is successful.
* 3. Session restoration fails and the handshake is interrupted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC007(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = g_uiPort;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
HLT_CleanFrameHandle();
HLT_FrameHandle handle = {0};
handle.pointType = POINT_RECV;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
/* 3. During session recovery, modify the server hello command on the server to cause the master key
* extension to be lost. */
handle.frameCallBack = FrameCallBack_SerrverHello_MasteKey_MISS;
handle.ctx = clientSsl;
ASSERT_TRUE(HLT_SetFrameHandle(&handle) == 0);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
// 4. Establish a connection and observe the connection setup status.
ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
} else {
// 1. The client and server support the extension connection establishment.
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
// 2. Disconnect the connection, save the session, and restore the session.
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
HLT_CleanFrameHandle();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC008
* @title Resume sessions on servers that do not support session recovery.
* @precon nan
* @brief 1. The client and server support the extension connection establishment. Disconnect the connection and save
* the session.
* 2. Apply for another server that does not support the extension and establish a connection. Expected result
* 2 is obtained.
* @expect 1. The connection is successfully established.
* 2. Session restoration fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC008(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HLT_FD sockFd2 = {0};
int cunt = 1;
int32_t serverConfigId = 0;
int32_t serverConfigId2 = 0;
HITLS_Session *session = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_Ctx_Config *serverCtxConfig2 = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
serverConfigId2 = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
serverConfigId2 = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// 2. Apply for another server that does not support the extension and establish a connection.
HLT_SetExtenedMasterSecretSupport(serverCtxConfig2, false);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (session != NULL) {
DataChannelParam channelParam2;
channelParam2.port = g_uiPort;
channelParam2.type = connType;
channelParam2.isBlock = true;
sockFd2 = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam2);
ASSERT_TRUE((sockFd2.srcFd > 0) && (sockFd2.peerFd > 0));
remoteProcess->connFd = sockFd2.peerFd;
localProcess->connFd = sockFd2.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId2 = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId2);
HLT_Ssl_Config *serverSslConfig2;
serverSslConfig2 = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig2 != NULL);
serverSslConfig2->sockFd = remoteProcess->connFd;
serverSslConfig2->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId2, serverSslConfig2) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId2);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
} else {
DataChannelParam channelParam;
channelParam.port = g_uiPort;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
cunt++;
} while (cunt <= 2);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_ClientHelloWithnoEMS(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extendedMasterSecret.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC009
* @title Resume sessions that both support no EMS on the client and server
* @precon nan
* @brief 1. The client and server that does not support the extension connection establishment.
* Disconnect the connection and save the session.
* 2. Apply for another server that does not support the extension and establish a connection. Expected result
* 2 is obtained.
* @expect 1. The connection is successfully established.
* 2. Session restoration success.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC7627_CONSISTENCY_EXTENDED_MASTER_SECRET_FUNC_TC009(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_SetExtenedMasterSecretSupport(clientCtxConfig, false);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetExtenedMasterSecretSupport(serverCtxConfig, false);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (session != NULL) {
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
} else {
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloWithnoEMS};
RegisterWrapper(wrapper);
}
DataChannelParam channelParam;
channelParam.port = g_uiPort;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_TRUE(isReused != 0);
} else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}
cnt++;
} while (cnt < 3);
EXIT:
ClearWrapper();
HLT_CleanFrameHandle();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc7627.c | C | unknown | 25,918 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls12_consistency_rfc5246 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "conn_init.h"
/* END_HEADER */
#define g_uiPort 12121
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_ECDSA_FUNC_TC001
* @title ECDHE_ECDSA requires an ECDSA certificate
* @precon nan
* @brief Set the cipher suite to ECDHE_ECDSA and the certificate to ECDSA. The expected connection setup success is
* expected.
* @expect 1. A success message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_ECDSA_FUNC_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
// Set the cipher suite to ECDHE_ECDSA and the certificate to ECDSA.
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_ECDSA_FUNC_TC002
* @title ECDHE_ECDSA requires an ECDSA certificate
* @precon nan
* @brief Set the algorithm set to ECDHE_ECDSA and the certificate to the RSA certificate, Expected chain building
* failure
* @expect 1. A failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_ECDHE_ECDSA_FUNC_TC002(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
HLT_SetClientVerifySupport(serverCtxConfig, true);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
// Set the algorithm set to ECDHE_ECDSA and the certificate to the RSA certificate,
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_CURVE_AND_AUTH_FUNC_TC001
* @title When the server selects the ECC cipher suite, the extension of the client must be considered for key
exchange and certificate.
* @precon nan
* @brief 1. Set the curve secp256r1 and secp384r1 on the client and server, set the certificate curve secp384r1 on
* the server, and set the ECC cipher suite. It is expected that the certificate is loaded successfully.
* Set serverkeyexchange to secp256r1, and the connection is set up successfully.
* @expect 1. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_CURVE_AND_AUTH_FUNC_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
/* 1. Set the curve secp256r1 and secp384r1 on the client and server, set the certificate curve secp384r1 on
* the server, and set the ECC cipher suite. It is expected that the certificate is loaded successfully.
* Set serverkeyexchange to secp256r1 */
int ret = HLT_TlsConnect(clientRes->ssl);
ASSERT_TRUE(ret == 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_CURVE_AND_AUTH_FUNC_TC002
* @spec -
* @title When the server selects the ECC cipher suite, both the key exchange and certificate must comply with the
* extension of the client.
* @precon nan
* @brief 1. Set the curve secp256r1 on the client and server, set the certificate curve secp384r1, and set the ECC
* cipher suite. The certificate is loaded successfully.
* Set the serverkeyexchange parameter is set to secp256r1, the connection fails to be established.
* @expect 1. Connect establishment fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_CURVE_AND_AUTH_FUNC_TC002(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
TestSetCertPath(serverCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384");
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
/* Set the curve secp256r1 on the client and server, set the certificate curve secp384r1, and set the ECC
* cipher suite. */
int ret = HLT_TlsConnect(clientRes->ssl);
ASSERT_TRUE(ret != 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
}
/* END_CASE */
static void Test_SetCipherSuites_With_Link(CipherInfo serverCipher, CipherInfo clientCipher, bool expectSuccess)
{
int ret;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *client_remote = NULL;
HLT_Process *server_local = NULL;
server_local = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(server_local != NULL);
client_remote = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, false);
ASSERT_TRUE(client_remote != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCipherSuites(serverCtxConfig, serverCipher.cipher);
HLT_SetGroups(serverCtxConfig, serverCipher.groups);
HLT_SetSignature(serverCtxConfig, serverCipher.signAlg);
TestSetCertPath(serverCtxConfig, serverCipher.signAlg);
serverRes = HLT_ProcessTlsAccept(server_local, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCipherSuites(clientCtxConfig, clientCipher.cipher);
HLT_SetGroups(clientCtxConfig, clientCipher.groups);
HLT_SetSignature(clientCtxConfig, clientCipher.signAlg);
TestSetCertPath(clientCtxConfig, clientCipher.signAlg);
clientRes = HLT_ProcessTlsConnect(client_remote, TLS1_2, clientCtxConfig, NULL);
if (expectSuccess) {
ASSERT_TRUE(clientRes != NULL);
} else {
ASSERT_TRUE(clientRes == NULL);
goto EXIT;
}
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(server_local, serverRes, (uint8_t *)"Hello", strlen("Hello")) == 0);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ret = HLT_ProcessTlsRead(client_remote, clientRes, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(ret == 0);
ASSERT_TRUE(readLen == strlen("Hello"));
ASSERT_TRUE(memcmp("Hello", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC001
* @title One configuration, the configuration algorithm suite at both ends is the same,
* and the negotiation behavior is verified
* @precon nan
* @brief 1. Set the cipher suite to HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 at both ends.Expected result 1 is obtained.
* @expect 1. The negotiation is expected to succeed.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC001()
{
/* 1. Set the cipher suite to HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 at both ends. */
CipherInfo clientCipher = {.cipher = "HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
.groups = "HITLS_EC_GROUP_SECP384R1",
.signAlg = "CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
.cert = "rsa_sha256"};
CipherInfo serverCipher = {.cipher = "HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
.groups = "HITLS_EC_GROUP_SECP384R1",
.signAlg = "CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
.cert = "rsa_sha256"};
Test_SetCipherSuites_With_Link(serverCipher, clientCipher, true);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC002
* @title One configuration, the cipher suites configured at both ends are the same, and the negotiation behavior is
* verified
* @precon nan
* @brief 1. Set the cipher suite to HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 at both ends.
* Expected result 1 is obtained.
* @expect 1. The negotiation is expected to succeed.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC002()
{
CipherInfo clientCipher = {.cipher = "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
.cert = "ecdsa_sha256"};
CipherInfo serverCipher = {.cipher = "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
.cert = "ecdsa_sha256"};
Test_SetCipherSuites_With_Link(serverCipher, clientCipher, true);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC003
* @title One configuration, the cipher suites configured at both ends are the same, and the negotiation behavior is
* verified.
* @precon nan
* @brief 1. Set the cipher suite to HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA at both ends.Expected result 1 is obtained.
* @expect 1. The negotiation is expected to succeed.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC003()
{
// 1. Set the cipher suite to HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA at both ends.
CipherInfo clientCipher = {.cipher = "HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
.cert = "ecdsa_sha256"};
CipherInfo serverCipher = {.cipher = "HITLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
.cert = "ecdsa_sha256"};
Test_SetCipherSuites_With_Link(serverCipher, clientCipher, true);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC004
* @title One configuration,the cipher suites configured at both ends are the same, and the negotiation behavior is
* verified.
* @precon nan
* @brief 1. Set the cipher suite to HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA at both ends.Expected result 1 is obtained.
* @expect 1. The negotiation is expected to succeed.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCY_SET_CIPHERSUITES_FUNC_TC004()
{
// 1. Set the cipher suite to HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA at both ends.
CipherInfo clientCipher = {.cipher = "HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
.cert = "rsa_sha256"};
CipherInfo serverCipher = {.cipher = "HITLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
.groups = "HITLS_EC_GROUP_SECP256R1",
.signAlg = "CERT_SIG_SCHEME_RSA_PKCS1_SHA256",
.cert = "rsa_sha256"};
Test_SetCipherSuites_With_Link(serverCipher, clientCipher, true);
}
/* END_CASE */
static void MalformedClientHelloMsgCallback(void *msg, void *userData)
{
// 2. Obtain the message, modify the field content, and send the message.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->pointFormats.exDataLen.data = 1;
clientHello->pointFormats.exData.state = MISSING_FIELD;
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ERR_POINT_FUNC_TC001
* @title Set the ECC cipher suite.Before the server receives the client hello message, the extended value of the
* point format is changed to 1. As a result, the negotiation on the server is expected to fail.
* @precon nan
* @brief 1. The tested end functions as the client, and the tested end functions as the server.Expected result 1 is
* obtained.
* 2. Obtain the message, modify the field content, and send the message.(Expected result 2)
* 3. Check the status of the tested end.Expected result 3 is obtained.
* 4. Check the status of the test end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
* 2. A success message is returned .
* 3. The tested end returns an alert message, and the status is alerted.
* 4. The status of the test end is alerted, and the handshake status is ready to receive the serverHello
* message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ERR_POINT_FUNC_TC001(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the client, and the tested end functions as the server.
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedClientHelloMsgCallback;
TestPara testPara = {0};
testPara.port = g_uiPort;
// 4. Check the status of the test end.
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedServerHelloMsgCallback(void *msg, void *userData)
{
// 2. Obtain the message, modify the field content, and send the message.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverHello = &frameMsg->body.hsMsg.body.serverHello;
serverHello->pointFormats.exState = INITIAL_FIELD;
serverHello->pointFormats.exType.state = INITIAL_FIELD;
serverHello->pointFormats.exType.data = HS_EX_TYPE_POINT_FORMATS;
uint8_t data[] = {0};
FRAME_ModifyMsgArray8(data, sizeof(data), &serverHello->pointFormats.exData, &serverHello->pointFormats.exDataLen);
serverHello->pointFormats.exLen.state = INITIAL_FIELD;
serverHello->pointFormats.exLen.data = serverHello->pointFormats.exDataLen.data + sizeof(uint8_t);
serverHello->pointFormats.exDataLen.data = 1;
serverHello->pointFormats.exData.state = MISSING_FIELD;
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ERR_POINT_FUNC_TC002
* @title uses the ECC cipher suite.During connection setup, the serverhello message carries the point format and the
* point format is set to 1. The connection setup fails.
* @precon nan
* @brief 1. The tested end functions as the server and the tested end functions as the client.Expected result 1 is
* obtained.
* 2. Obtain the message, modify the field content, and send the message.(Expected result 2)
* 3. Check the status of the tested end.Expected result 3 is obtained.
* 4. Check the status of the test end.Expected result 4 is obtained.
* @expect 1. A success message is returned.
* 2. A success message is returned
* 3. The tested end returns an alert message, indicating that the status is alerted.
* 4. The status of the tested end is alerted.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ERR_POINT_FUNC_TC002(void)
{
HLT_FrameHandle handle = {0};
handle.userData = (void *)&handle;
handle.pointType = POINT_SEND;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The tested end functions as the server and the tested end functions as the client.
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = MalformedServerHelloMsgCallback;
TestPara testPara = {0};
testPara.port = g_uiPort;
testPara.isSupportExtendMasterSecret = true;
// 4. The status of the tested end is alerted.
testPara.expectHsState = TRY_RECV_CLIENT_KEY_EXCHANGE;
// 3. Check the status of the tested end.
testPara.expectDescription = ALERT_DECODE_ERROR;
ServerSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
static void MalformedNoCurveExternsionCallback(void *msg, void *userData)
{
// 2. Modify the client to send a client hello message and remove the elliptic curve extension.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clientHello = &frameMsg->body.hsMsg.body.clientHello;
clientHello->supportedGroups.exState = MISSING_FIELD;
clientHello->supportedGroups.exData.state = MISSING_FIELD;
clientHello->supportedGroups.exDataLen.state = MISSING_FIELD;
clientHello->supportedGroups.exLen.state = MISSING_FIELD;
clientHello->supportedGroups.exType.state = MISSING_FIELD;
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ECDHE_LOSE_CURVE_FUNC_TC001
* @title: Configure the ECC cipher suite and remove the elliptic curve extension before the server receives the client
* hello message.As a result,the connection establishment fails.
* @precon nan
* @brief 1. The server stops receiving client Hello messages.Expected result 1 is obtained
* 2. Modify the client to send a client hello message and remove the elliptic curve extension.Expected result
* 2 is obtained.
* 3. The server continues to establish a connection.(Expected result 3)
* @expect 1. Success
* 2. Success
* 3. A decryption failure message is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS12_RFC8422_CONSISTENCYECDHE_ECDHE_LOSE_CURVE_FUNC_TC001(void)
{
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
// 1. The server stops receiving client Hello messages.
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = MalformedNoCurveExternsionCallback;
TestPara testPara = {0};
testPara.port = g_uiPort;
testPara.isSupportExtendMasterSecret = true;
testPara.expectHsState = TRY_RECV_SERVER_HELLO;
// 3. The server continues to establish a connection.
testPara.expectDescription = ALERT_HANDSHAKE_FAILURE;
ClientSendMalformedRecordHeaderMsg(&handle, &testPara);
return;
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_sdv_hlt_tls12_consistency_rfc8422.c | C | unknown | 23,303 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "app.h"
#include "alert.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "logger.h"
#include "alert.h"
#include "stub_crypt.h"
#include "rec_wrapper.h"
#define PARSEMSGHEADER_LEN 13 /* Message header length */
#define ILLEGAL_VALUE 0xFF /* Invalid value */
#define HASH_EXDATA_LEN_ERROR 23 /* Length of the content of the client_HELLOW signature hash field */
#define SIGNATURE_ALGORITHMS 0x04, 0x03 /* Fields added to the SERVER_HELLOW message */
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
#define READ_BUF_LEN_18K (18 * 1024)
#define TEMP_DATA_LEN 1024 /* Length of a single message */
#define ALERT_BODY_LEN 2u /* Alert data length */
#define GetEpochSeq(epoch, seq) (((uint64_t)(epoch) << 48) | (seq))
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isServerExtendMasterSecret;
bool isSupportRenegotiation; /* Renegotiation support flag */
bool needStopBeforeRecvCCS; /* CCS test, so that the TRY_RECV_FINISH stops before the CCS message is received */
} HandshakeTestInfo;
typedef struct {
char *cipher;
char *groups;
char *signAlg;
char *cert;
} CipherInfo;
typedef struct {
int port;
HITLS_HandshakeState expectHsState; // Expected Local Handshake Status.
bool alertRecvFlag; // Indicates whether the alert is received. The value fasle indicates the sent alert, and the value true indicates the received alert.
ALERT_Description expectDescription; // Expected alert description of the test end.
bool isSupportClientVerify; // Indicates whether to use the dual-end verification.
bool isSupportExtendMasterSecret; // Indicates whether to use an extended master key.
bool isSupportDhCipherSuites; // Indicates whether to use the DHE cipher suite
bool isExpectRet; // Indicates whether the return value is expected.
int expectRet; // Expected return value. The isExpectRet function needs to be enabled.
const char *serverGroup; // Configure the group supported by the server. If this parameter is not specified, the default value is used.
const char *serverSignature; // Configure the signature algorithm supported by the server. If this parameter is left empty, the default value is used.
const char *clientGroup; // Configure the group supported by the client. If this parameter is left empty, the default value is used.
const char *clientSignature; // Configure the signature algorithm supported by the client. If this parameter is left empty, the default value is used.
} TestPara;
int32_t StatusPark(HandshakeTestInfo *testInfo)
{
/** Construct link */
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/* Perform the CCS test so that the TRY_RECV_FINISH is stopped before the CCS message is received.
* The default value is False, which does not affect the original test.
*/
testInfo->client->needStopBeforeRecvCCS = testInfo->isClient ? testInfo->needStopBeforeRecvCCS : false;
testInfo->server->needStopBeforeRecvCCS = testInfo->isClient ? false : testInfo->needStopBeforeRecvCCS;
/** Establish a link and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/** Construct configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark(testInfo);
}
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/** Construct configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo);
}
int32_t StatusPark1(HandshakeTestInfo *testInfo)
{
/* Construct a link. */
if(testInfo->isServerExtendMasterSecret == true){
testInfo->config->isSupportExtendMasterSecret = true;
}else {
testInfo->config->isSupportExtendMasterSecret = false;
}
testInfo->config->isSupportRenegotiation = false;
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if(testInfo->isServerExtendMasterSecret == true){
testInfo->config->isSupportExtendMasterSecret = false;
}else {
testInfo->config->isSupportExtendMasterSecret = true;
}
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/* Set up a link and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark1(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/* Construct the configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark1(testInfo);
}
int32_t StatusPark2(HandshakeTestInfo *testInfo)
{
/* Construct a link. */
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/* Establish a link and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server,
testInfo->isClient, testInfo->state) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t SendHelloReq(HITLS_Ctx *ctx)
{
/** Initialize the message buffer. */
uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
size_t len = HS_MSG_HEADER_SIZE;
/** Write records. */
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
int32_t ConstructAnEmptyCertMsg(FRAME_LinkObj *link)
{
FRAME_Msg frameMsg = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(link->io);
/** Obtain the message buffer. */
uint8_t *buffer = ioUserData->recMsg.msg;
uint32_t len = ioUserData->recMsg.len;
if (len == 0) {
return HITLS_MEMCPY_FAIL;
}
/** Parse the message. */
uint32_t parseLen = 0;
if (ParserTotalRecord(link, &frameMsg, buffer, len, &parseLen) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
/** Construct a message. */
CERT_Item *tmpCert = frameMsg.body.handshakeMsg.body.certificate.cert;
frameMsg.body.handshakeMsg.body.certificate.cert = NULL;
frameMsg.bodyLen = 15;
/** reassemble */
if (PackFrameMsg(&frameMsg) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
/** Message injection */
ioUserData->recMsg.len = 0;
if (FRAME_TransportRecMsg(link->io, frameMsg.buffer, frameMsg.len) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_SUCCESS;
}
int32_t RandBytes(uint8_t *randNum, uint32_t randLen)
{
srand(time(0));
const int maxNum = 256u;
for (uint32_t i = randLen - 1; i < randLen; i++) {
randNum[i] = (uint8_t)(rand() % maxNum);
}
return HITLS_SUCCESS;
}
#define TEST_CLIENT_SEND_FAIL 1
uint32_t g_uiPort = 8889;
void TestSetCertPath(HLT_Ctx_Config *ctxConfig, char *SignatureType)
{
if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA1", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA1")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA256")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA384")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA384_EE_PATH, RSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA512")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA512_EE_PATH, RSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
strlen("CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
strlen("CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
strlen("CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_ECDSA_SHA1", strlen("CERT_SIG_SCHEME_ECDSA_SHA1")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA1_CA_PATH,
ECDSA_SHA1_CHAIN_PATH,
ECDSA_SHA1_EE_PATH,
ECDSA_SHA1_PRIV_PATH,
"NULL",
"NULL");
}
}
void ClientSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
// Create a process.
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, false);
ASSERT_TRUE(remoteProcess != NULL);
// The remote server listens on the TLS link.
HLT_Ctx_Config *serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetGroups(serverConfig, "HITLS_EC_GROUP_SECP256R1") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256") == 0);
TestSetCertPath(serverConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the TLS connection on the local client.
HLT_Ctx_Config *clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetGroups(clientConfig, "HITLS_EC_GROUP_SECP256R1") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256") == 0);
TestSetCertPath(clientConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_Tls_Res *clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a link and wait for the local end to complete the link.
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
// Wait the remote end.
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
// Confirm the final status.
ASSERT_EQ(HLT_RpcTlsGetStatus(remoteProcess, serverRes->sslId), CM_STATE_ALERTED);
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, serverRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ((ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, serverRes->sslId),
testPara->expectDescription);
ASSERT_EQ(((HITLS_Ctx *)(clientRes->ssl))->hsCtx->state, testPara->expectHsState);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ServerSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
// Create a process.
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, true);
ASSERT_TRUE(remoteProcess != NULL);
// The local server listens on the TLS link.
HLT_Ctx_Config *serverConfig1 = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig1 != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig1, testPara->isSupportClientVerify) == 0);
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig1, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetGroups(serverConfig1, "HITLS_EC_GROUP_SECP256R1") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig1, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256") == 0);
TestSetCertPath(serverConfig1, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverConfig1, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a TLS link on the remote client.
HLT_Ctx_Config *clientConfig1 = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig1 != NULL);
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig1, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetGroups(clientConfig1, "HITLS_EC_GROUP_SECP256R1") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig1, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256") == 0);
TestSetCertPath(clientConfig1, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
HLT_Tls_Res *clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientConfig1, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId) != 0);
// Wait for the local end.
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
// Confirm the final status.
ASSERT_EQ(((HITLS_Ctx *)(serverRes->ssl))->hsCtx->state, testPara->expectHsState);
ASSERT_TRUE(HLT_RpcTlsGetStatus(remoteProcess, clientRes->sslId) == CM_STATE_ALERTED);
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ((ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId),
testPara->expectDescription);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
HITLS_KeyExchAlgo keyExType)
{
frametype->versionType = versionType;
frametype->recordType = recordType;
frametype->handshakeType = handshakeType;
frametype->keyExType = keyExType;
frametype->transportType = BSL_UIO_TCP;
} | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_tls12_consistency_rfc5246.base.c | C | unknown | 20,219 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <semaphore.h>
#include "securec.h"
#include "hitls_error.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "tls.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "alert.h"
#include "record.h"
#include "bsl_uio.h"
#include "hitls.h"
#include "pack_frame_msg.h"
#include "parser_frame_msg.h"
#define MAX_SESSION_ID_SIZE TLS_HS_MAX_SESSION_ID_SIZE
#define MIN_SESSION_ID_SIZE TLS_HS_MIN_SESSION_ID_SIZE
#define COOKIE_SIZE 32u
#define DN_SIZE 32u
#define EXTRA_DATA_SIZE 12u
#define PORT 8005 // The SDV test is a parallel test. The port number used by each test suite must be unique.
// for sni
int32_t ServernameCbErrOK(HITLS_Ctx *ctx, int *alert, void *arg)
{
(void)ctx;
(void)alert;
(void)arg;
return HITLS_ACCEPT_SNI_ERR_OK;
}
// end for sni
// for alpn
#define MAX_PROTOCOL_LEN 65536
/* Protocol matching function at the application layer */
static int32_t ExampleAlpnSelectProtocol(uint8_t **out, uint8_t *outLen, uint8_t *clientAlpnList,
uint8_t clientAlpnListLen, uint8_t *servAlpnList, uint8_t servAlpnListLen)
{
int32_t ret = HITLS_ALPN_ERR_ALERT_FATAL;
if (out == NULL || outLen == NULL || clientAlpnList == NULL || servAlpnList == NULL) {
return HITLS_NULL_INPUT;
}
uint8_t i = 0;
uint8_t j = 0;
for (i = 0; i < servAlpnListLen;) {
for (j = 0; j < clientAlpnListLen;) {
if (servAlpnList[i] == clientAlpnList[j] &&
(memcmp(&servAlpnList[i + 1], &clientAlpnList[j + 1], servAlpnList[i]) == 0)) {
*out = &servAlpnList[i + 1];
*outLen = servAlpnList[i];
ret = HITLS_ALPN_ERR_OK;
goto EXIT;
}
j = j + clientAlpnList[j];
++j;
}
i = i + servAlpnList[i];
++i;
}
EXIT:
return ret;
}
/* UserData structure transferred by the server to the alpnCb callback. */
typedef struct TlsAlpnExtCtx_ {
uint8_t *serverAlpnList;
uint32_t serverAlpnListLen;
} TlsAlpnExtCtx;
/* Select callback for the alpn on the server. */
int32_t ExampleAlpnCbForLlt(HITLS_Ctx *ctx, uint8_t **selectedProto, uint8_t *selectedProtoSize,
uint8_t *clientAlpnList, uint32_t clientAlpnListSize, void *userData)
{
(void)ctx;
int32_t ret = 0u;
TlsAlpnExtCtx *alpnData = (TlsAlpnExtCtx *)userData;
uint8_t *selected = NULL;
uint8_t selectedLen = 0u;
ret = ExampleAlpnSelectProtocol(&selected, &selectedLen, clientAlpnList, clientAlpnListSize,
alpnData->serverAlpnList, alpnData->serverAlpnListLen);
if (ret != HITLS_ALPN_ERR_OK) {
return ret;
}
*selectedProto = selected;
*selectedProtoSize = selectedLen;
return HITLS_SUCCESS;
}
/* Parse the comma-separated application layer protocols transferred by the executable function. */
int32_t ExampleAlpnParseProtocolList(uint8_t *out, uint32_t *outLen, uint8_t *in, uint32_t inLen)
{
if (out == NULL || outLen == NULL || in == NULL) {
return HITLS_NULL_INPUT;
}
if (inLen == 0 || inLen > MAX_PROTOCOL_LEN) {
return HITLS_CONFIG_INVALID_LENGTH;
}
uint32_t i = 0u;
uint32_t commaNum = 0u;
uint32_t startPos = 0u;
for (i = 0u; i <= inLen; ++i) {
if (i == inLen || in[i] == ',') {
if (i == startPos) {
++startPos;
++commaNum;
continue;
}
out[startPos - commaNum] = (uint8_t)(i - startPos);
startPos = i + 1;
} else {
out[i + 1 - commaNum] = in[i];
}
}
*outLen = inLen + 1 - commaNum;
return HITLS_SUCCESS;
}
int32_t ExampleAlpnParseProtocolList2(uint8_t *out, uint32_t *outLen, uint8_t *in, uint32_t inLen)
{
if (out == NULL || outLen == NULL || in == NULL) {
return HITLS_NULL_INPUT;
}
if (inLen == 0 || inLen > MAX_PROTOCOL_LEN) {
return HITLS_CONFIG_INVALID_LENGTH;
}
uint32_t i = 0u;
uint32_t commaNum = 0u;
uint32_t startPos = 0u;
for (i = 0u; i <= inLen; ++i) {
if (i == inLen || in[i] == ',') {
if (i == startPos) {
++startPos;
++commaNum;
continue;
}
out[startPos - commaNum] = (uint8_t)(i - startPos);
startPos = i + 1;
} else {
out[i + 1 - commaNum] = in[i];
}
}
*outLen = inLen + 1 - commaNum;
return HITLS_SUCCESS;
}
// end for alpn
typedef struct {
int port;
HITLS_HandshakeState expectHsState;
bool alertRecvFlag;
ALERT_Description expectDescription;
bool isSupportClientVerify;
bool isSupportExtendMasterSecret;
bool isSupportSni;
bool isSupportALPN;
bool isSupportDhCipherSuites;
bool isSupportSessionTicket;
bool isExpectRet;
int expectRet;
const char *serverGroup;
const char *serverSignature;
const char *clientGroup;
const char *clientSignature;
} TestPara;
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isSupportSessionTicket;
bool needStopBeforeRecvCCS;
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/* CCS test, so that the TRY_RECV_FINISH is stopped before the CCS packet is received.
* The default value is False, which does not affect the original test.
*/
testInfo->client->needStopBeforeRecvCCS = testInfo->isClient ? testInfo->needStopBeforeRecvCCS : false;
testInfo->server->needStopBeforeRecvCCS = testInfo->isClient ? false : testInfo->needStopBeforeRecvCCS;
/** Set up a connection and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/* Construct the configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo->config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(testInfo->config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportSessionTicket = testInfo->isSupportSessionTicket;
return StatusPark(testInfo);
}
/* The local server initiates a connection creation request: Ignore whether the link creation is successful.*/
void ServerAccept(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, true);
ASSERT_TRUE(remoteProcess != NULL);
// The local server listens on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a TLS connection on the remote client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_RpcTlsConnect(remoteProcess, clientRes->sslId);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ServerSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, true);
ASSERT_TRUE(remoteProcess != NULL);
// The local server listens on the TLS link.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
ASSERT_TRUE(HLT_SetSessionTicketSupport(serverConfig, testPara->isSupportSessionTicket) == 0);
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(
serverConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->serverGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(serverConfig, testPara->serverGroup) == 0);
}
if (testPara->serverSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(serverConfig, testPara->serverSignature) == 0);
}
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a TLS connection on the remote client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
ASSERT_TRUE(HLT_SetSessionTicketSupport(clientConfig, testPara->isSupportSessionTicket) == 0);
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(
clientConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->clientGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(clientConfig, testPara->clientGroup) == 0);
}
if (testPara->clientSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(clientConfig, testPara->clientSignature) == 0);
}
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
if (testPara->isExpectRet) {
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), testPara->expectRet);
} else {
ASSERT_TRUE(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId) != 0);
}
// Wait for the local.
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
// Confirm the final status.
ASSERT_TRUE(((HITLS_Ctx *)(serverRes->ssl))->state == CM_STATE_ALERTED);
ASSERT_TRUE(((HITLS_Ctx *)(serverRes->ssl))->hsCtx != NULL);
ASSERT_EQ(((HITLS_Ctx *)(serverRes->ssl))->hsCtx->state, testPara->expectHsState);
ASSERT_TRUE(HLT_RpcTlsGetStatus(remoteProcess, clientRes->sslId) == CM_STATE_ALERTED);
if (testPara->alertRecvFlag) {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_RECV);
} else {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
}
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(
(ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId), testPara->expectDescription);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ClientSendMalformedRecordHeaderMsg(HLT_FrameHandle *handle, TestPara *testPara)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, testPara->port, false);
ASSERT_TRUE(remoteProcess != NULL);
// The remote server listens on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HLT_SetSessionTicketSupport(serverConfig, testPara->isSupportSessionTicket) == 0);
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerNameCb(serverConfig, "ExampleSNIArg") == 0);
ASSERT_TRUE(HLT_SetServerNameArg(serverConfig, "ExampleSNIArg") == 0);
}
if (testPara->isSupportALPN) {
ASSERT_TRUE(HLT_SetAlpnProtosSelectCb(serverConfig, "ExampleAlpnCb", "ExampleAlpnData") == 0);
}
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(serverConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(serverConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(
serverConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
ASSERT_TRUE(HLT_SetClientVerifySupport(serverConfig, testPara->isSupportClientVerify) == 0);
if (testPara->serverGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(serverConfig, testPara->serverGroup) == 0);
}
if (testPara->serverSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(serverConfig, testPara->serverSignature) == 0);
}
serverConfig->isSupportExtendMasterSecret = false;
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure the TLS connection on the local client.
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(HLT_SetSessionTicketSupport(clientConfig, testPara->isSupportSessionTicket) == 0);
if (testPara->isSupportSni) {
ASSERT_TRUE(HLT_SetServerNameCb(clientConfig, "testServer") == 0);
}
if (testPara->isSupportALPN) {
static const char *alpn = "http,ftp";
uint8_t ParsedList[100] = {0};
uint32_t ParsedListLen;
ExampleAlpnParseProtocolList2(ParsedList, &ParsedListLen, (uint8_t *)alpn, (uint32_t)strlen(alpn));
ASSERT_TRUE(HLT_SetAlpnProtos(clientConfig, (const char *)ParsedList) == 0);
}
if (testPara->isSupportDhCipherSuites) {
ASSERT_TRUE(HLT_SetCipherSuites(clientConfig, "HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256") == 0);
ASSERT_TRUE(HLT_SetSignature(clientConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256") == 0);
HLT_SetCertPath(
clientConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
}
if (testPara->clientGroup != NULL) {
ASSERT_TRUE(HLT_SetGroups(clientConfig, testPara->clientGroup) == 0);
}
if (testPara->clientSignature != NULL) {
ASSERT_TRUE(HLT_SetSignature(clientConfig, testPara->clientSignature) == 0);
}
ASSERT_TRUE(HLT_SetExtenedMasterSecretSupport(clientConfig, testPara->isSupportExtendMasterSecret) == 0);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Set up a connection and wait until the local is complete.
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
// Wait the remote.
int ret = HLT_GetTlsAcceptResult(serverRes);
ASSERT_TRUE(ret != 0);
if (testPara->isExpectRet) {
ASSERT_EQ(ret, testPara->expectRet);
}
// Final status confirmation
ASSERT_EQ(HLT_RpcTlsGetStatus(remoteProcess, serverRes->sslId), CM_STATE_ALERTED);
if (testPara->alertRecvFlag) {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, serverRes->sslId), ALERT_FLAG_RECV);
} else {
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, serverRes->sslId), ALERT_FLAG_SEND);
}
ASSERT_EQ((ALERT_Level)HLT_RpcTlsGetAlertLevel(remoteProcess, serverRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(
(ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, serverRes->sslId), testPara->expectDescription);
ASSERT_TRUE(((HITLS_Ctx *)(clientRes->ssl))->state == CM_STATE_ALERTED);
ASSERT_TRUE(((HITLS_Ctx *)(clientRes->ssl))->hsCtx != NULL);
ASSERT_EQ(((HITLS_Ctx *)(clientRes->ssl))->hsCtx->state, testPara->expectHsState);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
// for UT_TLS1_2_RFC5246_RECV_ZEROLENGTH_MSG_TC009 - UT_TLS1_2_RFC5246_RECV_ZEROLENGTH_MSG_TC010
int32_t g_writeRet;
uint32_t g_writeLen;
bool g_isUseWriteLen;
uint8_t g_writeBuf[REC_DTLS_RECORD_HEADER_LEN + REC_MAX_CIPHER_TEXT_LEN];
int32_t STUB_MethodWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
{
(void)uio;
if (memcpy_s(g_writeBuf, sizeof(g_writeBuf), buf, len) != EOK) {
return BSL_MEMCPY_FAIL;
}
*writeLen = len;
if (g_isUseWriteLen) {
*writeLen = g_writeLen;
}
return g_writeRet;
}
int32_t g_readRet;
uint32_t g_readLen;
uint8_t g_readBuf[REC_DTLS_RECORD_HEADER_LEN + REC_MAX_CIPHER_TEXT_LEN];
int32_t STUB_MethodRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen)
{
(void)uio;
if (g_readLen != 0 && memcpy_s(buf, len, g_readBuf, g_readLen) != EOK) {
return BSL_MEMCPY_FAIL;
}
*readLen = g_readLen;
return g_readRet;
}
int32_t g_ctrlRet;
BSL_UIO_CtrlParameter g_ctrlCmd;
int32_t STUB_MethodCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *param)
{
(void)larg;
(void)uio;
(void)param;
if ((int32_t)g_ctrlCmd == cmd) {
return g_ctrlRet;
}
return BSL_SUCCESS;
}
HITLS_Config *g_tlsConfig = NULL;
HITLS_Ctx *g_tlsCtx = NULL;
BSL_UIO *g_uio = NULL;
int32_t TlsCtxNew(BSL_UIO_TransportType type)
{
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
BSL_UIO *uio = NULL;
const BSL_UIO_Method *ori = NULL;
switch (type) {
case BSL_UIO_TCP:
#ifdef HITLS_BSL_UIO_TCP
ori = BSL_UIO_TcpMethod();
#endif
break;
default:
#ifdef HITLS_BSL_UIO_SCTP
ori = BSL_UIO_SctpMethod();
#endif
break;
}
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
BSL_UIO_Method method = {0};
memcpy(&method, ori, sizeof(method));
method.uioWrite = STUB_MethodWrite;
method.uioRead = STUB_MethodRead;
method.uioCtrl = STUB_MethodCtrl;
uio = BSL_UIO_New(&method);
ASSERT_TRUE(uio != NULL);
ASSERT_TRUE(HITLS_SetUio(ctx, uio) == HITLS_SUCCESS);
/* Default value of stub function */
g_writeRet = HITLS_SUCCESS;
g_writeLen = 0;
g_isUseWriteLen = false;
g_readLen = 0;
g_readRet = HITLS_SUCCESS;
g_tlsConfig = config;
g_tlsCtx = ctx;
g_uio = uio;
return HITLS_SUCCESS;
EXIT:
BSL_UIO_Free(uio);
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(config);
return HITLS_INTERNAL_EXCEPTION;
}
void TlsCtxFree(void)
{
BSL_UIO_Free(g_uio);
HITLS_Free(g_tlsCtx);
HITLS_CFG_FreeConfig(g_tlsConfig);
g_uio = NULL;
g_tlsCtx = NULL;
g_tlsConfig = NULL;
}
// for UT_TLS1_2_RFC5246_RECV_ZEROLENGTH_MSG_TC009 - UT_TLS1_2_RFC5246_RECV_ZEROLENGTH_MSG_TC010
// for UT_TLS1_2_RFC5246_MISS_CLIENT_KEYEXCHANGE_TC001
#define PARSEMSGHEADER_LEN 13
#define ILLEGAL_VALUE 0xFF
#define HASH_EXDATA_LEN_ERROR 23 /* Length of the CLIENT_HELLOW signature hash field. */
#define SIGNATURE_ALGORITHMS 0x04, 0x03 /* Field added to the end of the SERVER_HELLOW message */
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
#define TEMP_DATA_LEN 1024 /* Length of a single packet. */
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/** Construct the configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo);
}
int32_t SendHelloReq(HITLS_Ctx *ctx)
{
uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
size_t len = HS_MSG_HEADER_SIZE;
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
int32_t ConstructAnEmptyCertMsg(FRAME_LinkObj *link)
{
FRAME_Msg frameMsg = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(link->io);
uint8_t *buffer = ioUserData->recMsg.msg;
uint32_t len = ioUserData->recMsg.len;
if (len == 0) {
return HITLS_MEMCPY_FAIL;
}
/** Parse the message. */
uint32_t parseLen = 0;
if (ParserTotalRecord(link, &frameMsg, buffer, len, &parseLen) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
/** Construct a message. */
CERT_Item *tmpCert = frameMsg.body.handshakeMsg.body.certificate.cert;
frameMsg.body.handshakeMsg.body.certificate.cert = NULL;
frameMsg.bodyLen = 15;
if (PackFrameMsg(&frameMsg) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
ioUserData->recMsg.len = 0;
if (FRAME_TransportRecMsg(link->io, frameMsg.buffer, frameMsg.len) != HITLS_SUCCESS) {
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_INTERNAL_EXCEPTION;
}
frameMsg.body.handshakeMsg.body.certificate.cert = tmpCert;
CleanRecordBody(&frameMsg);
return HITLS_SUCCESS;
}
int32_t RandBytes(uint8_t *randNum, uint32_t randLen)
{
srand(time(0));
const int maxNum = 256u;
for (uint32_t i = 0; i < randLen; i++) {
randNum[i] = (uint8_t)(rand() % maxNum);
}
return HITLS_SUCCESS;
}
// for UT_TLS1_2_RFC5246_MISS_CLIENT_KEYEXCHANGE_TC001
// for UT_TLS1_2_RFC5246_CERTFICATE_VERITY_FAIL_TC006 - UT_TLS1_2_RFC5246_CERTFICATE_VERITY_FAIL_TC007
typedef struct {
int connectExpect; // Expected connect result Return value on end C.
int acceptExpect; // Expected accept result returned value on the s end.
ALERT_Level expectLevel; // Expected alert level.
ALERT_Description expectDescription; // Expected alert description of the tested end.
} TestExpect;
// Replace the sent message with ClientKeyExchange.
void TEST_SendUnexpectClientKeyExchangeMsg(void *msg, void *data)
{
FRAME_Type *frameType = (FRAME_Type *)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_Msg newFrameMsg = {0};
HS_MsgType hsTypeTmp = frameType->handshakeType;
REC_Type recTypeTmp = frameType->recordType;
frameType->handshakeType = CLIENT_KEY_EXCHANGE;
FRAME_Init(); // Callback for changing the certificate algorithm, which is used to generate the negotiation
// handshake message.
FRAME_GetDefaultMsg(frameType, &newFrameMsg);
HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT); // recovery callback
FRAME_ModifyMsgInteger(frameMsg->epoch.data, &newFrameMsg.epoch);
FRAME_ModifyMsgInteger(frameMsg->sequence.data, &newFrameMsg.sequence);
FRAME_ModifyMsgInteger(frameMsg->body.hsMsg.sequence.data, &newFrameMsg.body.hsMsg.sequence);
// Release the original msg.
frameType->handshakeType = hsTypeTmp;
frameType->recordType = recTypeTmp;
FRAME_CleanMsg(frameType, frameMsg);
// Change message.
frameType->recordType = REC_TYPE_HANDSHAKE;
frameType->handshakeType = CLIENT_KEY_EXCHANGE;
frameType->keyExType = HITLS_KEY_EXCH_ECDHE;
if (memcpy_s(msg, sizeof(FRAME_Msg), &newFrameMsg, sizeof(newFrameMsg)) != EOK) {
Print("TEST_SendUnexpectClientKeyExchangeMsg memcpy_s Error!");
}
}
// Replace the message to be sent with the certificate.
void TEST_SendUnexpectCertificateMsg(void *msg, void *data)
{
FRAME_Type *frameType = (FRAME_Type *)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_Msg newFrameMsg = {0};
HS_MsgType hsTypeTmp = frameType->handshakeType;
frameType->handshakeType = CERTIFICATE;
/* Callback for changing the certificate algorithm, which is used to generate the negotiation handshake message. */
FRAME_Init();
FRAME_GetDefaultMsg(frameType, &newFrameMsg);
HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT); // recovery callback
// Release the original msg.
frameType->handshakeType = hsTypeTmp;
FRAME_CleanMsg(frameType, frameMsg);
// Change message.
frameType->recordType = REC_TYPE_HANDSHAKE;
frameType->handshakeType = CERTIFICATE;
frameType->keyExType = HITLS_KEY_EXCH_ECDHE;
if (memcpy_s(msg, sizeof(FRAME_Msg), &newFrameMsg, sizeof(newFrameMsg)) != EOK) {
Print("TEST_SendUnexpectCertificateMsg memcpy_s Error!");
}
}
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls12/test_suite_tls12_consistency_rfc5246_malformed_msg.base.c | C | unknown | 27,530 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "rec.h"
#include "parse.h"
#include "hs_msg.h"
#include "hs.h"
#include "alert.h"
#include "hitls_type.h"
#include "session_type.h"
#include "hitls_crypt_init.h"
#include "common_func.h"
#include "hlt.h"
#include "process.h"
#include "rec_read.h"
/* END_HEADER */
#define g_uiPort 6543
// REC_Read calls TlsRecordRead calls RecParseInnerPlaintext
int32_t RecParseInnerPlaintext(TLS_Ctx *ctx, const uint8_t *text, uint32_t *textLen, uint8_t *recType);
int32_t STUB_RecParseInnerPlaintext(TLS_Ctx *ctx, const uint8_t *text, uint32_t *textLen, uint8_t *recType)
{
(void)ctx;
(void)text;
(void)textLen;
*recType = (uint8_t)REC_TYPE_APP;
return HITLS_SUCCESS;
}
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} ResumeTestInfo;
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_APP_DATA_BEFORE_FINISH_FUNC_TC001
* @spec Application Data MUST NOT be sent prior to sending the Finished message
* @brief 2.Protocol Overview row5
* 1. Initializing Configurations.
* 2. Stay in the try finish state and send a message.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_APP_DATA_BEFORE_FINISH_FUNC_TC001(int isClient)
{
FRAME_Init();
STUB_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
FRAME_LinkObj *sender = isClient ? client : server;
FRAME_LinkObj *recver = isClient ? server : client;
// During connection establishment, the client stops in the TRY_SEND_FINISH state.
ASSERT_TRUE(FRAME_CreateConnection(sender, recver, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FuncStubInfo stubInfo = {0};
/*
* Plaintext header of the wrapped record, which is of the app type for finish and app data.
* After the wrapped record body is parsed (that is, the body is decrypted), the last nonzero byte of the body is
* the actual record type. This case is constructed by tampering with the rec type to the app type,
* which should be the hs type.
*/
STUB_Replace(&stubInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
if (isClient) {
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
} else {
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&stubInfo);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NO_SUPPORTED_GROUP_FUNC_TC001
* @spec 1. Construct a scenario where the supported groups of the client and server do not overlap. Expect the server
* to terminate the handshake and send a handshake_failure message after receiving the client hello message.
* alert
* @brief 4.1.1. Cryptographic Negotiation row 10
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE.
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NO_SUPPORTED_GROUP_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
// Set the groups of the client
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
// Set the groups of the server
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
bool isClient = true;
int32_t ret = FRAME_CreateConnection(client, server, !isClient, TRY_RECV_CLIENT_HELLO);
ASSERT_EQ(ret, HITLS_SUCCESS);
ret = HITLS_Accept(server->ssl);
ASSERT_EQ(ret, HITLS_MSG_HANDLE_HANDSHAKE_FAILURE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
FRAME_Msg parsedAlert = {0};
uint32_t parseLen;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf, sndLen, &parsedAlert, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_HANDSHAKE_FAILURE);
EXIT:
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_PSS_FUNC_TC001
* @spec
* The client signature algorithm is set to RSA_PSS_PSS_SHA256 and the server certificate signature algorithm
* is set to RSA_PSS_RSAE_SHA256, the connection fails to be established.
* @brief 4.2.3. Signature Algorithms row 54
* 1. Initialize configuration
* 2. If the signature algorithms are inconsistent, the expected connection setup fails and
* HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE is returned.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_PSS_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
// The client signature algorithm is set to RSA_PSS_PSS_SHA256
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256};
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
// The server signature algorithm is set to RSA_PSS_RSAE_SHA256
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256};
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
FRAME_CertInfo certInfo = {
"rsa_pss_sha256/rsa_pss_root.der",
"rsa_pss_sha256/rsa_pss_intCa.der",
"rsa_pss_sha256/rsa_pss_dev.der",
0,
"rsa_pss_sha256/rsa_pss_dev.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_PSS_FUNC_TC002
* @spec -
* The signature algorithm on the client is set to RSA_PSS_RSAE_SHA256 and the signature algorithm on the server
* is set to RSA_PSS_PSS_SHA256, the connection fails to be established.
* @brief 4.2.3. Signature Algorithms row 53
* 1. Initialize configuration
* 2. If the signature algorithms are inconsistent, the expected connection setup fails and
* HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE is returned.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_PSS_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
// The signature algorithm on the client is set to RSA_PSS_RSAE_SHA256
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256};
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
// The signature algorithm on the server is set to RSA_PSS_PSS_SHA256
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256};
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
FRAME_CertInfo certInfo = {
"rsa_pss_sha256/rsa_pss_root.der",
"rsa_pss_sha256/rsa_pss_intCa.der",
"rsa_pss_sha256/rsa_pss_dev.der",
0,
"rsa_pss_sha256/rsa_pss_dev.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SIG_FUNC_TC001
* @brief 4.2.3. Signature Algorithms row 55
* 1. Set the client server to tls1.3 and the client signature algorithm to rsa-sha1.
* 2. Set the client server to tls1.3 and the client signature algorithm to ecdsa-sha1.
* 3. Set the client server to tls1.3 and the hash in the client signature algorithm to des-sha-224. The expected
* connection establishment fails.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SIG_FUNC_TC001(int sig)
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t signAlgs_c[] = {(uint16_t)sig};
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_SUPPORT_BY_TLS12SERVER_FUNC_TC001
* @brief 4.2.3. Signature Algorithms row 57
* 1. Initialize configuration
* 2. Set the client to tls1.3, server to tls1.2, and the signature algorithm RSA_PSS_RSAE_SHA256 on the client. The
* expected connection setup is successful.
* @expect
* 1.Initialization succeeded.
* 2.The connection is successfully established.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RSAE_SUPPORT_BY_TLS12SERVER_FUNC_TC001()
{
FRAME_Init();
// tls 11, 12, 13
HITLS_Config *config_c = HITLS_CFG_NewTLSConfig();
HITLS_Config *config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
// 1. Set the client to tls1.3, server to tls1.2, and the signature algorithm RSA_PSS_RSAE_SHA256 on the client.
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256};
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256};
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
uint16_t cipherSuite[] = {HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config_c, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(config_s, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_SESSID_FROM_SH_FUNC_TC001
* @spec legacy_session_id_echo: The contents of the client's
* legacy_session_id field. Note that this field is echoed even if
* the client' s value corresponded to a cached pre-TLS 1.3 session
* which the server has chosen not to resume. A client which
* receives a legacy_session_id_echo field that does not match what
* it sent in the ClientHello MUST abort the handshake with an
* "illegal_parameter" alert.
* @brief 4.1.3. Server Hello row 25
* 1.Initialize configuration
* 2.A client which receives a legacy_session_id_echo field that does not match what
* it sent in the ClientHello MUST abort the handshake with an "illegal_parameter" alert.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_SESSID_FROM_SH_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType = {0};
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
memset_s((shMsg->sessionId.data), shMsg->sessionId.size, 1, shMsg->sessionId.size);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_SESSID_FROM_SH_FUNC_TC002
* @spec legacy_session_id_echo: The contents of the client's
* legacy_session_id field. Note that this field is echoed even if
* the client' s value corresponded to a cached pre-TLS 1.3 session
* which the server has chosen not to resume. A client which
* receives a legacy_session_id_echo field that does not match what
* it sent in the ClientHello MUST abort the handshake with an
* "illegal_parameter" alert.
* @brief 4.1.3. Server Hello row 25
* 1.Initialize configuration
* 2.A client which receives a legacy_session_id_echo field that does not match what
* it sent in the ClientHello MUST abort the handshake with an "illegal_parameter" alert.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_SESSID_FROM_SH_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType = {0};
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
shMsg->sessionId.size = 0;
shMsg->sessionId.state = MISSING_FIELD;
shMsg->sessionIdSize.data = 0;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_CIPHERSUITE_FROM_SH_FUNC_TC001
* @spec serverSelect one of ClientHello.cipher_suites. If the server is not provided by the client, the client must
* terminate the handshake and respond with an illegal message. parameter alarm row 26
* @brief cipher_suite: The single cipher suite selected by the server from
* the list in ClientHello.cipher_suites. A client which receives a
* cipher suite that was not offered MUST abort the handshake with an "illegal_parameter" alert.
* 1.Initialize configuration
* 2.A client which receives a cipher suite that was not offered MUST abort the handshake with an
* "illegal_parameter" alert.
* @expect
* 1.Initialization succeeded.
* 2.Return ALERT_ILLEGAL_PARAMETER.
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_CIPHERSUITE_FROM_SH_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
shMsg->cipherSuite.data = HITLS_AES_128_CCM_SHA256;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
FrameUioUserData *userData = BSL_UIO_GetUserData(client->io);
uint8_t *alertBuf = userData->sndMsg.msg;
uint32_t alertLen = userData->sndMsg.len;
FRAME_Msg parsedAlert = {0};
uint32_t parsedAlertLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(alertBuf, alertLen, &parsedAlert, &parsedAlertLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISSING_SIG_ALG_FROM_CH_FUNC_TC001
* @spec
* 1. Set the client server to tls1.3 and construct the client hello message that does not contain the
* signature_algorithm extension. The server is expected to return the missing_extension alarm.
* @brief If a server is authenticating via
* a certificate and the client has not sent a "signature_algorithms"
* extension, then the server MUST abort the handshake with a
* "missing_extension" alert
* 4.2.3. Signature Algorithms row 51
* 1.Initialize configuration
* 2.A client which receives a cipher suite that was not offered MUST abort the handshake with an
* "illegal_parameter" alert.
* @expect
* 1.Initialization succeeded.
* 2.Return ALERT_MISSING_EXTENSION.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISSING_SIG_ALG_FROM_CH_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedCH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType = {0};
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedCH, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *chMsg = &parsedCH.body.hsMsg.body.clientHello;
chMsg->signatureAlgorithms.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedCH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_MISSING_EXTENSION);
FrameUioUserData *userData = BSL_UIO_GetUserData(server->io);
uint8_t *alertBuf = userData->sndMsg.msg;
uint32_t alertLen = userData->sndMsg.len;
FRAME_Msg parsedAlert = {0};
uint32_t parsedAlertLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(alertBuf, alertLen, &parsedAlert, &parsedAlertLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_MISSING_EXTENSION);
EXIT:
FRAME_CleanMsg(&frameType, &parsedCH);
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_CERT_VERIFY_FUNC_TC001
* @brief 4.4.3. Certificate Verify row 147 row 148
* 1. Set the signature algorithms on the client and server to CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
* Change the value of signature_algorithms in the CertificateVerify message sent by the server to
* CERT_SIG_SCHEME_DSA_SHA224 and continue to establish a connection. Expected result: After receiving the
* CertificateVerify message, the client sends an alert message and the connection is disconnected.
* 2. Set the dual-end verification, set the signature algorithm supported by the client and server to
* CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384, and establish a connection, Change the signature algorithm field in
* the CertificateVerify message sent by the client to CERT_SIG_SCHEME_DSA_SHA224. Expected result:
* After receiving the CertificateVerify message, the server sends an alert message and the connection
* is disconnected.
* @expect
* 1.The client sends an alert message and the connection is disconnected.
* 2.The server sends an alert message and the connection is disconnected.
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MODIFIED_CERT_VERIFY_FUNC_TC001(int isClient)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportClientVerify = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
/* 1.Set the signature algorithms on the client and server to CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
* Change the value of signature_algorithms in the CertificateVerify message sent by the server to
* CERT_SIG_SCHEME_DSA_SHA224 and continue to establish a connection. */
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384};
HITLS_CFG_SetSignature(tlsConfig, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
ASSERT_TRUE(FRAME_CreateConnection(client, server, isClient, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
int32_t ret;
HITLS_Ctx *ctx = isClient ? client->ssl : server->ssl;
HS_Ctx *hsCtx = ctx->hsCtx;
uint8_t *buf = hsCtx->msgBuf;
uint32_t dataLen = 0;
HS_MsgInfo hsMsgInfo = {0};
ret = REC_Read(ctx, REC_TYPE_HANDSHAKE, buf, &dataLen, hsCtx->bufferLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
/* 2. Set the dual-end verification, set the signature algorithm supported by the client and server to
* CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384, and establish a connection, Change the signature algorithm field in
* the CertificateVerify message sent by the client to CERT_SIG_SCHEME_DSA_SHA224. */
memset_s(buf + HS_MSG_HEADER_SIZE, sizeof(uint16_t), CERT_SIG_SCHEME_DSA_SHA224, sizeof(uint16_t));
ret = HS_ParseMsgHeader(ctx, buf, dataLen, &hsMsgInfo);
ASSERT_TRUE(ret == HITLS_SUCCESS);
HS_Msg hsMsg = {0};
ret = HS_ParseMsg(ctx, &hsMsgInfo, &hsMsg);
ASSERT_EQ(ret, HITLS_PARSE_UNSUPPORT_SIGN_ALG);
ALERT_Info alertInfo = {0};
(void)ALERT_GetInfo(ctx, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_ILLEGAL_PARAMETER);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, isClient, HS_STATE_BUTT) != HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMPTION_FUNC_TC001
* @brief
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMPTION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
HITLS_SetSession(client->ssl, clientSession);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FUNC_TC001
* @brief
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetPskServerCallback(config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1); // 1.2 psk bind with sha256
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
HITLS_CFG_SetGroups(config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PREFER_PSS_TO_PKCS1_FUNC_TC001
* @brief 4.4.3. Certificate Verify row 149
* 1.Initialize configuration
* 2. Configure the RSA certificate on the server and set the signature algorithm supported by the server to
* {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256}, Establish a connection and check whether
* the negotiated signature algorithm is CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256. Expected result:
* CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256 is negotiated.
* @expect
* 1.Initialization succeeded.
* 2.The connection is successfully set up, and the negotiation algorithm is CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PREFER_PSS_TO_PKCS1_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256};
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
uint16_t cipherSuite[] = {HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(config_s, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_CertInfo certInfo = {
"rsa_pss_sha256/rsa_pss_root.der",
"rsa_pss_sha256/rsa_pss_intCa.der",
"rsa_pss_sha256/rsa_pss_dev.der",
0,
"rsa_pss_sha256/rsa_pss_dev.key.der",
0,
};
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfo);
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(server->ssl->negotiatedInfo.signScheme, CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyFinish(HITLS_Ctx *ctx, uint8_t *buf, uint32_t *bufLen, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
/* hs msg struct, the first byte indicates the HandshakeType,
* the following 3 bytes indicate the remaining bytes in message
*/
uint8_t modifiedHsMsg[] = {KEY_UPDATE, 0, 0, sizeof(uint8_t), HITLS_UPDATE_REQUESTED};
(void)memcpy_s(buf, bufSize, modifiedHsMsg, sizeof(modifiedHsMsg));
*bufLen = sizeof(modifiedHsMsg);
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_INVALID_REQ_VAL_FUNC_TC001
* @brief 4.6.3. Key and Initialization Vector Update row 178
* 1. Establish a connection, change the value of the updata message sent by the client to 3, and observe the server
* behavior. Expected result: The server returns the illegal parameter.
* 2. Establish a connection, change the value of the updata message sent by the server to 3, and observe the client
* behavior. Expected result: The client returns the illegal parameter.
* @expect
* 1.Initialization succeeded.
* 2.Return ALERT_ILLEGAL_PARAMETER.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_INVALID_REQ_VAL_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
uint8_t data[] = {KEY_UPDATE, 0x00, 0x00, 0x01, 0x03};
ASSERT_TRUE(REC_Write(clientTlsCtx, REC_TYPE_HANDSHAKE, data, sizeof(data)) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
/* 1. Establish a link, change the value of the updata message sent by the client to 3, and observe the server
behavior. Expected result: The server returns the illegal parameter.
2. Establish a link, change the value of the updata message sent by the server to 3, and observe the client
behavior.*/
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_MSG_HANDLE_ILLEGAL_KEY_UPDATE_TYPE);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(serverTlsCtx, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_INVALID_REQ_VAL_FUNC_TC002
* @brief 4.6.3. Key and Initialization Vector Update row 178
* 1. Establish a connection, change the value of the updata message sent by the client to 3, and observe the server
* behavior. Expected result: The server returns the illegal parameter.
* 2. Establish a connection, change the value of the updata message sent by the server to 3, and observe the client
* behavior. Expected result: The client returns the illegal parameter.
* @expect
* 1.Initialization succeeded.
* 2.Return ALERT_ILLEGAL_PARAMETER.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_INVALID_REQ_VAL_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
uint8_t data[] = {KEY_UPDATE, 0x00, 0x00, 0x01, 0x03};
ASSERT_TRUE(REC_Write(serverTlsCtx, REC_TYPE_HANDSHAKE, data, sizeof(data)) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
/* 1. Establish a link, change the value of the updata message sent by the client to 3, and observe the server
behavior. Expected result: The server returns the illegal parameter.
2. Establish a link, change the value of the updata message sent by the server to 3, and observe the client
behavior.*/
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_MSG_HANDLE_ILLEGAL_KEY_UPDATE_TYPE);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(clientTlsCtx, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_NO_REPLY_FUNC_TC001
* @brief 4.6.3. Key and Initialization Vector Update row 179
* 1. After the client receives the update_requested message, the update message returned by the client is lost.
* The client continues to send app messages and observe the server behavior. Expected result: The server sends an
* alert message and the connection is disconnected.
* Analysis: The server application traffic secret is updated on both communication ends,
* The client application traffic secret is updated on the client, but the server does not
* update the client application traffic secret. When the server sends a message to the client,
* the client can parse the message, but the server cannot parse the message.
* @expect
* 1.the server cannot parse the message and return ALERT_BAD_RECORD_MAC
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_NO_REPLY_FUNC_TC001()
{
FRAME_Init();
int32_t ret;
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_KeyUpdate(server->ssl, HITLS_UPDATE_REQUESTED) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t dest[READ_BUF_SIZE] = {0};
uint32_t readbytes = 0;
ASSERT_EQ(HITLS_Read(client->ssl, dest, READ_BUF_SIZE, &readbytes), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
/* 1. After the client receives the update_requested message, the update message returned by the client is lost.
* The client continues to send app messages and observe the server behavior. */
uint8_t lostBuffer[MAX_RECORD_LENTH] = {0};
uint32_t lostLen = 0;
ret = FRAME_TransportSendMsg(client->io, lostBuffer, MAX_RECORD_LENTH, &lostLen);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_NE(lostLen, 0);
uint8_t src[] = "Client is sending msg with new application traffic key";
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(client->ssl, src, sizeof(src), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
memset_s(dest, READ_BUF_SIZE, 0, READ_BUF_SIZE);
readbytes = 0;
ASSERT_EQ(HITLS_Read(server->ssl, dest, READ_BUF_SIZE, &readbytes), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_NO_REPLY_FUNC_TC002
* @brief 4.6.3. Key and Initialization Vector Update row 179
* 2. When the server receives the update_requested message, the update message returned by the server is lost.
* The server continues to send app messages and observe the customer behavior. Expected result: The client sends an
* alert message and the connection is disconnected. Analysis: The client application traffic secret is updated at both
* communication ends, The server application traffic secret is updated on the server, but the server application
* traffic secret is not updated on the client. When the client sends a message to the server, the server can parse the
* message, but the server cannot parse the message.
*
* @expect
* 1.the server cannot parse the message and return ALERT_BAD_RECORD_MAC
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_WITH_NO_REPLY_FUNC_TC002()
{
FRAME_Init();
int32_t ret;
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_REQUESTED) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t dest[READ_BUF_SIZE] = {0};
uint32_t readbytes = 0;
ASSERT_EQ(HITLS_Read(server->ssl, dest, READ_BUF_SIZE, &readbytes), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
/* 2. When the server receives the update_requested message, the update message returned by the server is lost. The
* server continues to send app messages and observe the customer behavior. */
uint8_t lostBuffer[MAX_RECORD_LENTH] = {0};
uint32_t lostLen = 0;
ret = FRAME_TransportSendMsg(server->io, lostBuffer, MAX_RECORD_LENTH, &lostLen);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_NE(lostLen, 0);
uint8_t src[] = "Server is sending msg with new application traffic key";
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, src, sizeof(src), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
memset_s(dest, READ_BUF_SIZE, 0, READ_BUF_SIZE);
readbytes = 0;
ASSERT_EQ(HITLS_Read(client->ssl, dest, READ_BUF_SIZE, &readbytes), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(client->ssl, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_BAD_RECORD_MAC);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_READ_WRITE_AFTER_FATAL_ALEART_FUNC_TC001
* @brief 6. Alert Protocol row 206
* Upon receiving an fatal alert, the TLS implementation SHOULD indicate an error to the application and MUST NOT
* allow any further data to be sent or received on the connection.
* 1. After receiving the fatal alert, the server invokes the read interface. The invoking fails.
* 2. After receiving the fatal alert, the server fails to invoke the write interface.
* 3. After receiving the fatal alert, the server fails to invoke the read interface.
* 4. After receiving the fatal alert, the server invokes the write interface. The invoking fails.
* @expect
* 1.the server invokes the read interface fails and return HITLS_CM_LINK_FATAL_ALERTED
* 4.the server invokes the write interface fails and return HITLS_CM_LINK_FATAL_ALERTED
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_READ_WRITE_AFTER_FATAL_ALEART_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
uint8_t data[] = {KEY_UPDATE, 0x00, 0x00, 0x01, 0x03};
ASSERT_TRUE(REC_Write(serverTlsCtx, REC_TYPE_HANDSHAKE, data, sizeof(data)) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
/* 1. Establish a link, change the value of the updata message sent by the client to 3, and observe the server
behavior. Expected result: The server returns the illegal parameter.
2. Establish a link, change the value of the updata message sent by the server to 3, and observe the client
behavior.*/
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_MSG_HANDLE_ILLEGAL_KEY_UPDATE_TYPE);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(clientTlsCtx, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_ILLEGAL_PARAMETER);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_ALERTED);
/* 1. After receiving the fatal alert, the server invokes the read interface. */
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_FATAL_ALERTED);
uint8_t src[] = "Hello world";
/* 4. After receiving the fatal alert, the server invokes the write interface. */
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(clientTlsCtx, src, sizeof(src), &writeLen), HITLS_CM_LINK_FATAL_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_READ_WRITE_AFTER_FATAL_ALEART_FUNC_TC002
* @brief 6. Alert Protocol row 206
* Upon receiving an fatal alert, the TLS implementation SHOULD indicate an error to the application and MUST NOT
* allow any further data to be sent or received on the connection.
* 1. After receiving the fatal alert, the server invokes the read interface. The invoking fails.
* 2. After receiving the fatal alert, the server fails to invoke the write interface.
* 3. After receiving the fatal alert, the server fails to invoke the read interface.
* 4. After receiving the fatal alert, the server invokes the write interface. The invoking fails.
* @expect
* 1.the server invokes the read interface fails and return HITLS_CM_LINK_FATAL_ALERTED
* 4.the server invokes the write interface fails and return HITLS_CM_LINK_FATAL_ALERTED
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_READ_WRITE_AFTER_FATAL_ALEART_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
uint8_t data[] = {KEY_UPDATE, 0x00, 0x00, 0x01, 0x03};
ASSERT_TRUE(REC_Write(clientTlsCtx, REC_TYPE_HANDSHAKE, data, sizeof(data)) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_MSG_HANDLE_ILLEGAL_KEY_UPDATE_TYPE);
ALERT_Info alertInfo = {0};
ALERT_GetInfo(serverTlsCtx, &alertInfo);
ASSERT_EQ(alertInfo.description, ALERT_ILLEGAL_PARAMETER);
ASSERT_EQ(serverTlsCtx->state, CM_STATE_ALERTED);
/* 1. After receiving the fatal alert, the server invokes the read interface. */
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_FATAL_ALERTED);
uint8_t src[] = "Hello world";
/* 4. After receiving the fatal alert, the server invokes the write interface. */
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(serverTlsCtx, src, sizeof(src), &writeLen), HITLS_CM_LINK_FATAL_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC001
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* An implementation that receives a KeyUpdate message before receiving a Finished message must terminate the
* connection with an unexpected_message alert. When the client receives the Finish message, construct abnormal
* packets so that the client receives the Updata message and observe the next message sent by the client.
* Expected result: The next ALERT_UNEXPECTED_MESSAGE message is sent.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
bool isRecRead = true;
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC002
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* An implementation that receives a KeyUpdate message before receiving a Finished message must terminate the
* connection with an unexpected_message alert. When the server receives the Finish message, construct abnormal packets
* so that the server receives the Updata message and observe the next message sent by the server. Expected result: The
* next ALERT_UNEXPECTED_MESSAGE message is sent.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
bool isRecRead = true;
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC003
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* An implementation that receives a KeyUpdate message before receiving a Finished message must terminate the
* connection with an unexpected_message alert. When the client receives the Finish message, construct an abnormal
* packet so that the client receives the Update message and observe the next message sent by the client. Expected
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
bool isRecRead = true;
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(tlsConfig, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(tlsConfig, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(tlsConfig, (HITLS_PskServerCb)ExampleServerCb);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC004
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* An implementation that receives a KeyUpdate message before receiving a Finished message must terminate the
* connection with an unexpected_message alert. When the PSK session is resumed and the connection is established, the
* client constructs an abnormal packet so that the client receives the update message and observes the next message
* sent by the client. Expected result: The next ALERT_UNEXPECTED_MESSAGE message is sent.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
HITLS_SetSession(client->ssl, clientSession);
bool isRecRead = true;
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC005
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* When the server receives the finish message, construct an abnormal packet so that the server receives the update
* message and observe the next message sent by the server. Expected result: The next ALERT_UNEXPECTED_MESSAGE message
* is sent.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC005()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(tlsConfig, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(tlsConfig, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(tlsConfig, (HITLS_PskServerCb)ExampleServerCb);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
bool isRecRead = true;
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC006
* @brief 4.6.3. Key and Initialization Vector Update row 175
* The KeyUpdate message can be sent by any peer after the Finished message is sent.
* When the PSK session is resumed and the connection is established, the server constructs an abnormal packet so that
* the server receives the update message and observes the next message sent by the server. Expected result: The next
* ALERT_UNEXPECTED_MESSAGE message is sent.
* @expect
* 1.Initialization succeeded.
* 2.Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYUPDATE_BEFORE_FINISH_FUNC_TC006()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
HITLS_SetSession(client->ssl, clientSession);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
bool isRecRead = true;
RecWrapper wrapper = {TRY_RECV_FINISH, REC_TYPE_HANDSHAKE, isRecRead, NULL, Test_ModifyFinish};
RegisterWrapper(wrapper);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVAPP_AFTER_CERT_FUNC_TC001
* @spec A Finished message MUST be sent regardless of whether the Certificate message is empty.
* @brief 4.4.2. Certificate row114
* 1. The certificate message sent by the server is not empty and the app message is directly sent.
* Expected result: The handshake between the two parties fails. The client sends an unexpected message alert. The level
* is ALERT_Level_FATAL, the description is ALERT_UNEXPECTED_MESSAGE, and the handshake is interrupted.
* 2. Dual-end verification: The peer certificate can be empty, the certificate message sent by the client is empty, and
* the app message is directly sent. Expected result: The handshake between the two parties fails. The server sends an
* unexpected message alert. The level is ALERT_Level_FATAL, the description is ALERT_UNEXPECTED_MESSAGE, and the
* handshake is interrupted.
* @expect
* 1.Handshake failed. The client sends ALERT_UNEXPECTED_MESSAGE.
* 2.Handshake failed. The server sends ALERT_UNEXPECTED_MESSAGE.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVAPP_AFTER_CERT_FUNC_TC001(int isClient)
{
FRAME_Init();
STUB_Init();
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
0,
0,
0,
0,
};
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
/* 1. The certificate message sent by the server is not empty and the app message is directly sent.
* tlsConfig->isSupportClientVerify = true;
* 2. Dual-end verification: The peer certificate can be empty, the certificate message sent by the client is empty,
* and the app message is directly sent. */
tlsConfig->isSupportNoClientCert = true;
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(tlsConfig, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(tlsConfig, BSL_UIO_TCP, &certInfo);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
if (isClient) {
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
}
FuncStubInfo stubInfo = {0};
/*
* Plaintext header of the wrapped record, which is of the app type for finish and app data.
* After the wrapped record body is parsed (that is, the body is decrypted), the last nonzero byte of the body is
* the actual record type. This case is constructed by tampering with the rec type to the app type, which should be
* the hs type.
*/
STUB_Replace(&stubInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
if (isClient) {
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
} else {
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&stubInfo);
}
/* END_CASE */
static int32_t SendCcs(HITLS_Ctx *ctx, uint8_t *data, uint8_t len)
{
return REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, data, len);
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC001
* @spec
* 1. If the server receives a CCS message before receiving the client hello message, the server sends the
* unexpected_message alarm to terminate the connection.
* @brief If an implementation detects a change_cipher_spec record received before the first ClientHello
* message or after the peer' s Finished message, it MUST be treated as an unexpected record type.
* 5. Record Protocol row 183
* @expect
* 1.the server sends the ALERT_UNEXPECTED_MESSAGE and terminate the connection.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CLIENT_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(client->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC002
* @spec
* 1. After receiving the finished message and CCS message, the client sends the unexpected_message alarm to terminate
* the connection.
* 2. After receiving the finished message and CCS message, the server sends the unexpected_message alarm to terminate
* the connection.
* @brief If an implementation detects a change_cipher_spec record received before the first ClientHello
* message or after the peer' s Finished message, it MUST be treated as an unexpected record type.
* 5. Record Protocol row 183
* @expect
* 1.the client sends the ALERT_UNEXPECTED_MESSAGE and terminate the connection.
* 2.the server sends the ALERT_UNEXPECTED_MESSAGE and terminate the connection.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC002(int isClient)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t data = 1;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
if (isClient != 0) {
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 2. After receiving the finished message and CCS message, the server sends the unexpected_message alarm to
* terminate the connection. */
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
} else {
memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE);
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 1. After receiving the finished message and CCS message, the client sends the unexpected_message alarm to
* terminate the connection. */
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ZERO_APPMSG_FUNC_TC001
* @spec
* 1. Establish a connection. After the connection is established, construct an APPdata message with 0 length and send
* it to the server. Then, send an APPdata message with data to the server. The message can be received normally.
* 2. Establish a connection. After the connection is established, construct an APPdata message with zero length and
* send it to the client. Then, send an APPdata message with data to the client. The message can be received normally.
* @brief Zero-length fragments of Application Data MAY be sent, as they are potentially useful as a traffic analysis
* countermeasure. 5.1. Record Layer row 189
* @expect
* 1.The connection is successfully established and received normally.
* 2.The connection is successfully established and received normally.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ZERO_APPMSG_FUNC_TC001(int isZeroClient)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t data[] = "";
uint8_t appData[] = "hello world";
uint8_t serverData[READ_BUF_SIZE] = {0};
uint8_t clientData[READ_BUF_SIZE] = {0};
ASSERT_TRUE((sizeof(data) <= READ_BUF_SIZE) && (sizeof(appData) <= READ_BUF_SIZE));
if (isZeroClient != 0) {
/* 1. Establish a connection. After the connection is established, construct an APPdata message with 0 length
* and send it to the server. Then, send an APPdata message with data to the server. */
(void)memcpy_s(clientData, READ_BUF_SIZE, data, sizeof(data));
(void)memcpy_s(serverData, READ_BUF_SIZE, appData, sizeof(appData));
} else {
/* 2. Establish a connection. After the connection is established, construct an APPdata message with zero length
* and send it to the client. Then, send an APPdata message with data to the client. */
(void)memcpy_s(clientData, READ_BUF_SIZE, appData, sizeof(appData));
(void)memcpy_s(serverData, READ_BUF_SIZE, data, sizeof(data));
}
size_t serverDataSize = strlen((char *)serverData) + 1;
size_t clientDataSize = strlen((char *)clientData) + 1;
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, serverData, serverDataSize, &writeLen), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == serverDataSize && memcmp(serverData, readBuf, readLen) == 0);
ASSERT_EQ(HITLS_Write(client->ssl, clientData, clientDataSize, &writeLen), HITLS_SUCCESS);
memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE);
readLen = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == clientDataSize && memcmp(clientData, readBuf, readLen) == 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_INCORRECT_LENGTH_CHMSG_FUNC_TC001
* @spec
* 1. If the server receives a ClientHello whose length exceeds the length of the entire packet, the server sends a
* decode_error alarm and the handshake fails.
* @brief Peers which receive a message which cannot be parsed according to the syntax (e.g.,
* have a length extending beyond the message boundary or contain an out-of-range length)
* MUST terminate the connection with a "decode_error" alert.
* 6. Alert Protocol row 209
* @expect
* 1.The server sends a ALERT_DECODE_ERROR and the handshake fails.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_INCORRECT_LENGTH_CHMSG_FUNC_TC001(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportClientVerify = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extensionLen.state = ASSIGNED_FIELD;
clientMsg->extensionLen.data = clientMsg->extensionLen.data + 1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info alert = {0};
ALERT_GetInfo(server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLOSE_NOTIFY_FUNC_TC001
* @spec
* Establish a connection between the client and server, and then close the connection on the client. Check whether
* the message sent by the client is a close_notify message.
* @brief The "close_notify" alert is used to indicate orderly closure of one direction of the connection. Upon
* receiving such an alert, the TLS implementation SHOULD indicate end-of-data to the application.
* 6.0.0. Alert Protocol row 205
* @expect
* 1.The client sends a ALERT_CLOSE_NOTIFY message.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLOSE_NOTIFY_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLOSE_NOTIFY_FUNC_TC003
* @spec
* Establish a connection between the client and server, and then close the connection on the server. Obtain the
* message sent by the server and check whether the message is close_notify.
* @brief The "close_notify" alert is used to indicate orderly closure of one direction of the connection. Upon
* receiving such an alert, the TLS implementation SHOULD indicate end-of-data to the application.
* 6.0.0. Alert Protocol row 205
* @expect
* 1.The server sends a ALERT_CLOSE_NOTIFY message.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLOSE_NOTIFY_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(server, client, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->recMsg.msg;
uint32_t clientreadLen = clientioUserData->recMsg.len;
uint32_t clientparseLen = 0;
ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_WRITE_FUNC_TC001
* @spec
* After the client sends the close_notify message, the client fails to invoke the write interface.
* @brief Either party MAY initiate a close of its write side of the connection by sending a "close_notify" alert. Any
* data received after a closure alert has been received MUST be ignored.
* 6.1.0. Alert Protocol row 213
* @expect
* 1.The client fails to invoke the write interface and send HITLS_CM_LINK_CLOSED
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_WRITE_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(client->ssl, data, sizeof(data), &writeLen), HITLS_CM_LINK_CLOSED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CLOSE_NOTIFY_WRITE_FUNC_TC001
* @spec
* After the server sends the close_notify message, the write interface fails to be invoked.
* @brief Either party MAY initiate a close of its write side of the connection by sending a "close_notify" alert. Any
* data received after a closure alert has been received MUST be ignored.
* 6.1.0. Alert Protocol row 213
* @expect
* 1.The server fails to invoke the write interface and send HITLS_CM_LINK_CLOSED
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CLOSE_NOTIFY_WRITE_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(server, client, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->sndMsg.msg;
uint32_t serverreadLen = serverioUserData->sndMsg.len;
uint32_t serverparseLen = 0;
int32_t ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(server->ssl, data, sizeof(data), &writeLen), HITLS_CM_LINK_CLOSED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_RECV_CLOSE_NOTIFY_CLIENTHELLO_FUNC_TC001
* @spec
* The connection is established normally. After receiving the close_notify message, the server receives the clienthello
* message. The message is ignored and the connection is interrupted.
* @brief close_notify: This alert notifies the recipient that the sender will not send any more messages on this
* connection. Any data received after a closure alert has been received MUST be ignored.
* 6.1.0. Alert Protocol row 211
* @expect
* 1.The connection is interrupted and return HITLS_CM_LINK_FATAL_ALERTED
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_RECV_CLOSE_NOTIFY_CLIENTHELLO_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
FrameUioUserData *clientioUserData = BSL_UIO_GetUserData(client->io);
FRAME_Msg clientframeMsg = {0};
uint8_t *clientbuffer = clientioUserData->sndMsg.msg;
uint32_t clientreadLen = clientioUserData->sndMsg.len;
uint32_t clientparseLen = 0;
int32_t ret = ParserTotalRecord(client, &clientframeMsg, clientbuffer, clientreadLen, &clientparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(clientframeMsg.type == REC_TYPE_ALERT && clientframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(clientframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
clientframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *serverioUserData = BSL_UIO_GetUserData(server->io);
FRAME_Msg serverframeMsg = {0};
uint8_t *serverbuffer = serverioUserData->recMsg.msg;
uint32_t serverreadLen = serverioUserData->recMsg.len;
uint32_t serverparseLen = 0;
ret = ParserTotalRecord(server, &serverframeMsg, serverbuffer, serverreadLen, &serverparseLen);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(serverframeMsg.type == REC_TYPE_ALERT && serverframeMsg.bodyLen == ALERT_BODY_LEN);
ASSERT_TRUE(serverframeMsg.body.alertMsg.level == ALERT_LEVEL_WARNING &&
serverframeMsg.body.alertMsg.description == ALERT_CLOSE_NOTIFY);
ASSERT_TRUE(server->ssl != NULL);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_IO_BUSY);
serverioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_CM_LINK_FATAL_ALERTED);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
serverioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_CM_LINK_FATAL_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_HRR_FUNC_TC001
* @spec
* The connection is established normally. After the client receives the close_notify message and the
* helloRetryRequest message, the client ignores the message and disconnects from the connection.
* @brief close_notify: This alert notifies the recipient that the sender will not send any more messages on this
* connection. Any data received after a closure alert has been received MUST be ignored.
* 6.1.0. Alert Protocol row 211
* @expect
* 1.The connection is interrupted and return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_HRR_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
HITLS_CFG_SetGroups(config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_LEVEL_WARNING, &frameMsg.body.alertMsg.alertLevel) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_CLOSE_NOTIFY, &frameMsg.body.alertMsg.alertDescription) == HITLS_SUCCESS);
uint8_t alertBuf[MAX_RECORD_LENTH] = {0};
uint32_t alertLen = MAX_RECORD_LENTH;
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, alertBuf, alertLen, &alertLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, alertBuf, alertLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_CM_LINK_CLOSED);
ASSERT_NE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_APP_FUNC_TC001
* @spec
* The connection is established normally. After the client receives the close_notify message and the app message, the
* client ignores the message and disconnects the connection.
* @brief close_notify: This alert notifies the recipient that the sender will not send any more messages on this
* connection. Any data received after a closure alert has been received MUST be ignored.
* 6.1.0. Alert Protocol row 211
* @expect
* 1.The connection is interrupted and return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_APP_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_LEVEL_WARNING, &frameMsg.body.alertMsg.alertLevel) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_CLOSE_NOTIFY, &frameMsg.body.alertMsg.alertDescription) == HITLS_SUCCESS);
uint8_t alertBuf[MAX_RECORD_LENTH] = {0};
uint32_t alertLen = MAX_RECORD_LENTH;
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, alertBuf, alertLen, &alertLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, alertBuf, alertLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_NE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC001
* @spec
* When the connection is established, the server sends some error alarms and closes the write end of the connection.
* The close_notify message is not sent.
* @brief Each party MUST send a "close_notify" alert before closing its write side of the connection, unless it has
* already sent some error alert. This does not have any effect on its read side of the connection.
* 6.1.0. Alert Protocol row 214
* @expect
* 1.The close_notify message is not sent.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
/* The client invokes the HITLS_Connect,
and the handshake process is not complete.Check the status of the security connection.The status is
CM_STATE_CALL. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_LEVEL_FATAL, &frameMsg.body.alertMsg.alertLevel) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_DECODE_ERROR, &frameMsg.body.alertMsg.alertDescription) == HITLS_SUCCESS);
uint8_t alertBuf[MAX_RECORD_LENTH] = {0};
uint32_t alertLen = MAX_RECORD_LENTH;
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, alertBuf, alertLen, &alertLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, alertBuf, alertLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
ALERT_Info alert = {0};
ALERT_GetInfo(client->ssl, &alert);
ASSERT_NE(alert.level, ALERT_LEVEL_WARNING);
ASSERT_NE(alert.description, ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC002
* @spec
* If the connection is established normally, the client sends some error alarms and closes the write end of the
* connection. The close_notify message is not sent.
* @brief Each party MUST send a "close_notify" alert before closing its write side of the connection, unless it has
* already sent some error alert. This does not have any effect on its read side of the connection.
* 6.1.0. Alert Protocol row 214
* @expect
* 1.The close_notify message is not sent.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_LEVEL_FATAL, &frameMsg.body.alertMsg.alertLevel) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_ModifyMsgInteger(ALERT_DECODE_ERROR, &frameMsg.body.alertMsg.alertDescription) == HITLS_SUCCESS);
uint8_t alertBuf[MAX_RECORD_LENTH] = {0};
uint32_t alertLen = MAX_RECORD_LENTH;
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, alertBuf, alertLen, &alertLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, alertBuf, alertLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
ALERT_Info alert = {0};
ALERT_GetInfo(server->ssl, &alert);
ASSERT_NE(alert.level, ALERT_LEVEL_WARNING);
ASSERT_NE(alert.description, ALERT_CLOSE_NOTIFY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC003
* @spec
* A connection is set up and the client is disconnected. Before the client receives a response, the client fails to
* invoke the read interface. After receiving the close_notify message, the server suspends the write end and returns a
* close_notify message to close the read end.
* @brief Each party MUST send a "close_notify" alert before closing its write side of the connection, unless it has
* already sent some error alert. This does not have any effect on its read side of the connection.
* 6.1.0. Alert Protocol row 214
* @expect
* @expect The server receives the close_notify message and responds to the close_notify message.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_CLOSED);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_CM_LINK_CLOSED);
ASSERT_EQ(server->ssl->shutdownState, HITLS_RECEIVED_SHUTDOWN);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC004
* @spec
* A connection is set up and the server is disconnected. Before receiving a response, the server fails to invoke the
* read interface. After receiving the close_notify message, the client suspends the write end and responds with the
* close_notify message to close the read end.
* @brief Each party MUST send a "close_notify" alert before closing its write side of the connection, unless it has
* already sent some error alert. This does not have any effect on its read side of the connection.
* 6.1.0. Alert Protocol row 214
* @expect The client receives the close_notify message and responds to the close_notify message.
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CLOSE_NOTIFY_READ_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_CLOSED);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_CLOSED);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_CM_LINK_CLOSED);
ASSERT_EQ(client->ssl->shutdownState, HITLS_RECEIVED_SHUTDOWN);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
int32_t DefaultCfgStatusParkWithSuite_1_3(HandshakeTestInfo *testInfo)
{
FRAME_Init();
testInfo->config = HITLS_CFG_NewTLS13Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC001
* @spec -
* @title During connection establishment, the server receives a message of the undefined record type after sending the
finished message. In this case, an alert message is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable the peer end verification function on
* the server. Expected result 1 is obtained.
* 2. When the client initiates a TLS connection request in the RECV_Client_Hello message, construct an APP
message
* and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
/* 1. Use the default configuration on the client and server, and disable the peer end verification function on
* the server. */
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(testInfo.server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_FINISH), HITLS_SUCCESS);
/* 2. When the client initiates a TLS connection request in the RECV_Client_Hello message, construct an APP message
* and send it to the client. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0xff, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(
memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len), EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC002
* @spec -
* @title After the connection is established, renegotiation is not enabled. The server receives the client hello message
and
* is expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable the peer end check function on the
* server. Expected result 1 is obtained.
* 2. After the connection is set up, do not enable renegotiation and the server receives the client hello
message.
* Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
/* 1. Use the default configuration on the client and server, and disable the peer end check function on the
* server. */
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg,
MAX_RECORD_LENTH,
ioUserData->recMsg.msg + REC_TLS_RECORD_HEADER_LEN,
ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN) == EOK);
sndMsg.len = ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN;
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
REC_Write(testInfo.client->ssl, REC_TYPE_HANDSHAKE, sndMsg.msg, sndMsg.len);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.client, testInfo.server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
/* 2. After the link is set up, do not enable renegotiation and the server receives the client hello message. */
ASSERT_EQ(HITLS_Read(testInfo.server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC003
* @spec -
* @title After initialization is complete, construct an app message and send it to the server. The expected alert is
* returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable the peer end verification function on
* the server. Expected result 1 is obtained.
* 2. When the client initiates a TLS connection application request, construct an APP message and send it to
the
* server in the RECV_CLIENT_HELLO message on the server. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The server sends the ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC003(void)
{
FRAME_Msg parsedAlert = {0};
HandshakeTestInfo testInfo = {0};
testInfo.isClient = false;
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportClientVerify = false;
/* 1. Use the default configuration on the client and server, and disable the peer end verification function on
* the server. */
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == 0);
/* 2. When the client initiates a TLS connection application request, construct an APP message and send it to the
* server in the RECV_CLIENT_HELLO message on the server. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(
memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len), EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(sndBuf, sndLen, &parsedAlert, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(parsedAlert.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC004
* @spec -
* @title After initialization, construct an app message and send it to the client. The expected alert is returned.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
* Expected result 1 is obtained.
* 2. When the client initiates a TLS connection application request in the RECV_SERVER_HELLO message,
construct an
* APP message and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC004(void)
{
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
HandshakeTestInfo testInfo = {0};
testInfo.isClient = true;
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportClientVerify = false;
/* 1. Use the default configuration on the client and server, and disable peer verification on the server. */
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == 0);
/* 2. When the client initiates a TLS connection application request in the RECV_SERVER_HELLO message, construct an
* APP message and send it to the client. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(
memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len), EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_TRUE(alertMsg->alertDescription.data == ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC005
* @spec -
* @title After the connection is established, the client receives the serverhello message when receiving the app
data. The
* client is expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
* Expected result 1 is obtained.
* 2. The client initiates a TLS connection request. After the handshake succeeds, the server constructs a
* serverhello message and sends it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC005()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
/* 1. Use the default configuration on the client and server, and disable peer verification on the server. */
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
/* 2. The client initiates a TLS connection request. After the handshake succeeds, the server constructs a
* serverhello message and sends it to the client. */
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg,
MAX_RECORD_LENTH,
ioUserData->recMsg.msg + REC_TLS_RECORD_HEADER_LEN,
ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN) == EOK);
sndMsg.len = ioUserData->recMsg.len - REC_TLS_RECORD_HEADER_LEN;
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
REC_Write(testInfo.server->ssl, REC_TYPE_HANDSHAKE, sndMsg.msg, sndMsg.len);
// Inject packets to the client.
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(testInfo.server, testInfo.client), HITLS_SUCCESS);
/* The server invokes HITLS_Read to receive data. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC006
* @spec -
* @title After the connection is established, the client receives an unknown message when receiving app data and is
* expected to return an alert message.
* @precon nan
* @brief 1. Use the default configuration on the client and server, and disable peer verification on the server.
* Expected result 1 is displayed.
* 2. The client initiates a TLS connection request. After the handshake succeeds, the client constructs an
unknown
* message and sends it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECT_RECODETYPE_FUNC_TC006()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
/* 1. Use the default configuration on the client and server, and disable peer verification on the server. */
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(testInfo.client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
/* 2. The client initiates a TLS connection request. After the handshake succeeds, the client constructs an unknown
* message and sends it to the client. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
uint8_t appdata[] = {0xff, 0x03, 0x03, 0x00, 0x02, 0x01, 0x01};
ASSERT_EQ(memcpy_s(data, len, appdata, sizeof(appdata)), EOK);
ASSERT_EQ(
memcpy_s(data + sizeof(appdata), len - sizeof(appdata), ioUserData->recMsg.msg, ioUserData->recMsg.len), EOK);
ASSERT_EQ(memcpy_s(ioUserData->recMsg.msg, MAX_RECORD_LENTH, data, ioUserData->recMsg.len + sizeof(appdata)), EOK);
ioUserData->recMsg.len += sizeof(appdata);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(testInfo.client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
#define BUF_TOOLONG_LEN ((1 << 14) + 1)
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC003
* @spec -
* @title The client sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client initiates a DTLS connection establishment request and sends a Change Cipher Spec message,
the
* client modifies one field as follows: Length is 2 ^ 14 + 1. After the modification is complete, send the
* modification to the server. Expected result 2 is obtained.
* 3. When the server receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
* interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
* 3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC003(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_FINISH;
testInfo.isClient = false;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
/* 1. Use the default configuration items to configure the client and server. */
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
ASSERT_EQ(
FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, TRY_RECV_FINISH), HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
/* 2. When the client initiates a DTLS connection establishment request and sends a Change Cipher Spec message, the
* client modifies one field as follows: Length is 2 ^ 14 + 1. After the modification is complete, send the
* modification to the server. */
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.ccsMsg.extra.data);
frameMsg.body.ccsMsg.extra.data = certDataTemp;
frameMsg.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.server->ssl != NULL);
/* 3. When the server receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
* interface. */
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_FINISH);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC004
* @spec -
* @title The client sends a Change Cipher Spec message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and client. Expected result 1 is obtained.
* 2. When the client initiates a DTLS connection establishment request and sends a Change Cipher Spec message,
the client modifies one field as follows: Length is 2 ^ 14 + 1. After the modification is complete, send the
modification to the client. Expected result 2 is obtained.
3. When the client receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC004(void)
{
HandshakeTestInfo testInfo = {0};
testInfo.state = TRY_RECV_FINISH;
testInfo.isClient = true;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isSupportClientVerify = true;
/* 1. Use the default configuration items to configure the client and server. */
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
ASSERT_EQ(
FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, TRY_RECV_FINISH), HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
/* 2. When the client initiates a DTLS connection establishment request and sends a Change Cipher Spec message, the
* client modifies one field as follows: Length is 2 ^ 14 + 1. After the modification is complete, send the
* modification to the server. */
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.ccsMsg.extra.data);
frameMsg.body.ccsMsg.extra.data = certDataTemp;
frameMsg.body.ccsMsg.extra.size = BUF_TOOLONG_LEN;
frameMsg.body.ccsMsg.extra.state = ASSIGNED_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
/* 3. When the client receives the Change Cipher Spec message, check the value returned by the HITLS_Accept
* interface. */
ASSERT_EQ(HITLS_Accept(testInfo.client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(testInfo.client->ssl->hsCtx->state == TRY_RECV_FINISH);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC001
* @spec -
* @titleThe client sends a clienthello message with the length of 2 ^ 14 + 1 byte.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. When the client initiates a DTLS connection creation request, the client needs to send a client hello
message,
* modify the following field as follows: Length is 2 ^ 14 + 1. After the modification, the modification is
* sent to the server. Expected result 2 is obtained.
* 3. When the server receives the client hello message, check the value returned by the HITLS_Accept
* interface. Expected result 3 is obtained.
* @expect 1. The initialization is successful.
* 2. The field is successfully modified and sent to the client.
* 3. The return value of the HITLS_Accept interface is HITLS_REC_NORMAL_RECV_BUF_EMPTY.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
/* 1. Use the default configuration items to configure the client and server. */
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* 2. When the client initiates a DTLS connection creation request, the client needs to send a client hello message,
* modify the following field as follows: Length is 2 ^ 14 + 1. After the modification, the modification is
* sent to the server. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.sessionId.data);
clientMsg->sessionId.state = ASSIGNED_FIELD;
clientMsg->sessionId.size = BUF_TOOLONG_LEN;
clientMsg->sessionId.data = certDataTemp;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* 3. When the server receives the client hello message, check the value returned by the HITLS_Accept
* interface. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_REC_RECORD_OVERFLOW);
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_RECORD_OVERFLOW);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC002
* @spec -
* @title Verify that the client receives a serverhello message with the length of 2 ^ 14 + 1. The client is expected to
* return an alert message.
* @precon nan
* @brief 1. Create a config and server connection, and construct a server hello message with the length of 2 ^ 14
+ 1.
* Expected result 1 is obtained.
* 2. The client invokes the HITLS_Connect interface. (Expected result 2)
* @expect 1. A success message is returned.
* 2. A failure message is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MSGLENGTH_TOOLONG_FUNC_TC002(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* 1. Create a config and server connection, and construct a server hello message with the length of 2 ^ 14 + 1. */
uint8_t *certDataTemp = (uint8_t *)BSL_SAL_Calloc(1, (uint32_t)BUF_TOOLONG_LEN);
ASSERT_TRUE(certDataTemp != NULL);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.serverHello.randomValue.data);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->randomValue.state = ASSIGNED_FIELD;
serverMsg->randomValue.size = BUF_TOOLONG_LEN;
serverMsg->randomValue.data = certDataTemp;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_TRUE(testInfo.client->ssl != NULL);
/* 2. The client invokes the HITLS_Connect interface. */
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_RECORD_OVERFLOW);
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_RECORD_OVERFLOW);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
int32_t STUB_HS_DoHandshake_Fatal(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t plainLen)
{
(void)recordType;
(void)data;
(void)plainLen;
ctx->method.sendAlert(ctx, ALERT_LEVEL_WARNING, ALERT_UNEXPECTED_MESSAGE); /* sends a fatal alert message.*/
return HITLS_INTERNAL_EXCEPTION;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_DESCRIPTION_FUNC_TC001
* @spec All the alerts listed in Section 6.2 MUST be sent with AlertLevel=fatal and MUST be treated as error alerts
* when received regardless of the AlertLevel in the message.
* Unknown Alert types MUST be treated as error alerts.
* @title If the AlertLevel field in the alarm message received by the client is non-critical but the alarm type is
* critical, the client immediately closes the connection.
* @precon nan
* @brief 6. Alert Protocol row208
* If the AlertLevel field in the alarm message received by the client is not critical but the alarm type is
* critical, the connection is closed immediately.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_DESCRIPTION_FUNC_TC001()
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo = {0};
HITLS_Config *tlsConfig = HITLS_CFG_NewTLSConfig();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig, 0x00000030U);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
server->ssl->recCtx->outBuf->end = 0;
STUB_Replace(&tmpRpInfo, HS_DoHandshake, STUB_HS_DoHandshake_Fatal);
int32_t ret = HITLS_Accept(server->ssl);
ASSERT_EQ(ret, HITLS_REC_NORMAL_IO_BUSY);
STUB_Reset(&tmpRpInfo);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_DESCRIPTION_FUNC_TC002
* All alerts listed in section 6.2 of the specification must be sent with AlertLevel=Fatal and must be treated as
* false alerts When received regardless of the AlertLevel in the message.
* Unknown alert types must be treated as false alerts.
* @title In the alarm message received by the server, if the value of AlertLevel is not critical but the alarm type is
* critical, the connection is closed immediately.
* @preconan
* @short 6. Alert protocol line 208
* In the alarm message received by the server, if the value of AlertLevel is not critical but the alarm type
* is critical, the connection is closed immediately.
* @expect forward to normal connection establishment
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_DESCRIPTION_FUNC_TC002()
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo = {0};
HITLS_Config *tlsConfig = HITLS_CFG_NewTLSConfig();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig, 0x00000030U);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
client->ssl->recCtx->outBuf->end = 0;
STUB_Replace(&tmpRpInfo, HS_DoHandshake, STUB_HS_DoHandshake_Fatal);
int32_t ret = HITLS_Connect(client->ssl);
ASSERT_EQ(ret, HITLS_REC_NORMAL_IO_BUSY);
STUB_Reset(&tmpRpInfo);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERROR_ENUM_FUNC_TC001
* @spec Peers which receive a message which is syntactically correct but semantically invalid
* (e.g., a DHE share of p - 1, or an invalid enum) MUST terminate the connection with
* an "illegal_parameter" alert.
* @title When the client receives a server keyexchange message in which the value of type is invalid, the client
* generates an illegal_parameter alarm and the handshake fails.
* @precon nan
* @brief 6. Alert Protocol row210
* When the client receives a server keyexchange message in which the value of type is invalid, the client
* generates an illegal_parameter alarm and the handshake fails.
* @expect Sending an alarm
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERROR_ENUM_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
tlsConfig->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_KEY_EXCHANGE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, SERVER_KEY_EXCHANGE, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
sendBuf[9] = 0x04;
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Accept(clientTlsCtx), HITLS_PARSE_UNSUPPORT_KX_CURVE_TYPE);
ALERT_Info alert = {0};
ALERT_GetInfo(server->ssl, &alert);
ASSERT_NE(alert.level, ALERT_LEVEL_FATAL);
ASSERT_NE(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC001
* @spec -
* @title To verify that the server receives a 0-length client hello and is expected to return an alarm.
* @precon nan
* @brief 1.Create a config and client connection, and construct a 0-length client hello. Expected result 1 is
displayed.
* 2.The client invokes the HITLS_Connect interface. Expected result 2 is obtained.
* @expect 1. Return success
* 2.Return failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_CLIENT_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = false;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
/* Obtain the message buffer */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
recvBuf[6] = 00;
recvBuf[7] = 00;
recvBuf[8] = 00;
recvLen = 6;
/* Invoke the test interface. Expected success in receiving and processing, and send Alert. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), HITLS_PARSE_INVALID_MSG_LEN);
/* Obtain the message buffer. */
ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
/* Parse to msg structure */
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Determine whether it is consistent with the expectation. */
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC002
* @spec -
* @title To verify that the client receives a 0-length server hello and returns an alert message.
* @precon nan
* @brief 1.Create a config and server connection, and construct a 0-length server hello. Expected result 1 is
displayed. 2.The client invokes the HITLS_Connect interface. Expected result 2 is obtained.
* @expect 1.Return success
2.Return failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC002(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
testInfo.state = TRY_RECV_SERVER_HELLO;
testInfo.isSupportExtendMasterSecret = true;
testInfo.isClient = true;
ASSERT_TRUE(DefaultCfgStatusParkWithSuite_1_3(&testInfo) == HITLS_SUCCESS);
/* Obtain the message buffer. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
recvBuf[6] = 00;
recvBuf[7] = 00;
recvBuf[8] = 00;
recvLen = 6;
/* Invoke the test interface. Expected success in receiving and processing, and send Alert. */
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_PARSE_INVALID_MSG_LEN);
/* Obtain the message buffer. */
ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
/* Parse to msg structure */
uint32_t parseLen = 0;
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Determine whether it is consistent with the expectation. */
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_DECODE_ERROR);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
FRAME_DeRegCryptMethod();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_1.c | C | unknown | 156,987 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hlt.h"
#include "hlt_type.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "process.h"
#include "securec.h"
#include "session_type.h"
#include "rec_wrapper.h"
#include "common_func.h"
#include "conn_init.h"
#include "hs_extensions.h"
#include "hitls_crypt_init.h"
#include "alert.h"
#include "record.h"
#include "hs_kx.h"
#include "bsl_log.h"
#include "cert_callback.h"
/* END_HEADER */
#define PORT 23456
#define READ_BUF_SIZE (18 * 1024)
#define ALERT_BODY_LEN 2u
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; // Set the session to the client for session recovery.
HITLS_TicketKeyCb serverKeyCb;
} ResumeTestInfo;
typedef struct{
char *ClientCipherSuite;
char *ServerCipherSuite;
char *ClientGroup;
char *ServerGroup;
uint8_t ClientKeyExchangeMode;
uint8_t ServerKeyExchangeMode;
uint8_t psk[PSK_MAX_LEN];
bool SetNothing;
bool SuccessOrFail;
} SetInfo;
void SetConfig(HLT_Ctx_Config *clientconfig, HLT_Ctx_Config *serverconfig, SetInfo setInfo)
{
if ( !setInfo.SetNothing ) {
// Configure the server configuration.
if (setInfo.ServerCipherSuite != NULL) {
HLT_SetCipherSuites(serverconfig, setInfo.ServerCipherSuite);
}
if (setInfo.ServerGroup != NULL) {
HLT_SetGroups(serverconfig, setInfo.ServerGroup);
}
memcpy_s(serverconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
if ( (setInfo.ClientKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
clientconfig->keyExchMode = setInfo.ClientKeyExchangeMode;
}
// Configure the client configuration.
if (setInfo.ClientCipherSuite != NULL) {
HLT_SetCipherSuites(clientconfig, setInfo.ClientCipherSuite);
}
if (setInfo.ClientGroup != NULL) {
HLT_SetGroups(clientconfig, setInfo.ClientGroup);
}
memcpy_s(clientconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
if ( (setInfo.ServerKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
serverconfig->keyExchMode = setInfo.ServerKeyExchangeMode;
}
}
}
static int32_t DoHandshake(ResumeTestInfo *testInfo)
{
/* Construct a connection. */
testInfo->client = FRAME_CreateLink(testInfo->c_config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
int32_t ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->s_config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
}
void ClientCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// The local client and remote server listen on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the config file.
SetConfig(clientConfig, serverConfig, setInfo);
// Listening connection.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Establish a connection.
if ( setInfo.SuccessOrFail ) {
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) == 0);
}else {
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
}
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ServerCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// The local client and remote server listen on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the config file.
SetConfig(clientConfig, serverConfig, setInfo);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Insert abnormal message callback.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Client listening connection.
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
if ( setInfo.SuccessOrFail ) {
ASSERT_TRUE(clientRes != NULL);
}else {
ASSERT_TRUE(clientRes == NULL);
}
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ResumeConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for the config context.
void *clientConfig = HLT_TlsNewCtx(TLS1_3);
ASSERT_TRUE(clientConfig != NULL);
// Configure the session restoration function.
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, TLS1_3, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, TLS1_3, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
SetConfig(clientCtxConfig, serverCtxConfig, setInfo);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = TCP;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = TCP;
localProcess->connType = TCP;
// The server applies for the context.
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = TCP;
// Set the FD.
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
// Client, applying for context
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = TCP;
// Set the FD.
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
handle->ctx = clientSsl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
if(!setInfo.SuccessOrFail){
ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
}else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
}
else {
// Negotiation
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
// Data read/write
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// Disable the connection.
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_HasTicket(session) == true);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}cnt++;
} while (cnt < 3); // Perform the connection twice.
EXIT:
HITLS_SESS_Free(session);
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
static void Test_ServerAddKeyExchangeMode(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// Add the KeyExchangeMode extension to server hello.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
uint8_t pskMode[] = {0, 0x2d, 0, 2, 1, 1};
frameMsg.body.hsMsg.length.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.length.data += sizeof(pskMode);
frameMsg.body.hsMsg.body.serverHello.extensionLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.extensionLen.data = frameMsg.body.hsMsg.body.serverHello.extensionLen.data + sizeof(pskMode);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
ASSERT_EQ(memcpy_s(&data[*len], bufSize - *len, &pskMode, sizeof(pskMode)), EOK);
*len += sizeof(pskMode);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Server_KeyShare_Miss(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// The keyshare extension of server hello is lost.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.keyShare.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Client_MasterSecret_Miss(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// The MasterSecret extension of client hello is lost.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.extendedMasterSecret.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Client_ObfuscatedTicketAge_NotZero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// The value of ObfuscatedTicketAge is not 0 for the external preset PSK.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.data = 2;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Client_ObfuscatedTicketAge_Zero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// The value of ObfuscatedTicketAge is 0 for the PSK generated by the session.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.data = 0;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Client_Binder_Unnormal(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
// Change the binder value of the psk extension of the client hello message.
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
uint8_t binder[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,};
frameMsg.body.hsMsg.body.clientHello.psks.binders.data->binder.state = ASSIGNED_FIELD;
memcpy_s(frameMsg.body.hsMsg.body.clientHello.psks.binders.data->binder.data, PSK_MAX_LEN, binder, sizeof(binder));
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void FrameCallBack_ServerHello_KeyShare_Add(void *msg, void *userData)
{
// ServerHello exception: The sent ServerHello message carries the keyshare extension.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverhello = &frameMsg->body.hsMsg.body.serverHello;
serverhello->keyShare.exState = INITIAL_FIELD;
serverhello->keyShare.exLen.state = INITIAL_FIELD;
serverhello->keyShare.data.state = INITIAL_FIELD;
serverhello->keyShare.data.group.state = ASSIGNED_FIELD;
serverhello->keyShare.data.group.data = HITLS_EC_GROUP_SECP256R1;
FRAME_ModifyMsgInteger(HS_EX_TYPE_KEY_SHARE, &serverhello->keyShare.exType);
uint8_t uu[] = {0x00, 0x15, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56};
FRAME_ModifyMsgArray8(uu, sizeof(uu), &serverhello->keyShare.data.keyExchange, &serverhello->keyShare.data.keyExchangeLen);
EXIT:
return;
}
static void FrameCallBack_ClientHello_PskExchangeMode_Miss(void *msg, void *userData)
{
// ClientHello exception: The sent ClientHello message causes the Psk_Exchange_Mode extension to be lost.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
clienthello->pskModes.exState = MISSING_FIELD;
EXIT:
return;
}
static void FrameCallBack_ClientHello_KeyShare_Miss(void *msg, void *userData)
{
// ClientHello exception: The KeyShare extension of the ClientHello message is lost.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
clienthello->keyshares.exState = MISSING_FIELD;
EXIT:
return;
}
/** @
* @test UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC001
* @brief 2.1-Incorrect DHE Share-6
* @spec If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
* appropriate alert.
* @title The server sends an hrr message to request the key_share. The negotiation succeeds.
* @precon nan
* @brief
* 1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
* (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
* 2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and check whether the value of
* the group field is HITLS_EC_GROUP_SECP384R1. (Expected result 2)
* 3. Continue to establish the connection. Expected result 3 is obtained.
* @expect
* 1. The setting is successful.
* 2. The value of the group field is HITLS_EC_GROUP_SECP384R1.
* 3. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC001()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
// Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
// (HITLS_EC_GROUP_SECP384R1) on the server.
uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and check whether the value of
// the group field is HITLS_EC_GROUP_SECP384R1.
FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(Hello_Retry_RequestMsg->keyShare.data.group.data == HITLS_EC_GROUP_SECP384R1);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_SEND_CLIENT_HELLO), HITLS_SUCCESS);
// Continue to establish the connection.
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC002
* @brief 2.1-Incorrect DHE Share-6
* @spec If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
* appropriate alert.
* @title The server receives two unsupported key_share messages.
* @precon nan
* @brief
* 1. Set the group (HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, and HITLS_EC_GROUP_SECP521R1) on the client and
* set the group (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
* 2. Establish a connection, stop the server in TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the group
* field in the connection to HITLS_EC_GROUP_SECP521R1. Expected result 2 is obtained.
* 3. Continue to establish a connection and observe the connection establishment result. Expected result 3 is obtained.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. The server sends a request again, and the client returns HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC002()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
// Set the group (HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, and HITLS_EC_GROUP_SECP521R1) on the client
// and set the group (HITLS_EC_GROUP_SECP384R1) on the server.
uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, HITLS_EC_GROUP_SECP521R1};
uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Establish a connection, stop the server in TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the group
// field in the connection to HITLS_EC_GROUP_SECP521R1.
FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP521R1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl) , HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
// Continue to establish a connection and observe the connection establishment result.
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC003
* @brief 2.1-Incorrect DHE Share-6
* @spec If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
* appropriate alert.
* @title The client receives the key_share with the same elliptic curve for the second time.
* @precon nan
* @brief
* 1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
* (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
* 2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
* group field to HITLS_EC_GROUP_SECP256R1. Expected result 2 is obtained.
* 3. Continue connection establishment and observe the connection establishment result. Expected result 3 is obtained.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. The connection fails to be established. After receiving the request message, the client returns
* HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC003()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
// Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
// (HITLS_EC_GROUP_SECP384R1) on the server.
uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
// group field to HITLS_EC_GROUP_SECP256R1.
FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP256R1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC004
* @brief 2.1-Incorrect DHE Share-6
* @spec If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
* appropriate alert.
* @title The client receives an unsupported elliptic curve key_share request.
* @precon nan
* @brief
* 1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
* (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
* 2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
* group field to HITLS_EC_GROUP_SECP521R1. Expected result 2 is obtained.
* 3. Continue connection establishment and observe the connection establishment result. Expected result 3 is obtained.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. The connection fails to be established. After receiving the request message, the client returns
* HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC004()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
// Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
// (HITLS_EC_GROUP_SECP384R1) on the server.
uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
// Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
// group field to HITLS_EC_GROUP_SECP521R1.
FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP521R1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
// Continue connection establishment and observe the connection establishment result.
ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC005
* @brief 2.1. Incorrect DHE Share
* @spec If the client has not provided a sufficient "key_share" extension (e.g., it includes only DHE or ECDHE groups
unacceptable to or unsupported by the server), the server corrects the mismatch with a HelloRetryRequest and the
client needs to restart the handshake with an appropriate "key_share" extension, as shown in Figure 2. If no common
cryptographic parameters can be negotiated, the server MUST abort the handshake with an appropriate alert.
* @title Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
server. Observe the link setup result and check whether the server sends Hello_Retry_Requset.
* @precon nan
* @brief
1. Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
server.
* @expect
1. Send clienthello with X25519 keyshare. The link is established successfully.
2. The server does not send Hello_Retry_Requset.
* @prior Level 2
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC005()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
uint16_t clientgroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
uint16_t servergroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverHello = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverHello->keyShare.data.group.data == HITLS_EC_GROUP_CURVE25519);
ASSERT_EQ(server->ssl->hsCtx->haveHrr, false);
// Continue to establish the link.
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC001
* @brief 4.2.9-Pre-Shared Key Exchange Modes-77
* @spec In order to use PSKs, clients MUST also send a "psk_key_exchange_modes" extension.
* @title Preset psk Client Lost Pre-Shared Key Exchange Modes Extension
* @precon nan
* @brief
* 1. Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
* lost, and observe the server behavior.
* @expect
* 1. Connect establishment is interrupted.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC001()
{
// Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
// lost, and observe the server behavior.
SetInfo setInfo = {0};
memcpy_s(setInfo.psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
setInfo.SuccessOrFail = 0;
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = FrameCallBack_ClientHello_PskExchangeMode_Miss;
ClientCreatConnectWithPara(&handle, setInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC002
* @brief 4.2.9-Pre-Shared Key Exchange Modes-77
* @spec In order to use PSKs, clients MUST also send a "psk_key_exchange_modes" extension.
* @title psk session recovery client lost Pre-Shared Key Exchange Modes extension
* @precon nan
* @brief
* 1. Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
lost, and observe the server behavior.
* @expect
* 1. Connect establishment is interrupted.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC002()
{
// Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
// lost, and observe the server behavior.
SetInfo setInfo = {0};
setInfo.SetNothing = 1;
setInfo.SuccessOrFail = 0;
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = FrameCallBack_ClientHello_PskExchangeMode_Miss;
ResumeConnectWithPara(&handle, setInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_ADD_FUNC_TC001
* @brief 4.2.9-Pre-Shared Key Exchange Modes-80
* @spec The server MUST NOT send a "psk_key_exchange_modes" extension.
* @title The session restoration server carries psk_key_exchange_modes.
* @precon nan
* @brief
* 1. Establish a connection, save the session, and restore the session.
* 2. Modify the server hello message sent by the server to carry the psk_key_exchange_mode extension and observe the
* client behavior.
* @expect
* 1. The connection is successfully established and the session is restored.
* 2. The client sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_ADD_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
// Establish a connection, save the session, and restore the session.
testInfo.s_config = HITLS_CFG_NewTLS13Config();
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
ASSERT_TRUE(testInfo.c_config != NULL);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
// Modify the server hello message sent by the server to carry the psk_key_exchange_mode extension and observe the
// client behavior.
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerAddKeyExchangeMode};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_PARSE_UNSUPPORTED_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_ADD_FUNC_TC001
* @brief 4.2.9-Pre-Shared Key Exchange Modes-80
* @spec psk_ke: PSK-only key establishment. In this mode, the server MUST NOT supply a "key_share" value.
* @title Preset the PSK. In psk_ke mode, the server carries the key_share extension.
* @precon nan
* @brief
* 1. Preset PSK
* 2. Set psk_key_exchange_mode to psk_ke on the client and server,
* 3. Modify the server hello message sent by the server to carry the key_share extension and observe the client
* behavior.
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The client sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_ADD_FUNC_TC001()
{
// Preset PSK
SetInfo setInfo = {0};
memcpy_s(setInfo.psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
// Set psk_key_exchange_mode to psk_ke on the client and server
setInfo.ClientKeyExchangeMode = TLS13_KE_MODE_PSK_ONLY;
setInfo.ServerKeyExchangeMode = TLS13_KE_MODE_PSK_ONLY;
setInfo.SuccessOrFail = 0;
// Modify the server hello message sent by the server to carry the key_share extension and observe the client
// behavior.
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = FrameCallBack_ServerHello_KeyShare_Add;
ServerCreatConnectWithPara(&handle, setInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC001
* @brief 4.2.9-Pre-Shared Key Exchange Modes-77
* @spec psk_dhe_ke: PSK with (EC)DHE key establishment. In this mode, the
* client and server MUST supply "key_share" values as described in Section 4.2.8.
* @title Session Recovery Client Lost Key_share Extension
* @precon nan
* @brief
* 1. When the session is recovered, modify the client hello message sent by the client so that the Key_Share extension
* is lost. Observe the server behavior.
* @expect
* 1. Connect establishment is interrupted.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC001()
{
// When the session is recovered, modify the client hello message sent by the client so that the Key_Share extension
// is lost. Observe the server behavior.
SetInfo setInfo = {0};
setInfo.SetNothing = 1;
setInfo.SuccessOrFail = 0;
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = FrameCallBack_ClientHello_KeyShare_Miss;
ResumeConnectWithPara(&handle, setInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC002
* @brief 4.2.9-Pre-Shared Key Exchange Modes-80
* @spec psk_dhe_ke: PSK with (EC)DHE key establishment. In this mode, the
* client and server MUST supply "key_share" values as described in Section 4.2.8.
* @title Server: psk_key_exchange_mode: The key_share extension is lost under psk_dhe_ke.
* @precon nan
* @brief
* 1. Preset PSK
* 2. Set psk_key_exchange_mode to psk_dhe_ke on the client server, modify the server hello message sent by the server to
* lose the key_share extension, and observe the client behavior.
* @expect
* 1. The setting is successful.
* 2. The client sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Server_KeyShare_Miss
};
RegisterWrapper(wrapper);
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
// Preset PSK
char psk[] = "aaaaaaaaaaaaaaaa";
ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
// Set psk_key_exchange_mode to psk_dhe_ke on the client server, modify the server hello message sent by the server
// to lose the key_share extension, and observe the client behavior.
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-93
* @spec For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
* the value
* @title The obfuscated_ticket_age of the preset PSK is not 0.
* @precon nan
* @brief
* 1. Preset PSK
* 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
* ensure that the field is not 0.
* 3. Establish a connection.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. The connection is successfully established and certificate authentication is performed.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Client_ObfuscatedTicketAge_NotZero
};
RegisterWrapper(wrapper);
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
// Preset PSK
char psk[] = "aaaaaaaaaaaaaaaa";
ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
// ensure that the field is not 0.
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverhelloMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverhelloMsg->pskSelectedIdentity.exLen.data == 0);
// Establish a connection.
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
extern int32_t CompareBinder(TLS_Ctx *ctx, const PreSharedKey *pskNode, uint8_t *psk, uint32_t pskLen,
uint32_t truncateHelloLen);
static int32_t CompareBinder_Success(TLS_Ctx *ctx, const PreSharedKey *pskNode, uint8_t *psk, uint32_t pskLen,
uint32_t truncateHelloLen)
{
(void)ctx;
(void)pskNode;
(void)psk;
(void)pskLen;
(void)truncateHelloLen;
return 0;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC002
* @brief 4.2.11-Pre-Shared Key Extension-93
* @spec For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
* the value
* @title The obfuscated_ticket_age of the PSK generated by the session is 0.
* @precon nan
* @brief
* 1. Preset PSK
* 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
* ensure that the value is not 0.
* 3. Establish a connection.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. Certificate authentication
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
// Preset PSK
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
// ensure that the value is not 0.
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_ObfuscatedTicketAge_Zero};
RegisterWrapper(wrapper);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
STUB_Init();
FuncStubInfo stubInfo = {0};
STUB_Replace(&stubInfo, CompareBinder, CompareBinder_Success);
// Establish a connection.
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(testInfo.client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
ClearWrapper();
STUB_Reset(&stubInfo);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC003
* @brief 4.2.11-Pre-Shared Key Extension-93
* @spec For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
* the value
* @title The obfuscated_ticket_age of the PSK generated by the session is different from the original value.
* @precon nan
* @brief
* 1. Preset PSK
* 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
* ensure that the field is not 0.
* 3. Establish a connection.
* @expect
* 1. The setting is successful.
* 2. The modification is successful.
* 3. Certificate authentication
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
// Preset PSK
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
// ensure that the field is not 0.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_ObfuscatedTicketAge_NotZero};
RegisterWrapper(wrapper);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
STUB_Init();
FuncStubInfo stubInfo = {0};
STUB_Replace(&stubInfo, CompareBinder, CompareBinder_Success);
// Establish a connection.
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
STUB_Reset(&stubInfo);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
#define SESSION_SZ 10
static HITLS_Session *g_userSession[SESSION_SZ];
static int g_sessionSz = 0;
void ClearSessoins()
{
for (int i = 0; i < g_sessionSz; i++) {
HITLS_SESS_Free(g_userSession[i]);
}
g_sessionSz = 0;
}
HITLS_Session *GetSession(int index)
{
if (index < g_sessionSz) {
return HITLS_SESS_Dup(g_userSession[index]);
}
return NULL;
}
void AddSession(HITLS_Session *session)
{
if (g_sessionSz < SESSION_SZ - 1) {
g_userSession[g_sessionSz] = session;
g_sessionSz++;
}
}
int32_t Test_NewSessionCb(HITLS_Ctx *ctx, HITLS_Session *session)
{
(void)ctx;
if (ctx->isClient && HITLS_SESS_IsResumable(session)) {
AddSession(session);
return 1;
}
return 0;
}
static int32_t Test_PskUseSessionCb_WithSHA256(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
uint32_t *idLen, HITLS_Session **session)
{
(void)ctx;
(void)hashAlgo;
(void)id;
(void)idLen;
static uint8_t identity[] = "123456";
if (g_sessionSz > 0) {
*id = identity;
*idLen = sizeof(identity);
*session = GetSession(g_sessionSz - 1);
(*session)->cipherSuite = HITLS_AES_128_GCM_SHA256;
return HITLS_PSK_USE_SESSION_CB_SUCCESS;
}
return HITLS_PSK_USE_SESSION_CB_FAIL;
}
static int32_t Test_PskUseSessionCb_WithSHA384(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
uint32_t *idLen, HITLS_Session **session)
{
(void)ctx;
(void)hashAlgo;
(void)id;
(void)idLen;
static uint8_t identity[] = "123456";
if (g_sessionSz > 0) {
*id = identity;
*idLen = sizeof(identity);
*session = GetSession(g_sessionSz - 1);
(*session)->cipherSuite = HITLS_AES_256_GCM_SHA384;
return HITLS_PSK_USE_SESSION_CB_SUCCESS;
}
return HITLS_PSK_USE_SESSION_CB_FAIL;
}
static int32_t Test_PskUseSessionCb_Default(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
uint32_t *idLen, HITLS_Session **session)
{
(void)ctx;
(void)hashAlgo;
(void)id;
(void)idLen;
static uint8_t identity[] = "123456";
if (g_sessionSz > 0) {
*id = identity;
*idLen = sizeof(identity);
*session = GetSession(g_sessionSz - 1);
HITLS_Session *newSession = HITLS_SESS_New();
(*session)->cipherSuite = newSession->cipherSuite;
HITLS_SESS_Free(newSession);
return HITLS_PSK_USE_SESSION_CB_SUCCESS;
}
return HITLS_PSK_USE_SESSION_CB_FAIL;
}
static int32_t Test_PskFindSessionCb_WithSHA256(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
HITLS_Session **session)
{
(void)ctx;
(void)identity;
(void)identityLen;
if (g_sessionSz > 0) {
*session = GetSession(g_sessionSz - 1);
(*session)->cipherSuite = HITLS_AES_128_GCM_SHA256;
return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
}
return HITLS_PSK_FIND_SESSION_CB_FAIL;
}
static int32_t Test_PskFindSessionCb_WithSHA384(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
HITLS_Session **session)
{
(void)ctx;
(void)identity;
(void)identityLen;
if (g_sessionSz > 0) {
*session = GetSession(g_sessionSz - 1);
(*session)->cipherSuite = HITLS_AES_256_GCM_SHA384;
return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
}
return HITLS_PSK_FIND_SESSION_CB_FAIL;
}
static int32_t Test_PskFindSessionCb_Default(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
HITLS_Session **session)
{
(void)ctx;
(void)identity;
(void)identityLen;
if (g_sessionSz > 0) {
*session = GetSession(g_sessionSz - 1);
HITLS_Session *newSession = HITLS_SESS_New();
(*session)->cipherSuite = newSession->cipherSuite;
HITLS_SESS_Free(newSession);
return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
}
return HITLS_PSK_FIND_SESSION_CB_FAIL;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256 if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The hash settings on the client and server are inconsistent.
* @precon nan
* @brief
* 1. Preset the PSK. The client invokes HITLS_PskUseSessionCb to set the hash to sha256, and the service invokes
* HITLS_PskFindSessionCb to set the hash to sha384.
* 2. Connect establishment
* @expect
* 1. The setting is successful.
* 2. Connect establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The client invokes HITLS_PskUseSessionCb to set the hash to sha256, and the service invokes
// HITLS_PskFindSessionCb to set the hash to sha384.
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA384);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Connect establishment
ASSERT_TRUE(
FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT) == HITLS_MSG_HANDLE_PSK_INVALID);
EXIT:
ClearSessoins();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @ UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC007
* @test UT_TLS13_RFC8446_PSKHASH_TC001_1
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256. if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The hash settings on the client and server are inconsistent.
* @precon nan
* @brief
* 1. Preset the PSK. The client invokes HITLS_PskUseSessionCb to set the hash to sha384, and the service invokes
* HITLS_PskFindSessionCb to set the hash to sha256.
* 2. Connect establishment
* @expect
* 1. The setting is successful.
* 2. Connect establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC007()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The client invokes HITLS_PskUseSessionCb to set the hash to sha384, and the service invokes
// HITLS_PskFindSessionCb to set the hash to sha256.
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA384);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Connect establishment
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
ClearSessoins();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC002
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The hash set does not match the hash of the negotiated cipher suite.
* @precon nan
* @brief
* 1. Preset the PSK. The client and service monotonically use the HITLS_PskUseSessionCb to set the hash to sha256 and
* the negotiation cipher suite to sha384.
* 2. Connect establishment
* @expect
* 1. The setting is successful.
* 2. If the PSK authentication fails, perform certificate authentication.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The client and service monotonically use the HITLS_PskUseSessionCb to set the hash to sha256 and the negotiation
// cipher suite to sha384.
uint16_t cipher_suite[] = {HITLS_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Connect establishment
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearSessoins();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC003
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The hash set by the hash matches the hash of the negotiated cipher suite.
* @precon nan
* @brief
* 1. Preset the PSK. Use the HITLS_PskUseSessionCb command to set the hash to sha256 and the negotiation cipher suite to
* sha256.
* 2. Connect establishment
* @expect
* 1. The setting is successful.
* 2. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Use the HITLS_PskUseSessionCb command to set the hash to sha256 and the negotiation cipher suite to sha256.
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Connect establishment
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *ServerHello = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(ServerHello->pskSelectedIdentity.data.data == 0);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
ClearSessoins();
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC004
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256 if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The default client hash is sha256.
* @precon nan
* @brief
* 1. Preset the PSK. The client does not set the hash algorithm. The server sets the hash algorithm to 256 and the
* negotiation cipher suite to sha256.
* 2. Establish a connection and check the hash algorithm on the client.
* @expect
* 1. The setting is successful.
* 2. The connection is set up successfully, and the hash algorithm on the client is 256.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC004()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The client does not set the hash algorithm. The server sets the hash algorithm to 256 and the negotiation cipher
// suite to sha256.
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_Default);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Establish a connection and check the hash algorithm on the client.
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) != HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC005
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The default hash on the server is sha256.
* @precon nan
* @brief
* 1. Preset the PSK. The server does not set the hash algorithm. The client sets the hash algorithm to 256 and the
* negotiation cipher suite to sha256.
* 2. Establish a connection and check the hash algorithm on the server.
* @expect
* 1. The setting is successful.
* 2. The connection is set up successfully, and the hash algorithm on the server is 256.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC005()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
// Preset the PSK.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The server does not set the hash algorithm. The client sets the hash algorithm to 256 and the negotiation cipher
// suite to sha256.
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_Default);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Establish a connection and check the hash algorithm on the server.
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) != HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC006
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
* SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
* cipher suite.
* @title The hash setting is inconsistent with the negotiated cipher suite.
* @precon nan
* @brief
* 1. The client invokes the HITLS_PskClientCb interface to set the preset PSK. The server invokes the HITLS_PskClientCb
* interface to set the preset PSK.
* 2. The algorithm suite negotiation result is 384,
* 3. Establish a connection.
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The connection is successfully established and certificate authentication is performed.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC006()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
// The client invokes the HITLS_PskClientCb interface to set the preset PSK. The server invokes the HITLS_PskClientCb interface to set the preset PSK.
char psk[] = "aaaaaaaaaaaaaaaa";
ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
// The algorithm suite negotiation result is 384
uint16_t cipher_suite[] = { HITLS_AES_256_GCM_SHA384 };
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
// Establish a connection.
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec the server MUST validate the corresponding binder value (see Section 4.2.11.2 below).
* If this value is not present or does not validate, the server MUST abort the handshake.
* @title Modify the binder of client hello to make the server verification fail.
* @precon nan
* @brief
* 1. The connection is established and the session is restored.
* 2. Change the value of binder in the psk extension of the client hello message sent by the client, and observe the
* behavior on the server.
* @expect
* 1. The setting is successful.
* 2. The server terminates the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
// The connection is established and the session is restored.
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Change the value of binder in the psk extension of the client hello message sent by the client, and observe the
// behavior on the server.
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_Binder_Unnormal};
RegisterWrapper(wrapper);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_PSK_INVALID);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC003
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec the server MUST validate the corresponding binder value (see Section 4.2.11.2 below).
* If this value is not present or does not validate, the server MUST abort the handshake.
* @title Modify the client hello message so that the server fails to verify the binder.
* @precon nan
* @brief
* 1. The connection is established and the session is restored.
* 2. Discard the master_secret extension of the client hello message sent by the client and observe the behavior of the
* server.
* @expect
* 1. The setting is successful.
* 2. The server terminates the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
// The connection is established and the session is restored.
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Discard the master_secret extension of the client hello message sent by the client and observe the behavior of
// the server.
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_MasterSecret_Miss};
RegisterWrapper(wrapper);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
ASSERT_EQ(
FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_PSK_INVALID);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec Implementations MUST NOT send any records with a version less than 0x0300.
* Implementations SHOULD NOT accept any records with a version less than 0x0300
* @title The server receives a client hello message whose recode version is 0x0300/0x0200.
* @precon nan
* @brief
* 1. Change the recode version of the client hello message sent by the client to 0x0300/0x0200.
* 2. Observe the server behavior.
* @expect
* 1. The setting is successful.
* 2. Connect establishment success/failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC001(int value, int expect)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
/* Change the recode version of the client hello message sent by the client to 0x0300/0x0200. */
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
frameMsg.recVersion.data = value;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* Observe the server behavior. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), expect);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC002
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec Implementations MUST NOT send any records with a version less than 0x0300.
* Implementations SHOULD NOT accept any records with a version less than 0x0300
* @title The client receives the server hello message whose recode version is 0x0300/0x0200.
* @precon nan
* @brief
* 1. Change the recode version of the client hello message sent by the server to 0x0300/0x0200.
* 2. Observe client behavior.
* @expect
* 1. The setting is successful.
* 2. Connect establishment success/failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC002(int value, int expect)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
/* Change the recode version of the client hello message sent by the server to 0x0300/0x0200. */
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
frameMsg.recVersion.data = value;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
/* Observe client behavior. */
ASSERT_TRUE(testInfo.server->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), expect);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec
* @title Two PSKs. Select the first one when the conditions are met.
* @precon nan
* @brief
* 1. The PSK is generated during connection establishment, and set to the client.
* 2. Preset the PSK and establish a connection.
* @expect
* 1. The setting is successful.
* 2. The connection is successfully set up and the first PSK is selected.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// The PSK is generated during connection establishment, and set to the client.
HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Preset the PSK and establish a connection. */
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 0);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_SESS_Free(testInfo.clientSession);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC002
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec
* @title Two psks. If the former one does not meet the requirements, select the second one.
* @precon nan
* @brief
* 1. The PSK is generated during connection establishment. Configure the default 384 algorithm suite on the client.
* 2. Set the 256 cipher suite, preset the PSK, and establish a connection.
* @expect
* 1. The setting is successful.
* 2. The connection is successfully established and the second cipher suite is selected.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
// The PSK is generated during connection establishment. Configure the default 384 algorithm suite on the client.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
/* Set the 256 cipher suite, preset the PSK, and establish a connection. */
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256, HITLS_AES_256_GCM_SHA384 };
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite[0])/sizeof(uint16_t));
HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 1);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_SESS_Free(testInfo.clientSession);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC003
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec
* @title Neither of the two PSKs meets the requirements. Select certificate authentication.
* @precon nan
* @brief
* 1. Set the 256 algorithm suite and set up a connection to generate a PSK, and set to the client.
* 2. Set the 384 algorithm suite, preset the PSK, and establish a connection.
* @expect
* 1. The setting is successful.
* 2. The connection is successfully established and the PSK authentication is performed.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
// Set the 256 algorithm suite and set up a connection to generate a PSK, and set to the client.
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Set the 384 algorithm suite, preset the PSK, and establish a connection.
cipher_suite[0] = HITLS_AES_256_GCM_SHA384;
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
/* Obtain the message buffer. */
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
/* Parse the structure to the msg structure. */
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data == 0);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_SESS_Free(testInfo.clientSession);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC004
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec
* @title Trigger the hrr message and select the PSK.
* @precon nan
* @brief
* 1. The PSK is generated during connection establishment, and set on the client.
* 2. Set a group so that the server triggers the hrr message, preset the PSK, and establish a connection.
* @expect
* 1. The setting is successful.
* 2. The connection is successfully set up. The server sends the hrr message and selects the PSK.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC004()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
// The PSK is generated during connection establishment, and set on the client.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
// Set a group so that the server triggers the hrr message, preset the PSK, and establish a connection.
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.c_config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.s_config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(testInfo.server->ssl->hsCtx->haveHrr == true);
serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 0);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(testInfo.client->io);
uint32_t sendLen = ioUserData2->sndMsg.len;
ASSERT_TRUE(sendLen != 0);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_SESS_Free(testInfo.clientSession);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC005
* @brief 4.2.11-Pre-Shared Key Extension-94
* @spec
* @title Compatibility between session restoration and user-configured PSK
* @precon nan
* @brief
1. create a tls1.3 connection
2. Configure the user-defined PSK through the callback function.
3. Ensure that the PSK length configured by the user is greater than the PSK length for session restoration.
4. Establish a link again.
* @expect
1. The connection is successful.
2. Configuration succeeded.
3. Configuration succeeded.
4. The connection is successful.
* @prior Level 2
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC005()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.c_config = HITLS_CFG_NewTLS13Config();
testInfo.s_config = HITLS_CFG_NewTLS13Config();
// The PSK is generated during link establishment. Configure the default 384 algorithm suite on the client.
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
/* Set the 256 cipher suite, preset the PSK, and establish a link. */
uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256, HITLS_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite[0])/sizeof(uint16_t));
ExampleSetPsk("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 1);
ASSERT_TRUE(testInfo.client->ssl != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_SESS_Free(testInfo.clientSession);
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* During the TLS1.3 HRR handshaking, application messages can not be received*/
/* BEGIN_CASE */
void UT_TLS13_RFC8446_HRR_APP_RECV_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLSConfig();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
/* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
by serverhello and hrr are different, */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint32_t sendLenapp = 7;
uint8_t sendBufapp[7] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x05, 0x05};
uint32_t writeLen;
BSL_UIO_Write(clientTlsCtx->uio, sendBufapp, sendLenapp, &writeLen);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS1_3_RFC8446_Legacy_Version_TC001
* @spec For TLS 1.3, the legacy_record_version set to 0x0403 to client will get alert
* @title For TLS 1.3, the legacy_record_version set to 0x0403 to client will get alert
* @precon nan
* @brief 5.1. Record Layer line 190
* legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
@ */
/* BEGIN_CASE */
void UT_TLS1_3_RFC8446_Legacy_Version_TC001(int statehs)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
/* Configure the server to support only the non-default curve. The server sends the HRR message. */
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, statehs) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.msg[1] = 0x04u;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
if (statehs == TRY_RECV_SERVER_HELLO) {
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
} else {
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS1_3_RFC8446_Legacy_Version_TC002
* @spec For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
* @title For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
* @precon nan
* @brief 5.1. Record Layer line 190
* legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
@ */
/* BEGIN_CASE */
void UT_TLS1_3_RFC8446_Legacy_Version_TC002(int statehs)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
/* Configure the server to support only the non-default curve. The server sends the HRR message. */
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, statehs) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(server->io);
ioClientData->recMsg.msg[1] = 0x04u;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
if (statehs == TRY_RECV_CLIENT_HELLO) {
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
} else {
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_PROCESS_TC001
* @spec -
* @title During connection establishment, tls13 server receives a warning alert, the connection state change to alerted
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1.
* 2. Initiate a connection, keep the connection status in the receive_client_key_exchange state,
and simulate the scenario where the server receives a warning alert message. (Expected result 2)
* @expect 1. Complete initialization.
* 2. the connection state of server change to alerted
* @prior Level 2
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_PROCESS_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = false;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint8_t alertMsg[2] = {ALERT_LEVEL_WARNING, ALERT_NO_RENEGOTIATION};
ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_ALERT, alertMsg, sizeof(alertMsg)), HITLS_SUCCESS);
// clear the certificate verify in the cache
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_ALERT, alertMsg, sizeof(alertMsg)), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_HELLO_REQUEST_TC001
* @spec -
* @title Send a hello request when the link status is CM_STATE_IDLE.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a HelloRequest message and send it to the client. The client invokes the HITLS_Connect interface
to receive the message. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the HelloRequest message, the client ignores the message and stays in the
TRY_RECV_SERVER_HELLO state after sending the ClientHello message.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_HELLO_REQUEST_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
// Construct a HelloRequest message and send it to the client.
uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
size_t len = HS_MSG_HEADER_SIZE;
REC_Write(server->ssl, REC_TYPE_HANDSHAKE, buf, len);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl != NULL);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(client->ssl->hsCtx->state == TRY_RECV_SERVER_HELLO);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
HITLS_KeyExchAlgo keyExType)
{
frametype->versionType = versionType;
frametype->recordType = recordType;
frametype->handshakeType = handshakeType;
frametype->keyExType = keyExType;
frametype->transportType = BSL_UIO_TCP;
}
/** @
* @test UT_TLS_TLS13_RFC8446_MODIFIED_SESSID_FROM_SH_TC002
* @spec -
* @title Send a empty session id to client.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a server hello message with empty session id and send it to the client.
Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the HelloRequest message, the client send a ILLEGAL_PARAMETER alert.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_MODIFIED_SESSID_FROM_SH_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType = {0};
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
shMsg->sessionId.size = 0;
shMsg->sessionId.state = MISSING_FIELD;
shMsg->sessionIdSize.data = 0;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_MUTI_CCS_TC001
* @spec -
* @title IN TLS1.3, mutiple ccs can be received.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct a ChangeCipherSpec message and send it to the client five times. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. return HITLS_REC_NORMAL_RECV_BUF_EMPTY.
* @prior Level 1
* @auto TRUE
@ */
/* IN TLS1.3, mutiple ccs can be received*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_MUTI_CCS_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint32_t sendLenccs = 6;
uint8_t sendBufccs[6] = {0x14, 0x03, 0x03, 0x00, 0x01, 0x01};
uint32_t writeLen;
for (int i = 0; i < 5; i++) {
BSL_UIO_Write(serverTlsCtx->uio, sendBufccs, sendLenccs, &writeLen);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
}
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t SendCcs(HITLS_Ctx *ctx, uint8_t *data, uint8_t len)
{
/** Write records. */
int32_t ret = REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, data, len);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
uint8_t isFlightTransmitEnable;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
if (ret == BSL_UIO_IO_BUSY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
if (ret != BSL_SUCCESS) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
}
return HITLS_SUCCESS;
}
static int32_t SendAlert(HITLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
uint8_t data[ALERT_BODY_LEN];
/** Obtain the alert level. */
data[0] = level;
data[1] = description;
/** Write records. */
int32_t ret = REC_Write(ctx, REC_TYPE_ALERT, data, ALERT_BODY_LEN);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
uint8_t isFlightTransmitEnable;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
if (ret == BSL_UIO_IO_BUSY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
if (ret != BSL_SUCCESS) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
}
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_RECEIVES_ENCRYPTED_CCS_TC001
* @spec -
* @title The encrypted CCS is received when the plaintext CCS is received.
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct encrypted CCS and send it to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the CCS message, the client send a UNEXPECTED_MESSAGE alert.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_RECEIVES_ENCRYPTED_CCS_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
// Sends serverhello to the peer end.
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
// Processing serverhello
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
serverTlsCtx->recCtx->outBuf->end = 0;
uint32_t hashLen = SAL_CRYPT_DigestSize(serverTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
ASSERT_EQ(HS_SwitchTrafficKey(serverTlsCtx, serverTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true),
HITLS_SUCCESS);
uint8_t data = 1;
// send crypto ccs
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->sndMsg.msg[0] = REC_TYPE_APP;
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
// process crypto ccs
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
#define ALERT_UNKNOWN_DESCRIPTION 254
/** @
* @test UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_UNKNOWN_DESCRIPTION_TC001
* @spec -
* @title RFC8446 6.2 All alerts defined below in this section, as well as all unknown alerts,
are universally considered fatal as of TLS 1.3 (see Section 6).
* @precon nan
* @brief 1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
* 2. Construct alert message with alert level warning and ALERT_UNKNOWN_DESCRIPTION, and send it to the server.
Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. After receiving the alert message, the server send a FATAL alert.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_UNKNOWN_DESCRIPTION_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportClientVerify = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(SendAlert(client->ssl, ALERT_LEVEL_WARNING, ALERT_UNKNOWN_DESCRIPTION) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
HITLS_Ctx *Ctx = FRAME_GetTlsCtx(server);
ALERT_Info alert = { 0 };
ALERT_GetInfo(Ctx, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_UNKNOWN_DESCRIPTION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test SDV_TLS13_RFC8446_REQUEST_CLIENT_HELLO_TC008
* @brief 2.1. Incorrect DHE Share
* @spec If the client has not provided a sufficient "key_share" extension (e.g., it includes only DHE or ECDHE groups
unacceptable to or unsupported by the server), the server corrects the mismatch with a HelloRetryRequest and the
client needs to restart the handshake with an appropriate "key_share" extension, as shown in Figure 2. If no common
cryptographic parameters can be negotiated, the server MUST abort the handshake with an appropriate alert.
* @title Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
server. Observe the link setup result and check whether the server sends Hello_Retry_Requset.
* @precon nan
* @brief
1. Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
server.
* @expect
1. Send clienthello with X25519 keyshare. The link is established successfully.
2. The server does not send Hello_Retry_Requset.
* @prior Level 2
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_REQUEST_CLIENT_HELLO_TC001()
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *clientconfig = NULL;
HITLS_Config *serverconfig = NULL;
clientconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(clientconfig != NULL);
serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
uint16_t clientgroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
uint16_t servergroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recBuf = ioUserData->recMsg.msg;
uint32_t recLen = ioUserData->recMsg.len;
ASSERT_TRUE(recLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = SERVER_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverHello = &frameMsg.body.hsMsg.body.serverHello;
ASSERT_TRUE(serverHello->keyShare.data.group.data == HITLS_EC_GROUP_CURVE25519);
ASSERT_EQ(server->ssl->hsCtx->haveHrr, false);
// Continue to establish the link.
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_Legacy_Version_TC001
* @spec For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
* @title For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
* @precon nan
* @brief 5.1. Record Layer line 190
* legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
@ */
/* BEGIN_CASE */
void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_Legacy_Version_TC001(int statehs)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
/* Configure the server to support only the non-default curve. The server sends the HRR message. */
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, statehs) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(server->io);
ioClientData->recMsg.msg[1] = 0x04u;
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
if (statehs == TRY_RECV_CLIENT_HELLO) {
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
} else {
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_MIDDLE_BOX_COMPAT_TC001
* @spec -
* @title Test TLS 1.3 connection and data transfer when middlebox compatibility mode is enabled or disabled
* @precon nan
* @brief 1. Initialize the client and server using the default configuration. Expected result 1.
* 2. Set the middlebox compatibility mode for both the client and server based on the isMiddleBoxCompat parameter.
Expected result 2.
* 3. Stop the server state at TRY_RECV_CLIENT_HELLO and determine the length of the session ID at this time.
Expected result 3.
* 4. The server calls the HITLS_Accept function and then determines the next state of the server. Expected result 4.
* 5. Continue to establish a connection. Expected result 5.
* @expect 1 Initialization succeeded.
* 2. Successful setup.
* 3. If isMiddleBoxCompat is true, then the session ID length is 32, otherwise it is 0
* 4. If isMiddleBoxCompat is true, the next state of the server is TRY_SEND_CHANGE_CIPHER_SPEC,
otherwise it is TRY_SEND_ENCRYPTED_EXTENSIONS.
* 5. The connection between the client and server is successfully established.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_MIDDLE_BOX_COMPAT_TC001(int isMiddleBoxCompat)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_CFG_SetMiddleBoxCompat(tlsConfig, isMiddleBoxCompat);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
uint32_t parseLen = 0;
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
if (isMiddleBoxCompat) {
ASSERT_TRUE(clientMsg->sessionIdSize.data == TLS_HS_MAX_SESSION_ID_SIZE);
ASSERT_TRUE(clientMsg->sessionId.data != NULL);
(void)HITLS_Accept(server->ssl);
ASSERT_EQ(server->ssl->hsCtx->state, TRY_SEND_CHANGE_CIPHER_SPEC);
} else {
ASSERT_TRUE(clientMsg->sessionIdSize.data == 0);
ASSERT_TRUE(clientMsg->sessionId.data == NULL);
(void)HITLS_Accept(server->ssl);
ASSERT_EQ(server->ssl->hsCtx->state, TRY_SEND_ENCRYPTED_EXTENSIONS);
}
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_2.c | C | unknown | 145,248 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "rec_wrapper.h"
#include "alert.h"
#include "hitls_crypt_init.h"
#include "common_func.h"
#include "securec.h"
#include "hitls_error.h"
#include "hs.h"
#include "stub_replace.h"
#include "frame_tls.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_link.h"
#include "hlt.h"
#define UT_TIMEOUT 3
/* END_HEADER */
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} HsTestInfo;
int32_t NewConfig(HsTestInfo *testInfo)
{
switch (testInfo->version) {
case HITLS_VERSION_DTLS12:
testInfo->config = HITLS_CFG_NewDTLS12Config();
break;
case HITLS_VERSION_TLS13:
testInfo->config = HITLS_CFG_NewTLS13Config();
break;
case HITLS_VERSION_TLS12:
testInfo->config = HITLS_CFG_NewTLS12Config();
break;
default:
break;
}
if (testInfo->config == NULL || testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetClientVerifySupport(testInfo->config, true);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo->config, true);
HITLS_CFG_SetNoClientCertSupport(testInfo->config, true);
HITLS_CFG_SetRenegotiationSupport(testInfo->config, true);
HITLS_CFG_SetPskServerCallback(testInfo->config, (HITLS_PskServerCb)ExampleServerCb);
return HITLS_SUCCESS;
}
static int32_t DoHandshake(HsTestInfo *testInfo)
{
/* Construct a connection. */
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_OBSOLETE_RESERVED_FUNC_TC001
* @spec -
* @title Expired signature algorithm
* @precon nan
* @brief 1. Initialize the client server to tls1.3 and construct the scenario where the client uses all the
obsolete_RESERVED signature algorithms in polling mode, Expected result: The connection fails to be
established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBSOLETE_RESERVED_FUNC_TC001(int signAlg)
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
HITLS_CFG_SetSignature(testInfo.config, (uint16_t *)&signAlg, 1);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_OBSOLETE_RESERVED_FUNC_TC002
* @spec - tls1.3 Expired group algorithm OBSOLETE_RESERVED test
* @title
* @precon nan
* @brief 1. Initialize the client and server to tls1.3 and construct the scenario where the client uses all the
obsolete_RESERVED group algorithms in polling mode, Expected result: The connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBSOLETE_RESERVED_FUNC_TC002(int group)
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
HITLS_CFG_SetGroups(testInfo.config, (uint16_t *)&group, 1);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_ServerHelloNoSupportedVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC001
* @spec -
* @title server hello really necessary extensions
* @precon nan
* @brief 1. If the server sends a HelloRetryRequest message without supported_versions, the connection fails to be
established and the client generates an alarm.
Expected result: The connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC001()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_HELLO_RETRY_REQUEST, REC_TYPE_HANDSHAKE, false, NULL,
Test_ServerHelloNoSupportedVersion };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC002
* @spec -
* @title server hello really necessary extension
* @precon nan
* @brief 1. In certificate authentication, the first connection is established. If the ServerHello message sent by the
Server does not contain the signature_algorithms field, the connection fails to be established and the client generates
an alarm.
Expected result: The connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC002()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerHelloNoSupportedVersion };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_ResumeServerHelloNoSupportedVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
if (ctx->session == NULL) {
return;
}
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_ResumeClientHelloNoSupportedVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
if (ctx->session == NULL) {
return;
}
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_ClientHelloNoSupportedVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC003
* @spec -
* @title server Hello message extension is missing.
* @precon nan
* @brief Certificate authentication, session recovery, the Server sends the ServerHello message without the
signature_algorithms, the session recovery fails, and the client generates an alarm.
* Expected result: connect establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC003()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = { TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL,
Test_ResumeServerHelloNoSupportedVersion };
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
ClearWrapper();
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC004
* @spec -
* @title client hello is missing the necessary extension.
* @precon nan
* @brief 1. In certificate authentication, the first connection is established. If the clientHello message sent by the
* client does not contain signature_algorithms, the connection fails to be established and the server
* generates an alarm. Expected result: The connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC004()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloNoSupportedVersion };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC005
* @spec -
* @title client Hello packet extension is missing.
* @precon nan
* @brief Certificate authentication, session recovery. If the client sends a clientHello message without
* signature_algorithms, the session recovery fails and the server generates an alarm. *Expected result: The
* connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC005()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
HITLS_CFG_SetSessionTimeout(testInfo.config, UT_TIMEOUT);
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ResumeClientHelloNoSupportedVersion};
RegisterWrapper(wrapper);
/* First handshake */
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
ClearWrapper();
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ClientHello2NoSupportedVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
if (!ctx->hsCtx->haveHrr) {
return;
}
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC006
* @spec -
* @title client hello is missing the necessary extension.
* @precon nan
* @brief 1. After receiving the HelloRetryRequest message, the client sends the second ClientHello message. If the
* client does not have the signature_algorithms message, the connection fails to be established and the server
* generates an alarm. Expected result: The connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC006()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL,
Test_ClientHello2NoSupportedVersion };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_Client2HelloNoSupportedGroup(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
if (!ctx->hsCtx->haveHrr) {
return;
}
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_ClientHelloNoSupportedGroup(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC007
* @spec -
* @title client hello is missing the necessary extension.
* @precon nan
* @brief 1. During DHE key exchange, if the ClientHello does not contain "supported_groups", the connection fails to be
established and the server generates an alarm. Expected result: The connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC007()
{
FRAME_Init();
HsTestInfo testInfo = { 0 };
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloNoSupportedGroup };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_MISSING_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC008
* @spec -
* @title client hello really necessary extension
* @precon nan
* @brief 1. After the client receives the HelloRetryRequest message, the client sends the second ClientHello message
without the supported_groups field. In this case, the connection fails to be established and the server
generates an alarm.
Expected result: The connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NECESSARY_EXTENSION_FUNC_TC008()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = { TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client2HelloNoSupportedGroup };
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_MISSING_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_appendix.c | C | unknown | 24,714 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "rec.h"
#include "parse.h"
#include "hs_msg.h"
#include "alert.h"
#include "hitls_crypt_init.h"
#include "common_func.h"
/* END_HEADER */
#define g_uiPort 2987
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} HsTestInfo;
int32_t NewConfig(HsTestInfo *testInfo)
{
/* Construct the configuration.*/
switch (testInfo->version) {
case HITLS_VERSION_DTLS12:
testInfo->config = HITLS_CFG_NewDTLS12Config();
break;
case HITLS_VERSION_TLS13:
testInfo->config = HITLS_CFG_NewTLS13Config();
break;
case HITLS_VERSION_TLS12:
testInfo->config = HITLS_CFG_NewTLS12Config();
break;
default:
break;
}
if (testInfo->config == NULL || testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetClientVerifySupport(testInfo->config, true);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo->config, true);
HITLS_CFG_SetNoClientCertSupport(testInfo->config, true);
HITLS_CFG_SetRenegotiationSupport(testInfo->config, true);
HITLS_CFG_SetPskServerCallback(testInfo->config, (HITLS_PskServerCb)ExampleServerCb);
return HITLS_SUCCESS;
}
static int32_t DoHandshake(HsTestInfo *testInfo)
{
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
}
static void Test_Cert_len0(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
FRAME_CertificateMsg *certifiMsg = &frameMsg.body.hsMsg.body.certificate;
certifiMsg->certsLen.state = ASSIGNED_FIELD;
certifiMsg->certsLen.data = 0;
certifiMsg->certificateReqCtxSize.state = ASSIGNED_FIELD;
certifiMsg->certificateReqCtxSize.data = 0;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_CertPackAndParse(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
if (frameMsg.body.hsMsg.body.certificate.certificateReqCtx.data == NULL) {
frameMsg.body.hsMsg.body.certificate.certificateReqCtxSize.data = 1;
frameMsg.body.hsMsg.body.certificate.certificateReqCtxSize.state = INITIAL_FIELD;
uint8_t *cerReqData = BSL_SAL_Calloc(1, 1);
frameMsg.body.hsMsg.body.certificate.certificateReqCtx.data = cerReqData;
frameMsg.body.hsMsg.body.certificate.certificateReqCtx.size = 1;
frameMsg.body.hsMsg.body.certificate.certificateReqCtx.state = INITIAL_FIELD;
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC003
* @spec -
* @titleThe client receives a Certificate message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP connection request. When the client receives the request from the server,
the client receives the request from the server. After receiving the Hello message, the server constructs a
Certificate message with zero length and sends the message to the client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_ LEVEL_FATAL and description of
* ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC003(void)
{
FRAME_Init();
HsTestInfo testInfo = {0};
/* 1. Use the default configuration items to configure the client and server. */
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
testInfo.config->isSupportNoClientCert = false;
testInfo.config->isSupportClientVerify = true;
/* 2. The client initiates a TLS over TCP connection request. When the client receives the request from the server,
* the client receives the request from the server. After receiving the Hello message, the server constructs a
* Certificate message with zero length and sends the message to the client. */
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, &wrapper, Test_Cert_len0};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_EE_len0(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, ENCRYPTED_EXTENSIONS);
memset_s(data, bufSize, 0, bufSize);
if (ctx->isClient) {
data[0] = ENCRYPTED_EXTENSIONS;
data[1] = 0X00;
data[2] = 0X00;
data[3] = 0X00;
}
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC004
* @spec -
* @title The client receives an EE message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP connection request. After receiving the client Hello message, the
* client constructs a certificate with zero length. Send the request message to the client. Expected result 2
* is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_ LEVEL_FATAL and description
ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC004(void)
{
FRAME_Init();
HsTestInfo testInfo = {0};
/* 1. Use the default configuration items to configure the client and server. */
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
testInfo.config->isSupportNoClientCert = false;
testInfo.config->isSupportClientVerify = true;
/* 2. The client initiates a TLS over TCP connection request. After receiving the CH message, the client
* constructs a EE with zero length. Send the request message to the client. */
RecWrapper wrapper = {TRY_RECV_ENCRYPTED_EXTENSIONS, REC_TYPE_HANDSHAKE, true, &wrapper, Test_EE_len0};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC008
* @title The client receives an key update message with zero length.
* @brief The client receives an key update message with zero length. Expect result 1
* @expect
1. The client sends an ALERT message with the level of ALERT_ LEVEL_FATAL and description ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC008(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
uint8_t keyUpdateMsg[] = {KEY_UPDATE, 0, 0, 0};
ASSERT_TRUE(REC_Write(serverTlsCtx, REC_TYPE_HANDSHAKE, keyUpdateMsg, sizeof(keyUpdateMsg)) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen) == HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(clientTlsCtx, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_Cert_verify_len0(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_VERIFY);
FRAME_CertificateVerifyMsg *CertveriMsg = &frameMsg.body.hsMsg.body.certificateVerify;
CertveriMsg->signSize.data = 0;
CertveriMsg->signSize.state = ASSIGNED_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC005
* @spec -
* @titleThe client receives a Certificate verify message whose length is zero.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP connection request. After receiving the client Hello message, the
* client constructs a certificate with zero length. Send the verify message to the client. Expected result
* 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message with the level of ALERT_ LEVEL_FATAL and description
ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC005(void)
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
testInfo.config->isSupportNoClientCert = false;
testInfo.config->isSupportClientVerify = true;
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_VERIFY, REC_TYPE_HANDSHAKE, false, &wrapper, Test_Cert_verify_len0};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_finished_len0(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
FRAME_FinishedMsg *FinishedMsg = &frameMsg.body.hsMsg.body.finished;
FinishedMsg->verifyData.size = 0;
FinishedMsg->verifyData.state = ASSIGNED_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC006
* @spec -
* @titleThe client receives a finished message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP connection request and receives the request from the client.
After the Hello message is sent, construct a finished message with zero length and send the message to the
client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends an ALERT message. The level is ALERT_Level_FATAL and the description is
ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC006(void)
{
FRAME_Init();
HsTestInfo testInfo = {0};
/* 1. Use the default configuration items to configure the client and server. */
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
testInfo.config->isSupportNoClientCert = false;
testInfo.config->isSupportClientVerify = true;
/* 2. The client initiates a TLS over TCP connection request and receives the request from the client.
* After the Hello message is sent, construct a finished message with zero length and send the message to the
* client. */
RecWrapper wrapper = {TRY_SEND_FINISH, REC_TYPE_HANDSHAKE, false, &wrapper, Test_finished_len0};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_NewSessionTicket_len0(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, NEW_SESSION_TICKET);
FRAME_NewSessionTicketMsg *newsessionTMsg = &frameMsg.body.hsMsg.body.newSessionTicket;
newsessionTMsg->ticketSize.data = 0;
newsessionTMsg->ticketSize.state = ASSIGNED_FIELD;
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC007
* @spec -
* @titleThe client receives a NewSessionTicket message with zero length.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. Expected result 1 is obtained.
* 2. The client initiates a TLS over TCP connection request and receives the request from the client.
After receiving the Hello message, construct a New SessionTicket message with zero length and send it to the
client. Expected result 2 is obtained.
* @expect 1. The initialization is successful.
* 2. The client sends the ALERT message. The level is ALERT_ LEVEL_FATAL and the description is
ALERT_DECODE_ERROR.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_ZEROLENGTH_MSG_FUNC_TC007(void)
{
FRAME_Init();
HsTestInfo testInfo = {0};
/* 1. Use the default configuration items to configure the client and server. */
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
testInfo.config->isSupportNoClientCert = false;
testInfo.config->isSupportClientVerify = true;
/* 2. The client initiates a TLS over TCP connection request and receives the request from the client.
* After receiving the Hello message, construct a New SessionTicket message with zero length and send it to
* the client. */
RecWrapper wrapper = {TRY_SEND_NEW_SESSION_TICKET, REC_TYPE_HANDSHAKE, false, &wrapper, Test_NewSessionTicket_len0};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC001
* @spec -
* @title Abnormal CertMsg packet
* @precon nan
* @brief 1. Enable the dual-end verification. Change the value of certificate_request_context in the certificate message
sent by the client to a value other than 0. Expected result 1 is obtained.
Result 1: The server sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC001()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_CertPackAndParse};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertReqAbCtxLen(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
if (frameMsg.body.hsMsg.body.certificateReq.certificateReqCtx.data == NULL) {
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_REQUEST);
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtxSize.data = 1;
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtxSize.state = INITIAL_FIELD;
uint8_t *cerReqData = BSL_SAL_Calloc(1, 1);
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtx.data = cerReqData;
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtx.size = 1;
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtx.state = INITIAL_FIELD;
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC001
* @spec -
* @title Abnormal CertReqMsg message
* @precon nan
* @brief 1. Enable the dual-end check. Change the value of certificate_request_context in the certificate_request
* message sent by the server to a value other than 0. Expected result 1 is obtained. Result 1: The server
* sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC001()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqAbCtxLen};
RegisterWrapper(wrapper);
/* Handshake */
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_NO_CERT_FUNC_TC001
* @spec A Finished message MUST be sent regardless of whether the Certificate message is empty.
* @title During the normal handshake, the peer certificate can be empty, the certificate message sent by the client is
* empty, and the certificate message sent by the server is not empty. The handshake is successful. The finished
* message is sent after the certificate message and the content is correct.
* @precon nan
* @brief 4.4.2. Certificate row114
1. Enable the dual-end verification, allow the peer certificate to be empty, set the client certificate to
be empty, and establish a connection.
* @expect 1. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_NO_CERT_FUNC_TC001(int isSupportNoClientCert)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
0,
0,
0,
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = (bool)isSupportNoClientCert;
if (config->isSupportNoClientCert) {
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
} else {
client = FRAME_CreateLink(config, BSL_UIO_TCP);
}
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC002
* @spec
1. If the client does not set the root certificate but the server sets the complete certificate chain, the client
fails to construct the certificate chain, and the handshake fails and the unsupported_certificate alarm is reported.
By default, the preceding alarm is generated. In the current code, the alarm is generated as bad_certificate.
* @brief If the client cannot construct an acceptable chain using the provided
* certificates and decides to abort the handshake, then it MUST abort the
* handshake with an appropriate certificate-related alert
* (by default, "unsupported_certificate"; see Section 6.2 for more information).
* 4.4.2 Certificate row 136
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
0,
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC003
* @spec
1. The root certificate is configured on the client. Incomplete certificate chain is configured on both the client
and server. If the server fails to construct the certificate chain, the handshake fails and the unsupported_certificate
alarm is reported. By default, the alarm is the preceding alarm. In the current code, the alarm is the
bad_certificate alarm.
* @brief If the client cannot construct an acceptable chain using the provided
* certificates and decides to abort the handshake, then it MUST abort the
* handshake with an appropriate certificate-related alert
* (by default, "unsupported_certificate"; see Section 6.2 for more information).
* 4.4.2 Certificate row 136
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der",
0,
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC004
* @spec
1. If the client sets an incorrect root certificate and the server sets a complete certificate chain, the client
fails to construct the certificate chain, and the handshake fails and the unsupported_certificate alarm is reported.
By default, the preceding alarm is generated. In the current code, the alarm is generated as bad_certificate.
* @brief If the client cannot construct an acceptable chain using the provided
* certificates and decides to abort the handshake, then it MUST abort the
* handshake with an appropriate certificate-related alert
* (by default, "unsupported_certificate"; see Section 6.2 for more information).
* 4.4.2 Certificate row 136
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"rsa_sha/ca-3072.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC005
* @spec
1. If the client does not match a proper algorithm and fails to construct a certificate chain, the handshake fails
and the unsupported_certificate alarm is reported. By default, the preceding alarm is generated. In the current code,
the HANDSHAKE_FAILURE alarm is generated.
* @brief If the client cannot construct an acceptable chain using the provided
* certificates and decides to abort the handshake, then it MUST abort the
* handshake with an appropriate certificate-related alert
* (by default, "unsupported_certificate"; see Section 6.2 for more information).
* 4.4.2 Certificate row 136
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTCHAIN_FUNC_TC005(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t serverSignAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
uint16_t clientSignAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetSignature(&client->ssl->config.tlsConfig, clientSignAlgs, sizeof(clientSignAlgs) / sizeof(uint16_t));
HITLS_CFG_SetSignature(&server->ssl->config.tlsConfig, serverSignAlgs, sizeof(serverSignAlgs) / sizeof(uint16_t));
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *sndBuf = ioUserData->sndMsg.msg;
uint32_t sndLen = ioUserData->sndMsg.len;
ASSERT_TRUE(sndLen != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {0};
FRAME_Type frameType = {0};
frameType.recordType = REC_TYPE_ALERT;
ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_HANDSHAKE_FAILURE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_CertReqPackAndParseNoEx(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_REQUEST);
frameMsg.body.hsMsg.body.certificateReq.signatureAlgorithmsSize.data = 0;
frameMsg.body.hsMsg.body.certificateReq.signatureAlgorithmsSize.state = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC002
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Enable the dual-end verification. Change the value of certificate_request_context in the certificate_request
message sent by the server to a value other than 0. Expected result 1 is obtained. Result 1: The server sends an alert
message and disconnects the connection.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC002()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqPackAndParseNoEx};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_PARSE_INVALID_MSG_LEN);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertReqPackAndParseNoSign(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
// The eighth digit indicates the type of the first extension. Change the type of the first extension to key share.
*(uint16_t *)(data + 8) = HS_EX_TYPE_KEY_SHARE;
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC003
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Enable the dual-end verification, set the extension field in the server certificate request to exclude the
signature algorithm, and establish a connection.
Expected result: The connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC003()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqPackAndParseNoSign};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertReqPackAndParseUnknownEx(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize,
void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_REQUEST);
frameMsg.body.hsMsg.body.certificateReq.signatureAlgorithms.state = DUPLICATE_FIELD;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
*(uint16_t *)(data + 8) = HS_EX_TYPE_KEY_SHARE;
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC004
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Enable the dual-end verification, set the extension field in the server certificate request to exclude
* the signature algorithm, and establish a connection. Expected result: The connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC004()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqPackAndParseUnknownEx};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC005
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Initialize the client and server to tls1.3, and construct the scenario where the client and server sends an
alert message after receiving the hrr message and serverhello message, Expected Alert Message Encryption
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC005()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqAbCtxLen};
RegisterWrapper(wrapper);
ASSERT_EQ(
FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_CERTIFICATE_REQUEST), HITLS_SUCCESS);
HITLS_Connect(testInfo.client->ssl);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
uint8_t *buffer = ioUserData->sndMsg.msg;
uint32_t len = ioUserData->sndMsg.len;
ASSERT_TRUE(len != 0);
uint32_t parseLen = 0;
FRAME_Msg frameMsg = {};
ASSERT_TRUE(ParserTotalRecord(testInfo.client, &frameMsg, buffer, len, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.type, REC_TYPE_ALERT);
ASSERT_EQ(frameMsg.body.alertMsg.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_EmptyCertMsg(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
FrameCertItem *certItem = frameMsg.body.hsMsg.body.certificate.certItem;
frameMsg.body.hsMsg.body.certificate.certItem = NULL;
while (certItem != NULL) {
FrameCertItem *temp = certItem->next;
BSL_SAL_FREE(certItem->cert.data);
BSL_SAL_FREE(certItem->extension.data);
BSL_SAL_FREE(certItem);
certItem = temp;
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC002
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Enable the dual-end verification. If the certificate message does not contain the certificate, the connection
is established.
Expected result: A decode error is returned when the connection fails to be established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC002()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_EmptyCertMsg};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC003
* @spec -
* @title Abnormal CertReqMsg packet
* @precon nan
* @brief 1. Enable the single-end authentication. If the certificate message does not contain the certificate,
establish a connection.
Expected result: Decode error is returned when the connection fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTMSG_FUNC_TC003()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetClientVerifySupport(testInfo.config, false), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_EmptyCertMsg};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertReqPackAndParse(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_REQUEST);
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC000
* @spec -
* @title Verify the parsing and packaging functions of the cerreqmsg test framework.
* @precon nan
* @brief 1. Enable the dual-end check. Change the value of certificate_request_context in the certificate_request
message sent by the server to a value other than 0.
Expected result 1 is obtained. Result 1: The server sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ABNORMAL_CERTREQMSG_FUNC_TC000()
{
FRAME_Init();
HsTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
ASSERT_EQ(NewConfig(&testInfo), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertReqPackAndParse};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SIGN_ERR_FUNC_TC001
* @spec
* 1. Set the client server to tls1.3 and the client signature algorithm to ecdsa-sha1. The expected connection
* establishment fails.
* @brief If the server cannot produce a certificate chain that is signed only via the indicated
* supported algorithms, then it SHOULD continue the handshake by sending the client a certificate
* chain of its choice that may include algorithms that are not known to be supported by the client.
* This fallback chain SHOULD NOT use the deprecated SHA-1 hash algorithm in general, but MAY do so
* if the client' s advertisement permits it, and MUST NOT do so otherwise.
* 4.4.2.2. Server Certificate Selection row 135
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SIGN_ERR_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo serverCertInfo = {
"ecdsa_sha1/ca-nist521.der",
"ecdsa_sha1/inter-nist521.der",
"ecdsa_sha1/end384-sha1.der",
0,
"ecdsa_sha1/end384-sha1.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SHA1};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &serverCertInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &serverCertInfo);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH);
ALERT_Info alert = {0};
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_INTERNAL_ERROR);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t GetDisorderServerEEMsg(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void)HITLS_Accept(server->ssl);
int32_t ret = FRAME_TransportSendMsg(server->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
uint8_t serverHelloData[TEMP_DATA_LEN] = {0};
uint32_t serverHelloLen = sizeof(serverHelloData);
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, serverHelloData, serverHelloLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
serverHelloLen = readLen;
(void)HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, serverHelloData, serverHelloLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += serverHelloLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECTMSG_FUNC_TC001
* @spec
1. During the handshake, the server sends an EncryptedExtensions message before the ServerHello message.
Expected result: The client returns an alert. The level is ALERT_Level_FATAL, description is ALERT_UNEXPECTED_MESSAGE,
and the handshake is interrupted.
* @brief In all handshakes, the server MUST send the EncryptedExtensions message immediately after the ServerHello
message.
* 4.3.1. Encrypted Extensions row 105
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECTMSG_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(client->ssl->hsCtx->state, TRY_RECV_SERVER_HELLO);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerEEMsg(server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(ioUserData->recMsg.len != 0);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info alert = {0};
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t GetDisorderServerCertMsg(FRAME_LinkObj *server, uint8_t *data, uint32_t len, uint32_t *usedLen)
{
uint32_t readLen = 0;
uint32_t offset = 0;
uint8_t tmpData[TEMP_DATA_LEN] = {0};
uint32_t tmpLen = sizeof(tmpData);
(void) HITLS_Accept(server->ssl);
int32_t ret = FRAME_TransportSendMsg(server->io, tmpData, tmpLen, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
tmpLen = readLen;
(void) HITLS_Accept(server->ssl);
ret = FRAME_TransportSendMsg(server->io, &data[offset], len - offset, &readLen);
if (readLen == 0 || ret != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
offset += readLen;
if (memcpy_s(&data[offset], len - offset, tmpData, tmpLen) != EOK) {
return HITLS_MEMCPY_FAIL;
}
offset += tmpLen;
*usedLen = offset;
return HITLS_SUCCESS;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECTMSG_FUNC_TC002
* @spec
1. The handshake uses certificate authentication. During the handshake, the server sends a Certificate Request message
before sending the EncryptedExtensions extension. Expected result: The client returns an alert, whose level is
ALERT_LEVEL_FATAL, description is ALERT_UNEXPECTED_MESSAGE, and the handshake is interrupted.
* @brief A server which is authenticating with a certificate MAY optionally request
* a certificate from the client. This message, if sent, MUST follow EncryptedExtensions.
* 4.3.2. Certificate Request row 107
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNEXPECTMSG_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = {0};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportClientVerify = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_ENCRYPTED_EXTENSIONS), HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(client->ssl->hsCtx->state, TRY_RECV_ENCRYPTED_EXTENSIONS);
uint8_t data[MAX_RECORD_LENTH] = {0};
uint32_t len = MAX_RECORD_LENTH;
ASSERT_TRUE(GetDisorderServerCertMsg(server, data, len, &len) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ASSERT_TRUE(ioUserData->recMsg.len == 0);
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(ioUserData->recMsg.len != 0);
// TLS1.3 Ciphertext Handshake Messages Are Out of Order, Causing Decryption Failure
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_BAD_RECORD_MAC);
ALERT_Info alert = { 0 };
ALERT_GetInfo(client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_BAD_RECORD_MAC);
EXIT:
CleanRecordBody(&recvframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
bool g_client = false;
static void Test_NoServerCertPackAndParse001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_VERIFY);
if (ctx->isClient == g_client) {
FRAME_CleanMsg(&frameType, &frameMsg);
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_GetDefaultMsg(&frameType, &frameMsg) == HITLS_SUCCESS);
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NO_CERTVERIFY_FUNC_TC001
* @spec
* 1. Enable the dual-end verification, make the CertificateVerify message sent by the server lose, and observe the
* client behavior. Expected result: The client sends an alert message and the connection is disconnected.
* 2. Enable the dual-end verification, make the CertificateVerify message sent by the client lose, and observe the
* client behavior. Expected result: The client sends an alert message and the connection is disconnected.
* @brief Servers MUST send this message when authenticating via a certificate.
* Clients MUST send this message whenever authenticating via a certificate.
* 4.4.3. Certificate Verify row 145
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NO_CERTVERIFY_FUNC_TC001(int isClient)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
g_client = (bool)isClient;
/* 1. Enable the dual-end verification, make the CertificateVerify message sent by the server lose, and
* observe the client behavior.
* 2. Enable the dual-end verification, make the CertificateVerify message sent by the client lose, and observe
* the client behavior. */
RecWrapper wrapper = {TRY_RECV_CERTIFICATE_VERIFY,
REC_TYPE_HANDSHAKE,
true,
NULL,
Test_NoServerCertPackAndParse001};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_NoCertificateSignPackAndParse001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_VERIFY);
if (ctx->isClient == g_client) {
FRAME_CertificateVerifyMsg *certVerify = &frameMsg.body.hsMsg.body.certificateVerify;
certVerify->signHashAlg.data = CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256;
certVerify->signHashAlg.state = ASSIGNED_FIELD;
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CERTVERIFY_SIGN_FUNC_TC001
* @spec
* 1. Enable the dual-end verification, modify the signature field in the CertificateVerify message sent by the server,
* and observe the client behavior. The expected result is that the client sends decrypt_error and the connection is
* disconnected.
* 2. Enable dual-end verification, modify the signature field in the CertificateVerify message sent by the client, and
* observe the server behavior. Expected result: The server sends decrypt_error and disconnects the connection.
* @brief The receiver of a CertificateVerify message MUST verify the signature field.
* 4.4.3. Certificate Verify row 151
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CERTVERIFY_SIGN_FUNC_TC001(int isClient)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
g_client = (bool)isClient;
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_VERIFY,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_NoCertificateSignPackAndParse001
};
RegisterWrapper(wrapper);
FRAME_CertInfo serverCertInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_CertInfo clientCertInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &serverCertInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &clientCertInfo);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_PARSE_UNSUPPORT_SIGN_ALG);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_FinishedPackAndParse001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
if (ctx->isClient == g_client) {
FRAME_FinishedMsg *finishMsg = &frameMsg.body.hsMsg.body.finished;
finishMsg->verifyData.state = ASSIGNED_FIELD;
finishMsg->verifyData.size = finishMsg->verifyData.size - 1;
}
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_FINISHEDMSG_ERR_FUNC_TC001
* @spec
* 1. Modify the Finished message sent by the server and observe the client behavior. The expected result is that the
* client sends a decrypt_error message and the connection is disconnected.
* 2. Modify the Finished message sent by the client and observe the server behavior. The expected result is that the
* server sends a decrypt_error message and the connection is disconnected.
* @brief Recipients of Finished messages MUST verify that the contents are correct
* and if incorrect MUST terminate the connection with a "decrypt_error" alert.
* 4.4.4. Finished row 153
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_FINISHEDMSG_ERR_FUNC_TC001(int isClient)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
g_client = (bool)isClient;
RecWrapper wrapper = {TRY_SEND_FINISH,
REC_TYPE_HANDSHAKE,
false,
NULL,
/* 1. Modify the Finished message sent by the server and observe the client behavior. The expected result is
* that the client sends a decrypt_error message and the connection is disconnected.
* 2. Modify the Finished message sent by the client and observe the server behavior. */
Test_FinishedPackAndParse001};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RSA1024CERT_FUNC_TC001
* @spec
* 1. Initialize the client and server to TLS1.3 and use the 1024-bit RSA certificate. The certificate verification
* fails and connection establishment fails.
* @brief Applications SHOULD also enforce minimum and maximum key sizes. For example,
* certification paths containing keys or signatures weaker than 2048-bit RSA or 224-bit
* ECDSA are not appropriate for secure applications.
* Appendix C. Implementation Notes row 238
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RSA1024CERT_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo serverCertInfo = {
"rsa_1024/rsa_root.crt",
"rsa_1024/rsa_intCa.crt",
"rsa_1024/rsa_dev.crt",
0,
"rsa_1024/rsa_dev.key",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
const int32_t level = 2;
HITLS_CFG_SetSecurityLevel(config, level);
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &serverCertInfo);
ASSERT_TRUE(server == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_AlertPackAndParse004(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_ALERT;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.recType.data, REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &frameMsg.body.alertMsg;
ASSERT_EQ(alertMsg->alertLevel.data, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_BAD_CERTIFICATE);
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC001
* @spec
* 1, If no certificate is set on the server, the client sends a complete certificate chain, and the connection fails to be
* established.
* @brief Because certificate validation requires that trust anchors be distributed independently,
* a certificate that specifies a trust anchor MAY be omitted from the chain, provided that supported
* peers are known to possess any omitted certificates.
* 4.4.2. Certificate row 119
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
0,
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
RecWrapper wrapper = {HS_STATE_BUTT, REC_TYPE_ALERT, false, NULL, Test_AlertPackAndParse004};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC002
* @spec
* 1. The root certificate (any algorithm) is set on the server. The client sends a certificate chain that does not
* contain the root certificate. The connection is successfully set up.
* 2. The root certificate (any algorithm) is set on the client. The server sends a certificate chain without the root
* certificate. The connection is successfully set up.
* @brief Because certificate validation requires that trust anchors be distributed independently,
* a certificate that specifies a trust anchor MAY be omitted from the chain, provided that supported
* peers are known to possess any omitted certificates.
* 4.4.2. Certificate row 119
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo serverCertInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
FRAME_CertInfo clientCertInfo = {
"ecdsa/ca-nist521.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &serverCertInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &clientCertInfo);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC003
* @spec
* 1. The root certificate is incorrectly set on the server. After the client sends a complete certificate chain, the
* connection fails to be established and the error code is displayed. ALERT_BAD_CERTIFICATE
* @brief Because certificate validation requires that trust anchors be distributed independently,
* a certificate that specifies a trust anchor MAY be omitted from the chain, provided that supported
* peers are known to possess any omitted certificates.
* 4.4.2. Certificate row 119
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_CERTCHAIN_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"rsa_sha/ca-3072.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
0,
"ecdsa/end256-sha256.key.der",
0,
};
RecWrapper wrapper = {
HS_STATE_BUTT,
REC_TYPE_ALERT,
true,
NULL,
Test_AlertPackAndParse004
};
RegisterWrapper(wrapper);
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTNULL_FUNC_TC001
* @spec If the client does not send any certificates (i.e., it sends an empty Certificate message),
the server MAY at its discretion either continue the handshake without client authentication
or abort the handshake with a "certificate_required" alert.
* @title The certificate list of the server is not empty. The certificate of the peer end cannot be empty. The client
sends an empty certificate.
* Expected result: The handshake between the two parties fails, and the server sends an alarm. The alarm level
is ALERT_LEVEL_FATAL and the description is certificate_required.
* @precon nan
* @brief 4.4.2.4. Receiving a Certificate Message row142
1. Enable dual-end verification. Do not allow the peer certificate to be empty, set the client certificate
to be empty, and establish a connection.
* @expect 1. If the connection fails to be established, the server generates an alarm. The alarm level is ALERT_LEVEL_FATAL
and the description is certificate_required.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CLIENT_CERTNULL_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der",
0,
0,
0,
0,
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE);
ALERT_Info alert = { 0 };
ALERT_GetInfo(server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_CERTIFICATE_REQUIRED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ECDSA_SIGN_RSA_CERT_FUNC_TC001
* @spec Note that a certificate containing a key for one signature algorithm MAY be signed
* using a different signature algorithm (for instance, an RSA key signed with an ECDSA key).
* @title Apply for an RSA certificate issued by the ECDSA, set the certificate on the server, and set up a connection.
Expected result: The connection is successfully set up.
* @precon nan
* @brief 4.4.2.4. Receiving a Certificate Message row144
1. Enable dual-end verification, apply for an RSA certificate issued by the ECDSA, set the certificate on the
server, and set up a connection.
* @expect 1. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ECDSA_SIGN_RSA_CERT_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_CertInfo certInfo = {
"ecdsa_rsa_cert/rootCA.der",
"ecdsa_rsa_cert/CA1.der",
"ecdsa_rsa_cert/ee.der",
0,
"ecdsa_rsa_cert/ee.key.der",
0,
};
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
config->isSupportClientVerify = true;
config->isSupportNoClientCert = false;
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MD5_CERT_TC001
* @spec Apply for the ee certificate signed by the MD5 signature algorithm, set the MD5 certificate at both ends,
* set up a link, and observe the server behavior.
* Expected result: The server cannot select a proper certificate, sends bad_certificate, and disconnects the link.
* @title
* @precon nan
* @brief 4.4.2.4. Receiving a Certificate Message row143
Any endpoint receiving any certificate which it would need to validate using any signature algorithm using an
MD5 hash MUST abort the handshake with a "bad_certificate" alert.
* @expect 1. The link is set up successfully.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MD5_CERT_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
FRAME_CertInfo certInfo = {
"md5_cert/rsa_root.der",
"md5_cert/rsa_intCa.der",
"md5_cert/md5_dev.der",
0,
"md5_cert/md5_dev.key",
0,
};
FRAME_LinkObj *server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
FRAME_LinkObj *client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_CERT_CTRL_ERR_GET_SIGN_ALGO);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(client->ssl, &alertInfo);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_BAD_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_cert.c | C | unknown | 84,003 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "process.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "bsl_sal.h"
/* END_HEADER */
#define MAX_BUF 16384
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} ResumeTestInfo;
static int32_t DoHandshake(ResumeTestInfo *testInfo)
{
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
int32_t ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
}
static void Test_PskGetCert(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
static uint8_t certBuf[MAX_BUF] = {0};
static uint32_t bufLen = 0;
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
if (user != NULL) { // cert message
ASSERT_EQ(memcpy_s(certBuf, MAX_BUF, data, *len), EOK);
bufLen = *len;
} else {
ASSERT_EQ(memcpy_s(data, bufSize, certBuf, bufLen), EOK);
*len = bufLen;
}
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_CERT_FUNC_TC001
* @spec -
* When the @title psk session is resumed, the server sends the certificate message after sending the server hello
* message. The expected handshake fails.
* @precon nan
* @brief 2.2 Resumption and Pre-Shared Key line 7
* @expect 1. The expected handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_CERT_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, &wrapper, Test_PskGetCert};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
wrapper.ctrlState = TRY_SEND_FINISH;
wrapper.userData = NULL;
RegisterWrapper(wrapper);
ASSERT_NE(DoHandshake(&testInfo), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC001
* @spec -
* @title Set the client and server to tls1.3. Set the cipher suites on the client and server to mismatch. As a result,
* the expected connection establishment fails and a handshake_failure alert message is sent.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 13
* @expect 1. Expected handshake failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
cipherSuite = HITLS_AES_256_GCM_SHA384;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_PskGetCertReq(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
static uint8_t certBuf[READ_BUF_SIZE] = {0};
static uint32_t bufLen = 0;
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
if (user != NULL) { // cert message
ASSERT_EQ(memcpy_s(certBuf, READ_BUF_SIZE, data, *len), EOK);
bufLen = *len;
} else {
ASSERT_EQ(memcpy_s(data, bufSize, certBuf, bufLen), EOK);
*len = bufLen;
}
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_CERTREQ_FUNC_TC001
* @spec -
* @title 1. Preset the PSK. After the server sends the encryption extension, construct the certficaterequest message and
* observe the client behavior.
* @precon nan
* @brief 4.3.2. Certificate Request line 110
* @expect 1. The client sends an alert message and disconnects the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_CERTREQ_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.config->isSupportClientVerify = true;
RecWrapper wrapper = {TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, &wrapper, Test_PskGetCertReq};
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
wrapper.ctrlState = TRY_SEND_FINISH;
wrapper.userData = NULL;
RegisterWrapper(wrapper);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC002
* @spec -
* @title Set the client and server to tls1.3. Set the group on the client and server to different values. If the
* expected connection establishment fails, the handshake_failurealert message is sent.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 13
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t group = HITLS_EC_GROUP_SECP256R1;
HITLS_CFG_SetGroups(testInfo.config, &group, 1);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
group = HITLS_EC_GROUP_SECP384R1;
HITLS_CFG_SetGroups(testInfo.config, &group, 1);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC003
* @spec -
* @title Set the client server to tls1.3 and the server certificate does not match the signature algorithm specified by
* the client. In this case, the connection establishment fails and a handshake_failure alert message is sent.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 13
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t signature = CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256;
HITLS_CFG_SetSignature(testInfo.config, &signature, 1);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
signature = CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384;
HITLS_CFG_SetSignature(testInfo.config, &signature, 1);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ClientHelloMissKeyShare(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exState = MISSING_FIELD;
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
ASSERT_NE(parseLen, *len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC004
* @spec -
* @title Set the client server to tls1.3 and set the client psk mode to psk only. The provided psk server does not
* support the psk. As a result, the connection fails to be established. Send handshake_failure alert
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 13
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC004()
{
FRAME_Init();
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloMissKeyShare};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_HANDSHAKE_FAILURE);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC005
* @spec -
* @title Set the client server to tls1.3 and set the psk mode of the client to psk only. No psk extension is provided.
* As a result, connection establishment fails. Send handshake_failure alert
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 13
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_MISMATCH_FUNC_TC005()
{
FRAME_Init();
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ClientHelloMissKeyShare};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_MISSING_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ErrorOrderPsk(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.extensionLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.length.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.pskModes.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
uint8_t pskMode[] = {0, 0x2d, 0, 2, 1, 1}; // psk with dhe mode
ASSERT_NE(parseLen, *len);
ASSERT_EQ(memcpy_s(&data[*len], bufSize - *len, &pskMode, sizeof(pskMode)), EOK);
*len += sizeof(pskMode);
ASSERT_EQ(parseLen, *len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC001
* @spec -
* @title Set the client server to tls1.3 and construct the clienthello pre_shared_key extension that is not the last.
* The server is expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC001()
{
FRAME_Init();
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ErrorOrderPsk};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_RepeatClientHelloExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FieldState *extensionState = GetDataAddress(&frameMsg, user);
*extensionState = DUPLICATE_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
ASSERT_NE(parseLen, *len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void RepeatClientHelloExtension(void *memberAddress, bool isPsk)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
if (isPsk) {
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
}
char serverName[] = "testServer";
uint8_t alpn[6] = {4, '1', '2', '3', '4', 0};
HITLS_CFG_SetServerName(testInfo.config, (uint8_t *)serverName, strlen(serverName));
HITLS_CFG_SetAlpnProtos(testInfo.config, alpn, strlen((char *)alpn));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, memberAddress, Test_RepeatClientHelloExtension};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC002
* @spec -
* @title The client server is tls1.3. Two signature algorithms are used to construct the clienthello. The server is
* expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC002()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.signatureAlgorithms.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC003
* @spec -
* @title The client server is tls1.3. Two supportedGroups are displayed in the clienthello. The server is expected to
* return an alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC003()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC004
* @spec -
* @title The client server is tls1.3. Two pointFormats are displayed in the constructed clienthello. The server is
* expected to return an alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC004()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.pointFormats.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC005
* @spec -
* @title The client server is tls1.3. Two supportedVersion fields are displayed in the clienthello. The server is
expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC005()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedVersion.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC006
* @spec -
* @title The client server is tls1.3. When two extendedMasterSecrets are displayed in the clienthello message, the
* server is expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC006()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.extendedMasterSecret.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC007
* @spec -
* @title The client server is tls1.3. Two pskModes are displayed in the clienthello. The server is expected to return an
* alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC007()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.pskModes.exState, true);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC008
* @spec -
* @title The client server is tls1.3. Two keyshares are displayed when the clienthello is constructed. The server is
* expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC008()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC009
* @spec -
* @title The client server is tls1.3. Two psks are displayed when the clienthello is constructed. The server is expected
* to return an alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC009()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.psks.exState, true);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC010
* @spec -
* @title The client server is tls1.3. Two serverNames are displayed in the clienthello. The server is expected to return
* alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC010()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.serverName.exState, false);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC011
* @spec -
* @title The client server is tls1.3. Two alpn extensions are displayed when the clienthello is constructed. The server is expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC011()
{
RepeatClientHelloExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.alpn.exState, false);
}
/* END_CASE */
static void Test_RepeatServerHelloExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FieldState *extensionState = GetDataAddress(&frameMsg, user);
*extensionState = DUPLICATE_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC012
* @spec -
* @title The client server is tls1.3. Two supportedversion extensions are displayed when the serverhello is
* constructed. The client is expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC012()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&((FRAME_Msg *)0)->body.hsMsg.body.serverHello.supportedVersion.exState,
Test_RepeatServerHelloExtension};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC013
* @spec -
* @title The client server is tls1.3. Two key_share extensions are displayed when the serverhello is constructed. The
* client is expected to return alert.
* @precon nan
* @brief 4.2. Extensions line 40
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_ERR_HEELO_FUNC_TC013()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&((FRAME_Msg *)0)->body.hsMsg.body.serverHello.keyShare.exState,
Test_RepeatServerHelloExtension};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC002
* @spec -
* @title 1. Set the client to tls1.2 and the server to tls1.3. Initiate a connection establishment request. The expected
* connection establishment is successful and the negotiated version is tls1.2.
* @precon nan
* @brief 4.2.1 Supported Versions line 42
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS12Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint16_t version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS12);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ErrLegacyVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.version.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.version.data = HITLS_VERSION_TLS13;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC003
* @spec -
* @title 1. Set the client to tls1.2 and server to tls1.3, initiate a connection establishment request, change the
* version value in the client hello message to 0x0304, and expect the server to stop handshake.
* @precon nan
* @brief 4.2.1 Supported Versions line 42
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC003()
{
FRAME_Init();
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ErrLegacyVersion};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS12Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(testInfo.client->ssl->negotiatedInfo.version == HITLS_VERSION_TLS12);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC004
* @spec -
* @title 1. Set the client to support TLS1.1 and TLS1.0 and the server to support TLS1.3. Initiate a connection
* establishment request. The server is expected to negotiate TLS1.3.
* @precon nan
* @brief 4.2.1 Supported Versions line 43
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.3.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC004()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.version = HITLS_VERSION_TLS13;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint16_t version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS13);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_UnknownVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.supportedVersion.exData.data);
uint16_t version[] = { 0x01, 0x02, *(uint16_t *)user };
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exData.data =
BSL_SAL_Calloc(sizeof(version) / sizeof(uint16_t), sizeof(uint16_t));
ASSERT_EQ(memcpy_s(frameMsg.body.hsMsg.body.clientHello.supportedVersion.exData.data,
sizeof(version), version, sizeof(version)), EOK);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exData.size = sizeof(version) / sizeof(uint16_t);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exData.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exDataLen.data = sizeof(version);
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exLen.data = sizeof(version) + sizeof(uint8_t);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC005
* @spec -
* @title 1. Set the client server to tls1.3 and modify the supportedversion field in the client hello packet,
* An invalid version number 0x0001,0x0002 is added before tils1.3. It is expected that the server can
* negotiate the TLS1.3 version.
* @precon nan
* @brief 4.2.1 Supported Versions line 43
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.3.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC005()
{
FRAME_Init();
uint16_t version = HITLS_VERSION_TLS13;
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, &version, Test_UnknownVersion};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS13);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC006
* @spec -
* @title 1. Configure the TLS1.2 client and TLS1.3 server, and construct the clienthello message that carries the
* supportedversion extension. If the value is 0x0303 and 0x0302, the server negotiates TLS1.2 normally and
* the handshake succeeds.
* @precon nan
* @brief 4.2.1 Supported Versions line 44
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC006()
{
FRAME_Init();
uint16_t version = HITLS_VERSION_TLS12;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
&version,
Test_UnknownVersion
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS12Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS12);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ServerVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
if (*(uint16_t *)user == 0) {
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState, MISSING_FIELD);
} else if (*(uint16_t *)user == HITLS_VERSION_TLS13) {
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.version.data, HITLS_VERSION_TLS12);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.supportedVersion.data.data, HITLS_VERSION_TLS13);
} else if (*(uint16_t *)user == HITLS_VERSION_TLS12) {
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exLen.data = 0;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exType.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exType.data = HS_EX_TYPE_SUPPORTED_VERSIONS;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.data.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.data.data = HITLS_VERSION_TLS12;
} else {
ASSERT_EQ(0, 1);
}
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC007
* @spec -
* @title 1. Set the client to TLS1.2 and server to TLS1.3. The earlier version of TLS1.2 is expected to be negotiated.
* The server hello message sent does not carry the supportedversion extension.
* @precon nan
* @brief 4.2.1 Supported Versions line 45
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.2.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC007()
{
FRAME_Init();
uint16_t version = 0;
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, &version, Test_ServerVersion};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS12Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS12);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CHECK_SERVERHELLO_MASTER_SECRET_FUNC_TC001
* @spec
* The client does not support the extended master key and performs negotiation. After receiving the server hello
* message with the extended master key, the client sends an alert message. Check whether the two parties enter the
* alerted state, and the read and write operations fail.
* @brief When an error is detected, the detecting party sends a message to its peer. Upon transmission or receipt of a
* fatal alert message, both parties MUST immediately close the connection.
* 6.2.0. Alert Protocol row 216
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CHECK_SERVERHELLO_MASTER_SECRET_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo.c_config, true);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_ServerHelloSessionId(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.sessionIdSize.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.sessionIdSize.data = *len;
memset_s(data, bufSize, 0, bufSize);
ASSERT_EQ(parseLen, *len);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMESH_SESSIONId_LENGTHERR_FUNC_TC001
* @spec
* 1. Session recovery. If the client receives a ServerHello message whose session_id length exceeds the length of the
* entire packet, the client sends a decode_error alarm and the session recovery fails.
* @brief Peers which receive a message which cannot be parsed according to the syntax (e.g.,
* have a length extending beyond the message boundary or contain an out-of-range length)
* MUST terminate the connection with a "decode_error" alert.
* 6. Alert Protocol row 209
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMESH_SESSIONId_LENGTHERR_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerHelloSessionId};
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_DECODE_ERROR);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC008
* @spec -
* @title 1. Set the client to tls1.2 and server to tls1.3. Construct the serverhello message that carries the
supportedversion extension. tls1.2 in the extension, the client is expected to abort the negotiation,
* @precon nan
* @brief 4.2.1 Supported Versions line 45
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.3.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC008()
{
FRAME_Init();
uint16_t version = HITLS_VERSION_TLS12;
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, &version, Test_ServerVersion};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS12Config();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC009
* @spec -
* @title Set the client server to tls1.3, set up a connection normally, and the serverhello message is expected to carry the
* supportedversion extension. The value contains only 0x0304, and the value of legacy_version is 0x0303.
* @precon nan
* @brief 4.2.1 Supported Versions line 45
* @expect 1. The expected connection setup is successful and the negotiated version is tls1.3.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC009()
{
FRAME_Init();
uint16_t version = HITLS_VERSION_TLS13;
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&version,
Test_ServerVersion
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
version = 0;
ASSERT_EQ(HITLS_GetNegotiatedVersion(testInfo.client->ssl, &version), HITLS_SUCCESS);
ASSERT_EQ(version, HITLS_VERSION_TLS13);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ErrorServerVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
if (*(uint16_t *)user == 0) {
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState = MISSING_FIELD;
} else if (*(uint16_t *)user == HITLS_VERSION_TLS13) {
frameMsg.body.hsMsg.body.serverHello.version.data = HITLS_VERSION_TLS13;
} else if (*(uint16_t *)user == HITLS_VERSION_TLS12) {
frameMsg.body.hsMsg.body.serverHello.supportedVersion.data.data = HITLS_VERSION_TLS12;
} else {
ASSERT_EQ(0, 1);
}
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void ErrorServerVersion(uint16_t version)
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&version,
Test_ErrorServerVersion
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLSConfig();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
if (version == HITLS_VERSION_TLS12) {
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
}
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0010
* @spec -
* @title 1. Set the client server to tls1.3, construct the legacy_version value of serverhello to 0x0304, and expect the
* client to stop connection establishment.
* @precon nan
* @brief 4.2.1 Supported Versions line 45
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0010()
{
ErrorServerVersion(HITLS_VERSION_TLS13);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0011
* @spec -
* @title 1. Set the client server to tls1.3 and construct that the supportedversion parameter of the serverhello does
* not exist. The client is expected to stop connection establishment.
* @precon nan
* @brief 4.2.1 Supported Versions line 45
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0011()
{
ErrorServerVersion(0);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0012
* @spec -
* @title 1. Set the client server to tls1.3 and set the supportedversion value of the serverhello to tls1.2. Expect the
* client to stop connection establishment. The client is expected to send the illegal_parameter alarm.
* @precon nan
* @brief 4.2.1 Supported Versions line 45/47
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SUPPORT_VERSION_FUNC_TC0012()
{
ErrorServerVersion(HITLS_VERSION_TLS12);
}
/* END_CASE */
static void Test_AbsentGroup(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
uint32_t sz = frameMsg.body.hsMsg.body.clientHello.supportedGroups.exData.size;
ASSERT_TRUE(sz > 1);
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exData.size = 1;
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exData.data[0] =
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exData.data[1];
frameMsg.body.hsMsg.body.clientHello.supportedGroups.exDataLen.data = sizeof(uint16_t);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC001
* @spec -
* @title 1. Initialize the client server to tls1.3, and construct that the keyshareentry.group in the sent client hello
* message is not contained in the supported_group of the client. The server aborts the handshake and returns
* the illegal_parameter alarm.
* @precon nan
* @brief 4.2.8 key share line 68
* @expect 1. Expected connection establishment failure
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC001()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_AbsentGroup
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void HelloRetryRequest(WrapperFunc func)
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_HELLO_RETRY_REQUEST,
REC_TYPE_HANDSHAKE,
false,
NULL,
func
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
static void Test_HelloRetryRequest(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = HITLS_EC_GROUP_SECP384R1;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC002
* @spec -
* @title 1. Initialize the client and server to tls1.3 and construct the scenario of sending hrr messages.
* Construct the scenario where the selected_group in the hrr message is not in the group supported by the
* client. Expect the client to terminate the handshake and return the illegal_parameter alarm.
* @precon nan
* @brief 4.2.8 key share line 69
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC002()
{
HelloRetryRequest(Test_HelloRetryRequest);
}
/* END_CASE */
static void Test_HelloRetryRequestSameGroup(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data, HITLS_EC_GROUP_SECP256R1);
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = HITLS_EC_GROUP_CURVE25519;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC003
* @spec -
* @title 1. Initialize the client server to tls1.3, construct the scenario of sending hrr messages, and construct the
* group corresponding to the key_share key provided by the client. The client terminates the handshake and
* returns the illegal_parameter alarm.
* @precon nan
* @brief 4.2.8 key share line 69
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC003()
{
HelloRetryRequest(Test_HelloRetryRequestSameGroup);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC004
* @spec -
* @title 1. Initialize the client and server to tls1.3 and construct the scenario of sending hrr messages.
* Construct the scenario where the key_share carried in the clienthello message sent again is not the
* selected_group specified in the hrr message. The server is expected to terminate the handshake and return
* the illegal_parameter alarm.
* @precon nan
* @brief 4.2.8 key share line 71
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_FUNC_TC004()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_HELLO_RETRY_REQUEST,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_HelloRetryRequest
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC001
* @spec -
* @title 1. The client initiates connection establishment by using the PSK encrypted by the unknown key.
* Expected result: A non-PSK handshake is performed.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 96
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(testInfo.client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_InvalidSelectedIdentity(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.pskSelectedIdentity.exState, INITIAL_FIELD);
frameMsg.body.hsMsg.body.serverHello.pskSelectedIdentity.data.data = 1;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC002
* @spec -
* @title 1. In the first handshake, the selected_identity of the server is not in the identity list provided by the
* client, The client uses the illegal_parameter alarm to terminate the handshake. As a result, the first
* connection fails to be established.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 100
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC002()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_InvalidSelectedIdentity
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC003
* @spec -
* @title 1. During session restoration, the selected_identity of the server is not in the identity list provided by the
* client, The client uses the illegal_parameter alarm to terminate the handshake. As a result, the first
* connection fails to be established.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 100
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_InvalidSelectedIdentity
};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_InvalidCipherSuites(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
(void)bufSize;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_EQ(frameMsg.body.hsMsg.body.serverHello.pskSelectedIdentity.exState, INITIAL_FIELD);
frameMsg.body.hsMsg.body.serverHello.cipherSuite.data = HITLS_AES_256_GCM_SHA384;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC004
* @spec -
* @title 1. In the first handshake, the hash algorithm in the cipher suite selected by the server does not match the
* PSK. The client uses the (overwritten) illegal_parameter alarm to terminate the handshake. As a result, the
* onnection fails to be established.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 100
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC004()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_InvalidCipherSuites
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
HITLS_Connect(testInfo.client->ssl);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC005
* @spec -
* @title 1. Preset the PSK. When the client sends the client hello message, the client parses the extension item of the
* client hello message so that the pre_share_key extension is not the last extension and continues to
* establish the connection. Expected result: The server returns ALERT illegal_parameter and the handshake is
* interrupted.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 100
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC005()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ErrorOrderPsk
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC006
* @spec -
* @title 1. During PSK-based session recovery, the client sends the client hello message, and parses the client hello
* extension. Enable the pre_share_key extension not to establish a connection for the last extension. Expected
* result: The server returns ALERT illegal_parameter and the handshake is interrupted.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 102
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC006()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ErrorOrderPsk
};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = { 0 };
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_ServerErrorOrderPsk(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.extensionLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.length.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
uint8_t supportedVersion[] = {0, 0x2b, 0, 2, 3, 4};
ASSERT_NE(parseLen, *len);
ASSERT_EQ(memcpy_s(&data[*len], bufSize - *len, &supportedVersion, sizeof(supportedVersion)), EOK);
*len += sizeof(supportedVersion);
ASSERT_EQ(parseLen, *len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC007
* @spec -
* @title 1. Preset the PSK. When the server sends the server hello message, the server parses the extension item of the
* server hello message so that the pre_share_key extension is not the last extension and continues to
* establish the connection. Expected result: The session is restored successfully.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 100
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC007()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_VERIFY,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ServerErrorOrderPsk
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC008
* @spec -
* @title 1. During PSK-based session recovery, the server parses the extension items of the server hello message when
* the server sends the server hello message, Make the pre_share_key extension not the last extension and
* continue to establish the connection. Expected result: The session is restored successfully.
* @precon nan
* @brief 4.2.9 pre-shared key exchange line 102
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_FUNC_TC008()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ServerErrorOrderPsk
};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_HrrMisClientHelloExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
if (ctx->hsCtx->haveHrr) {
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FieldState *extensionState = GetDataAddress(&frameMsg, user);
*extensionState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
}
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void WithoutPskMisKeyExtension(void *memberAddress, bool isResume, bool isHrr)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
if (isResume) {
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
} else {
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
}
if (isHrr) {
uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(testInfo.config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
}
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
WrapperFunc func = isHrr ? Test_HrrMisClientHelloExtension : Test_MisClientHelloExtension;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
memberAddress,
func
};
RegisterWrapper(wrapper);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.server->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_MISSING_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC001
* @spec -
* @title 1. When the first connection is established, the first ClientHello message received by the server does not
* contain the pre_shared_key extension. If signature_algorithms, supported_groups, or key_share is missing,
* the connection fails to be established and the server reports the missing_extension alarm.
* @precon nan
* @brief 9.2 Mandatory-to-Implement Extensions line 233
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC001()
{
bool isResume = false;
bool isHrr = false;
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.signatureAlgorithms.exState,
isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, isResume, isHrr);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC002
* @spec -
* @title 1. Certificate handshake in the hrr scenario, second ClientHello
* If signature_algorithms, supported_groups, or key_share is missing, the connection fails to be established and the
* server reports the missing_extension alarm.
* @precon nan
* @brief 9.2 Mandatory-to-Implement Extensions line 233
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC002()
{
bool isResume = false;
bool isHrr = true;
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.signatureAlgorithms.exState,
isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, isResume, isHrr);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC003
* @spec -
* @title 1. When the first connection is established, the first ClientHello message received by the server does not
* contain supported_groups. But include key_share, and vice versa. The connection fails to be established and
* theserver generates the missing_extension alarm.
* @precon nan
* @brief 9.2 Mandatory-to-Implement Extensions line 233
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC003()
{
bool isResume = false;
bool isHrr = false;
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, isResume, isHrr);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC004
* @spec -
* @title 1. The session is resumed. The first ClientHello message received by the server does not contain
* supported_groups, But include key_share, and vice versa. The session fails to be restored, and the server
* generates the missing_extension alarm.
* @precon nan
* @brief 9.2 Mandatory-to-Implement Extensions line 233
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC004()
{
bool isResume = true;
bool isHrr = false;
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, isResume, isHrr);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC005
* @spec -
* @title 1. In the hrr scenario, the certificate handshake second ClientHello does not contain supported_groups.
* share is contained, the connection fails to be established and the server generates the
* missing_extension alarm.
* @precon nan
* @brief 9.2 Mandatory-to-Implement Extensions line 233
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_EXTENSION_ASSOCIATION_FUNC_TC005()
{
bool isResume = false;
bool isHrr = true;
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.supportedGroups.exState, isResume, isHrr);
WithoutPskMisKeyExtension(&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState, isResume, isHrr);
}
/* END_CASE */
static void Test_CertificateExtensionError001(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
FrameCertItem *certItem = frameMsg.body.hsMsg.body.certificate.certItem;
// status_request Certificate Allowed Extensions. type(2) + len(2) + ctx(len)
uint8_t certExtension[] = {0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00};
uint32_t extensionLen = sizeof(certExtension);
uint8_t *extensionData = BSL_SAL_Calloc(extensionLen, sizeof(uint8_t));
ASSERT_TRUE(extensionData != NULL);
ASSERT_EQ(memcpy_s(extensionData, extensionLen, certExtension, extensionLen), EOK);
certItem->extension.state = ASSIGNED_FIELD;
BSL_SAL_FREE(certItem->extension.data);
certItem->extension.data = extensionData;
certItem->extension.size = extensionLen;
certItem->extensionLen.state = ASSIGNED_FIELD;
certItem->extensionLen.data = extensionLen;
*len += extensionLen;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC001
* @spec -
* @title The test certificate message carries the extension of the response. However, due to the current feature not
* being supported, requests will not be sent proactively. Expected to send illegal alerts and disconnect links.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the certificate message sent by the server to the client to include the status_request extension
* 3. Establish a connection and observe client behavior
* @expect
* 1. Initialization successful
* 2. Setup successful
* 3. The client returns alert ALERT_ILLEGAL_PARAMETER.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportClientVerify = true;
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_CertificateExtensionError001};
RegisterWrapper(wrapper);
HITLS_CFG_SetCheckKeyUsage(testInfo.config, false);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertificateExtensionError002(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
FrameCertItem *certItem = frameMsg.body.hsMsg.body.certificate.certItem;
// signature_algorithm Certificate-recognized but disallowed extensions. type(2) + len(2) + ctx(len)
uint8_t certExtension[] = {0x00, 0x0d, 0x00, 0x04, 0x00, 0x00, 0x08, 0x09};
uint32_t extensionLen = sizeof(certExtension);
uint8_t *extensionData = BSL_SAL_Calloc(extensionLen, sizeof(uint8_t));
ASSERT_TRUE(extensionData != NULL);
ASSERT_EQ(memcpy_s(extensionData, extensionLen, certExtension, extensionLen), EOK);
certItem->extension.state = ASSIGNED_FIELD;
BSL_SAL_FREE(certItem->extension.data);
certItem->extension.data = extensionData;
certItem->extension.size = extensionLen;
certItem->extensionLen.state = ASSIGNED_FIELD;
certItem->extensionLen.data = extensionLen;
*len += extensionLen;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC002
* @spec -
* @title The test certificate message carries identifiable but not allowed extensions. Expected to send illegal
* alerts and disconnect.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the certificate message sent by the server to the client to include the signature_algorithm extension
* 3. Establish a connection and observe client behavior
* @expect
* 1. Initialization successful
* 2. Setup successful
* 3. The client returns alert ALERT_ILLEGAL_PARAMETER.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC002()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportClientVerify = true;
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_CertificateExtensionError002};
RegisterWrapper(wrapper);
HITLS_CFG_SetCheckKeyUsage(testInfo.config, false);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_CertificateExtensionError003(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
FrameCertItem *certItem = frameMsg.body.hsMsg.body.certificate.certItem;
// Unrecognized Extensions. type(2) + len(2) + ctx(len)
uint8_t certExtension[] = {0x00, 0x56, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00};
uint32_t extensionLen = sizeof(certExtension);
uint8_t *extensionData = BSL_SAL_Calloc(extensionLen, sizeof(uint8_t));
ASSERT_TRUE(extensionData != NULL);
ASSERT_EQ(memcpy_s(extensionData, extensionLen, certExtension, extensionLen), EOK);
certItem->extension.state = ASSIGNED_FIELD;
BSL_SAL_FREE(certItem->extension.data);
certItem->extension.data = extensionData;
certItem->extension.size = extensionLen;
certItem->extensionLen.state = ASSIGNED_FIELD;
certItem->extensionLen.data = extensionLen;
*len += extensionLen;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC003
* @spec -
* @title Test certificate message carrying unrecognized extensions. Expected to send illegal alerts and disconnect
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the certificate message sent by the server to the client to include the unrecognized extension
* 3. Establish a connection and observe client behavior
* @expect
* 1. Initialization successful
* 2. Setup successful
* 3. The client returns alert ALERT_ILLEGAL_PARAMETER.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CERT_EXTENSION_TC003()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.config->isSupportClientVerify = true;
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_CertificateExtensionError003};
RegisterWrapper(wrapper);
HITLS_CFG_SetCheckKeyUsage(testInfo.config, false);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ALERT_Info alert = {0};
ALERT_GetInfo(testInfo.client->ssl, &alert);
ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_extensions_1.c | C | unknown | 100,938 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "process.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "bsl_sal.h"
#include "hs_extensions.h"
/* END_HEADER */
#define MAX_BUF 16384
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} ResumeTestInfo;
static int32_t DoHandshake(ResumeTestInfo *testInfo)
{
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->client = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
int32_t ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->config, testInfo->uioType);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
}
typedef enum {
WITHOUT_PSK,
PRE_CONFIG_PSK,
SESSION_RESUME_PSK
} PskStatus;
static void Test_PskConnect(uint32_t serverMode, PskStatus pskStatus)
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE);
if (pskStatus == SESSION_RESUME_PSK) {
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
} else if (pskStatus == PRE_CONFIG_PSK) {
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
} else {
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
}
HITLS_CFG_SetKeyExchMode(testInfo.config, serverMode);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(testInfo.client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, pskStatus == SESSION_RESUME_PSK ? 1 : 0);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC001
* @spec -
* @title 1. Set key_exchange_mode to 3 on the client and server to establish a connection for the first time.
* The expected result indicates that the connection is successfully established.
* @precon nan
* @brief psk Supplement the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC001()
{
Test_PskConnect(TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE, WITHOUT_PSK);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC002
* @spec -
* @title 1. Set key_exchange_mode to 3 on the client and server to establish a connection for the first time.
* The expected result indicates that the connection is successfully established.
* @precon nan
* @brief psk Supplement the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC002()
{
Test_PskConnect(TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE, PRE_CONFIG_PSK);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC003
* @spec -
* @title 1. Restore the session. Set key_exchange_mode to 3 on the client and server. The client carries key_share and
* connection. The expected result indicates that the connection is successfully established.
* @precon nan
* @brief psk Supplement the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC003()
{
Test_PskConnect(TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE, SESSION_RESUME_PSK);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC004
* @spec -
* @title 1. Preset the PSK, set the key_exchange_mode of the client and server to 3 and set the key_exchange_mode of the
* server to psk_only, and establish a connection. The expected result indicates that the connection is
* successfully established.
* @precon nan
* @brief psk Supplement the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC004()
{
Test_PskConnect(TLS13_KE_MODE_PSK_ONLY, PRE_CONFIG_PSK);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC005
* @spec -
* @title 1. Restore the session. Set the key_exchange_mode of the client and server to 3. Set the key_share extended by
* the client to establish a connection and enable the server to reject the PSK. Expected result:
* The session fails and certificate authentication is rejected.
* @precon nan
* @brief psk Supplement the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC005()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_FreeConfig(testInfo.config);
testInfo.config = HITLS_CFG_NewTLS13Config();
HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(testInfo.client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC006
* @spec -
* @title 1. Preset the PSK and set the key_exchange_mode on the client server to 3. The key_share extension on the
* client is lost and a connection is established. Expected result: The server sends an alert message and
* the connection is disconnected.
* @precon nan
* @brief psk Added the test case line 269.
* @expect 1. Expected connection establishment failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_MODES_FUNC_TC006()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
&((FRAME_Msg *)0)->body.hsMsg.body.clientHello.keyshares.exState,
Test_MisClientHelloExtension
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY | TLS13_KE_MODE_PSK_WITH_DHE);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_NE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/**
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CERTICATE_VERIFY_FAIL_FUNC_TC001
* @brief 6.3. Error Alerts row 216
* The client does not support extended master keys and performs negotiation. After receiving the server hello
* message with the extended master keys, the client sends an alert message. Check whether the two parties enter the
* alerted state, and the read and write operations fail.
*
*/
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CERTICATE_VERIFY_FAIL_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.s_config != NULL);
testInfo.c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.c_config != NULL);
HITLS_CFG_SetExtenedMasterSecretSupport(testInfo.c_config, false);
testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
ASSERT_TRUE(testInfo.server != NULL);
ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.c_config);
HITLS_CFG_FreeConfig(testInfo.s_config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
static void Test_Client_Mode(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.pskModes.exData.state = ASSIGNED_FIELD;
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data);
uint16_t version[] = { 0x03, };
frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data =
BSL_SAL_Calloc(sizeof(version) / sizeof(uint8_t), sizeof(uint8_t));
ASSERT_EQ(memcpy_s(frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data,
sizeof(version), version, sizeof(version)), EOK);
frameMsg.body.hsMsg.body.clientHello.keyshares.exState = MISSING_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.state = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKMODEZERO_FUNC_TC001
* @spec -
* @title Initialize the client and server to tls1.3. Construct a scenario where the psk is carried but key_share is not
* carried. Construct a scenario where the psk_mode carried in the clienthello message is 3. It is expected
* that the handshake fails.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation line 11
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKMODEZERO_FUNC_TC001()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Client_Mode
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_MISSING_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_Server_Keyshare(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.keyShare.data.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = *(uint64_t *)user;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC001
* @spec -
* @title Initialize the client server to tls1.3 and construct the selected_group carried in the key_share extension in
* the sent serverhello message. If the group is not the keyshareentry group carried in the client hello message
* but the group provided in the client hello message, the connection fails to be established.
* @precon nan
* @brief 4.2.8. Key Share line 72
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC001()
{
FRAME_Init();
uint64_t groupreturn[] = {HITLS_EC_GROUP_SECP384R1, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&groupreturn,
Test_Server_Keyshare
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t group[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
HITLS_CFG_SetGroups(testInfo.config, group, 2);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_Server_Keyshare3(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_TRUE(frameMsg.body.hsMsg.body.serverHello.keyShare.exState == MISSING_FIELD);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKKEYSHARE_FUNC_TC001
* @spec -
* @title 1. Initialize the client and server to tls1.3 and construct a scenario where psk_ke is used. The expected
* serverhello message sent does not carry the key_share extension.
* @precon nan
* @brief 4.2.8. Key Share line 73
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKKEYSHARE_FUNC_TC001()
{
FRAME_Init();
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Server_Keyshare3
};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
static void Test_Server_Keyshare4(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.keyShare.exState = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.exLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = HITLS_EC_GROUP_CURVE25519;
FRAME_ModifyMsgInteger(HS_EX_TYPE_KEY_SHARE, &frameMsg.body.hsMsg.body.serverHello.keyShare.exType);
uint8_t uu[] = {0x3b, 0xb5, 0xe4, 0x3c, 0xf6, 0xc4, 0x70, 0x0f, 0x3c, 0x7f, 0x05, 0x0b, 0xd4, 0xfb, 0x24, 0x39,
0xc8, 0xb6, 0x13, 0x50, 0xc6, 0xee, 0xde, 0x69, 0xc5, 0x09, 0xef, 0x2e, 0x21, 0x4d, 0xd8, 0x1e};
FRAME_ModifyMsgArray8(uu, sizeof(uu), &frameMsg.body.hsMsg.body.serverHello.keyShare.data.keyExchange,
&frameMsg.body.hsMsg.body.serverHello.keyShare.data.keyExchangeLen);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKKEYSHARE_FUNC_TC002
* @spec -
* @title 1. Initialize the client and server to tls1.3, construct the psk_ke scenario, and construct the sent
* serverhello message carrying the key_share extension. It is expected that the connection fails to be
* established.
* @precon nan
* @brief 4.2.8. Key Share line 73
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKKEYSHARE_FUNC_TC002()
{
FRAME_Init();
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Server_Keyshare4};
RegisterWrapper(wrapper);
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(testInfo.config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetPskServerCallback(testInfo.config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(testInfo.config, (HITLS_PskClientCb)ExampleClientCb);
testInfo.client = FRAME_CreateLink(testInfo.config, testInfo.uioType);
HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY);
testInfo.server = FRAME_CreateLink(testInfo.config, testInfo.uioType);
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
HITLS_MSG_HANDLE_HANDSHAKE_FAILURE);
ALERT_Info info = {0};
ALERT_GetInfo(testInfo.client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC001
* @spec -
* @title The supported_versions in the clientHello is extended to 0x0304 (TLS 1.3). If the server supports only 1.2, the
* server returns a "protocol_version" warning and the handshake fails.
* @precon nan
* @brief Appendix D. Backward Compatibility line 247
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLS13Config();
config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->cipherSuites.data[0] = 0xC02F;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(serverTlsCtx->hsCtx->state, TRY_SEND_CERTIFICATE);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC004
* @spec -
* @title clientHello version is 0x0303 and the server supports only 1.3. The server returns "protocol_version" and the
* handshake fails.
* @precon nan
* @brief Appendix D. Backward Compatibility line 247
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLS12Config();
config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC005
* @spec -
* @titleSet that the server supports only TLS 1.2 and the client supports TLS 1.3. ClientHello legacy_version contains
0x0303 (TLS 1.2). The supported_versions field is extended to 0x0304 and 0x0303. The server responds with
serverHello and ServerHello.version is 0x0303, The client agrees to use this version, and the handshake
negotiation is successful.
* @precon nan
* @brief Appendix D. Backward Compatibility line 245
* @expect 1. The handshake negotiation is successful.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC005()
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLSConfig();
config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config_c, TLS12_VERSION_BIT|TLS13_VERSION_BIT) == HITLS_SUCCESS);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC006
* @spec -
* @title: The server supports only TLS 1.2, and the client supports TLS 1.3. ClientHello legacy_version contains 0x0303
* (TLS 1.2). The supported_versions field is extended to 0x0304 and 0x0303. The server responds with
* serverHello and ServerHello.version is 0x0303, The client agrees to use this version. The connection
* establishment is interrupted, and the session is restored. The restoration is successful.
* @precon nan
* @brief Appendix D. Backward Compatibility line 245
* @expect 1. The handshake negotiation is successful.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC006()
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLSConfig();
config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config_c, TLS12_VERSION_BIT|TLS13_VERSION_BIT) == HITLS_SUCCESS);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = NULL;
clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
static void Test_SERVERHELLO_VERSION(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.version.data = 0x0301;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC007
* @spec -
* @titleSet that the server supports only TLS 1.2 and the client supports TLS 1.3. ClientHello legacy_version contains
* 0x0303 (TLS 1.2), and supported_versions is extended to 0x0304, 0x0303, and 0x0301: The server responds with
* serverHello. The value of ServerHello.version is 0x0303. The client agrees to use this version. The
* connection establishment is interrupted and the session is restored, Before the client receives the
* ServerHello message, the session fails to be resumed after Server.version is changed to 0x0301.
* @precon nan
* @brief Appendix D. Backward Compatibility line 245
* @expect 1. Failed to restore the session.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC007()
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLSConfig();
config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config_c, TLS12_VERSION_BIT | TLS13_VERSION_BIT) ==
HITLS_SUCCESS);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = NULL;
clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_SERVERHELLO_VERSION};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKTICKETLIFETIME_FUNC_TC001
* @spec -
* @title Set the life cycle of ticket_lifetime to 10s. After the first connection is established, send the session using the
* ticket through the client to resume, The server processes the message 10 seconds after receiving the ticket. The
* server determines that the ticket has expired and the session fails to be restored.
* @precon nan
* @brief 4.6.1. New Session Ticket Message line 164
* @expect 1. Expected handshake failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKTICKETLIFETIME_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetSessionTimeout(testInfo.config, 10);
ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
ASSERT_TRUE(testInfo.clientSession != NULL);
FRAME_FreeLink(testInfo.client);
testInfo.client = NULL;
FRAME_FreeLink(testInfo.server);
testInfo.server = NULL;
HITLS_CFG_FreeConfig(testInfo.config);
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, testInfo.clientSession), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
sleep(11);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(testInfo.clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC001
* @spec -
* @title Certificate chain: ecdsa_secp256r1_sha256. If the signature algorithm is ecdsa_secp256r1_sha256, the certificate
* and certificate chain are successfully verified.
* @precon nan
* @brief 9.1. Mandatory-to-Implement Cipher Suites line 229
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256, CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
ASSERT_TRUE(HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t))== HITLS_SUCCESS);
FRAME_CertInfo certInfo = {
"rsa_sha/ca-3072.der:rsa_sha/inter-3072.der",
NULL, NULL, NULL, NULL, NULL,};
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC002
* @spec -
* @title Certificate chain: ecdsa_secp256r1_sha256. If the signature algorithm is ecdsa_secp256r1_sha256, the
* certificate and certificate chain are successfully verified.
* @precon nan
* @brief 9.1. Mandatory-to-Implement Cipher Suites line 229
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256 };
ASSERT_TRUE(HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t))== HITLS_SUCCESS);
FRAME_CertInfo certInfo1 = {
"rsa_pss_rsae/rsa_root.der:rsa_pss_rsae/rsa_intCa.der",
NULL, NULL, NULL, NULL, NULL,};
FRAME_CertInfo certInfo2 = {
"rsa_pss_rsae/rsa_root.der:rsa_pss_rsae/rsa_intCa.der",
"rsa_pss_rsae/rsa_intCa.der", "rsa_pss_rsae/rsa_dev.der", NULL, "rsa_pss_rsae/rsa_dev.key.der", NULL,};
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo1);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo2);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC003
* @spec -
* @title Certificate chain: ecdsa_secp256r1_sha256. If the signature algorithm is ecdsa_secp256r1_sha256, the
* certificate and certificate chain are successfully verified.
* @precon nan
* @brief 9.1. Mandatory-to-Implement Cipher Suites line 229
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CERT_SIGNATURE_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
ASSERT_TRUE(HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t))== HITLS_SUCCESS);
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der:ecdsa/inter-nist521.der",
NULL, NULL, NULL, NULL, NULL,};
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVERSION_FUNC_TC001
* @spec -
* @title After the server negotiates the version number for the first time, the serverHello, Certificate, Server Key
* Exchange, Certificate Request, Server Hello Done, Certificate, Certificate Key Exchange, The
* legacy_record_version of all record messages, such as Certificate Verify, Change Cipher Spec, and Finished,
* is the negotiated version.
* @precon nan
* @brief Appendix D. Backward Compatibility line 242
* @expect 1. The expected version number is the negotiated version.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVERSION_FUNC_TC001(int flag, int type)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FrameUioUserData *ioUserData = NULL;
config = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetClientVerifySupport(config, true);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, flag, type) == HITLS_SUCCESS);
if (flag == 1) {
HITLS_Connect(client->ssl);
ioUserData = BSL_UIO_GetUserData(client->io);
} else {
HITLS_Accept(server->ssl);
ioUserData = BSL_UIO_GetUserData(server->io);
}
uint8_t *recvBuf = ioUserData->sndMsg.msg;
uint32_t recvLen = ioUserData->sndMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recVersion.data, 0x0303);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVERSION_FUNC_TC002
* @spec -
* @title Renegotiation. After the server negotiates the version number, the serverHello, Certificate, Server Key
* Exchange, Certificate Request, Server Hello Done, Certificate, Certificate Key Exchange, Certificate Verify,
* Change Cipher Spec, Finished, and other record messages legacy_record_version indicates the renegotiation
version.
* @precon nan
* @brief Appendix D. Backward Compatibility line 242
* @expect 1. The expected version number is the negotiated version.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECVERSION_FUNC_TC002(int flag, int type)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FrameUioUserData *ioUserData = NULL;
config = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetClientVerifySupport(config, true);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(server->ssl, true);
HITLS_SetRenegotiationSupport(client->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, flag, type), HITLS_SUCCESS);
if (flag == 1) {
HITLS_Connect(client->ssl);
ioUserData = BSL_UIO_GetUserData(client->io);
} else {
HITLS_Accept(server->ssl);
ioUserData = BSL_UIO_GetUserData(server->io);
}
uint8_t *recvBuf = ioUserData->sndMsg.msg;
uint32_t recvLen = ioUserData->sndMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS12, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.recVersion.data, 0x0303);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_PARSE_CA_LIST_TC001
* @spec -
* @title The CA list is parsed correctly.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server. stop the server in the TRY_RECV_CLIENT_HELLO
* state. Expected result 1 is obtained.
* 2. Get the client hello message from the server. Expected result 2 is obtained.
* 3. Add the CA list to the client hello message. Expected result 3 is obtained.
* 4. Reconnect the client. Expected result 4 is obtained.
* @expect 1. The initialization is successful.
* 2. The recvLen is not 0.
* 3. The CA list is packed correctly.
* 4. The client hello message is parsed correctly.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_PARSE_CA_LIST_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FrameUioUserData *ioUserData = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.handshakeType = CLIENT_HELLO;
frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
ASSERT_TRUE(FRAME_ParseMsgHeader(&frameType, recvBuf, recvLen, &frameMsg, &recvLen) == HITLS_SUCCESS);
uint8_t caList[] = {0x00, 0x06, 0x00, 0x04, 0x4a, 0x4b, 0x4c, 0x4d};
frameMsg.body.hsMsg.body.clientHello.caList.exState = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.caList.exType.data = HS_EX_TYPE_CERTIFICATE_AUTHORITIES;
frameMsg.body.hsMsg.body.clientHello.caList.exType.state = ASSIGNED_FIELD;
FRAME_ModifyMsgArray8(caList, sizeof(caList), &frameMsg.body.hsMsg.body.clientHello.caList.list,
&frameMsg.body.hsMsg.body.clientHello.caList.listSize);
frameMsg.body.hsMsg.body.clientHello.caList.exLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.caList.exLen.data = sizeof(caList) + sizeof(uint16_t);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
ASSERT_NE(FRAME_CreateConnection(server, client, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_extensions_2.c | C | unknown | 50,229 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "hitls_error.h"
#include "tls.h"
#include "change_cipher_spec.h"
#include "frame_tls.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_link.h"
#include "frame_io.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "stub_replace.h"
#include "hs.h"
#include "alert.h"
#include "bsl_sal.h"
#include "securec.h"
#include "app.h"
#include "hs_kx.h"
#include "hs_msg.h"
#include "rec.h"
#include "conn_init.h"
#include "parse.h"
#include "hs_common.h"
#include "common_func.h"
#include "hlt.h"
#include "process.h"
#include "hitls_crypt_init.h"
#include "rec_wrapper.h"
#define REC_TLS_RECORD_HEADER_LEN 5
#define ALERT_BODY_LEN 2u
#define READ_BUF_SIZE 18432
#define ERROR_VERSION_BIT 0x00000000U
#define READ_BUF_LEN_18K (18 * 1024)
#define BUF_SIZE_DTO_TEST (18 * 1024)
#define ROOT_DER "%s/ca.der:%s/inter.der"
#define INTCA_DER "%s/inter.der"
#define SERVER_DER "%s/server.der"
#define SERVER_KEY_DER "%s/server.key.der"
#define CLIENT_DER "%s/client.der"
#define CLIENT_KEY_DER "%s/client.key.der"
static char *g_serverName = "testServer";
uint32_t g_uiPort = 18890;
/* END_HEADER */
int32_t RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType);
int32_t STUB_RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType)
{
(void)ctx;
(void)text;
(void)textLen;
*recType = (uint8_t)REC_TYPE_APP;
return HITLS_SUCCESS;
}
void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
HITLS_KeyExchAlgo keyExType)
{
frametype->versionType = versionType;
frametype->recordType = recordType;
frametype->handshakeType = handshakeType;
frametype->keyExType = keyExType;
frametype->transportType = BSL_UIO_TCP;
}
void TestSetCertPath(HLT_Ctx_Config *ctxConfig, char *SignatureType)
{
if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA1", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA1")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA256")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA384")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA384_EE_PATH, RSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA512")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA512_EE_PATH, RSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
strlen("CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
strlen("CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
strlen("CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_ECDSA_SHA1", strlen("CERT_SIG_SCHEME_ECDSA_SHA1")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA1_CA_PATH,
ECDSA_SHA1_CHAIN_PATH,
ECDSA_SHA1_EE_PATH,
ECDSA_SHA1_PRIV_PATH,
"NULL",
"NULL");
}
}
static int SetCertPath(HLT_Ctx_Config *ctxConfig, const char *certStr, bool isServer)
{
char caCertPath[50];
char chainCertPath[30];
char eeCertPath[30];
char privKeyPath[30];
int32_t ret = sprintf_s(caCertPath, sizeof(caCertPath), ROOT_DER, certStr, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(chainCertPath, sizeof(chainCertPath), INTCA_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(eeCertPath, sizeof(eeCertPath), isServer ? SERVER_DER : CLIENT_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(privKeyPath, sizeof(privKeyPath), isServer ? SERVER_KEY_DER : CLIENT_KEY_DER, certStr);
ASSERT_TRUE(ret > 0);
HLT_SetCaCertPath(ctxConfig, (char *)caCertPath);
HLT_SetChainCertPath(ctxConfig, (char *)chainCertPath);
HLT_SetEeCertPath(ctxConfig, (char *)eeCertPath);
HLT_SetPrivKeyPath(ctxConfig, (char *)privKeyPath);
return 0;
EXIT:
return -1;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVE_RENEGOTIATION_REQUEST_FUNC_TC001
* @spec Because TLS 1.3 forbids renegotiation, if a server has negotiated
* TLS 1.3 and receives a ClientHello at any other time, it MUST
* terminate the connection with an "unexpected_message" alert.
* @title Initialize the client server to tls1.3. After the connection is established, the client sends a client hello message.
* The expected server sends an alarm after receiving the message and disconnects the connection.
* @precon nan
* @brief 4.1.1. ryptographic Negotiation row15
* Initialize the client server to tls1.3. After the connection is established, the client sends a client hello
* message.
* The expected server sends an alarm after receiving the message and disconnects the connection.
* @expect 1. The server sends an alarm and the connection is disconnected.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVE_RENEGOTIATION_REQUEST_FUNC_TC001()
{
FRAME_Init();
/* Initialize the client server to tls1.3. */
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportRenegotiation = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
/* After the connection is established, the client sends a client hello message. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
FrameMsg recMsg = {0};
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg + REC_TLS_RECORD_HEADER_LEN,
ioServerData->recMsg.len - REC_TLS_RECORD_HEADER_LEN) == EOK);
recMsg.len = ioServerData->recMsg.len - 5;
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS13);
ASSERT_TRUE(clientTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS13);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_HANDSHAKE, recMsg.msg, recMsg.len), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyClientHello(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->supportedVersion.exState = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_DOWN_GRADE_FUNC_TC001
* @spec random: 32 bytes generated by a secure random number generator. See
* Appendix C for additional information. The last 8 bytes MUST be
* overwritten as described below if negotiating TLS 1.2 or TLS 1.1,
* but the remaining bytes MUST be random. This structure is
* generated by the server and MUST be generated independently of the ClientHello.random.
* @title Initialize the client and server to tls1.3. Delete the supportversion extension when sending clienthello
* messages.
* After receiving the message, the server negotiates with the TLS1.2 and returns the serverhello after the random
* number is overwrritened.
* After receiving the serverhello message, the client checks the random number and sends the
ALERT_ELLEGAL_PARAMETER alarm.
* @precon nan
* @brief 4.1.3. Server Hello row24
* Initialize the client server to tls1.3 and delete the supportversion extension when sending client hello
* messages.
* After receiving the message, the server negotiates TLS1.2 and returns the serverhello after the random number is
* overwrritened.
* After receiving the serverhello message, the client checks the random number and sends the
ALERT_LOCKGAL_PARAMETER a larm.
* @expect 1. The client sends the ALERT_AIRGAL_PARAMETER alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_DOWN_GRADE_FUNC_TC001()
{
FRAME_Init();
/* Initialize the client and server to tls1.3. Delete the supportversion extension when sending clienthello
* messages. */
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_CFG_SetVersionSupport(tlsConfig, 0x00000030U);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyClientHello};
RegisterWrapper(wrapper);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
/* After receiving the message, the server negotiates with the TLS1.2 and returns the serverhello after the random
* number is overwrritened. */
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
/* After receiving the serverhello message, the client checks the random number and sends the
* ALERT_ELLEGAL_PARAMETER alarm. */
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC001
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client. The certificate is received after the clienthello message is sent.
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, receiving the certificate after sending the clienthello message.
* @expect 1. Return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FRAME_LinkObj *client2 = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server2 = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client2 != NULL);
ASSERT_TRUE(server2 != NULL);
HITLS_Ctx *clientTlsCtx2 = FRAME_GetTlsCtx(client2);
HITLS_Ctx *serverTlsCtx2 = FRAME_GetTlsCtx(server2);
ASSERT_TRUE(FRAME_CreateConnection(client2, server2, true, TRY_RECV_CERTIFICATE_REQUEST) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx2->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx2->state == CM_STATE_HANDSHAKING);
char *buffer = BSL_SAL_Calloc(1u, MAX_RECORD_LENTH);
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(client2->io);
uint8_t *recvBuf2 = ioUserData2->recMsg.msg;
uint32_t recvLen2 = ioUserData2->recMsg.len;
memcpy_s(buffer, MAX_RECORD_LENTH, recvBuf2, recvLen2);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->recMsg.len = 0;
ASSERT_EQ(FRAME_TransportRecMsg(client->io, buffer, recvLen2), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
FRAME_FreeLink(client2);
FRAME_FreeLink(server2);
BSL_SAL_FREE(buffer);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC002
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, unidirectional authentication, certificateverify received after receiving the serverhello message
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, unidirectional authentication, certificateverify received after receiving the server hello message
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
client->ssl->hsCtx->state = TRY_RECV_ENCRYPTED_EXTENSIONS;
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC003
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, two-way authentication, certificate received after receiving the serverhello message
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, two-way authentication, certificate received after receiving the serverhello message
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
client->ssl->hsCtx->state = TRY_RECV_ENCRYPTED_EXTENSIONS;
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC004
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, two-way authentication, receiving certificateverify after receiving certificaterequest
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, two-way authentication, receiving certificateverify after receiving certificaterequest
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
client->ssl->hsCtx->state = TRY_RECV_CERTIFICATE;
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC005
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, unidirectional authentication. After receiving the certificate, the client receives the finished message.
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, unidirectional authentication. After receiving the certificate, the client receives the finished message.
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC005()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
client->ssl->hsCtx->state = TRY_RECV_CERTIFICATE_VERIFY;
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC006
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, unidirectional authentication, receiving appdata after receiving certificateverify
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, unidirectional authentication, receiving appdata after receiving certificateverify
* @expect 1. Return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC006()
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo = { 0 };
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
STUB_Replace(&tmpRpInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC007
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title Client, unidirectional authentication, certificateverify received after receiving encryptedextensions
* @precon nan
* @brief 4.Handshake Protocol row9
* Client, unidirectional authentication, certificateverify received after receiving encryptedextensions
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC007()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
client->ssl->hsCtx->state = TRY_RECV_CERTIFICATE_REQUEST;
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC008
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title The server receives the certificate message in idle state.
* @precon nan
* @brief 4.Handshake Protocol row9
* The server receives a certificate message in idle state.
* @expect 1. Return HITLS_CM_LINK_UNESTABLICED
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC008()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FRAME_LinkObj *client2 = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server2 = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client2 != NULL);
ASSERT_TRUE(server2 != NULL);
HITLS_Ctx *clientTlsCtx2 = FRAME_GetTlsCtx(client2);
HITLS_Ctx *serverTlsCtx2 = FRAME_GetTlsCtx(server2);
ASSERT_TRUE(FRAME_CreateConnection(client2, server2, false, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx2->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx2->state == CM_STATE_HANDSHAKING);
char *buffer = BSL_SAL_Calloc(1u, MAX_RECORD_LENTH);
FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(server2->io);
uint8_t *recvBuf2 = ioUserData2->recMsg.msg;
uint32_t recvLen2 = ioUserData2->recMsg.len;
memcpy_s(buffer, MAX_RECORD_LENTH, recvBuf2, recvLen2);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
ioUserData->recMsg.len = 0;
ASSERT_EQ(FRAME_TransportRecMsg(server->io, buffer, recvLen2), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_CM_LINK_UNESTABLISHED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
FRAME_FreeLink(client2);
FRAME_FreeLink(server2);
BSL_SAL_FREE(buffer);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC009
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title The server uses unidirectional authentication. The certificate message is received after the client hello message is received.
* @precon nan
* @brief 4.Handshake Protocol row9
* Server, unidirectional authentication, certificate message received after receiving client hello messages.
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC009()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
server->ssl->hsCtx->state = TRY_RECV_FINISH;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC010
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title server, unidirectional authentication, receiving the app message after receiving the client hello message
* @precon nan
* @brief 4.Handshake Protocol row9
* Server, unidirectional authentication, receiving the app message after receiving the client hello message
* @expect 1. Return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC010()
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo = { 0 };
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
STUB_Replace(&tmpRpInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC011
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title server, two-way authentication, certificateverify message received after client hello is received
* @precon nan
* @brief 4.Handshake Protocol row9
* The server, two-way authentication, receives the certificateverify message after receiving the client hello
* message.
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC011()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC012
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title server, two-way authentication, receiving the finish message after receiving the certificate message
* @precon nan
* @brief 4.Handshake Protocol row9
* The server, two-way authentication, receives the finish message after receiving the certificate message.
* @expect 1. Return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC012()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = false;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
server->ssl->hsCtx->state = TRY_RECV_CERTIFICATE_VERIFY;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC013
* @spec Protocol messages MUST be sent in the order defined in Section 4.4.1
* and shown in the diagrams in Section 2. A peer which receives a
* handshake message in an unexpected order MUST abort the handshake
* with an "unexpected_message" alert.
* @title server, two-way authentication, receiving the app message after receiving the certificateverify message
* @precon nan
* @brief 4.Handshake Protocol row9
* The server, two-way authentication, receives the app message after receiving the certificateverify message.
* @expect 1. Return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_UNEXPECTMSG_FUNC_TC013()
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo = { 0 };
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
STUB_Replace(&tmpRpInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
}
/* END_CASE */
static void Test_ModifyClientHelloNullKeyshare(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->keyshares.exLen.data = 2;
clientMsg->keyshares.exKeyShareLen.data = 0;
clientMsg->keyshares.exKeyShares.state = MISSING_FIELD;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_NULL_KEYSHARE_FUNC_TC001
* @spec If the server selects an (EC)DHE group and the client did not offer a
* compatible "key_share" extension in the initial ClientHello, the
* server MUST respond with a HelloRetryRequest (Section 4.1.4) message.
* @title Construct the clienthello message sent by the client. The key_share extension is empty and the psk extension is
not carried. The server is expected to send a hellorequest message.
* @precon nan
* @brief 4.Handshake Protocol row9
* Construct the client hello message sent by the client. The key_share extension is empty and the psk extension is
not carried. The server is expected to send a hellorequest message.
* @expect 1. The server sends hrr and waits for receiving the second client hello.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_NULL_KEYSHARE_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ModifyClientHelloNullKeyshare};
RegisterWrapper(wrapper);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ClearWrapper();
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC001
* @spec When a client first connects to a server, it is REQUIRED to send the
* ClientHello as its first TLS message. The client will also send a
* ClientHello when the server has responded to its ClientHello with a
* HelloRetryRequest. In that case, the client MUST send the same
* ClientHello without modification, except as follows:
* @title 1. Set the client server to tls1.3. Set the first group server not to support the client, but the second group
* server to support the client,
* The server is expected to send a helloretryrequest message. The client hello message is sent after the client
* hello message is updated. The group in the keyshare of the client hello message is changed. The connection is
* expected to be successfully set up.
* @precon nan
* @brief 4.1.2. Client Hello row14
* 1. Set the client server to tls1.3. Set the first group server to not support the client server, and set the
* second group server to support the client server.
* The server is expected to send a helloretryrequest message. The client hello message is sent after the client
* hello message is updated. The group in the keyshare of the client hello message is changed. The connection is
* expected to be successfully established.
* @expect 1. The server sends hrr and the connection is successfully established.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC001()
{
FRAME_Init();
/* Set the client server to tls1.3. Set the first group server not to support the client, but the second group
* server to support the client */
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC002
* @spec When a client first connects to a server, it is REQUIRED to send the
* ClientHello as its first TLS message. The client will also send a
* ClientHello when the server has responded to its ClientHello with a
* HelloRetryRequest. In that case, the client MUST send the same
* ClientHello without modification, except as follows:
* @title 1. Set the client server to tls1.3. Set the first group server not to support the client, but the second group
* server to support the client,
* The server is expected to send a helloretryrequest message, construct the same client hello message sent by the
* client, and the server is expected to reject connection establishment.
* @precon nan
* @brief 4.1.2. Client Hello row14
* 1. Set the client server to tls1.3. Set the first group server not to support the client, and the second group
* server to support the client.
* The server is expected to send a helloretryrequest message, construct the same client hello message sent by the
* client, and the server is expected to reject connection establishment.
* @expect 1. Link establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
char *buffer = BSL_SAL_Calloc(1u, MAX_RECORD_LENTH);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
memcpy_s(buffer, MAX_RECORD_LENTH, recvBuf, recvLen);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, recvBuf, recvLen) == HITLS_SUCCESS);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_INVALID_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
BSL_SAL_FREE(buffer);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC003
* @spec When a client first connects to a server, it is REQUIRED to send the
* ClientHello as its first TLS message. The client will also send a
* ClientHello when the server has responded to its ClientHello with a
* HelloRetryRequest. In that case, the client MUST send the same
* ClientHello without modification, except as follows:
* @titleSet the client server to tls1.3, and set the first group server not to support the client, and the second group
* server to support the client,
* The hash of the cipher suite on the server does not match the hash corresponding to the psk set on the
* client. The server is expected to send a helloretryrequest message,
* The client resends the client hello message and removes the psk extension.
* @precon nan
* @brief 4.1.2. Client Hello row14
* Set the client server to tls1.3. Set the first group server not to support the client, but the second group
* server to support the client,
* The hash of the cipher suite on the server does not match the hash corresponding to the psk set on the
* client. The server is expected to send a helloretryrequest message,
* The client resends the client hello message and removes the psk extension.
* @expect 1. The second clienthello does not contain the psk.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SECOND_GROUP_SUPPORT_FUNC_TC003()
{
FRAME_Init();
HITLS_Session *Session = {0};
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
Session = HITLS_GetDupSession(client->ssl);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(HITLS_SetSession(client->ssl, Session) == HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->psks.exState == INITIAL_FIELD);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ioUserData = BSL_UIO_GetUserData(client->io);
recvBuf = ioUserData->recMsg.msg;
recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->cipherSuite.data = HITLS_AES_128_GCM_SHA256;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ioUserData = BSL_UIO_GetUserData(server->io);
recvBuf = ioUserData->recMsg.msg;
recvLen = ioUserData->recMsg.len;
parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->psks.exState == MISSING_FIELD);
FRAME_CleanMsg(&frameType, &frameMsg);
EXIT:
HITLS_SESS_Free(Session);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RENEGOTIATION_OLD_VERSION_FUNC_TC001
* @spec If a server established a TLS connection with a previous version of
* TLS and receives a TLS 1.3 ClientHello in a renegotiation, it MUST
* retain the previous protocol version. In particular, it MUST NOT
* negotiate TLS 1.3.
* @title 1. On the server end of 1.3, after the connection between 1.2 and 1.2 is successfully established, initiate
renegotiation and change the client version to 1.3. The supported version includes tls1.2. The expected version
is tls1.2.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation row16
* 1. On the 1.3 server, after the 1.2 connection is successfully established, initiate renegotiation and change the
client version to 1.3. The supported version includes tls1.2. The tls1.2 version is expected to be negotiated.
* @expect 1. The TLS1.2 version is expected to be negotiated.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RENEGOTIATION_OLD_VERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS13Config();
tlsConfig_s->isSupportClientVerify = true;
tlsConfig_s->isSupportRenegotiation = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLSConfig();
HITLS_CFG_SetVersionSupport(tlsConfig_c, 0x00000010U);
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
tlsConfig_c->isSupportRenegotiation = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
ASSERT_TRUE(clientTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
HITLS_CFG_SetVersionSupport(&(clientTlsCtx->config.tlsConfig), 0x00000030U);
ASSERT_EQ(HITLS_Renegotiate(clientTlsCtx), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_RENEGOTIATION);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_s);
HITLS_CFG_FreeConfig(tlsConfig_c);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RENEGOTIATION_OLD_VERSION_FUNC_TC002
* @spec If a server established a TLS connection with a previous version of
* TLS and receives a TLS 1.3 ClientHello in a renegotiation, it MUST
* retain the previous protocol version. In particular, it MUST NOT
* negotiate TLS 1.3.
* @title 1. On the server end of 1.2, after the connection is successfully established, the client version is changed to 1.3
* and the minimum supported version is tls1.3. The renegotiation fails.
* @precon nan
* @brief 4.1.1. Cryptographic Negotiation row16
* 1. After the connection between the client and the client is set up successfully, the client version is changed
* to 1.3 and the minimum supported version is tls1.3. The renegotiation fails.
* @expect 1. Expected renegotiation failure
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RENEGOTIATION_OLD_VERSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS13Config();
tlsConfig_s->isSupportClientVerify = true;
tlsConfig_s->isSupportRenegotiation = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384, HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS12Config();
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
tlsConfig_c->isSupportRenegotiation = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
ASSERT_TRUE(clientTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
ASSERT_EQ(HITLS_Renegotiate(clientTlsCtx), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_RENEGOTIATION);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
HITLS_CFG_SetVersionSupport(&(clientTlsCtx->config.tlsConfig), 0x00000020U);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_s);
HITLS_CFG_FreeConfig(tlsConfig_c);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC001
* @spec In TLS 1.3, the client indicates its version preferences in the
* "supported_versions" extension (Section 4.2.1) and the
* legacy_version field MUST be set to 0x0303, which is the version
* number for TLS 1.2. TLS 1.3 ClientHellos are identified as having a legacy_version of 0x0303 and a
* supported_versions extension
* present with 0x0304 as the highest version indicated therein.
* @title The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello
* message is changed to 0x0302. The server is expected to return an alert.
* @precon nan
* @brief 4.1.2. Client Hello row17
* The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello message
* is changed to 0x0302. The server is expected to return an alert.
* @expect 1. The server sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
/* The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello
* message is changed to 0x0302 */
clientMsg->version.data = 0x0302;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC002
* @spec In TLS 1.3, the client indicates its version preferences in the
* "supported_versions" extension (Section 4.2.1) and the
* legacy_version field MUST be set to 0x0303, which is the version
* number for TLS 1.2. TLS 1.3 ClientHellos are identified as having a legacy_version of 0x0303 and a
* supported_versions extension present with 0x0304 as the highest version indicated therein.
* @title The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello
* message is changed to 0x0304. The server is expected to return an alert.
* @precon nan
* @brief 4.1.2. Client Hello row17
* The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello message
* is changed to 0x0304. The server is expected to return an alert.
* @expect 1. The server sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
/* The client server is initialized to the tls1.3 version, and the legacy_version in the sent clienthello
* message is changed to 0x0304. */
clientMsg->version.data = 0x0304;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC001
* @spec A client which has a cached session ID set by a pre-TLS 1.3 server SHOULD set this field to that value.
* In compatibility mode (see Appendix D.4),this field MUST be non-empty, so a client not offering a
* pre-TLS 1.3 session MUST generate a new 32-byte value. This value need not be random but SHOULD be
unpredictable to avoid
* implementations fixating on a specific value (also known as
* ossification). Otherwise, it MUST be set as a zero-length vector
* (i.e., a zero-valued single byte length field).
* @title Set the client server to tls1.3 and check the legacy_session_id of the sent clienthello message. The value
is a 32-byte value.
* @precon nan
* @brief 4.1.2. Client Hello row18
* Set the client server to tls1.3 and check the legacy_session_id of the sent clienthello to a 32-byte value.
* @expect 1. Check the session ID.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Set the client server to tls1.3 and check the legacy_session_id of the sent clienthello message. The value
* is a 32-byte value. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_EQ(clientMsg->sessionIdSize.data, 32);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyClientHello_Sessionid_002(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->sessionIdSize.data = 0;
clientMsg->sessionId.size = 0;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC002
* @spec A client which has a cached session ID set by a pre-TLS 1.3 server SHOULD set this field to that value.
* In compatibility mode (see Appendix D.4),this field MUST be non-empty, so a client not offering a
* pre-TLS 1.3 session MUST generate a new 32-byte value. This value need not be random but SHOULD be
unpredictable to avoid implementations fixating on a specific value (also known as
* ossification). Otherwise, it MUST be set as a zero-length vector
* (i.e., a zero-valued single byte length field).
* @title Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message
to a single byte 0. The expected connection establishment is successful.
* @precon nan
* @brief 4.1.2. Client Hello row18
* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message
to a single byte 0. The expected connection establishment is successful.
* @expect 1. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
/* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message
* to a single byte 0 */
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyClientHello_Sessionid_002};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
clientTlsCtx->hsCtx->sessionIdSize = 0;
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC003
* @spec A client which has a cached session ID set by a pre-TLS 1.3 server SHOULD set this field to that value.
* In compatibility mode (see Appendix D.4),this field MUST be non-empty, so a client not offering a
* pre-TLS 1.3 session MUST generate a new 32-byte value. This value need not be random but SHOULD be
* unpredictable to avoid implementations fixating on a specific value (also known as
* ossification). Otherwise, it MUST be set as a zero-length vector
* (i.e., a zero-valued single byte length field).
* @title Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
* 2-byte 0. The expected connection establishment failure
* @precon nan
* @brief 4.1.2. Client Hello row18
* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
* two bytes 0. The expected connection establishment fails.
* @expect 1. Link establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->sessionIdSize.data = 0;
/* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
* 2-byte 0. */
clientMsg->sessionId.size = 2;
clientMsg->sessionId.data[0] = 0x00;
clientMsg->sessionId.data[1] = 0x00;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_PARSE_INVALID_MSG_LEN);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC004
* @spec A client which has a cached session ID set by a pre-TLS 1.3 server SHOULD set this field to that value.
* In compatibility mode (see Appendix D.4),this field MUST be non-empty, so a client not offering a
* pre-TLS 1.3 session MUST generate a new 32-byte value. This value need not be random but SHOULD be unpredictable
to avoid implementations fixating on a specific value (also known as ossification). Otherwise, it MUST be set as
a zero-length vector
* (i.e., a zero-valued single byte length field).
* @title Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
1 byte 1. The expected connection establishment fails.
* @precon nan
* @brief 4.1.2. Client Hello row18
* Set the client server to tls1.3 and construct the value of legacy_session_id in the clienthello message to 1
byte 1. The expected connection establishment fails.
* @expect 1. Connect establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
/* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
* 1 byte 1. */
clientMsg->sessionIdSize.data = 1;
clientMsg->sessionId.size = 1;
clientMsg->sessionId.data[0] = 0x01;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_PARSE_INVALID_MSG_LEN);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyClientHello_Sessionid_005(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->sessionIdSize.data = 26;
clientMsg->sessionId.size = 26;
const uint8_t sessionId_temp[26] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
ASSERT_TRUE(memcpy_s(clientMsg->sessionId.data, sizeof(sessionId_temp) / sizeof(uint8_t),
sessionId_temp, sizeof(sessionId_temp) / sizeof(uint8_t)) == 0);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC005
* @spec A client which has a cached session ID set by a pre-TLS 1.3 server SHOULD set this field to that value.
* In compatibility mode (see Appendix D.4),this field MUST be non-empty, so a client not offering a
* pre-TLS 1.3 session MUST generate a new 32-byte value. This value need not be random but SHOULD be unpredictable
to avoid implementations fixating on a specific value (also known as ossification). Otherwise, it MUST be set as
a zero-length vector (i.e., a zero-valued single byte length field).
* @title Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
26 bytes 0. The expected connection is successfully established..
* @precon nan
* @brief 4.1.2. Client Hello row18
* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
26 bytes 0. The expected link establishment success.
* @expect 1. Link establishment success.
* @expect 1. Link establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SESSION_ID_FUNC_TC005()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
/* Set the client server to tls1.3 and construct the value of legacy_session_id in the sent clienthello message to
* 26 bytes 0. */
RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ModifyClientHello_Sessionid_005};
RegisterWrapper(wrapper);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CH_CIPHERSUITES_FUNC_TC001
* @spec cipher_suites: A list of the symmetric cipher options supported by
* the client, specifically the record protection algorithm
* (including secret key length) and a hash to be used with HKDF, in
* descending order of client preference. Values are defined in
* Appendix B.4. If the list contains cipher suites that the server
* does not recognize, support, or wish to use, the server MUST
* ignore those cipher suites and process the remaining ones as
* usual. If the client is attempting a PSK key establishment, it SHOULD advertise at least one cipher suite
* indicating a Hash associated with the PSK.
* @title clienthello The first three cipher suites are abnormal values, tls1.2 cipher suites, and tls1.3 cipher suites
* that are not configured on the server, The fourth cipher suite is supported by the server. It is expected that
* the server selects the fourth cipher suite to establish a connection.
* @precon nan
* @brief 4.1.2. Client Hello row19
* The first three cipher suites of client hello are abnormal values, tls1.2 cipher suites, and tls1.3 cipher
* suites that are not configured on the server,
* The fourth cipher suite is supported by the server. It is expected that the server selects the fourth cipher
* suite to establish a connection.
* @expect 1. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CH_CIPHERSUITES_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config_c = HITLS_CFG_NewTLS13Config();
HITLS_Config *config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
config_c->isSupportClientVerify = true;
config_s->isSupportClientVerify = true;
/* clienthello The first three cipher suites are abnormal values, tls1.2 cipher suites, and tls1.3 cipher suites
* that are not configured on the server, The fourth cipher suite is supported by the server. */
uint16_t cipherSuits_c[] = {
0x0041, HITLS_DHE_RSA_WITH_AES_256_CBC_SHA256, HITLS_CHACHA20_POLY1305_SHA256, HITLS_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(config_c, cipherSuits_c, sizeof(cipherSuits_c) / sizeof(uint16_t));
uint16_t cipherSuits_s[] = {HITLS_AES_256_GCM_SHA384};
HITLS_CFG_SetCipherSuites(config_s, cipherSuits_s, sizeof(cipherSuits_s) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(server->ssl->negotiatedInfo.cipherSuiteInfo.cipherSuite == HITLS_AES_256_GCM_SHA384);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CH_CIPHERSUITES_FUNC_TC002
* @spec cipher_suites: A list of the symmetric cipher options supported by
* the client, specifically the record protection algorithm
* (including secret key length) and a hash to be used with HKDF, in
* descending order of client preference. Values are defined in
* Appendix B.4. If the list contains cipher suites that the server
* does not recognize, support, or wish to use, the server MUST
* ignore those cipher suites and process the remaining ones as
* usual. If the client is attempting a PSK key establishment, it SHOULD advertise at least one cipher suite
* indicating a Hash associated with the PSK.
* @title The hash of the configured psk does not match the specified cipher suite when tls1.3 is set on the client
* server. The expected psk does not exist in the client hello.
* @precon nan
* @brief 4.1.2. Client Hello row19
* When tls1.3 is set on the client and server, the hash of the psk does not match the specified cipher suite. It
* is expected that the psk does not exist in the client hello.
* @expect 1. No psk is expected in the clienthello.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CH_CIPHERSUITES_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_CFG_SetPskServerCallback(tlsConfig, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(tlsConfig, (HITLS_PskClientCb)ExampleClientCb);
/* The hash of the configured psk does not match the specified cipher suite when tls1.3 is set on the client
* server. */
uint16_t cipherSuite = HITLS_AES_256_GCM_SHA384;
HITLS_CFG_SetCipherSuites(tlsConfig, &cipherSuite, 1);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
ASSERT_TRUE(clientMsg->psks.exState == MISSING_FIELD);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC001
* @spec legacy_compression_methods: Versions of TLS before 1.3 supported
* compression with the list of supported compression methods being
* sent in this field. For every TLS 1.3 ClientHello, this vector
* MUST contain exactly one byte, set to zero, which corresponds to
* the "null" compression method in prior versions of TLS. If a
* TLS 1.3 ClientHello is received with any other value in this
* field, the server MUST abort the handshake with an
* "illegal_parameter" alert. Note that TLS 1.3 servers might
* receive TLS 1.2 or prior ClientHellos which contain other
* compression methods and (if negotiating such a prior version) MUST follow the procedures for the appropriate
* prior version of TLS.
* @title Construct clienthello compression algorithm. The value is 0. The server is expected to return a decode
* error alert.
* @precon nan
* @brief 4.1.2. Client Hello row20
* Construct the clienthello compression algorithm with a two-byte value and the value 0. The server is
* expected to return a decode error alert.
* @expect 1. Return the decode error alert.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
/* Construct clienthello compression algorithm. The value is 0. */
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->compressionMethodsLen.data = 2;
clientMsg->compressionMethods.data = realloc(clientMsg->compressionMethods.data, 2 * sizeof(uint8_t));
clientMsg->compressionMethods.size = 2;
clientMsg->compressionMethods.data[0] = 0x00;
clientMsg->compressionMethods.data[1] = 0x00;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_INVALID_COMPRESSION_METHOD);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC002
* @spec legacy_compression_methods: Versions of TLS before 1.3 supported
* compression with the list of supported compression methods being
* sent in this field. For every TLS 1.3 ClientHello, this vector
* MUST contain exactly one byte, set to zero, which corresponds to
* the "null" compression method in prior versions of TLS. If a
* TLS 1.3 ClientHello is received with any other value in this
* field, the server MUST abort the handshake with an
* "illegal_parameter" alert. Note that TLS 1.3 servers might
* receive TLS 1.2 or prior ClientHellos which contain other
* compression methods and (if negotiating such a prior version) MUST follow the procedures for the appropriate
* prior version of TLS.
* @title Constructs clienthello compression algorithm. The value is 1, indicating that the server returns
* illegal_parameter alert.
* @precon nan
* @brief 4.1.2. Client Hello row20
* Construct the clienthello compression algorithm with a bit of one byte and the value is 1. The server is
* expected to return illegal_parameter alert.
* @expect 1. Return ALERT_ELLEGAL_PARAMETER
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
/* Construct the clienthello compression algorithm with a bit of one byte and the value is 1. */
clientMsg->compressionMethodsLen.data = 1;
clientMsg->compressionMethods.size = 1;
clientMsg->compressionMethods.data[0] = 0x01;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_INVALID_COMPRESSION_METHOD);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC003
* @spec legacy_compression_methods: Versions of TLS before 1.3 supported
* compression with the list of supported compression methods being
* sent in this field. For every TLS 1.3 ClientHello, this vector
* MUST contain exactly one byte, set to zero, which corresponds to
* the "null" compression method in prior versions of TLS. If a
* TLS 1.3 ClientHello is received with any other value in this
* field, the server MUST abort the handshake with an
* "illegal_parameter" alert. Note that TLS 1.3 servers might
* receive TLS 1.2 or prior ClientHellos which contain other
* compression methods and (if negotiating such a prior version) MUST follow the procedures for the appropriate
* prior version of TLS.
* @title Construct that the client version is TLS1.2 and the server version is TLS1.3. It is expected that the connection
* can be set up normally.
* @precon nan
* @brief 4.1.2. Client Hello row20
* Construct the scenario where the client version is TLS1.2 and the server version is TLS1.3 and the expected
* connection establishment is normal.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_COMPRESSION_METHOD_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS13Config();
tlsConfig_s->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384, HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS12Config();
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
ASSERT_TRUE(clientTlsCtx->negotiatedInfo.version == HITLS_VERSION_TLS12);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_s);
HITLS_CFG_FreeConfig(tlsConfig_c);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_UNKNOWN_EXTENSION_FUNC_TC001
* @spec extensions: Clients request extended functionality from servers by
* sending data in the extensions field. The actual "Extension"
* format is defined in Section 4.2. In TLS 1.3, the use of certain extensions is mandatory, as functionality has
* moved into extensions to preserve ClientHello compatibility with previous
* versions of TLS. Servers MUST ignore unrecognized extensions
* @title Set the client server to tls1.3, construct a client hello message that carries the sni extension, and change
* the sni extension type to 55 (unknown extension). It is expected that the server can establish a connection normally.
* @precon nan
* @brief 4.1.2. Client Hello row21
* Set the client server to tls1.3, construct a client hello message that carries the SNI extension, and change the
* SNI extension type to 55 (unknown extension). The server is expected to establish a connection normally.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_UNKNOWN_EXTENSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetServerName(tlsConfig, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName));
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = {0};
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
/** Set the client server to tls1.3, construct a client hello message that carries the sni extension, and change
* the sni extension type to 55 (unknown extension). */
clientMsg->serverName.exType.data = 55;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_ENCRYPTED_EXTENSIONS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC001
* @spec TLS 1.3 servers will need to perform this check first and
* only attempt to negotiate TLS 1.3 if the "supported_versions"
* extension is present. If negotiating a version of TLS prior to 1.3,
* a server MUST check that the message either contains no data after
* legacy_compression_methods or that it contains a valid extensions
* block with no data following. If not, then it MUST abort the
* handshake with a "decode_error" alert.
* @title: Set tls1.2 on the client and tls1.3 on the server. Construct the clienthello compression algorithm without
* any extension. It is expected that the server can establish a connection.
* @precon nan
* @brief 4.1.2. Client Hello row22
* Set TLS 1.2 on the client and TLS 1.3 on the server. Construct the clienthello compression algorithm without
* any extension. It is expected that the server can establish a connection.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLSConfig();
tlsConfig_s->isSupportExtendMasterSecret = false;
tlsConfig_s->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_DHE_DSS_WITH_AES_128_GCM_SHA256,
HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_ECDHE_ECDSA_WITH_AES_128_CCM,
HITLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
/* Set tls1.2 on the client and tls1.3 on the server. Construct the clienthello compression algorithm without
* any extension. */
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS12Config();
tlsConfig_c->isSupportExtendMasterSecret = false;
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extensionState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_c);
HITLS_CFG_FreeConfig(tlsConfig_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC002
* @spec TLS 1.3 servers will need to perform this check first and
* only attempt to negotiate TLS 1.3 if the "supported_versions"
* extension is present. If negotiating a version of TLS prior to 1.3,
* a server MUST check that the message either contains no data after
* legacy_compression_methods or that it contains a valid extensions
* block with no data following. If not, then it MUST abort the
* handshake with a "decode_error" alert.
* @title: Set the client TLS 1.2 and server TLS 1.3. Construct the clienthello compression algorithm and carry the
* extension and 3-byte data after the extension. The expected connection establishment fails and the decode_error
* alert is returned.
* @precon nan
* @brief 4.1.2. Client Hello row22
* Set TLS 1.2 on the client and TLS 1.3 on the server. Construct the compression algorithm of the clienthello
* message and carry the extension. After the extension, carry the 3-byte data. In this case, the connection
* establishment fails and the decode_error alert is returned.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLSConfig();
tlsConfig_s->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLSConfig();
HITLS_CFG_SetVersionSupport(tlsConfig_c, 0x00000010U);
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint32_t *recvLen = &ioUserData->recMsg.len;
uint8_t *recvBuf = ioUserData->recMsg.msg;
ASSERT_TRUE(recvLen != 0);
recvBuf[4] += 3;
recvBuf[8] += 3;
recvBuf[*recvLen] = 0x01;
recvBuf[(*recvLen)+1] = 0x01;
recvBuf[(*recvLen)+2] = 0x01;
*recvLen += 3;
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_c);
HITLS_CFG_FreeConfig(tlsConfig_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC003
* @spec TLS 1.3 servers will need to perform this check first and
* only attempt to negotiate TLS 1.3 if the "supported_versions"
* extension is present. If negotiating a version of TLS prior to 1.3,
* a server MUST check that the message either contains no data after
* legacy_compression_methods or that it contains a valid extensions
* block with no data following. If not, then it MUST abort the
* handshake with a "decode_error" alert.
* @title: Set the client TLS 1.2 and server TLS 1.3. Construct the clienthello compression algorithm and carry 3-byte
* data without extension. Expected connection establishment failure and return decode_error alert.
* @precon nan
* @brief 4.1.2. Client Hello row22
* Set TLS 1.2 on the client and TLS 1.3 on the server. Construct the compression algorithm of the clienthello
* message without extension and carry 3-byte data. Expectedly, connection establishment fails and decode_error
* alert is returned.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS13Config();
tlsConfig_s->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384, HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS12Config();
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->extensionState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
/* Set the client TLS 1.2 and server TLS 1.3. Construct the clienthello compression algorithm and carry 3-byte
* data without extension. */
sendBuf[4] += 3;
sendBuf[8] += 3;
sendBuf[sendLen] = 0x01;
sendBuf[sendLen + 1] = 0x01;
sendBuf[sendLen + 2] = 0x01;
sendLen += 3;
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_c);
HITLS_CFG_FreeConfig(tlsConfig_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC004
* @spec TLS 1.3 servers will need to perform this check first and
* only attempt to negotiate TLS 1.3 if the "supported_versions"
* extension is present. If negotiating a version of TLS prior to 1.3,
* a server MUST check that the message either contains no data after
* legacy_compression_methods or that it contains a valid extensions
* block with no data following. If not, then it MUST abort the
* handshake with a "decode_error" alert.
* @title 4. Set tls1.2 on the client and tls1.3 on the server. Construct the clienthello message that carries the SNI
* extension. The SNI length is too large and does not match the content. As a result, the expected connection
* establishment fails and a decode_error alert message is returned.
* @precon nan
* @brief 4.1.2. Client Hello row22
* 4. Set tls1.2 on the client and tls1.3 on the server. Construct a clienthello message that carries the SNI
* extension. The SNI length is too large and does not match the content. As a result, the expected connection
* establishment fails and a decode_error alert message is returned.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_DATA_AFTER_COMPRESSION_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS13Config();
tlsConfig_s->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_s, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_s, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig_s, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_s != NULL);
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetServerName(tlsConfig_c, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName));
tlsConfig_c->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_c != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->serverName.exDataLen.data += 1;
/* Set tls1.2 on the client and tls1.3 on the server. Construct the clienthello message that carries the SNI
* extension. */
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_PARSE_INVALID_MSG_LEN);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_c);
HITLS_CFG_FreeConfig(tlsConfig_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_LEGACY_VERSION_FUNC_TC001
* @spec legacy_version: In previous versions of TLS, this field was used for version negotiation and represented the
* selected version number
* for the connection. In TLS 1.3, the TLS server indicates
* its version using the "supported_versions" extension
* (Section 4.2.1), and the legacy_version field MUST be set to
* 0x0303, which is the version number for TLS 1.2. (See Appendix D
* for details about backward compatibility.)
* @title The client server is initialized to the tls1.3 version. The legacy_version in the sent serverhello message
* is changed to 0x0304. The client is expected to return illegal_parameter alert.
* @precon nan
* @brief 4.1.3. Server Hello row23
* The client and server are initialized to the tls1.3 version, and the legacy_version in the sent serverhello
* message is changed to 0x0304. The client is expected to return illegal_parameter alert.
* @expect 1. The server sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_LEGACY_VERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
/* The client server is initialized to the tls1.3 version. The legacy_version in the sent serverhello message
* is changed to 0x0304. */
serverMsg->version.data = 0x0304;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_LEGACY_VERSION_FUNC_TC002
* @spec legacy_version: In previous versions of TLS, this field was used for version negotiation and represented the
* selected version number for the connection. In TLS 1.3, the TLS server indicates
* its version using the "supported_versions" extension
* (Section 4.2.1), and the legacy_version field MUST be set to
* 0x0303, which is the version number for TLS 1.2. (See Appendix D
* for details about backward compatibility.)
* @title The client server is initialized to the tls1.3 version, and the legacy_version in the sent serverhello
* message is changed to 0x0302. The client is expected to return illegal_parameter alert.
* @precon nan
* @brief 4.1.3. Server Hello row23
* The client and server are initialized to tls1.3 and the legacy_version in the sent serverhello message is
* changed to 0x0302. The client is expected to return illegal_parameter alert.
* @expect 1. The server sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_LEGACY_VERSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
/* The client server is initialized to the tls1.3 version, and the legacy_version in the sent serverhello
* message is changed to 0x0302. */
serverMsg->version.data = 0x0302;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_COMPRESSION_METHOD_FUNC_TC001
* @spec legacy_compression_method: A single byte which MUST have the
* value 0.
* @title Construct serverhello compression algorithm. The value is 1, indicating that the server returns
* illegal_parameter alert.
* @precon nan
* @brief 4.1.3. Server Hello row27
* Construct the serverhello compression algorithm with a one-byte value. The server returns the
* illegal_parameter alert message.
* @expect 1. The server sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_COMPRESSION_METHOD_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
/* Construct serverhello compression algorithm. The value is 1 */
serverMsg->compressionMethod.data = 0x01;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_PARSE_COMPRESSION_METHOD_ERR);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_EXTENSION_FUNC_TC001
* @spec extensions: A list of extensions. The ServerHello MUST only include
* extensions which are required to establish the cryptographic
* context and negotiate the protocol version. All TLS 1.3
* ServerHello messages MUST contain the "supported_versions"
* extension. Current ServerHello messages additionally contain
* either the "pre_shared_key" extension or the "key_share"
* extension, or both (when using a PSK with (EC)DHE key
* establishment). Other extensions (see Section 4.2) are sent
* separately in the EncryptedExtensions message.
* @title Initialize the client and server as tls1.3. Construct a serverhello message that carries the SNI extension. It
* is expected that the connection fails to be established.
* @precon nan
* @brief 4.1.3. Server Hello row28
* Initialize the client server to tls1.3 and construct a serverhello message that carries the SNI extension.
* The expected connection establishment fails.
* @expect 1. The client sends an alert message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_EXTENSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->serverName.exState = INITIAL_FIELD;
serverMsg->serverName.exType.state = INITIAL_FIELD;
serverMsg->serverName.exLen.state = INITIAL_FIELD;
serverMsg->serverName.exLen.data = 0x00;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNSUPPORTED_EXTENSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_EXTENSION_FUNC_TC002
* @spec extensions: A list of extensions. The ServerHello MUST only include
* extensions which are required to establish the cryptographic
* context and negotiate the protocol version. All TLS 1.3
* ServerHello messages MUST contain the "supported_versions"
* extension. Current ServerHello messages additionally contain
* either the "pre_shared_key" extension or the "key_share"
* extension, or both (when using a PSK with (EC)DHE key
* establishment). Other extensions (see Section 4.2) are sent
* separately in the EncryptedExtensions message.
* @title Initialize the client and server to tls1.3 and construct the serverhello message that does not carry the
* supportedversion extension, client send illegal parameter after receive serverhello
* @precon nan
* @brief 4.1.3. Server Hello row28
* Initialize the client server to tls1.3 and construct the serverhello message without the supportedversion
* extension, client send illegal parameter because server send a tls13 ciphersuite without supportedversion
* extension
* @expect 1. The client receives an alert response from the CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_EXTENSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportExtendMasterSecret = false;
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Initialize the client and server to tls1.3 and construct the serverhello message that does not carry the
* supportedversion extension */
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->supportedVersion.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_RANDOM_FUNC_TC001
* @spec For reasons of backward compatibility with middleboxes (see
* Appendix D.4), the HelloRetryRequest message uses the same structure
* as the ServerHello, but with Random set to the special value of the
* SHA-256 of "HelloRetryRequest":
*
* CF 21 AD 74 E5 9A 61 11 BE 1D 8C 02 1E 65 B8 91
* C2 A2 11 16 7A BB 8C 5E 07 9E 09 E2 C8 A8 33 9C
*
* Upon receiving a message with type server_hello, implementations MUST first examine the Random value and,
* if it matches this value, process it as described in Section 4.1.4).
* @title The client and server are initialized to the TLS1.3 version and construct the scenario of sending hrr
* messages. After receiving hrr messages, The next packet sent by the client is expected to be a client hello
* message, and the random value of the expected received hrr packet is the specified value.
* @precon nan
* @brief 4.1.3. Server Hello row29
* The client and server are initialized to the TLS1.3 version, construct the scenario of sending hrr messages.
* After receiving hrr messages,
* The next packet sent by the client is expected to be a client hello packet, and the random value of the
* expected received hrr packet is the specified value.
* @expect 1. Proofreading succeeded.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_RANDOM_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* The client and server are initialized to the TLS1.3 version and construct the scenario of sending hrr
* messages. */
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
const uint8_t g_hrrRandom[HS_RANDOM_SIZE] = {
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
};
ASSERT_TRUE(memcmp(serverMsg->randomValue.data, g_hrrRandom, sizeof(g_hrrRandom) / sizeof(uint8_t)) == 0);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_RANDOM_FUNC_TC002
* @spec For reasons of backward compatibility with middleboxes (see
* Appendix D.4), the HelloRetryRequest message uses the same structure
* as the ServerHello, but with Random set to the special value of the
* SHA-256 of "HelloRetryRequest":
*
* CF 21 AD 74 E5 9A 61 11 BE 1D 8C 02 1E 65 B8 91
* C2 A2 11 16 7A BB 8C 5E 07 9E 09 E2 C8 A8 33 9C
*
* Upon receiving a message with type server_hello, implementations MUST first examine the Random value and,
* if it matches this value, process it as described in Section 4.1.4).
* @title The client and server are initialized to the TLS1.3 version and construct the scenario of sending hrr
* messages. After receiving hrr messages,
* The next packet sent by the client is expected to be client hello, and the random value of the expected
* received hrr is the specified value.
* @precon nan
* @brief 4.1.3. Server Hello row29
* The client and server are initialized to the TLS1.3 version. The connection is established normally. The
* client and server directly send the server hello packet without sending the hrr message. The random value of
* the server hello packet is changed to the value specified by hrr,
* The client is expected to send a client hello packet after receiving the packet.
* @expect 1. It is expected that the client sends a client hello packet after receiving the packet.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_RANDOM_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
const uint8_t g_hrrRandom[HS_RANDOM_SIZE] = {
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
};
ASSERT_TRUE(memcpy_s(serverMsg->randomValue.data, sizeof(g_hrrRandom) / sizeof(uint8_t),
g_hrrRandom, sizeof(g_hrrRandom) / sizeof(uint8_t)) == 0);
serverMsg->keyShare.data.keyExchangeLen.state = MISSING_FIELD;
serverMsg->keyShare.data.keyExchange.state = MISSING_FIELD;
serverMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP521R1;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_SEND_CLIENT_HELLO);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_DOWN_GRADE_RANDOM_FUNC_TC001
* @spec TLS 1.3 clients receiving a ServerHello indicating TLS 1.2 or below
* MUST check that the last 8 bytes are not equal to either of these values.
* TLS 1.2 clients SHOULD also check that the last 8 bytes are not equal to the second value if the ServerHello
* indicates TLS 1.1 or below.
* If a match is found, the client MUST abort the handshake with an "illegal_parameter" alert.
* Note: This is a change from [RFC5246], so in practice many TLS 1.2
* clients and servers will not behave as specified above.
* @title The client is tls1.3, and the server is tls1.2. Construct a scenario where the last eight random bytes of
* the server hello packet received by the client are equal to the specified value. The expected result is that
* the connection fails to be established and the client returns the illegal_parameter alarm.
* @precon nan
* @brief 4.1.3. Server Hello row31
* When the client is tls1.3 and the server is tls1.2, construct the last eight random bytes of the server hello
* packet received by the client equal to the specified value. In this case, the connection fails to be established
* and the client returns the illegal_parameter alarm.
* @expect The connection is set up normally.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_DOWN_GRADE_RANDOM_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig_c = HITLS_CFG_NewTLS13Config();
tlsConfig_c->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig_c, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetVersionSupport(tlsConfig_c, 0x00000030U);
uint16_t cipherSuites[] = {
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(
HITLS_CFG_SetCipherSuites(tlsConfig_c, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(tlsConfig_c != NULL);
HITLS_Config *tlsConfig_s = HITLS_CFG_NewTLS12Config();
tlsConfig_s->isSupportClientVerify = true;
ASSERT_TRUE(tlsConfig_s != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
/* The client is tls1.3, and the server is tls1.2. Construct a scenario where the last eight random bytes of
* the server hello packet received by the client are equal to the specified value. */
const uint8_t g_tls12Downgrade[HS_DOWNGRADE_RANDOM_SIZE] = {0x44, 0x4f, 0x57, 0x4e, 0x47, 0x52, 0x44, 0x01};
ASSERT_TRUE(memcpy_s(serverMsg->randomValue.data + (HS_RANDOM_SIZE - HS_DOWNGRADE_RANDOM_SIZE), sizeof(g_tls12Downgrade) / sizeof(uint8_t),
g_tls12Downgrade, sizeof(g_tls12Downgrade) / sizeof(uint8_t)) == 0);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig_s);
HITLS_CFG_FreeConfig(tlsConfig_c);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static void Test_ModifyServerHello(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *userData)
{
(void)ctx;
(void)userData;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->keyShare.exState = INITIAL_FIELD;
serverMsg->keyShare.exType.state = INITIAL_FIELD;
serverMsg->keyShare.exLen.state = INITIAL_FIELD;
serverMsg->keyShare.exLen.data = 0x00;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_RENEGOTIATION_VERSION_FUNC_TC001
* @spec A legacy TLS client performing renegotiation with TLS 1.2 or prior
* and which receives a TLS 1.3 ServerHello during renegotiation MUST
* abort the handshake with a "protocol_version" alert. Note that
* renegotiation is not possible when TLS 1.3 has been negotiated.
* @title Construct the TLS1.2 serverhello message received by the TLS1.3 serverhello message during renegotiation.
* @precon nan
* @brief 4.1.3. Server Hello row32
* Construct the scenario where the TLS1.2 server hello message of the TLS1.3 version is received during
* renegotiation.
* @expect 1. The client sends an alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SERVER_RENEGOTIATION_VERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportRenegotiation = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(HITLS_Renegotiate(clientTlsCtx), HITLS_SUCCESS);
/* Construct the TLS1.2 serverhello message received by the TLS1.3 serverhello message during renegotiation. */
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_ModifyServerHello};
RegisterWrapper(wrapper);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_RENEGOTIATION);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNSUPPORTED_EXTENSION);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC001
* @spec The server's extensions must contain "supported_versions".
* Additionally, it SHOULD contain the minimal set of extensions
* necessary for the client to generate a correct ClientHello pair. As
* with the ServerHello, a HelloRetryRequest MUST NOT contain any
* extensions that were not first offered by the client in its
* ClientHello, with the exception of optionally the "cookie" (see
* Section 4.2.2) extension.
* @title Initialize the client and server to tls1.3. Construct the scenario where the HRR message is sent and the HRR
* message does not carry the supportedversion extension,
* The client is expected to perform the 1.2 handshake process and the status is TRY_RECV_CERTIFICATIONATE.
* @precon nan
* @brief 4.1.4. Hello Retry Request row33
* Initialize the client and server to tls1.3, construct the scenario where the HRR message is sent, and construct
* the HRR message that does not carry the supportedversion extension,
* The client is expected to perform the 1.2 handshake process and the status is TRY_RECV_CERTIFICATIONATE.
* @expect 1. The client is in the TRY_RECV_CERTIFICATIONATE state.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportExtendMasterSecret = false;
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->supportedVersion.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(clientTlsCtx->hsCtx->state, TRY_RECV_CERTIFICATE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC002
* @spec The server's extensions must contain "supported_versions".
* Additionally, it SHOULD contain the minimal set of extensions
* necessary for the client to generate a correct ClientHello pair. As
* with the ServerHello, a HelloRetryRequest MUST NOT contain any
* extensions that were not first offered by the client in its
* ClientHello, with the exception of optionally the "cookie" (see
* Section 4.2.2) extension.
* @title Initialize the client server to tls1.3, construct the scenario where the hrr message is sent, and construct the
* hrr message carrying the sni extension. The client is expected to return the illegal_parameter alarm.
* @precon nan
* @brief 4.1.4. Hello Retry Request row33
* Initialize the client server to tls1.3, construct the scenario where the hrr message is sent, and construct the
* hrr message carrying the sni extension. The client is expected to return the illegal_parameter alarm.
* @expect 1. The client returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->serverName.exState = INITIAL_FIELD;
serverMsg->serverName.exType.state = INITIAL_FIELD;
serverMsg->serverName.exLen.state = INITIAL_FIELD;
serverMsg->serverName.exLen.data = 0x00;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC003
* @spec The server's extensions must contain "supported_versions".
* Additionally, it SHOULD contain the minimal set of extensions
* necessary for the client to generate a correct ClientHello pair. As
* with the ServerHello, a HelloRetryRequest MUST NOT contain any
* extensions that were not first offered by the client in its
* ClientHello, with the exception of optionally the "cookie" (see
* Section 4.2.2) extension.
* @title Initialize the client and server to tls1.3, construct the scenario where the hrr message is sent and the hrr
* message does not carry the key_share extension, and the client is expected to return the illegal_parameter alarm.
* @precon nan
* @brief 4.1.4. Hello Retry Request row33
* Initialize the client server to tls1.3, construct the scenario where the hrr message is sent, and construct the hrr
* message that does not carry the key_share extension. The client is expected to return the illegal_parameter alarm.
* @expect 1. The client returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
/* Initialize the client and server to tls1.3, construct the scenario where the hrr message is sent and the hrr
* message does not carry the key_share extension */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->keyShare.exState = MISSING_FIELD;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_MISSING_EXTENSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC001
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest),
* it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title Initialize the client and server as tls1.3. Construct the scenario where two hrr messages are sent. The
* client is expected to stop handshake and send unexpected_message alarms.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* Initialize the client and server as tls1.3, construct the scenario where two hrr messages are sent, and the
* client is expected to stop handshake and send the unexpected_message alarm.
* @expect 1. The client sends the unexpected_message alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
/* Initialize the client and server as tls1.3. Construct the scenario where two hrr messages are sent. */
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_ENCRYPTED_EXTENSIONS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
const uint8_t g_hrrRandom[HS_RANDOM_SIZE] = {
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
};
ASSERT_TRUE(memcpy_s(serverMsg->randomValue.data, sizeof(g_hrrRandom) / sizeof(uint8_t),
g_hrrRandom, sizeof(g_hrrRandom) / sizeof(uint8_t)) == 0);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_DUPLICATE_HELLO_RETYR_REQUEST);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC002
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest),
* it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title The client server is initialized to the tls1.3 version, and the legacy_version in the hrr message is changed
* to 0x0304. The client is expected to return illegal_parameter alert.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* The client and server are initialized to tls1.3 and the legacy_version in the hrr message is changed to
* 0x0304. The client is expected to return illegal_parameter alert.
* @expect 1. The client sends the unexpected_message alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->version.data = 0x0304;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC003
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest), it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title The client and server are initialized to tls1.3. Change the legacy_version in the hrr message to 0x0302. The
* client is expected to return illegal_parameter alert.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* The client and server are initialized to tls1.3 and the legacy_version in the hrr message is changed to
* 0x0302. The client is expected to return illegal_parameter alert.
* @expect 1. The client sends the unexpected_message alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
/* The client and server are initialized to tls1.3. Change the legacy_version in the hrr message to 0x0302. */
serverMsg->version.data = 0x0302;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC004
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest),
* it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title Initialize the client and server to TLS1.3. Construct the scenario where the session_id field in the hrr is
* modified. The client is expected to send an illegal parameter alarm after receiving the modification.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* Initialize the client and server to TLS1.3 and construct the scenario where the hrr session_id field is
* modified. The client is expected to send an illegal parameter alarm after receiving the modification.
* @expect 1. The client sends an illegal parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC004()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType = {0};
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
memset_s((shMsg->sessionId.data), shMsg->sessionId.size, 1, shMsg->sessionId.size);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC005
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest),
* it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title The client server is initialized to the TLS1.3 version, and the value of cipher_suite in the hrr message is
* changed to a value other than the value provided by the client. The client is expected to return
* illegal_parameter alert.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* The client and server are initialized to the TLS1.3 version, and the value of cipher_suite in the hrr
* message is changed to a value that is not provided by the client. The client is expected to return
* illegal_parameter alert.
* @expect 1. The client sends an illegal parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC005()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg parsedSH = {0};
uint32_t parseLen = 0;
FRAME_Type frameType;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
shMsg->cipherSuite.data = HITLS_AES_128_CCM_SHA256;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
FrameUioUserData *userData = BSL_UIO_GetUserData(client->io);
uint8_t *alertBuf = userData->sndMsg.msg;
uint32_t alertLen = userData->sndMsg.len;
FRAME_Msg parsedAlert = {0};
uint32_t parsedAlertLen = 0;
ASSERT_TRUE(FRAME_ParseTLSNonHsRecord(alertBuf, alertLen, &parsedAlert, &parsedAlertLen) == HITLS_SUCCESS);
ASSERT_TRUE(parsedAlert.recType.data == REC_TYPE_ALERT);
FRAME_AlertMsg *alertMsg = &parsedAlert.body.alertMsg;
ASSERT_TRUE(alertMsg->alertLevel.data == ALERT_LEVEL_FATAL);
ASSERT_EQ(alertMsg->alertDescription.data, ALERT_ILLEGAL_PARAMETER);
EXIT:
FRAME_CleanMsg(&frameType, &parsedSH);
FRAME_CleanNonHsRecord(REC_TYPE_ALERT, &parsedAlert);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC006
* @spec Upon receipt of a HelloRetryRequest, the client MUST check the
* legacy_version, legacy_session_id_echo, cipher_suite, and
* legacy_compression_method as specified in Section 4.1.3 and then
* process the extensions, starting with determining the version using
* "supported_versions". Clients MUST abort the handshake with an
* "illegal_parameter" alert if the HelloRetryRequest would not result
* in any change in the ClientHello. If a client receives a second
* HelloRetryRequest in the same connection (i.e., where the ClientHello was itself in response to a
* HelloRetryRequest),
* it MUST abort the handshake with an "unexpected_message" alert.
* Otherwise, the client MUST process all extensions in the HelloRetryRequest and send a second updated
* ClientHello.
* @title Construct hrr compression algorithm. The value is 1. The server is expected to return illegal_parameter
* alert.
* @precon nan
* @brief 4.1.4. Hello Retry Request row34
* Construct the hrr compression algorithm byte and set the value to 1. The server is expected to return
* illegal_parameter alert.
* @expect 1. The client sends an illegal parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_FORMAT_FUNC_TC006()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->compressionMethod.data = 0x01;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_PARSE_COMPRESSION_METHOD_ERR);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC001
* @spec The HelloRetryRequest extensions defined in this specification are:
* - supported_versions (see Section 4.2.1)
* - cookie (see Section 4.2.2)
* - key_share (see Section 4.2.8)
* A client which receives a cipher suite that was not offered MUST
* abort the handshake. Servers MUST ensure that they negotiate the
* same cipher suite when receiving a conformant updated ClientHello.
* Upon receiving the ServerHello,
* clients MUST check that the cipher suite supplied in the ServerHello is the same as that in the
* HelloRetryRequest and otherwise abort the handshake with an "illegal_parameter" alert.
* @title The client and server are initialized to the TLS1.3 version. In the scenario where the hrr message is sent,
* the hrr cipher suite is changed to an algorithm that is not provided by the client. The expected connection
* establishment fails and the illegal_parameter alarm is returned.
* @precon nan
* @brief 4.1.4. Hello Retry Request row35
* The client and server are initialized to the TLS1.3 version, construct the scenario where the hrr message is
* sent, and modify the hrr algorithm suite to an algorithm that is not provided by the client. In this case,
* the expected connection establishment fails and the illegal_parameter alarm is returned.
* @expect 1. The client returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->cipherSuite.data = HITLS_RSA_WITH_AES_128_CBC_SHA;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC002
* @spec The HelloRetryRequest extensions defined in this specification are:
* - supported_versions (see Section 4.2.1)
* - cookie (see Section 4.2.2)
* - key_share (see Section 4.2.8)
* A client which receives a cipher suite that was not offered MUST
* abort the handshake. Servers MUST ensure that they negotiate the
* same cipher suite when receiving a conformant updated ClientHello.
* Upon receiving the ServerHello,
* clients MUST check that the cipher suite supplied in the ServerHello is the same as that in the
* HelloRetryRequest and otherwise abort the handshake with an "illegal_parameter" alert.
* @title 2. Initialize the client and server to TLS1.3, construct the scenario where the hrr message is sent, and
* change the algorithm suite for the client hello message to be sent again to the new algorithm suite. It is
* expected that the connection fails to be established and the illegal_parameter alarm is returned.
* @precon nan
* @brief 4.1.4. Hello Retry Request row35
* 2. Initialize the client and server to TLS1.3, construct the scenario of sending hrr messages, and change
* the cipher suite of the client hello message to be sent again to the new cipher suite. It is expected that
* the connection fails to be established and the illegal_parameter alarm is returned.
* @expect 1. The server returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, CLIENT_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
/* Initialize the client and server to TLS1.3, construct the scenario where the hrr message is sent, and
* change the algorithm suite for the client hello message to be sent again to the new algorithm suite. */
FRAME_ClientHelloMsg *clientMsg = &frameMsg.body.hsMsg.body.clientHello;
clientMsg->cipherSuites.data[0] = HITLS_AES_128_GCM_SHA256;
clientMsg->cipherSuites.data[1] = HITLS_AES_256_GCM_SHA384;
clientMsg->cipherSuites.data[2] = HITLS_CHACHA20_POLY1305_SHA256;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(server->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_MSG_HANDLE_ILLEGAL_CIPHER_SUITE);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC003
* @spec The HelloRetryRequest extensions defined in this specification are:
* - supported_versions (see Section 4.2.1)
* - cookie (see Section 4.2.2)
* - key_share (see Section 4.2.8)
* A client which receives a cipher suite that was not offered MUST
* abort the handshake. Servers MUST ensure that they negotiate the
* same cipher suite when receiving a conformant updated ClientHello.
* Upon receiving the ServerHello,
* clients MUST check that the cipher suite supplied in the ServerHello is the same as that in the
* HelloRetryRequest and otherwise abort the handshake with an "illegal_parameter" alert.
* @title 3. Initialize the client and server to TLS1.3. Construct the scenario where the HRR message is sent. Modify
* the cipher suite in the serverhello message to be different from that in the HRR message. As a result, the
* expected connection establishment fails and the illegal_parameter alarm is returned.
* @precon nan
* @brief 4.1.4. Hello Retry Request row35
* 3. The client and server are initialized to TLS1.3, construct the scenario where hrr is sent, modify the
* cipher suite in serverhello and hrr to be different, and the expected connection setup fails and the
* illegal_parameter alarm is returned.
* @expect 1. The client returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_EXTENSION_CONTENT_FUNC_TC003()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_ENCRYPTED_EXTENSIONS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->cipherSuite.data = HITLS_AES_128_GCM_SHA256;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_ILLEGAL_CIPHER_SUITE);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_SUPPORT_VERSION_FUNC_TC001
* @spec The value of selected_version in the HelloRetryRequest
* "supported_versions" extension MUST be retained in the ServerHello,
* and a client MUST abort the handshake with an "illegal_parameter"
* alert if the value changes.
* @title 1. Initialize the client and server as tls1.3, construct a scenario where the supportedversion values
* carried by serverhello and hrr are different,
* The client is expected to return the illegal_parameter alarm.
* @precon nan
* @brief 4.1.4. Hello Retry Request row37
* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
* by serverhello and hrr are different,
* The client is expected to return the illegal_parameter alarm.
* @expect 1. The client returns the illegal_parameter alarm.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_SUPPORT_VERSION_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_CFG_SetVersionSupport(&client->ssl->config.tlsConfig, 0x00000030U);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
/* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
by serverhello and hrr are different, */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_SERVER_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_ENCRYPTED_EXTENSIONS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
uint8_t *recvBuf = ioUserData->recMsg.msg;
uint32_t recvLen = ioUserData->recMsg.len;
ASSERT_TRUE(recvLen != 0);
FRAME_Msg frameMsg = { 0 };
FRAME_Type frameType = { 0 };
uint32_t parseLen = 0;
SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
serverMsg->supportedVersion.data.data = 0x0303;
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ioUserData->recMsg.len = 0;
ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
FRAME_CleanMsg(&frameType, &frameMsg);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* If the client curve is HITLS_EC_GROUP_CURVE25519 and the certificate is SECP256R1, the connection is successfully
* established, indicating that the curve in tls1.3 is not associated with the certificate. */
/* BEGIN_CASE */
void SDV_TLS13_RFC8446_KeyShareGroup_TC003(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_CURVE25519");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, clientRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_TlsRead(serverRes->ssl, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HRR_SUPPORT_VERSION_FUNC_TC001
* @title During the TLS1.3 HRR handshaking, application messages can not be received
* @precon nan
* @brief
* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
* by serverhello and hrr are different, expect result 1.
* 2. Send a app data message the server, expect reslut 2.
* @expect 1. The client send secend client hello message.
8 2. The server send unexpected message alert.
@ */
/* BEGIN_CASE */
void UT_TLS13_RFC8446_HRR_APP_RECV_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLSConfig();
tlsConfig->isSupportClientVerify = true;
HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
/* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
by serverhello and hrr are different, */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
CONN_Deinit(serverTlsCtx);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint32_t sendLenapp = 7;
uint8_t sendBufapp[7] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x05, 0x05};
uint32_t writeLen;
BSL_UIO_Write(clientTlsCtx->uio, sendBufapp, sendLenapp, &writeLen);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* IN TLS1.3, mutiple ccs can be received*/
/* BEGIN_CASE */
void UT_TLS13_RFC8446_RECV_MUTI_CCS_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
uint32_t sendLenccs = 6;
uint8_t sendBufccs[6] = {0x14, 0x03, 0x03, 0x00, 0x01, 0x01};
uint32_t writeLen;
for (int i = 0; i < 5; i++) {
BSL_UIO_Write(serverTlsCtx->uio, sendBufccs, sendLenccs, &writeLen);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
}
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_kex.c | C | unknown | 229,523 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "process.h"
#include "bsl_sal.h"
/* END_HEADER */
#define MAX_BUF 16384
int32_t STUB_RecConnDecrypt(
TLS_Ctx *ctx, RecConnState *state, const REC_TextInput *cryptMsg, uint8_t *data, uint32_t *dataLen)
{
(void)ctx;
(void)state;
memcpy_s(data, cryptMsg->textLen, cryptMsg->text, cryptMsg->textLen);
(void)data;
*dataLen = cryptMsg->textLen;
return HITLS_SUCCESS;
}
int32_t STUB_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num)
{
(void)ctx;
(void)recordType;
(void)data;
(void)num;
return HITLS_SUCCESS;
}
extern int32_t __real_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num);
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC001
* @spec -
* @title The client does not support posthandshake, but receives a server certificate request
* message after the connection establishment is completed.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client not to support post-handshake extension
* 3. After the connection establishment is completed, the construction server sends a certificate request message to the
* client
* 4. Observe client behavior
* @expect
* 1. Initialization successful
* 2. Setup successful
* 3. Send successfully.
* 4. The client returns alert ALERT_UNEXPECTED_MESSAGE.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC001(void)
{
FRAME_Init();
// Apply and initialize config
HITLS_Config *c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(s_config != NULL);
// Set the client not to support post-handshake extension
HITLS_CFG_SetPostHandshakeAuthSupport(c_config, false);
HITLS_CFG_SetPostHandshakeAuthSupport(s_config, false);
FRAME_LinkObj *client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_GetTls13DisorderHsMsg(CERTIFICATE_REQUEST, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_EQ(REC_Write(server->ssl, REC_TYPE_HANDSHAKE, sendBuf, sendLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint8_t readbuff[READ_BUF_SIZE];
uint32_t readLen;
ASSERT_TRUE(client->ssl != NULL);
// The client returns alert ALERT_UNEXPECTED_MESSAGE
ASSERT_EQ(HITLS_Read(client->ssl, readbuff, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC010
* @spec -
* @title The server receives out-of-order messages during authentication after handshake.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. After the connection is established, the server receives the CertificateVerify message.
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. The server sends an alert message, and the connection is interrupted.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC010(void)
{
FRAME_Init();
// Apply and initialize config
HITLS_Config *c_config = HITLS_CFG_NewTLS13Config();
HITLS_Config *s_config = HITLS_CFG_NewTLS13Config();
// Set the client support post-handshake extension
HITLS_CFG_SetPostHandshakeAuthSupport(c_config, true);
HITLS_CFG_SetPostHandshakeAuthSupport(s_config, true);
HITLS_CFG_SetClientVerifySupport(c_config, true);
HITLS_CFG_SetClientVerifySupport(s_config, true);
FRAME_LinkObj *client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_EQ(HITLS_VerifyClientPostHandshake(server->ssl), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
uint32_t sendLen = MAX_RECORD_LENTH;
uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
ASSERT_TRUE(FRAME_GetTls13DisorderHsMsg(CERTIFICATE_VERIFY, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
ASSERT_EQ(REC_Write(client->ssl, REC_TYPE_HANDSHAKE, sendBuf, sendLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC018
* @spec -
* @title Invoke the HITLS_VerifyClientPostHandshake interface during connection establishment.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client and server to support post-handshake extension. Expected result 3 is obtained.
* 3. When a connection is established, the server is in the Try_RECV_CLIENT_HELLO state, and the
* HITLS_VerifyClientPostHandshake interface is invoked.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The interface fails to be invoked.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC018(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewTLS13Config();
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
// Configure the client and server to support post-handshake extension
client->ssl->config.tlsConfig.isSupportPostHandshakeAuth = true;
server->ssl->config.tlsConfig.isSupportPostHandshakeAuth = true;
ASSERT_TRUE(client->ssl->config.tlsConfig.isSupportPostHandshakeAuth == true);
ASSERT_TRUE(server->ssl->config.tlsConfig.isSupportPostHandshakeAuth == true);
// he server is in the Try_RECV_CLIENT_HELLO state
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(server->ssl->hsCtx->state == TRY_RECV_CLIENT_HELLO);
// the HITLS_VerifyClientPostHandshake interface is invoked
ASSERT_EQ(HITLS_VerifyClientPostHandshake(client->ssl), HITLS_INVALID_INPUT);
ASSERT_EQ(HITLS_VerifyClientPostHandshake(server->ssl), HITLS_MSG_HANDLE_STATE_ILLEGAL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC019
* @spec -
* @title The server does not support invoking the HITLS_VerifyClientPostHandshake interface after handshake
* authentication.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client to support the post-handshake extension. The server does not support the post-handshake
* extension.
* 3. Establish a connection. The server invokes the HITLS_VerifyClientPostHandshake interface to initiate
* authentication.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The interface fails to be invoked.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC019(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewTLS13Config();
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
// Configure the client to support the post-handshake extension
client->ssl->config.tlsConfig.isSupportPostHandshakeAuth = true;
server->ssl->config.tlsConfig.isSupportPostHandshakeAuth = false;
ASSERT_TRUE(client->ssl->config.tlsConfig.isSupportPostHandshakeAuth == true);
ASSERT_TRUE(server->ssl->config.tlsConfig.isSupportPostHandshakeAuth == false);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
// The server invokes the HITLS_VerifyClientPostHandshake interface to initiate authentication
ASSERT_EQ(HITLS_VerifyClientPostHandshake(client->ssl), HITLS_INVALID_INPUT);
ASSERT_EQ(HITLS_VerifyClientPostHandshake(server->ssl), HITLS_MSG_HANDLE_STATE_ILLEGAL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_pha.c | C | unknown | 10,630 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "conn_init.h"
#include "alert.h"
#include "hs_kx.h"
/* END_HEADER */
#define MAX_RECORD_LENTH (20 * 1024)
#define ALERT_BODY_LEN 2u
const uint8_t ccsMessage[] = {0x14, 0x03, 0x03, 0x00, 0x01, 0x01};
static int32_t SendCcs(HITLS_Ctx *ctx, uint8_t *data, uint8_t len)
{
/** Write records. */
int32_t ret = REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, data, len);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
uint8_t isFlightTransmitEnable;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
if (ret == BSL_UIO_IO_BUSY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
if (ret != BSL_SUCCESS) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
}
return HITLS_SUCCESS;
}
static int32_t SendAlert(HITLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
uint8_t data[ALERT_BODY_LEN];
/** Obtain the alert level. */
data[0] = level;
data[1] = description;
/** Write records. */
int32_t ret = REC_Write(ctx, REC_TYPE_ALERT, data, ALERT_BODY_LEN);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
uint8_t isFlightTransmitEnable;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
if (ret == BSL_UIO_IO_BUSY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
if (ret != BSL_SUCCESS) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
}
return HITLS_SUCCESS;
}
static int32_t SendErrorAlert(HITLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
uint8_t data[2 * ALERT_BODY_LEN] = {level, description, level, description};
/** Write records. */
int32_t ret = REC_Write(ctx, REC_TYPE_ALERT, data, 2 * ALERT_BODY_LEN);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
uint8_t isFlightTransmitEnable;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
if (ret == BSL_UIO_IO_BUSY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
if (ret != BSL_SUCCESS) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
}
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC001
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 1. After the client sends a client hello message, the CCS message received by the client is not encrypted
* (value: 0x01).
* Discard the message and do not process the message. If the CCS message that is not encrypted is received
* again (value: 0x01), the system discards the message.
* 3. Before the client receives the finished message, the client receives the CCS message that is not
* encrypted (value: 0x01) and discards the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 1. After the client sends a client hello message, the CCS message received by the client is not encrypted */
/* 3. Before the client receives the finished message, the client receives the CCS message that is not
* encrypted */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC002
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 2. After the first connection is established, the server receives the client hello message and receives the
* CCS message that is not encrypted (value: 0x01). The server discards the message and does not process
* the message.
* 4. If the server receives the CCS message that is not encrypted (value: 0x01) before the finished message is
* received during the first connection setup, Discard the message and do not process the message. If the
* CCS message that is not encrypted is received again (value: 0x01), the system sends the
* unexpected_message alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 2. After the first connection is established, the server receives the client hello message and receives the
* CCS message that is not encrypted (value: 0x01). The server discards the message and does not process
* the message.
* 4. If the server receives the CCS message that is not encrypted (value: 0x01) before the finished message is
* received during the first connection setup, Discard the message and do not process the message. If the
* CCS message that is not encrypted is received again (value: 0x01) */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* The server generates the unexpected_message alarm after receiving the CCS message for the second time. */
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC003
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 5. After the session is recovered, the clien sends the clienthello message, receives the CCS message that is
* not encrypted (value: 0x01), and discards the message.
* 7. The session is resumed. Before the finished message is received, the client receives a CCS message that
* is not encrypted (value: 0x01). The client discards the message and does not process the message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 5. After the session is recovered, the clien sends the clienthello message, receives the CCS message that is
* not encrypted (value: 0x01), and discards the message.
* 7. The session is resumed. Before the finished message is received, the client receives a CCS message
* that is not encrypted (value: 0x01). */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC004
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 6. After the session is resumed, the server receives a CCS message that is not encrypted (value: 0x01) after
* receiving the client hello message,
* The message is discarded and not processed. If the CCS message is received again and the unencrypted record
* (value: 0x01) is not encrypted, the alarm "unexpected_message" is sent to terminate the handshake.
* 8. The session is recovered. Before the server receives the finished message, the CCS message is not
* encrypted and the value is 0x01, and the message is discarded.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
ASSERT_EQ(server->ssl->hsCtx->state, TRY_RECV_FINISH);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 6.After the session is resumed, the server receives a CCS message that is not encrypted (value: 0x01) after
* receiving the client hello message,
* 8.The session is recovered. Before the server receives the finished message, the CCS message is not
* encrypted and the value is 0x01 */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* The server generates the unexpected_message alarm after receiving the CCS message for the second time. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC005
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving unencrypted CCS messages, the system discards the messages.
* @precon nan
* @brief 5 Record Protocol line 181
* 9. After receiving the helloretry request, the client sends the client hello message for the second time.
* The received CCS message is not encrypted (value: 0x01).
* Discard the message and do not process the message. If the CCS message is received again,
* discard the messages.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC005(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
/* Configure the server to support only the non-default curve. The server sends the HRR message. */
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_HELLO_RETRY_REQUEST), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 9.After receiving the helloretry request, the client sends the client hello message for the second time.
* The received CCS message is not encrypted (value: 0x01).
* Discard the message and do not process the message. If the CCS message is received again and the
* unencrypted record (value: 0x01) is received, the unexpected_message alarm is sent to terminate the handshake. */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* client will discard the ccs */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC006
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 10. After the server sends a helloretry request, the client hello message is received for the second time,
* Send the unexpected_message alarm to terminate the handshake if the received CCS message is not encrypted
* (value: 0x01).
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_IGNORE_CCS_FUNC_TC006(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 10. After the server sends a helloretry request, the client hello message is received for the second time,
* Send the unexpected_message alarm to terminate the handshake if the received CCS message is not
* encrypted (value: 0x01). */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC001
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 1. After the client sends a client hello message to the client, the client sends an unexpected_message alarm
* to terminate the handshake because the client receives a CCS whose value is not 0x01.
* 7. Before the client receives the finised message, the client sends the unexpected_message alarm to
* terminate the handshake because the client receives a CCS whose value is not 0x01.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 2;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* The client receives an unencrypted CCS message (value: 0x01) and sends an unexpected_message alarm to terminate
* the handshake before the client receives the finished message. */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC002
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 2. After the client sends the client hello message to the first connection setup, the client sends the
* unexpected_message alarm to terminate the handshake because the encrypted CCS is received.
* 9. Before the client receives the finised message, the client sends the unexpected_message alarm to
* terminate the handshake because the client receives the encrypted CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
serverTlsCtx->recCtx->outBuf->end = 0;
uint32_t hashLen = SAL_CRYPT_DigestSize(serverTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
ASSERT_EQ(
HS_SwitchTrafficKey(serverTlsCtx, serverTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true), HITLS_SUCCESS);
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 2. After the client sends the client hello message to the first connection setup, the client sends the
* unexpected_message alarm to terminate the handshake because the encrypted CCS is received.
* 9. Before the client receives the finised message, the client sends the unexpected_message alarm to
* terminate the handshake because the client receives the encrypted CCS. */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC003
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 3. Before the server receives the client hello message, the server sends the unexpected_message alarm to terminate
* the handshake because it receives a CCS with a value other than 0x01.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
uint8_t data = 2;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* Before the server receives the client hello message, the server sends the unexpected_message alarm to terminate
* the handshake because the server receives a CCS with a value other than 0x01. */
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC004
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 4. After the first connection is established, the server receives the client hello message and receives the
* CCS whose value is not 0x01. Therefore, the server sends the unexpected_message alarm to terminate the
* handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
uint8_t data = 2;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 4.After the first connection is established, the server receives the client hello message and receives the
* CCS whose value is not 0x01. Therefore, the server sends the unexpected_message alarm to terminate the
* handshake. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC005
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 5. After the first connection is established, the server receives the client hello message and receives the
* encrypted CCS. Therefore, the server sends the unexpected_message alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC005(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
clientTlsCtx->recCtx->outBuf->end = 0;
uint32_t hashLen = SAL_CRYPT_DigestSize(clientTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
ASSERT_EQ(
HS_SwitchTrafficKey(clientTlsCtx, clientTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true), HITLS_SUCCESS);
/* Construct a non-0x1 CCS packet. */
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* After the first connection is established, the server receives the client hello message and receives the
* encrypted CCS. Therefore, the server sends the unexpected_message alarm to terminate the handshake. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC006
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 8. After the client receives the finised message, the client sends the unexpected_message alarm to terminate
* the handshake because it receives a CCS whose value is not 0x01.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC006(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->sndMsg.len = sizeof(ccsMessage);
memcpy_s(ioServerData->sndMsg.msg, ioServerData->sndMsg.len, ccsMessage, sizeof(ccsMessage));
ioServerData->sndMsg.msg[5] = 0x2;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 8. After the client receives the finised message, the client sends the unexpected_message alarm to terminate
* the handshake because it receives a CCS whose value is not 0x01. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC007
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 9. After the client receives the finised message, the client sends the unexpected_message alarm to terminate
* the handshake because the client receives the encrypted CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC007(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ioServerData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 9. After the client receives the finised message, the client sends the unexpected_message alarm to terminate
* the handshake because the client receives the encrypted CCS */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC008
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 10. After the session is resumed, the client sends the unexpected_message alarm to terminate the handshake
* because the client receives a CCS with a value other than 0x01.
* 15. Before the session is recovered, the client receives a CCS whose value is not 0x01. Therefore, the
* client sends the unexpected_message alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC008(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg sndMsg;
ASSERT_TRUE(memcpy_s(sndMsg.msg, MAX_RECORD_LENTH, ioServerData->sndMsg.msg, ioServerData->sndMsg.len) == EOK);
sndMsg.len = ioServerData->sndMsg.len;
ioServerData->sndMsg.len = 0;
uint8_t data = 2;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 10. After the session is resumed, the client sends the unexpected_message alarm to terminate the handshake
* because the client receives a CCS with a value other than 0x01.
* 15. Before the session is recovered, the client receives a CCS whose value is not 0x01. Therefore, the
* client sends the unexpected_message alarm to terminate the handshake. */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC009
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 11. After the session is recovered, the client sends the "unexpected_message" alarm to terminate the
* handshake because the client receives the encrypted CCS.
* 16. The session is recovered. Before the client receives the finised message, the client sends the
* "unexpected_message" alarm to terminate the handshake because the client receives the encrypted CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC009(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
serverTlsCtx->recCtx->outBuf->end = 0;
uint32_t hashLen = SAL_CRYPT_DigestSize(serverTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
ASSERT_EQ(
HS_SwitchTrafficKey(serverTlsCtx, serverTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true), HITLS_SUCCESS);
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 11. After the session is recovered, the client sends the "unexpected_message" alarm to terminate the
* handshake because the client receives the encrypted CCS.
* 16. The session is recovered. Before the client receives the finised message, the client sends the
* "unexpected_message" alarm to terminate the handshake because the client receives the encrypted CCS. */
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC010
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 12. Before the session is recovered, the server receives a CCS with a value other than 0x01 before receiving
* the client hello message. Therefore, the server sends the unexpected_message alarm to terminate the
* handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC010(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
uint8_t data = 2;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 12. Before the session is recovered, the server receives a CCS with a value other than 0x01 before receiving
* the client hello message. Therefore, the server sends the unexpected_message alarm to terminate the
* handshake. */
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC011
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 13. After receiving the client hello message, the server sends the unexpected_message alarm to terminate the
* handshake because the server receives a CCS with a value other than 0x01.
* 19. Before the session is recovered, the server sends the unexpected_message alarm to terminate the
* handshake because the server receives a CCS whose value is not 0x01.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC011(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
clientTlsCtx->recCtx->outBuf->end = 0;
uint8_t data = 2;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 13. After receiving the client hello message, the server sends the unexpected_message alarm to terminate the
* handshake because the server receives a CCS with a value other than 0x01.
* 19. Before the session is recovered, the server sends the unexpected_message alarm to terminate the
* handshake because the server receives a CCS whose value is not 0x01. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC012
* @spec An implementation may receive an unencrypted record of
* type change_cipher_spec consisting of the single byte
* value 0x01 at any time after the first ClientHello message
* has been sent or received and before the peer's Finished message
* has been received and MUST simply drop it without further processing.
* @title When receiving an unencrypted CCS message, the system discards the message.
* @precon nan
* @brief 5 Record Protocol line 181
* 14. After the session is recovered, the server sends the unexpected_message alarm to terminate the handshake
* because the server receives the encrypted CCS.
* 20. The session is recovered. Before the server receives the finised message, it receives the encrypted CCS.
* Therefore, the server sends the "unexpected_message" alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC012(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FrameMsg recMsg;
ASSERT_TRUE(memcpy_s(recMsg.msg, MAX_RECORD_LENTH, ioServerData->recMsg.msg, ioServerData->recMsg.len) == EOK);
recMsg.len = ioServerData->recMsg.len;
ioServerData->recMsg.len = 0;
clientTlsCtx->recCtx->outBuf->end = 0;
uint32_t hashLen = SAL_CRYPT_DigestSize(clientTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
ASSERT_EQ(
HS_SwitchTrafficKey(clientTlsCtx, clientTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true), HITLS_SUCCESS);
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 14. After the session is recovered, the server sends the unexpected_message alarm to terminate the handshake
* because the server receives the encrypted CCS.
* 20. The session is recovered. Before the server receives the finised message, it receives the encrypted
* CCS. Therefore, the server sends the "unexpected_message" alarm to terminate the handshake. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC013
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 17. After the session is recovered, the client receives a CCS whose value is not 0x01 and sends the
* unexpected_message alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC013(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->sndMsg.len = sizeof(ccsMessage);
memcpy_s(ioServerData->sndMsg.msg, ioServerData->sndMsg.len, ccsMessage, sizeof(ccsMessage));
ioServerData->sndMsg.msg[5] = 0x2;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 17. After the session is recovered, the client receives a CCS whose value is not 0x01 and sends the
* unexpected_message alarm to terminate the handshake. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC014
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 18. After the session is recovered, the client receives the finised message and receives the encrypted CCS.
* Therefore, the client sends the unexpected_message alarm to terminate the handshake.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC014(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
uint8_t data = 1;
ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ioServerData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
/* 18. After the session is recovered, the client receives the finised message and receives the encrypted CCS.
* Therefore, the client sends the unexpected_message alarm to terminate the handshake. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(clientTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC015
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 21. After the session is recovered, the server sends the unexpected_message alarm to terminate the handshake
* because the server receives a CCS whose value is not 0x01.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC015(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->sndMsg.len = sizeof(ccsMessage);
memcpy_s(ioClientData->sndMsg.msg, ioClientData->sndMsg.len, ccsMessage, sizeof(ccsMessage));
ioClientData->sndMsg.msg[5] = 0x2;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 21. After the session is recovered, the server sends the unexpected_message alarm to terminate the handshake
* because the server receives a CCS whose value is not 0x01. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC016
* @spec An implementation which receives any other change_cipher_spec value or
* which receives a protected change_cipher_spec record MUST
* abort the handshake with an "unexpected_message" alert.
* @title Send the unexpected_message alarm when receiving other CCS messages.
* @precon nan
* @brief 5 Record Protocol line 182
* 22. After the session is recovered, the server sends the "unexpected_message" alarm to terminate the
* handshake because the server receives the encrypted CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECEIVES_OTHER_CCS_FUNC_TC016(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
clientTlsCtx = FRAME_GetTlsCtx(client);
serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
uint8_t data = 1;
ASSERT_EQ(SendCcs(client->ssl, &data, sizeof(data)), HITLS_SUCCESS);
ioClientData->sndMsg.msg[0] = REC_TYPE_CHANGE_CIPHER_SPEC;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
/* 22. After the session is recovered, the server sends the "unexpected_message" alarm to terminate the
* handshake because the server receives the encrypted CCS. */
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(serverTlsCtx, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_RECORD_TYPE_FUNC_TC001
* @spec Handshake messages MUST NOT be interleaved with other record types.
* That is, if a handshake message is split over two or more records,
* there MUST NOT be any other records between them.
* @title Handshake messages must not be interleaved with other record types.
* @precon nan
* @brief 5.1. Record Layer line 186
* 1. Handshake messages are sent to multiple records. Check whether records of other types exist between
* the records.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_RECORD_TYPE_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
int32_t RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType);
int32_t STUB_RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType)
{
(void)ctx;
(void)text;
(void)textLen;
*recType = (uint8_t)REC_TYPE_APP;
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_RECORD_TYPE_FUNC_TC002
* @spec Handshake messages MUST NOT be interleaved with other record types.
* That is, if a handshake message is split over two or more records,
* there MUST NOT be any other records between them.
* @title Handshake messages must not be interleaved with other record types.
* @precon nan
* @brief 5.1. Record Layer line 186
* 2. If multiple handshake messages are interspersed with other record (app) messages, the handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_HANDSHAKE_RECORD_TYPE_FUNC_TC002(void)
{
FRAME_Init();
STUB_Init();
FuncStubInfo tmpRpInfo;
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_FINISH) == HITLS_SUCCESS);
STUB_Replace(&tmpRpInfo, RecParseInnerPlaintext, STUB_RecParseInnerPlaintext);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
STUB_Reset(&tmpRpInfo);
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SINGLE_ALERT_FUNC_TC001
* @spec A record with an Alert type MUST contain exactly one message.
* @title A record with the Alert type must contain only one message.
* @precon nan
* @brief 5.1. Record Layer line 186
* 1. The client sends multiple alarm messages, and the server handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SINGLE_ALERT_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_FINISH) == HITLS_SUCCESS);
clientTlsCtx->recCtx->outBuf->end = 0;
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.len = 0;
ASSERT_TRUE(SendErrorAlert(client->ssl, ALERT_LEVEL_WARNING, ALERT_NO_CERTIFICATE_RESERVED) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SINGLE_ALERT_FUNC_TC002
* @spec A record with an Alert type MUST contain exactly one message.
* @title A record with the Alert type must contain only one message.
* @precon nan
* @brief 5.1. Record Layer line 186
* 2. When the server sends multiple alarm messages, the client handshake fails.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SINGLE_ALERT_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* Stop the client receiving the TRY_RECV_SERVER_HELLO state, and the server sending the TRY_SEND_SERVER_HELLO
* state. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
serverTlsCtx->recCtx->outBuf->end = 0;
ASSERT_TRUE(SendErrorAlert(server->ssl, ALERT_LEVEL_WARNING, ALERT_NO_CERTIFICATE_RESERVED) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.len = 0;
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC001
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* 1. In TLS1.3, legacy_record_version is 0x0303 in the record message of the CCS.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* The client stops receiving the TRY_RECV_ENCRYPTED_EXTENSIONS. The server sends the EE message, but the EE message
* is cached because the CCS message is sent first. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_ENCRYPTED_EXTENSIONS) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_ENCRYPTED_EXTENSIONS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
FRAME_Msg frameMsg = {0};
uint8_t *buffer = ioClientData->recMsg.msg;
uint32_t readLen = ioClientData->recMsg.len;
uint32_t parseLen = 0;
ASSERT_TRUE(ParserRecordHeader(&frameMsg, buffer, readLen, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.type, REC_TYPE_CHANGE_CIPHER_SPEC);
ASSERT_TRUE(frameMsg.version == HITLS_VERSION_TLS12);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC002
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* 2. In TLS1.3, legacy_record_version is 0x0303 in the alert record message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(SendAlert(server->ssl, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
FRAME_Msg frameMsg = {0};
uint8_t *buffer = ioClientData->recMsg.msg;
uint32_t readLen = ioClientData->recMsg.len;
uint32_t parseLen = 0;
ASSERT_TRUE(ParserRecordHeader(&frameMsg, buffer, readLen, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.type, REC_TYPE_ALERT);
ASSERT_TRUE(frameMsg.version == HITLS_VERSION_TLS12);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC003
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* In 5. TLS1.3, legacy_record_version is set to 0x0301 in the record message of the init clienthello.
* In 3.TLS1.3, legacy_record_version is 0x0303 in the session recovery clienthello message.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
FRAME_Msg frameMsg = {0};
uint8_t *buffer = ioServerData->recMsg.msg;
uint32_t readLen = ioServerData->recMsg.len;
uint32_t parseLen = 0;
ASSERT_TRUE(ParserRecordHeader(&frameMsg, buffer, readLen, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.type, REC_TYPE_HANDSHAKE);
/* For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301. */
ASSERT_TRUE(frameMsg.version == HITLS_VERSION_TLS10);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ioServerData = BSL_UIO_GetUserData(server->io);
buffer = ioServerData->recMsg.msg;
readLen = ioServerData->recMsg.len;
parseLen = 0;
ASSERT_TRUE(ParserRecordHeader(&frameMsg, buffer, readLen, &parseLen) == HITLS_SUCCESS);
ASSERT_EQ(frameMsg.type, REC_TYPE_HANDSHAKE);
ASSERT_TRUE(frameMsg.version == HITLS_VERSION_TLS10);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC004
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* In TLS1.3, the value of legacy_record_version in the serverhello message is changed to 0xffff when the
* session is recovered,
* After the client receives the message, the client ignores this field and the session is successfully
* restored.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC004(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
uint32_t bufOffset = 1;
ioClientData->recMsg.msg[bufOffset] = 0x03;
bufOffset++;
ioClientData->recMsg.msg[bufOffset] = 0xff;
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC005
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* In TLS 7.1.3, the legacy_record_version field in the record message of the client hello message is changed to
* 0x0300. After the server receives the message, the server ignores the field and the handshake is still
* successful.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC005(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
uint32_t bufOffset = 1;
ioClientData->recMsg.msg[bufOffset] = 0x03;
bufOffset++;
ioClientData->recMsg.msg[bufOffset] = 0xff;
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC006
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* 8. The server uses TLS1.2 and the value of legacy_record_version in the record message is 0x0303,
* The client uses TLS 1.3 and the value of legacy_record_version in the record message is 0x0303. The
* handshake is still successful.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC006(void)
{
FRAME_Init();
HITLS_Config *clientConfig = HITLS_CFG_NewTLSConfig();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(serverConfig != NULL);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC007
* @spec legacy_record_version: MUST be set to 0x0303
* for all records generated by a TLS 1.3 implementation
* other than an initial ClientHello (i.e., one not generated
* after a HelloRetryRequest), where it MAY also be 0x0301
* for compatibility purposes. This field is deprecated
* and MUST be ignored for all purposes. Previous versions of
* TLS would use other values in this field under some circumstances.
* @title For all records generated by the TLS 1.3 implementation, it must be set to 0x0303,
* where the initial ClientHello (i.e., records not generated after HelloRetryRequest) For compatibility
* purposes, it may be 0x0301.
* @precon nan
* @brief 5.1. Record Layer line 190
* 9. Change TLSCiphertext.legacy_record_version in the encryption record of the app to 0xffff,
* After the client receives the message, the client ignores this field and the session is still successful.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_RECORD_VERSION_FUNC_TC007(void)
{
FRAME_Init();
HITLS_Config *clientConfig = HITLS_CFG_NewTLSConfig();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(serverConfig != NULL);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CIPHERTEXT_LENGTH_FUNC_TC001
* @spec length: The length (in bytes) of the following TLSCiphertext.
* encrypted_record, which is the sum of the lengths of the content and the padding,
* plus one for the inner content type, plus any expansion added by the AEAD algorithm.
* The length MUST NOT exceed 2^14 + 256 bytes. An endpoint that receives a record that
* exceeds this length MUST terminate the connection with a "record_overflow" alert.
* @title For TLS 1.3, the length of the ciphertext cannot exceed 2 ^ 14 + 256 bytes.
* @precon nan
* @brief 5.2. Record Payload Protection line 194
* 1. A connection is established. During the connection establishment, the server receives a message whose
* ciphertext length is 2 ^ 14 + 257. The server is expected to send a record_overflow alarm to
* terminate the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CIPHERTEXT_LENGTH_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
/* The client stops receiving the TRY_RECV_ENCRYPTED_EXTENSIONS. The server sends the EE message. However, the EE
* message is cached because the CCS message is sent first. */
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.msg[3] = 0x41u;
ioServerData->recMsg.msg[4] = 0x01u;
/* For TLS 1.3, the length of the ciphertext cannot exceed 2 ^ 14 + 256 bytes. */
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_RECORD_OVERFLOW);
ALERT_Info info = {0};
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_RECORD_OVERFLOW);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_CIPHERTEXT_LENGTH_FUNC_TC002
* @spec length: The length (in bytes) of the following TLSCiphertext.
* encrypted_record, which is the sum of the lengths of the content and the padding,
* plus one for the inner content type, plus any expansion added by the AEAD algorithm.
* The length MUST NOT exceed 2^14 + 256 bytes. An endpoint that receives a record that
* exceeds this length MUST terminate the connection with a "record_overflow" alert.
* @title For TLS 1.3, the length of the ciphertext cannot exceed 2 ^ 14 + 256 bytes.
* @precon nan
* @brief 5.2. Record Payload Protection line 194
* 2. A connection is established. During the connection establishment, the client receives a message whose ciphertext
* length is 2 ^ 14 + 257. The server is expected to send a record_overflow alarm to terminate the connection.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_CIPHERTEXT_LENGTH_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_ENCRYPTED_EXTENSIONS) == HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_ENCRYPTED_EXTENSIONS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->recMsg.msg[3] = 0x41u;
ioClientData->recMsg.msg[4] = 0x01u;
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_RECORD_OVERFLOW);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_RECORD_OVERFLOW);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC001
* @spec Each sequence number is set to zero at the beginning of a connection and whenever the key is changed;
* the first record transmitted under a particular traffic key MUST use sequence number 0.
* @title The sequence number is 0 when the connection starts or the key changes.
* @precon nan
* @brief 5.3. Per-Record Nonce line 197
* 1. The client sends a finish packet and an app packet. After the seq number is not 0, the key is changed
* successfully and the seq number is reset to 0.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, (uint8_t *)"Hello World", sizeof("Hello World"), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
REC_Ctx *recCtx = (REC_Ctx *)client->ssl->recCtx;
ASSERT_TRUE(recCtx->writeStates.currentState->seq != 0);
ASSERT_TRUE(HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_REQUESTED) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(recCtx->writeStates.currentState->seq == 0);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC002
* @spec Each sequence number is set to zero at the beginning of a connection and whenever the key is changed;
* the first record transmitted under a particular traffic key MUST use sequence number 0.
* @title The sequence number is 0 when the connection starts or the key changes.
* @precon nan
* @brief 5.3. Per-Record Nonce line 197
* 2. The client sends a finish packet and an app packet. After the seq number is not 0, the key fails to be
* changed and the key is updated,
* The seq number is not reset to 0. (It is to be confirmed whether the seq number is updated at both ends.)
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC002(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, (uint8_t *)"Hello World", sizeof("Hello World"), &writeLen) == HITLS_SUCCESS);
REC_Ctx *recCtx = (REC_Ctx *)client->ssl->recCtx;
ASSERT_TRUE(recCtx->writeStates.currentState->seq != 0);
FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
ioClientData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_REQUESTED) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_TRUE(recCtx->writeStates.currentState->seq != 0);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC003
* @spec Each sequence number is set to zero at the beginning of a connection and whenever the key is changed;
* the first record transmitted under a particular traffic key MUST use sequence number 0.
* @title The sequence number is 0 when the connection starts or the key changes.
* @precon nan
* @brief 5.3. Per-Record Nonce line 197
* 1. The client sends a finish packet and an app packet. After the seq number is not 0, the key is changed
* successfully and the seq number is reset to 0.
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RFC8446_CONSISTENCY_SEQUENCE_NUMBER_FUNC_TC003(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportExtendMasterSecret = true;
tlsConfig->isSupportClientVerify = true;
tlsConfig->isSupportNoClientCert = true;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
REC_Ctx *recCtx = (REC_Ctx *)server->ssl->recCtx;
ASSERT_TRUE(recCtx->writeStates.currentState->seq != 0);
ASSERT_TRUE(HITLS_KeyUpdate(server->ssl, HITLS_UPDATE_REQUESTED) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(recCtx->writeStates.currentState->seq == 0);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(server->ssl, (uint8_t *)"Hello World", sizeof("Hello World"), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(recCtx->writeStates.currentState->seq == 1);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_frame_tls13_consistency_rfc8446_record.c | C | unknown | 115,909 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "securec.h"
#include "rec_wrapper.h"
#include "conn_init.h"
#include "rec.h"
#include "parse.h"
#include "hs_msg.h"
#include "hs.h"
#include "alert.h"
#include "hitls_type.h"
#include "session_type.h"
#include "hitls_crypt_init.h"
#include "common_func.h"
#include "hlt.h"
#include "process.h"
#include "rec_read.h"
/* END_HEADER */
#define g_uiPort 6543
// REC_Read calls TlsRecordRead calls RecParseInnerPlaintext
int32_t RecParseInnerPlaintext(TLS_Ctx *ctx, const uint8_t *text, uint32_t *textLen, uint8_t *recType);
int32_t STUB_RecParseInnerPlaintext(TLS_Ctx *ctx, const uint8_t *text, uint32_t *textLen, uint8_t *recType)
{
(void)ctx;
(void)text;
(void)textLen;
*recType = (uint8_t)REC_TYPE_APP;
return HITLS_SUCCESS;
}
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} ResumeTestInfo;
static void TestFrameChangeCerts(void *msg, void *data)
{
(void)data;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
FRAME_CertificateMsg *certicate = &frameMsg->body.hsMsg.body.certificate;
FrameCertItem *cert = certicate->certItem->next->next; // 1 ->2 ->3 ->0
cert->next = certicate->certItem->next; // 3->2
certicate->certItem->next = cert; // 1->3
cert->next->next = NULL; // 2-> 0
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_TWO_DISOEDER_CHAIN_CERT_FUNC_TC001
* @spec -
* @title The certificate chain sent by the server contains two intermediate certificates, which are out of order
* (excluding the device certificate).
* @precon nan
* @brief The sender' s certificate MUST come in the first CertificateEntry in the list.
* 1. The certificate chain sent by the server contains two intermediate certificates, and the two intermediate
* certificates are out of order. Expected result 1 is displayed.
* @expect 1. The connection fails to be established. The error code is ALERT_BAD_CERTIFICATIONATE.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_TWO_DISOEDER_CHAIN_CERT_FUNC_TC001(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetCertPath(serverCtxConfig,
"rsa_sha512/otherRoot.der",
"rsa_sha512/otherInter.der:rsa_sha512/otherInter2.der",
"rsa_sha512/otherEnd.der",
"rsa_sha512/otherEnd.key.der",
"NULL",
"NULL");
HLT_SetClientVerifySupport(serverCtxConfig, true);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig,
"rsa_sha512/otherRoot.der",
"rsa_sha512/otherInter.der:rsa_sha512/otherInter2.der",
"rsa_sha512/otherEnd.der",
"rsa_sha512/otherEnd.key.der",
"NULL",
"NULL");
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_RSA_WITH_AES_256_CBC_SHA");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_FrameHandle frameHandle = {
.ctx = serverRes->ssl,
/* 1. The certificate chain sent by the server contains two intermediate certificates, and the two intermediate
* certificates are out of order. */
.frameCallBack = TestFrameChangeCerts,
.userData = NULL,
.expectHsType = CERTIFICATE,
.expectReType = REC_TYPE_HANDSHAKE,
.ioState = EXP_NONE,
.pointType = POINT_SEND,
};
ASSERT_TRUE(HLT_SetFrameHandle(&frameHandle) == HITLS_SUCCESS);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_MIDDLE_BOX_COMPAT_FUNC_TC001
* @spec -
* @title Test TLS 1.3 connection and data transfer when middlebox compatibility mode is enabled or disabled
* @precon nan
* @brief
* 1. The server and client are configured with middlebox compatibility mode controlled by the input parameter isMiddleBoxCompat.
* 2. Establish a TLS 1.3 connection between server and client.
* 3. Verify bidirectional data transmission integrity.
* @expect 1. The TLS 1.3 connection is successfully established.
* 2. Data sent by the server is correctly received by the client (length and content match).
* 3. Data sent by the client is correctly received by the server (length and content match).
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_MIDDLE_BOX_COMPAT_FUNC_TC001(int isMiddleBoxCompat)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetClientVerifySupport(serverCtxConfig, true);
HLT_SetMiddleBoxCompat(serverCtxConfig, isMiddleBoxCompat);
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetMiddleBoxCompat(clientCtxConfig, isMiddleBoxCompat);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
uint8_t writeData[REC_MAX_PLAIN_LENGTH] = {1};
uint32_t writeLen = REC_MAX_PLAIN_LENGTH;
uint8_t readData[REC_MAX_PLAIN_LENGTH] = {0};
uint32_t readLen = REC_MAX_PLAIN_LENGTH;
ASSERT_EQ(HLT_ProcessTlsWrite(localProcess, serverRes, writeData, writeLen), 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readData, readLen, &readLen), 0);
ASSERT_EQ(readLen, REC_MAX_PLAIN_LENGTH);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
ASSERT_EQ(HLT_ProcessTlsWrite(remoteProcess, clientRes, writeData, writeLen), 0);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readData, readLen, &readLen), 0);
ASSERT_EQ(readLen, REC_MAX_PLAIN_LENGTH);
ASSERT_EQ(memcmp(writeData, readData, readLen), 0);
EXIT:
HLT_FreeAllProcess();
HLT_CleanFrameHandle();
return;
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_hlt_tls13_consistency_rfc8446_1.c | C | unknown | 8,318 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "bsl_sal.h"
#include "tls.h"
#include "hlt.h"
#include "hlt_type.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "cert.h"
#include "process.h"
#include "securec.h"
#include "session_type.h"
#include "rec_wrapper.h"
#include "common_func.h"
#include "conn_init.h"
#include "hs_extensions.h"
#include "hitls_crypt_init.h"
#include "crypt_util_rand.h"
#include "alert.h"
/* END_HEADER */
#define PORT 23456
#define READ_BUF_SIZE (18 * 1024)
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; // Set the session to the client for session recovery.
HITLS_TicketKeyCb serverKeyCb;
} ResumeTestInfo;
typedef struct{
char *ClientCipherSuite;
char *ServerCipherSuite;
char *ClientGroup;
char *ServerGroup;
uint8_t ClientKeyExchangeMode;
uint8_t ServerKeyExchangeMode;
uint8_t psk[PSK_MAX_LEN];
bool SetNothing;
bool SuccessOrFail;
} SetInfo;
void SetConfig(HLT_Ctx_Config *clientconfig, HLT_Ctx_Config *serverconfig, SetInfo setInfo)
{
if ( !setInfo.SetNothing ) {
// Configure the server configuration.
if (setInfo.ServerCipherSuite != NULL) {
HLT_SetCipherSuites(serverconfig, setInfo.ServerCipherSuite);
}
if (setInfo.ServerGroup != NULL) {
HLT_SetGroups(serverconfig, setInfo.ServerGroup);
}
memcpy_s(serverconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
if ( (setInfo.ClientKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
clientconfig->keyExchMode = setInfo.ClientKeyExchangeMode;
}
// Configure the client configuration.
if (setInfo.ClientCipherSuite != NULL) {
HLT_SetCipherSuites(clientconfig, setInfo.ClientCipherSuite);
}
if (setInfo.ClientGroup != NULL) {
HLT_SetGroups(clientconfig, setInfo.ClientGroup);
}
memcpy_s(clientconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
if ( (setInfo.ServerKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
serverconfig->keyExchMode = setInfo.ServerKeyExchangeMode;
}
}
}
void ClientCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// The local client and remote server listen on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the config file.
SetConfig(clientConfig, serverConfig, setInfo);
// Listening connection.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
// Configure the interface for constructing abnormal messages.
handle->ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Establish a connection.
if ( setInfo.SuccessOrFail ) {
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) == 0);
}else {
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
}
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ServerCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
// Create a process.
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// The local client and remote server listen on the TLS connection.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the config file.
SetConfig(clientConfig, serverConfig, setInfo);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Insert abnormal message callback.
handle->ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
// Client listening connection.
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
if ( setInfo.SuccessOrFail ) {
ASSERT_TRUE(clientRes != NULL);
}else {
ASSERT_TRUE(clientRes == NULL);
}
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
void ResumeConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 1;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for the config context.
void *clientConfig = HLT_TlsNewCtx(TLS1_3);
ASSERT_TRUE(clientConfig != NULL);
// Configure the session restoration function.
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, TLS1_3, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, TLS1_3, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
if (cnt == 2) {
SetConfig(clientCtxConfig, serverCtxConfig, setInfo);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
}
DataChannelParam channelParam;
channelParam.port = PORT;
channelParam.type = TCP;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = TCP;
localProcess->connType = TCP;
// The server applies for the context.
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = TCP;
// Set the FD.
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
// Client, applying for context
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = TCP;
// Set the FD.
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
handle->ctx = clientSsl;
ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
if(!setInfo.SuccessOrFail){
ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
}else {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
}
}
else {
// Negotiation
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
// Data read/write
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// Disable the connection.
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_HasTicket(session) == true);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
}cnt++;
} while (cnt < 3); // Perform the connection twice.
EXIT:
HITLS_SESS_Free(session);
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
return;
}
static void FrameCallBack_ClientHello_PskBinder_Miss(void *msg, void *userData)
{
// ClientHello exception: The Binder field in the ClientHello message is lost.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
clienthello->psks.binders.state = MISSING_FIELD;
clienthello->psks.binderSize.state = ASSIGNED_FIELD;
clienthello->psks.binderSize.data = 0;
clienthello->psks.exLen.state = INITIAL_FIELD;
EXIT:
return;
}
static void FrameCallBack_ClientHello_LegacyVersion_Unsafe(void *msg, void *userData)
{
// ClientHello exception: The sent ClientHello message has its LegacyVersion set to SSL3.0.
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
clienthello->version.state = ASSIGNED_FIELD;
clienthello->version.data = HITLS_VERSION_SSL30;
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC002
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec the server MUST validate the corresponding binder value (see Section 4.2.11.2 below).
* If this value is not present or does not validate, the server MUST abort the handshake.
* @title Modify the binder of client hello so that it is lost.
* @precon nan
* @brief
* 1. The connection is established and the session is restored.
* 2. Modify the binder in the psk extension of the client hello message sent by the client so that the binder is lost.
* Observe the behavior of the server.
* @expect
* 1. The setting is successful.
* 2. The server terminates the handshake.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC002()
{
// The connection is established and the session is restored.
SetInfo setInfo = {0};
setInfo.SetNothing = 1;
setInfo.SuccessOrFail = 0;
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
// Modify the binder in the psk extension of the client hello message sent by the client so that the binder is lost.
// Observe the behavior of the server.
handle.frameCallBack = FrameCallBack_ClientHello_PskBinder_Miss;
ResumeConnectWithPara(&handle, setInfo);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC001
* @brief 4.2.11-Pre-Shared Key Extension-97
* @spec Implementations MUST NOT send a ClientHello.legacy_version or ServerHello.legacy_version set to 0x0300 or less.
* Any endpoint receiving a Hello message with ClientHello.legacy_version or ServerHello.legacy_version set to
* 0x0300 MUST abort the handshake with a "protocol_version" alert.
* @title The server receives a client hello message whose legacy_version is 0x0300.
* @precon nan
* @brief
* 1. Change the value of legacy_version in the client Hello message to 0x0300.
* 2. Observe the server behavior.
* @expect
* 1. The setting is successful.
* 2. The server terminates the handshake.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_LEGACY_VERSION_FUNC_TC001()
{
// Change the value of legacy_version in the client Hello message to 0x0300.
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Observe the server behavior.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = CLIENT_HELLO;
handle.frameCallBack = FrameCallBack_ClientHello_LegacyVersion_Unsafe;
handle.ctx = clientRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(&handle) == 0);
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
static void TEST_Server13_33_Err(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)data;
(void)len;
(void)bufSize;
uint32_t writeLen;
uint32_t sendLen = 5;
if (ctx->isClient==false){
uint8_t sendBuf[5] = {*(int *)user, 0x03, 0x03, 0x00, 0x00};
for (int i = 0; i < 33; i++) {
ASSERT_EQ(BSL_UIO_Write(ctx->uio, sendBuf, sendLen, &writeLen),0);
}
return;
}
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_EMPTY_RECORDS_FUNC_TC001
* @title 0-length CCS or handshake is received during tls13 handshake proccess.
* @precon nan
* @brief 1. Start a handshake with tls13 config, Expected result 1
* 2. Modify the server hello message. Expected result 2
* @expect 1. Return success
* 2. Handshake fails
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_EMPTY_RECORDS_FUNC_TC001(int rec_type)
{
CRYPT_RandRegist(TestSimpleRand);
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 8889, true);
ASSERT_TRUE(remoteProcess != NULL);
// Configure link information on the server.
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
serverCtxConfig->needCheckKeyUsage = true;
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&rec_type,
TEST_Server13_33_Err
};
RegisterWrapper(wrapper);
// The server listens on the TLS link.
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// Configure link information on the client.
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
ASSERT_EQ(
(ALERT_Description)HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId),ALERT_UNEXPECTED_MESSAGE);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_APPDATA_MAX_LENGTH
* @spec -
* @title In the TLS1.3 scenario, after the link is established, the app data with the maximum length is sent.
It is expected that the app data can be properly processed.
* @precon nan
* @brief 1.Configuring TLS1.3 Link Establishment. Expected result 1 is displayed.
2.Sending large packets. Expected result 2 is obtained.
* @expect 1.Return success
2.Return success
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_APPDATA_MAX_LENGTH(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 8888, false);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
uint8_t writeData[REC_MAX_PLAIN_LENGTH] = {1};
uint32_t writeLen = REC_MAX_PLAIN_LENGTH;
uint8_t readData[REC_MAX_PLAIN_LENGTH] = {0};
uint32_t readLen = REC_MAX_PLAIN_LENGTH;
ASSERT_EQ(HLT_ProcessTlsWrite(localProcess, clientRes, writeData, writeLen) , 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, serverRes, readData, readLen, &readLen) , 0);
ASSERT_EQ(readLen , REC_MAX_PLAIN_LENGTH);
ASSERT_EQ(memcmp(writeData, readData, readLen) , 0);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_hlt_tls13_consistency_rfc8446_2.c | C | unknown | 20,527 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "process.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "bsl_sal.h"
/* END_HEADER */
#define MAX_BUF 16384
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
} ResumeTestInfo;
static void Test_Client_Mode(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.pskModes.exData.state = ASSIGNED_FIELD;
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data);
uint16_t version[] = { 0x03, };
frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data =
BSL_SAL_Calloc(sizeof(version) / sizeof(uint8_t), sizeof(uint8_t));
ASSERT_EQ(memcpy_s(frameMsg.body.hsMsg.body.clientHello.pskModes.exData.data,
sizeof(version), version, sizeof(version)), EOK);
frameMsg.body.hsMsg.body.clientHello.keyshares.exState = MISSING_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.state = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Server_Keyshare(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.keyShare.data.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = *(uint64_t *)user;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
#define TEST_SERVERNAME_LENGTH 20
#define BUF_SIZE_DTO_TEST 18432
#define ROOT_DER "%s/ca.der:%s/inter.der"
#define INTCA_DER "%s/inter.der"
#define SERVER_DER "%s/server.der"
#define SERVER_KEY_DER "%s/server.key.der"
#define CLIENT_DER "%s/client.der"
#define CLIENT_KEY_DER "%s/client.key.der"
#define IP_ADDR_MAX_LEN 16
#define BYTE_SIZE 8
#define SNI_TYPE 2
#define LARGE_SIZE 1025
static int SetCertPath(HLT_Ctx_Config *ctxConfig, const char *certStr, bool isServer)
{
int ret;
char caCertPath[50];
char chainCertPath[30];
char eeCertPath[30];
char privKeyPath[30];
ret = sprintf_s(caCertPath, sizeof(caCertPath), ROOT_DER, certStr, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(chainCertPath, sizeof(chainCertPath), INTCA_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(eeCertPath, sizeof(eeCertPath), isServer ? SERVER_DER : CLIENT_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(privKeyPath, sizeof(privKeyPath), isServer ? SERVER_KEY_DER : CLIENT_KEY_DER, certStr);
ASSERT_TRUE(ret > 0);
HLT_SetCaCertPath(ctxConfig, (char *)caCertPath);
HLT_SetChainCertPath(ctxConfig, (char *)chainCertPath);
HLT_SetEeCertPath(ctxConfig, (char *)eeCertPath);
HLT_SetPrivKeyPath(ctxConfig, (char *)privKeyPath);
return 0;
EXIT:
return -1;
}
static void Test_Server_SVersion2(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
frameMsg.body.hsMsg.body.serverHello.supportedVersion.data.data = *(uint64_t *)user;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/* BEGIN_CASE */
void HITLS_TLS1_2_Config_SDV_23_0_5_0430(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Client_Mode
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "ecdsa_sha256/ca.der", "NULL", "NULL", "NULL", "NULL", "NULL");
clientRes = HLT_ProcessTlsInit(localProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC001
* @spec -
* @title Initialize the client server to tls1.3 and construct the selected_group carried in the key_share extension in
* the sent serverhello message. It is not the group of the keyshareentry carried in the clienthello message or
* the group provided in the clienthello message. As a result, the connection setup fails.
* @precon nan
* @brief 4.2.8. Key Share line 72
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC001(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
uint64_t groupreturn[] = {HITLS_EC_GROUP_SM2, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&groupreturn,
Test_Server_Keyshare
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
ASSERT_EQ(HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId), ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Server_Keyshare1(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_TRUE(frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data == *(uint64_t *)user);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC002
* @spec -
* @title clientHello's supported_groups is set to secp256r1. The handshake is successful.
* @precon nan
* @brief 9.1. Mandatory-to-Implement Cipher Suites line 230
* @expect 1. Expected connection setup success
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC002(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
uint64_t groupreturn[] = {HITLS_EC_GROUP_SECP256R1, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&groupreturn,
Test_Server_Keyshare1
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, clientRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_TlsRead(serverRes->ssl, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC003
* @spec -
* @title clientHello's supported_groups is set to X25519. The handshake succeeds.
* @precon nan
* @brief 9.1. Mandatory-to-Implement Cipher Suites line 230
* @expect 1. Expected connection setup success
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_KEYSHAREGROUP_FUNC_TC003(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
uint64_t groupreturn[] = {HITLS_EC_GROUP_CURVE25519, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&groupreturn,
Test_Server_Keyshare1
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_CURVE25519");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, clientRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_TlsRead(serverRes->ssl, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
#define HS_RANDOM_SIZE 32u
static const uint8_t g_hrrRandom[HS_RANDOM_SIZE] = {
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
};
static void Test_Server_Keyshare2(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
if (memcmp(frameMsg.body.hsMsg.body.serverHello.randomValue.data, g_hrrRandom, HS_RANDOM_SIZE) != 0) {
frameMsg.body.hsMsg.body.serverHello.keyShare.data.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.keyShare.data.group.data = *(uint64_t *)user;
}
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_NAMEDGROUP_FUNC_TC001
* @spec -
* @title 1. Initialize the client and server to tls1.3, construct the scenario where ecdhe is used, construct the
* scenario where hrr is sent, and construct the sent serverhello. The named group of the is different from
* that in the hrr. It is expected that the client terminates the handshake and sends the illegal_parameter
* alarm.
* @precon nan
* @brief 4.2.8. Key Share line 74
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_NAMEDGROUP_FUNC_TC001(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
uint64_t groupreturn[] = {HITLS_EC_GROUP_SM2, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&groupreturn,
Test_Server_Keyshare2
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetGroups(serverCtxConfig, "HITLS_EC_GROUP_CURVE25519:HITLS_EC_GROUP_SECP384R1");
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
ASSERT_EQ(HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId), ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Server_SVersion(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.cipherSuites.data);
uint16_t ciphers[3] = { 0xC02C, 0x1302, 0x1303};
frameMsg.body.hsMsg.body.clientHello.cipherSuites.data =
BSL_SAL_Calloc(sizeof(ciphers) / sizeof(uint16_t) + 1, sizeof(uint16_t));
ASSERT_EQ(memcpy_s(frameMsg.body.hsMsg.body.clientHello.cipherSuites.data,
sizeof(ciphers), ciphers, sizeof(ciphers)), EOK);
frameMsg.body.hsMsg.body.clientHello.cipherSuites.state = ASSIGNED_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC001
* @spec -
* @title The supported_versions in the clientHello is extended to 0x0304 (TLS 1.3). If the server supports only 1.2, the
* server returns a "protocol_version" warning and the handshake fails.
* @precon nan
* @brief Appendix D. Backward Compatibility line 247
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The connection is set up successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC001( )
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Server_SVersion
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ALERT_Info info = { 0 };
ALERT_GetInfo(clientRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC002
* @spec -
* @title The supported_versions field in clientHello is extended to 0x0304 (TLS 1.3). If the server supports TLS 1.3,
* the server returns serverHello, If the value of upported_versions is changed to 0x0300, the client returns the
* warning "ALERT_ELLEGAL_PARAMETER" and the handshake fails.
* @precon nan
* @brief Appendix D. Backward Compatibility line 247
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC002(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
uint64_t versions[] = {0x0300, };
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
&versions,
Test_Server_SVersion2
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath(serverCtxConfig, "ecdsa_sha256", true);
HLT_SetTls13CipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath(clientCtxConfig, "ecdsa_sha256", false);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetTls13CipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_MSG_HANDLE_UNSUPPORT_VERSION);
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
ASSERT_EQ(HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId), ALERT_ILLEGAL_PARAMETER);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Server_SVersion3(void *msg, void *userData)
{
HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
FRAME_ServerHelloMsg *serverhello = &frameMsg->body.hsMsg.body.serverHello;
serverhello->supportedVersion.exState = INITIAL_FIELD;
serverhello->supportedVersion.exLen.state = INITIAL_FIELD;
serverhello->supportedVersion.data.state = INITIAL_FIELD;
serverhello->supportedVersion.data.data = 0x0304;
FRAME_ModifyMsgInteger(HS_EX_TYPE_SUPPORTED_VERSIONS, &serverhello->supportedVersion.exType);
EXIT:
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC003
* @spec -
* @title clientHello version is 0x0303 and the server supports 1.3 and 1.2. In this case, the server returns
* serverHello and selects version 1.2, If supported_versions is set to 0x0304, the client returns a
* "ALERT_UNSUPPORTED_EXTENSION" warning and the handshake fails.
* @precon nan
* @brief Appendix D. Backward Compatibility line 247
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC003()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL,"SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetVersion(serverCtxConfig, HITLS_VERSION_TLS12, HITLS_VERSION_TLS13);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL,"CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_CleanFrameHandle();
HLT_FrameHandle handle = {0};
handle.pointType = POINT_SEND;
handle.userData = (void *)&handle;
handle.expectReType = REC_TYPE_HANDSHAKE;
handle.expectHsType = SERVER_HELLO;
handle.frameCallBack = Test_Server_SVersion3;
handle.ctx = serverRes->ssl;
ASSERT_TRUE(HLT_SetFrameHandle(&handle) == 0);
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
ASSERT_EQ(HLT_RpcTlsGetAlertFlag(remoteProcess, clientRes->sslId), ALERT_FLAG_SEND);
ASSERT_EQ(HLT_RpcTlsGetAlertLevel(remoteProcess, clientRes->sslId), ALERT_LEVEL_FATAL);
ASSERT_EQ(HLT_RpcTlsGetAlertDescription(remoteProcess, clientRes->sslId), ALERT_UNSUPPORTED_EXTENSION);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Server_SVersion6(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.version.data = 0x0304;
frameMsg.body.hsMsg.body.clientHello.supportedVersion.exState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC010
* @spec -
* @title ClientHello "supported_versions" extension does not exist, and ClientHello.legacy_version is TLS 1.3,
* The server supports TLS 1.3. Check that the server aborts the handshake with the "protocol_version" alert.
* @precon nan
* @brief Appendix D. Backward Compatibility line 248
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC010()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Server_SVersion6
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL,"SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL,"CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(clientRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC012
* @spec -
* @title ClientHello "supported_versions" extension does not exist, and ClientHello.legacy_version is TLS 1.2,
* The server only supports TLS 1.3. Check that the server aborts the handshake with the "protocol_version"
* alert.
* @precon nan
* @brief Appendix D. Backward Compatibility line 248
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_SVERSION_FUNC_TC012()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL,"SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetVersion(serverCtxConfig, HITLS_VERSION_TLS13, HITLS_VERSION_TLS13);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL,"CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_TlsConnect(clientRes->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(clientRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Server_MasterExtKey(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS12;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS12;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
ASSERT_TRUE(frameMsg.body.hsMsg.body.serverHello.extendedMasterSecret.exState == INITIAL_FIELD);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_MASTEREXTKEY_FUNC_TC001
* @spec -
* @title tls1.2 and tls1.3 carry the extended master key (overwrite the old and new versions). The handshake is
* successful.
* @precon nan
* @brief Appendix D. Backward Compatibility line 244
* @expect 1. Expected connection setup failure
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_MASTEREXTKEY_FUNC_TC001()
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
RecWrapper wrapper = {
TRY_SEND_SERVER_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Server_MasterExtKey
};
RegisterWrapper(wrapper);
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL,"SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetVersion(serverCtxConfig, HITLS_VERSION_TLS12, HITLS_VERSION_TLS13);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL,"CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsInit(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, clientRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_TlsRead(serverRes->ssl, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
static void Test_Client_PskTicket(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.psks.identities.data->identity.data[0] += 0x01;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_PSKTICKET_FUNC_TC001
* @spec -
* @title After the first connection is established, the ticket value is changed during session recovery. The session
* recovery is expected to fail.
* @precon nan
* @brief 4.6.1. New Session Ticket Message line 158
* @expect 1. Failed to restore the expected session.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_PSKTICKET_FUNC_TC001(int version, int connType)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[BUF_SIZE_DTO_TEST] = {0};
uint32_t readLen;
int32_t cnt = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportRenegotiation = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportRenegotiation = false;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = g_uiPort;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
if (cnt != 0) {
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Client_PskTicket
};
RegisterWrapper(wrapper);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, BUF_SIZE_DTO_TEST, 0, BUF_SIZE_DTO_TEST) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, BUF_SIZE_DTO_TEST, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt != 0) {
HITLS_SESS_Free(session);
session = NULL;
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_TRUE(isReused == 0);
}
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
cnt++;
} while (cnt < 2);
EXIT:
ClearWrapper();
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_hlt_tls13_consistency_rfc8446_extensions.c | C | unknown | 41,497 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "process.h"
#include "bsl_sal.h"
/* END_HEADER */
#define PORT 6666
#define MAX_BUF_SIZE 18432
void GetStrGroup(int ConnType, int group, char** strgroup)
{
if (ConnType == HITLS) {
switch (group) {
case HITLS_FF_DHE_2048:
*strgroup = "HITLS_FF_DHE_2048";break;
case HITLS_FF_DHE_3072:
*strgroup = "HITLS_FF_DHE_3072";break;
case HITLS_FF_DHE_4096:
*strgroup = "HITLS_FF_DHE_4096";break;
case HITLS_FF_DHE_6144:
*strgroup = "HITLS_FF_DHE_6144";break;
case HITLS_FF_DHE_8192:
*strgroup = "HITLS_FF_DHE_8192";break;
default:
break;
}
} else {
switch (group) {
case HITLS_FF_DHE_2048:
*strgroup = "ffdhe2048";break;
case HITLS_FF_DHE_3072:
*strgroup = "ffdhe3072";break;
case HITLS_FF_DHE_4096:
*strgroup = "ffdhe4096";break;
case HITLS_FF_DHE_6144:
*strgroup = "ffdhe6144";break;
case HITLS_FF_DHE_8192:
*strgroup = "ffdhe8192";break;
default:
break;
}
}
}
void HRR_ClientGroupSetInfo(int ClientType, int group, char** clientgroup)
{
if (ClientType == HITLS) {
switch (group) {
case HITLS_FF_DHE_2048:
*clientgroup = "HITLS_EC_GROUP_SECP256R1:HITLS_FF_DHE_2048";break;
case HITLS_FF_DHE_3072:
*clientgroup = "HITLS_EC_GROUP_SECP256R1:HITLS_FF_DHE_3072";break;
case HITLS_FF_DHE_4096:
*clientgroup = "HITLS_EC_GROUP_SECP256R1:HITLS_FF_DHE_4096";break;
case HITLS_FF_DHE_6144:
*clientgroup = "HITLS_EC_GROUP_SECP256R1:HITLS_FF_DHE_6144";break;
case HITLS_FF_DHE_8192:
*clientgroup = "HITLS_EC_GROUP_SECP256R1:HITLS_FF_DHE_8192";break;
default:
break;
}
} else {
switch (group) {
case HITLS_FF_DHE_2048:
*clientgroup = "P-256:ffdhe2048";break;
case HITLS_FF_DHE_3072:
*clientgroup = "P-256:ffdhe3072";break;
case HITLS_FF_DHE_4096:
*clientgroup = "P-256:ffdhe4096";break;
case HITLS_FF_DHE_6144:
*clientgroup = "P-256:ffdhe6144";break;
case HITLS_FF_DHE_8192:
*clientgroup = "P-256:ffdhe8192";break;
default:
break;
}
}
}
static void Test_FFDHE_Key_ERROR(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.state = ASSIGNED_FIELD;
memset_s(frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data,
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data, 255,
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data );
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_FFDHE_Key_Client_DecodeError(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.state = ASSIGNED_FIELD;
memset_s(frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data,
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data, 8, 10);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_FFDHE_KeyLen_LessThenStandard(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exLen.data += (120 - 256);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShareLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShareLen.data += (120 - 256);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data = 120;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.state = ASSIGNED_FIELD;
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data = BSL_SAL_Malloc(120);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.size = 120;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_FFDHE_KeyLen_MoreThenStandard(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exLen.data += (1100 - 256);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShareLen.state = INITIAL_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShareLen.data += (1100 - 256);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data = 1100;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.state = ASSIGNED_FIELD;
BSL_SAL_FREE(frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.data = BSL_SAL_Malloc(1100);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchange.size = 1100;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_FFDHE_KeyLen_Error(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = { 0 };
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = { 0 };
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.clientHello.keyshares.exKeyShares.data->keyExchangeLen.data = 128;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @brief tls1.3 ffdhe base testcase
* base test case
*/
/* BEGIN_CASE */
void UT_TLS13_RFC8446_FFDHE_TC001()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
HLT_SetGroups(clientCtxConfig, "HITLS_FF_DHE_4096");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->securitylevel = 0;
HLT_SetGroups(serverCtxConfig, "HITLS_FF_DHE_4096");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC001
* @spec -
* @title Verifying the HRR Link Setup Function When the FFDHE Group Is Used
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support ffdhe2048, and set the client to ffdhe2048 as the second supported
& group.
* 3. Establish a connection and read and write data.
* 4. Switch the group to ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The connection is set up successfully, and data is read and written successfully.
* 4. The connection is set up successfully and data is read and written successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC001(int group)
{
FRAME_Init();
int32_t ret;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(s_config != NULL);
uint16_t groups = group;
uint16_t clientGroups[2] = {HITLS_EC_GROUP_SECP256R1};
clientGroups[1] = group;
HITLS_CFG_SetGroups(c_config, clientGroups, 2);
HITLS_CFG_SetGroups(s_config, &groups, 1);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ret = FRAME_CreateConnection(client, server, false, HS_STATE_BUTT);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(client->ssl->negotiatedInfo.negotiatedGroup, group);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, data, strlen("Hello World"), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[MAX_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(server->ssl, readBuf, MAX_BUF_SIZE, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC002
* @spec -
* @title Verifying the FFDHE Curve Function in PSK Link Establishment
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Set the PSK mode to psk_with_dhe.
* 3. Configure the client and server to support FFDHE2048.
* 4. Establish a connection and read and write data.
* 5. Switch the group to ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The connection is set up successfully and data is read and written successfully.
* 5. The connection is successfully set up, and data is successfully read and written.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC002(int group)
{
FRAME_Init();
int32_t ret;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(s_config != NULL);
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(c_config, &cipherSuite, 1);
HITLS_CFG_SetCipherSuites(s_config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(c_config, TLS13_KE_MODE_PSK_WITH_DHE);
HITLS_CFG_SetKeyExchMode(s_config, TLS13_KE_MODE_PSK_WITH_DHE);
uint16_t groups = group;
HITLS_CFG_SetGroups(c_config, &groups, 1);
HITLS_CFG_SetGroups(s_config, &groups, 1);
HITLS_CFG_SetPskClientCallback(c_config, ExampleClientCb);
HITLS_CFG_SetPskServerCallback(s_config, ExampleServerCb);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ret = FRAME_CreateConnection(client, server, false, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(client->ssl->negotiatedInfo.negotiatedGroup, group);
ASSERT_EQ(client->ssl->negotiatedInfo.tls13BasicKeyExMode , TLS13_KE_MODE_PSK_WITH_DHE);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, data, strlen("Hello World"), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[MAX_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(server->ssl, readBuf, MAX_BUF_SIZE, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC003
* @spec -
* @title Verifying the Function of Using the FFDHE Curve for Certificate Rejection Authentication in psk_only Mode
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Set the PSK only on the client.
* 3. Set the PSK mode of the client and server to psk_with_only.
* 4. Set the client and server to ffdhe2048.
* 5. Establish a connection and read and write data.
* 6. Switch the group to ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The setting is successful.
* 5. The connection is successfully set up, and data is successfully read and written.
* 6. The connection is successfully set up, and data is successfully read and written.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC003(int group)
{
FRAME_Init();
int32_t ret;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *c_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(c_config != NULL);
HITLS_Config *s_config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(s_config != NULL);
uint16_t cipherSuite = HITLS_AES_128_GCM_SHA256;
HITLS_CFG_SetCipherSuites(c_config, &cipherSuite, 1);
HITLS_CFG_SetCipherSuites(s_config, &cipherSuite, 1);
HITLS_CFG_SetKeyExchMode(c_config, TLS13_KE_MODE_PSK_ONLY);
HITLS_CFG_SetKeyExchMode(s_config, TLS13_KE_MODE_PSK_ONLY);
uint16_t groups = group;
HITLS_CFG_SetGroups(c_config, &groups, 1);
HITLS_CFG_SetGroups(s_config, &groups, 1);
ExampleSetPsk("12121212121212");
HITLS_CFG_SetPskClientCallback(c_config, ExampleClientCb);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ret = FRAME_CreateConnection(client, server, false, HS_STATE_BUTT);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_EQ(client->ssl->negotiatedInfo.negotiatedGroup, group);
ASSERT_EQ(client->ssl->negotiatedInfo.tls13BasicKeyExMode , TLS13_CERT_AUTH_WITH_DHE);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, data, strlen("Hello World"), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[MAX_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(server->ssl, readBuf, MAX_BUF_SIZE, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC004
* @spec -
* @title The key length in the keyshare file is less than the length required by the RFC.
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support ffdhe2048.
* 3. Change the value of Key Exchange Length in the keyshare field in the client hello packet to 120.
* 4. Establish a connection and observe the server behavior.
* 5. Switch the group to ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192, and repeat the preceding operations.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The modification is successful.
* 4. The server sends an alert message to disconnect the connection.
* 5. The server sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC004(int ClientType, int ServerType, int group)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
char *servergroup;
char *clientgroup;
localProcess = HLT_InitLocalProcess(ClientType);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(ServerType, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the TLS1.3 configuration file.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support ffdhe2048.
GetStrGroup(ClientType, group, &clientgroup);
GetStrGroup(ServerType, group, &servergroup);
HLT_SetGroups(serverConfig, servergroup);
HLT_SetGroups(clientConfig, clientgroup);
// Change the value of Key Exchange Length in the keyshare field in the client hello packet to 120.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_FFDHE_KeyLen_LessThenStandard
};
RegisterWrapper(wrapper);
// Establish a connection and observe the server behavior.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC005
* @spec -
* @title The key length in the keyshare file is greater than the length required by the RFC.
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support FFDHE2048.
* 3. Change the value of Key Exchange Length in the keyshare message sent by the client to 8800.
* 4. Establish a connection and observe the server behavior.
* 5. Switch groups ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192. Repeat the preceding operations.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The modification is successful.
* 5. The server sends an alert message to disconnect the connection.
* 6. The server sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC005(int ClientType, int ServerType, int group)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
char *servergroup;
char *clientgroup;
localProcess = HLT_InitLocalProcess(ClientType);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(ServerType, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the TLS1.3 configuration file.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support FFDHE2048.
GetStrGroup(ClientType, group, &clientgroup);
GetStrGroup(ServerType, group, &servergroup);
HLT_SetGroups(serverConfig, servergroup);
HLT_SetGroups(clientConfig, clientgroup);
// Change the value of Key Exchange Length in the keyshare message sent by the client to 8800.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_FFDHE_KeyLen_MoreThenStandard
};
RegisterWrapper(wrapper);
// Establish a connection and observe the server behavior.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC006
* @spec -
* @title The server fails to parse the key in the keyshare file.
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support FFDHE2048.
* 3. Change the values of all bits of the key in the keyshare of the client hello packet to 0xff.
* 4. Establish a connection and observe the server behavior.
* 5. Switch groups ffdhe3072, ffdhe4096, ffdhe6144, and ffdhe8192. Repeat the preceding operations.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The modification is successful.
* 4. The server sends an alert message to disconnect the connection.
* 5. The server sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC006(int ClientType, int ServerType, int group)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
char *servergroup;
char *clientgroup;
localProcess = HLT_InitLocalProcess(ClientType);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(ServerType, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the TLS1.3 configuration file.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support FFDHE2048.
GetStrGroup(ClientType, group, &clientgroup);
GetStrGroup(ServerType, group, &servergroup);
HLT_SetGroups(serverConfig, servergroup);
HLT_SetGroups(clientConfig, clientgroup);
// Change the values of all bits of the key in the keyshare of the client hello packet to 0xff.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_FFDHE_Key_ERROR
};
RegisterWrapper(wrapper);
// Establish a connection and observe the server behavior.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_CRYPT_ERR_CALC_SHARED_KEY);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC007
* @spec -
* @title The server successfully parses the incorrect key in keyshare.
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support the elliptic curve ffdhe2048.
* 3. Change the value of the first 10 bits of the key in the keyshare of the client hello packet to 8.
* 4. Establish a connection and observe the client.
* 5. Switch the elliptic curve and repeat the preceding operations.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The modification is successful.
* 4. The client is disconnected due to decryption failure.
* 5. Client decryption fails.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC007(int ClientType, int ServerType, int group)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
char *servergroup;
char *clientgroup;
localProcess = HLT_InitLocalProcess(ClientType);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(ServerType, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the TLS1.3 configuration file.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support the elliptic curve ffdhe2048.
GetStrGroup(ClientType, group, &clientgroup);
GetStrGroup(ServerType, group, &servergroup);
HLT_SetGroups(serverConfig, servergroup);
HLT_SetGroups(clientConfig, clientgroup);
// Change the value of the first 10 bits of the key in the keyshare of the client hello packet to 8.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_FFDHE_Key_Client_DecodeError
};
RegisterWrapper(wrapper);
// Establish a connection and observe the client.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC008
* @spec -
* @title The key value in keyshare does not match the Key Exchange Length value. Parsing failed.
* @precon nan
* @brief
* 1. Apply for and initialize the TLS1.3 configuration file.
* 2. Configure the client and server to support FFDHE2048.
* 3. Change the length of the key in the keyshare of the client hello packet to 1024 bits.
* 4. Establish a connection and observe the server behavior.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The modification is successful.
* 4. The server sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_DHE_GROUP_FUNC_TC008(int ClientType, int ServerType, int group)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
char *servergroup;
char *clientgroup;
localProcess = HLT_InitLocalProcess(ClientType);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(ServerType, TCP, PORT, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the TLS1.3 configuration file.
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support FFDHE2048.
GetStrGroup(ClientType, group, &clientgroup);
GetStrGroup(ServerType, group, &servergroup);
HLT_SetGroups(serverConfig, servergroup);
HLT_SetGroups(clientConfig, clientgroup);
// Change the length of the key in the keyshare of the client hello packet to 1024 bits.
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_FFDHE_KeyLen_Error
};
RegisterWrapper(wrapper);
// Establish a connection and observe the server behavior.
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_PARSE_INVALID_MSG_LEN);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_hlt_tls13_consistency_rfc8446_ffdhe.c | C | unknown | 34,822 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_tls13_consistency_rfc8446 */
#include <stdio.h>
#include "stub_replace.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "tls.h"
#include "hs_ctx.h"
#include "pack.h"
#include "send_process.h"
#include "frame_link.h"
#include "frame_tls.h"
#include "frame_io.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "rec_wrapper.h"
#include "cert.h"
#include "securec.h"
#include "conn_init.h"
#include "hitls_crypt_init.h"
#include "hitls_psk.h"
#include "common_func.h"
#include "alert.h"
#include "process.h"
#include "bsl_sal.h"
/* END_HEADER */
#define MAX_BUF 16384
int32_t STUB_RecConnDecrypt(
TLS_Ctx *ctx, RecConnState *state, const REC_TextInput *cryptMsg, uint8_t *data, uint32_t *dataLen)
{
(void)ctx;
(void)state;
memcpy_s(data, cryptMsg->textLen, cryptMsg->text, cryptMsg->textLen);
(void)data;
*dataLen = cryptMsg->textLen;
return HITLS_SUCCESS;
}
int32_t STUB_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num)
{
(void)ctx;
(void)recordType;
(void)data;
(void)num;
return HITLS_SUCCESS;
}
extern int32_t __real_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num);
static void Test_FinishToAPP(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
STUB_Replace(user, __real_REC_Write, STUB_REC_Write);
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_ServerHello_Add_PhaExtensions(
HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
uint8_t posthandshake[] = {0x00, 0x31, 0x00, 0x00};
frameMsg.body.hsMsg.length.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.length.data += sizeof(posthandshake);
frameMsg.body.hsMsg.body.serverHello.extensionLen.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.serverHello.extensionLen.data =
frameMsg.body.hsMsg.body.serverHello.extensionLen.data + sizeof(posthandshake);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
ASSERT_EQ(memcpy_s(&data[*len], bufSize - *len, &posthandshake, sizeof(posthandshake)), EOK);
*len += sizeof(posthandshake);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_CertificateRequest_Ctx_Zero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE_REQUEST);
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtx.state = MISSING_FIELD;
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtxSize.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificateReq.certificateReqCtxSize.data = 0;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Certificate_Ctx_Zero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
frameMsg.body.hsMsg.body.certificate.certificateReqCtx.state = MISSING_FIELD;
frameMsg.body.hsMsg.body.certificate.certificateReqCtxSize.state = ASSIGNED_FIELD;
frameMsg.body.hsMsg.body.certificate.certificateReqCtxSize.data = 0;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Certificate_Ctx_NotSame(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CERTIFICATE);
frameMsg.body.hsMsg.body.certificate.certificateReqCtx.state = ASSIGNED_FIELD;
*(frameMsg.body.hsMsg.body.certificate.certificateReqCtx.data) += 1;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
static void Test_Finish_Error(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, FINISHED);
frameMsg.body.hsMsg.body.finished.verifyData.state = ASSIGNED_FIELD;
*(frameMsg.body.hsMsg.body.finished.verifyData.data) += 1;
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
}
/**
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_PHA_FUNC_TC001
* @brief tls1.3 post-handshake auth
* base test case
*/
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_PHA_FUNC_TC001()
{
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
bool isBlock = true;
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, isBlock);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *config_s = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(config_s != NULL);
HLT_SetPostHandshakeAuth(config_s, true);
HLT_SetClientVerifySupport(config_s, true);
HLT_Ctx_Config *config_c = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(config_c != NULL);
HLT_SetPostHandshakeAuth(config_c, true);
HLT_SetClientVerifySupport(config_c, true);
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, config_s, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, config_c, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
uint8_t src[] = "Hello world!";
uint32_t readbytes = 0;
uint8_t dest[READ_BUF_SIZE] = {0};
ASSERT_EQ(HLT_ProcessTlsWrite(localProcess, clientRes, src, sizeof(src)), 0);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, serverRes, dest, READ_BUF_SIZE, &readbytes), 0);
ASSERT_TRUE(readbytes == sizeof(src));
ASSERT_TRUE(memcmp(src, dest, readbytes) == 0);
memset_s(dest, READ_BUF_SIZE, 0, READ_BUF_SIZE);
EXIT:
HLT_FreeAllProcess();
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC002
* @spec -
* @title The client does not support post-handshake authentication, but receives the server hello message that carries
* the extension.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client not to support post-handshake extension
* 3. Set up a connection and modify the server hello message to carry the post-handshake extension.
* 4. Observe client behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
* 4. The client returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC002(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client not to support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, false);
HLT_SetClientVerifySupport(clientConfig, true);
// Set up a connection and modify the server hello message to carry the post-handshake extension.
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerHello_Add_PhaExtensions};
RegisterWrapper(wrapper);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// The client returns alert
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
EXIT:
HLT_FreeAllProcess();
ClearWrapper();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC003
* @spec -
* @title The client supports authentication after handshake, but receives the server hello message that carries the
* extension.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. Set up a connection and modify the server hello message to carry the post-handshake extension.
* 4. Observe client behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
* 4. The client returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC003(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
// Set up a connection and modify the server hello message to carry the post-handshake extension.
RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerHello_Add_PhaExtensions};
RegisterWrapper(wrapper);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
// The client returns alert
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) != 0);
EXIT:
HLT_FreeAllProcess();
ClearWrapper();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC004
* @spec -
* @title When the value of certificate_request_context in the certificate request message
* after handshake authentication is 0, the client reports an error.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. After the connection establishment is completed, modify the certificate_request_context of the
* certificate request message sent by the server to 0.
* 4. Observe client behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
* 4. The client returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC004(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
ASSERT_EQ(HITLS_VerifyClientPostHandshake(serverRes->ssl), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// modify the certificate_request_context of the certificate request message sent by the server to 0.
ClearWrapper();
RecWrapper wrapper = {
TRY_SEND_CERTIFICATE_REQUEST, REC_TYPE_HANDSHAKE, false, NULL, Test_CertificateRequest_Ctx_Zero};
RegisterWrapper(wrapper);
ASSERT_TRUE(HLT_TlsWrite(serverRes->ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == HITLS_SUCCESS);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
// The client returns alert
ASSERT_TRUE(HLT_RpcTlsRead(remoteProcess, clientRes->sslId, readBuf, READ_BUF_SIZE, &readLen) ==
HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC005
* @spec -
* @title The server reports an error when the value of certificate_request_context of the
* client certificate is 0 during authentication after handshake.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. After the connection is established, the server initiates a certificate request and changes the value of
* certificate_request_context in the certificate request message from the client to 0
* 4. Observe the server behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
* 4. The server returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC005(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverRes->sslId) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// changes the value of certificate_request_context in the certificate request message from the client to 0
ClearWrapper();
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_Certificate_Ctx_Zero};
RegisterWrapper(wrapper);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientRes->ssl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsWrite(clientRes->ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == HITLS_SUCCESS);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
// The server returns alert
ASSERT_EQ(HLT_RpcTlsRead(remoteProcess, serverRes->sslId, readBuf, READ_BUF_SIZE, &readLen),
HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC006
* @spec -
* @title During post-handshake authentication, the certificate_request_context sent by the client is inconsistent with
* that sent by the server. As a result, the server reports an error.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. After the connection is established, the server initiates a certificate request and changes the value of
* certificate_request_context in the certificate request message from the client
* 4. Observe the server behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
4. The server returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC006(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverRes->sslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// changes the value of certificate_request_context in the certificate request message from the client
ClearWrapper();
RecWrapper wrapper = {TRY_SEND_CERTIFICATE, REC_TYPE_HANDSHAKE, false, NULL, Test_Certificate_Ctx_NotSame};
RegisterWrapper(wrapper);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientRes->ssl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsWrite(clientRes->ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == HITLS_SUCCESS);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
// The server returns alert
ASSERT_EQ(HLT_RpcTlsRead(remoteProcess, serverRes->sslId, readBuf, READ_BUF_SIZE, &readLen),
HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC007
* @spec -
* @title After the PSK connection is established, the certificate is authenticated after handshake. The authentication is
* successful.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file
* 2. Setting the PSK on the Client and Server
* 3. Configure the client and server to support post-handshake extension
* 4. After the connection is established, the server sends a certificate request message for backhandshake authentication.
* @expect
* 1. Initialization succeeded.
* 2. Setting succeeded.
* 3. Setting succeeded.
* 4. Authentication succeeded.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC007(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the configuration file
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Configure the client and server to support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
// Setting the PSK on the Client and Server
memcpy_s(clientConfig->psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
memcpy_s(serverConfig->psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
HLT_SetCipherSuites(clientConfig, "HITLS_AES_128_GCM_SHA256");
HLT_SetCipherSuites(serverConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
// the server sends a certificate request message for backhandshake authentication.
ASSERT_EQ(HITLS_VerifyClientPostHandshake(serverRes->ssl), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_TlsWrite(serverRes->ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == HITLS_SUCCESS);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_EQ(HLT_RpcTlsRead(remoteProcess, clientRes->sslId, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, clientRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(serverRes->ssl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC008
* @spec -
* @title The server fails to verify the finish during post-authentication.
* @precon nan
* @brief
* 1. Apply and initialize config
* 2. Set the client support post-handshake extension
* 3. After the connection is established, the server initiates a certificate request and Modify the finish message sent
* by the client.
* 4. Observe the server behavior
* @expect
* 1. Initialization succeeded.
* 2. Set succeeded.
* 3. Modification succeeded.
* 4. The server returns alert
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC008(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18889, false);
ASSERT_TRUE(remoteProcess != NULL);
// Apply and initialize config
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverConfig != NULL);
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientConfig != NULL);
// Set the client support post-handshake extension
HLT_SetPostHandshakeAuth(serverConfig, true);
HLT_SetClientVerifySupport(serverConfig, true);
HLT_SetPostHandshakeAuth(clientConfig, true);
HLT_SetClientVerifySupport(clientConfig, true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), 0);
// the server initiates a certificate request
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverRes->sslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// Modify the finish message sent by the client.
ClearWrapper();
RecWrapper wrapper = {TRY_SEND_FINISH, REC_TYPE_HANDSHAKE, false, NULL, Test_Finish_Error};
RegisterWrapper(wrapper);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverRes->sslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientRes->ssl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsWrite(clientRes->ssl, (uint8_t *)writeBuf, strlen(writeBuf)) == HITLS_SUCCESS);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
// The server returns alert
ASSERT_EQ(HLT_RpcTlsRead(remoteProcess, serverRes->sslId, readBuf, READ_BUF_SIZE, &readLen),
HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC009
* @spec -
* @title Two certificates request messages are sent for backhandshake authentication,
and the required certificate_request_context is inconsistent.
* @precon nan
* @brief
1. Apply and initialize config
2. Set the client support post-handshake extension
3. After the connection is established, the server continuously sends certificate request messages for backhandshake
authentication
4. Check whether the certificate_request_context sent by the server is the same.
* @expect
1. Initialization succeeded.
2. Set succeeded.
3. Constructed successfully.
4. Inconsistent certificate_request_context
* @prior Level 1
* @auto TRUE+
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC009()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply and initialize config
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Set the client support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// the server continuously sends certificate request messages
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HITLS_Ctx *ctx = clientSsl;
uint8_t ReqCtx1[1 * 1024] = {0};
memcpy_s(ReqCtx1, ctx->certificateReqCtxSize, ctx->certificateReqCtx, ctx->certificateReqCtxSize);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// the server continuously sends certificate request messages
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// Inconsistent certificate_request_context
ASSERT_TRUE(memcmp(ReqCtx1, ctx->certificateReqCtx, ctx->certificateReqCtxSize) != 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC011
* @spec -
* @title The server does not allow the client to send an empty certificate. During authentication after handshake,
* the server receives an empty certificate from the client.
* @precon nan
* @brief
* 1. Apply for and initialize the config file. Expected result 1 is obtained.
* 2. Configure the server not to allow the client to send an empty certificate. Expected result 2 is obtained.
* 3. Configure the client to send an empty certificate. Expected result 3 is obtained.
* 4. Configure the client and server to support post-handshake extension. Expected result 4 is obtained.
* 5. Perform authentication after the connection is established. Expected result 5 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The setting is successful.
* 5. The connection is set up successfully. After receiving the client certificate, the server sends an alert message.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC011()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the config file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the server not to allow the client to send an empty certificate
// Configure the client and server to support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportNoClientCert = false;
serverCtxConfig->isSupportPostHandshakeAuth = true;
// Configure the client to send an empty certificate
HLT_SetCertPath(
clientCtxConfig, "rsa_sha256/ca.der:rsa_sha256/inter.der", "NULL", "NULL", "NULL", "NULL", "NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Perform authentication after the connection is established
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ret = HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
// the server sends an alert message.
ASSERT_EQ(ret, HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC012
* @spec -
* @title The server allows the client to send an empty certificate. During authentication after handshake,
* the server receives an empty certificate from the client.
* @precon nan
* @brief
* 1. Apply for and initialize the config file. Expected result 1 is obtained.
* 2. Configure the server to allow the client to send an empty certificate. Expected result 2 is obtained.
* 3. Configure the client to send an empty certificate. Expected result 3 is obtained.
* 4. Configure the client and server to support post-handshake extension. Expected result 4 is obtained.
* 5. Perform authentication after the connection is established. Expected result 5 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The setting is successful.
* 5. The connection is successfully set up, and the server initiates authentication.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC012()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the config file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the server to allow the client to send an empty certificate.
// Configure the client and server to support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportNoClientCert = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
// Configure the client to send an empty certificate
HLT_SetCertPath(
clientCtxConfig, "rsa_sha256/ca.der:rsa_sha256/inter.der", "NULL", "NULL", "NULL", "NULL", "NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Perform authentication after the connection is established
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// The connection is successfully set up, and the server initiates authentication.
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC013
* @spec -
* @title The server does not set the verification failure to continue the handshake. As a result, the server
* verification fails during the post-handshake authentication.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client and server to support post-handshake extension. Expected result 2 is obtained.
* 3. Set the server certificate to the RSA certificate and the client certificate to the ECDSA certificate. Expected
* result 3 is obtained.
* 4. After the connection is established, authentication is performed. Expected result 4 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The connection is successfully established, but the server fails to authenticate the certificate.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC013()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the configuration file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the client and server to support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
clientCtxConfig->isSupportVerifyNone = false;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportVerifyNone = false;
// Set the server certificate to the RSA certificate and the client certificate to the ECDSA certificate.
HLT_SetCertPath(clientCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
"ecdsa/end256-sha256.key.der",
"NULL",
"NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// After the connection is established, authentication is performed
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_EQ(
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen), HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC014
* @spec -
* @title The server continues the handshake if the verification fails. After the handshake,
* the server continues the handshake if the verification fails.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the server not to verify the peer certificate. Expected result 2 is obtained.
* 3. Configure the client and server to support post-handshake extension. Expected result 3 is obtained.
* 4. Set the client server certificate to RSA certificate, and set the client terminal certificate to ECDSA certificate.
* Expected result 4 is obtained.
* 5. After the connection is established, authentication is performed. Expected result 5 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The setting is successful.
* 5. The connection is successfully established and the authentication is successful.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC014()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
// Apply for and initialize the configuration file
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the server not to verify the peer certificate.
// Configure the client and server to support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportVerifyNone = true;
// Set the client server certificate to RSA certificate, and set the client terminal certificate to ECDSA
// certificate.
HLT_SetCertPath(clientCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"ecdsa/inter-nist521.der",
"ecdsa/end256-sha256.der",
"ecdsa/end256-sha256.key.der",
"NULL",
"NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// authentication is performed
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_EQ(memcmp(writeBuf, readBuf, readLen), 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC015
* @spec -
* @title During the authentication after the handshake on the client, the server is
* disconnected because app messages are mixed in the sent messages.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client and server to support post-handshake extension. Expected result 2 is obtained.
* 3. Establish a connection. The server initiates a handshake for authentication. Expected result 3 is displayed.
* 4. Modify the client to send messages. Enable the client to send an app message before sending the finish message.
* Expected result 4 is obtained.
* 5. Observe the server behavior. Expected result 5 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The connection is successfully set up and the server initiates authentication.
* 4. The client sends the message successfully.
* 5. The server sends an alert message to disconnect the connection.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC015()
{
STUB_Init();
FuncStubInfo tmpStubInfo;
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the configuration file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the client and server to support post-handshake extension
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
HLT_SetCertPath(clientCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// he server initiates a handshake for authentication
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// Enable the client to send an app message before sending the finish message.
RecWrapper wrapper = {TRY_SEND_FINISH, REC_TYPE_HANDSHAKE, false, &tmpStubInfo, Test_FinishToAPP};
RegisterWrapper(wrapper);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
STUB_Reset(&tmpStubInfo);
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_EQ(HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen),
HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
ClearWrapper();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC016
* @spec -
* @title During authentication after handshake, the server receives multiple app messages after
* sending the certificate request, and the processing is normal.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client and server to support post-handshake extension. Expected result 2 is obtained.
* 3. Establish a connection. The server initiates a handshake for authentication. Expected result 3 is obtained.
* 4. Send an app message to the server. After the server processes the message, check the server status.
* Expected result 4 is obtained.
* 5. Send an app message to the server. After the server processes the message, check the server status.
* Expected result 5 is obtained.
* 6. Continue the authentication. Expected result 6 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The connection is set up successfully, and the server sends a certificate request message.
* 4. The server is in try_recv_certifiacates state.
* 5. The server is in try_recv_certifiacates state.
* 6. The authentication is successful.
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC016()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the configuration file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the client and server to support post-handshake extension.
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
HLT_SetCertPath(clientCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// The server initiates a handshake for authentication
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
// Send an app message to the server
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
// Send an app message to the server
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// The authentication is successful.
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC017
* @spec -
* @title During post-handshake authentication, the server sends the app message after sending the certificate request
* message.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1 is obtained.
* 2. Configure the client and server to support post-handshake extension. Expected result 2 is obtained.
* 3. Establish a connection. The server initiates a handshake for authentication. Expected result 3 is displayed.
* 4. The server sends an app message. Expected result 4 is obtained.
* 5. Send an app message from the server. Expected result 5 is obtained.
* 6. Continue the authentication. Expected result 6 is obtained.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The connection is set up successfully, and the server sends a certificate request message.
* 4. The message is sent successfully.
* 5. The message is sent successfully.
* 6. The authentication is successful.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_TLS_TLS13_RFC8446_CONSISTENCY_POSTHANDSHAKE_FUNC_TC017()
{
int version = TLS1_3;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
// Apply for and initialize the configuration file
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(clientCtxConfig != NULL);
ASSERT_TRUE(serverCtxConfig != NULL);
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
// Configure the client and server to support post-handshake extension.
clientCtxConfig->isSupportClientVerify = true;
clientCtxConfig->isSupportPostHandshakeAuth = true;
serverCtxConfig->isSupportClientVerify = true;
serverCtxConfig->isSupportPostHandshakeAuth = true;
HLT_SetCertPath(clientCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
HLT_SetCertPath(serverCtxConfig,
"rsa_sha256/ca.der:rsa_sha256/inter.der",
"rsa_sha256/inter.der",
"rsa_sha256/server.der",
"rsa_sha256/server.key.der",
"NULL",
"NULL");
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
// The server initiates a handshake for authentication
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverSslId) == HITLS_SUCCESS);
;
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
const char *writeBuf = "Hello world";
// The server sends an app message
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
// The server sends an app message
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
// Continue the authentication.
HLT_TlsWrite(clientSsl, (uint8_t *)writeBuf, strlen(writeBuf));
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_sdv_hlt_tls13_consistency_rfc8446_pha.c | C | unknown | 75,885 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "logger.h"
#include "stub_crypt.h"
#define SIGNATURE_ALGORITHMS 0x04, 0x03 /* Fields added to the SERVER_HELLOW message */
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
#define TEMP_DATA_LEN 2048 /* Length of a single message */
#define ALERT_BODY_LEN 2u /* Alert data length */
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isServerExtendMasterSecret;
bool isSupportRenegotiation; /* Renegotiation support flag */
bool needStopBeforeRecvCCS; /* CCS test, so that the TRY_RECV_FINISH stops before the CCS message is received */
} HandshakeTestInfo;
int32_t StatusPark(HandshakeTestInfo *testInfo)
{
/** Construct connection */
testInfo->client = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
testInfo->server = FRAME_CreateLink(testInfo->config, BSL_UIO_TCP);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
/* Perform the CCS test so that the TRY_RECV_FINISH is stopped before the CCS message is received.
* The default value is False, which does not affect the original test.
*/
testInfo->client->needStopBeforeRecvCCS = testInfo->isClient ? testInfo->needStopBeforeRecvCCS : false;
testInfo->server->needStopBeforeRecvCCS = testInfo->isClient ? false : testInfo->needStopBeforeRecvCCS;
/** Establish a connection and stop in a certain state. */
if (FRAME_CreateConnection(testInfo->client, testInfo->server, testInfo->isClient, testInfo->state) !=
HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
int32_t DefaultCfgStatusPark(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/** Construct configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
testInfo->config->isSupportRenegotiation = testInfo->isSupportRenegotiation;
return StatusPark(testInfo);
}
int32_t DefaultCfgStatusParkWithSuite(HandshakeTestInfo *testInfo)
{
FRAME_Init();
/** Construct configuration. */
testInfo->config = HITLS_CFG_NewTLS12Config();
if (testInfo->config == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
HITLS_CFG_SetCheckKeyUsage(testInfo->config, false);
uint16_t cipherSuits[] = {HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(testInfo->config, cipherSuits, sizeof(cipherSuits) / sizeof(uint16_t));
testInfo->config->isSupportExtendMasterSecret = testInfo->isSupportExtendMasterSecret;
testInfo->config->isSupportClientVerify = testInfo->isSupportClientVerify;
testInfo->config->isSupportNoClientCert = testInfo->isSupportNoClientCert;
return StatusPark(testInfo);
}
#define TEST_CLIENT_SEND_FAIL 1
uint32_t g_uiPort = 8889;
void TestSetCertPath(HLT_Ctx_Config *ctxConfig, char *SignatureType)
{
if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA1", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA1")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA256")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA384")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA384_EE_PATH, RSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA512")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA512_EE_PATH, RSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
strlen("CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
strlen("CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
strlen("CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_ECDSA_SHA1", strlen("CERT_SIG_SCHEME_ECDSA_SHA1")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA1_CA_PATH,
ECDSA_SHA1_CHAIN_PATH,
ECDSA_SHA1_EE_PATH,
ECDSA_SHA1_PRIV_PATH,
"NULL",
"NULL");
}
}
void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
HITLS_KeyExchAlgo keyExType)
{
frametype->versionType = versionType;
frametype->recordType = recordType;
frametype->handshakeType = handshakeType;
frametype->keyExType = keyExType;
frametype->transportType = BSL_UIO_TCP;
}
FieldState *GetDataAddress(FRAME_Msg *data, void *member)
{
return (FieldState *)((size_t)data + (size_t)member);
}
void Test_MisClientHelloExtension(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len, uint32_t bufSize, void *user)
{
(void)ctx;
(void)bufSize;
(void)user;
FRAME_Type frameType = {0};
frameType.versionType = HITLS_VERSION_TLS13;
FRAME_Msg frameMsg = {0};
frameMsg.recType.data = REC_TYPE_HANDSHAKE;
frameMsg.length.data = *len;
frameMsg.recVersion.data = HITLS_VERSION_TLS13;
uint32_t parseLen = 0;
FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
ASSERT_EQ(parseLen, *len);
ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
FieldState *extensionState = GetDataAddress(&frameMsg, user);
*extensionState = MISSING_FIELD;
memset_s(data, bufSize, 0, bufSize);
FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
ASSERT_NE(parseLen, *len);
EXIT:
FRAME_CleanMsg(&frameType, &frameMsg);
return;
} | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/consistency/tls13/test_suite_tls13_consistency_rfc8446.base.c | C | unknown | 9,390 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include "securec.h"
#include "bsl_sal.h"
#include "frame_tls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "hitls_type.h"
#include "hitls_func.h"
#include "hitls.h"
#include "pack.h"
#include "bsl_err.h"
#include "bsl_bytes.h"
#include "custom_extensions.h"
#include "frame_tls.h"
#include "alert.h"
#include "frame_link.h"
#define CUSTOM_EXTENTIONS_TYPE_1 0x00001
#define CUSTOM_EXTENTIONS_TYPE_2 0x00002
// Simple add_cb function, allocates buffer with 1 byte length and 1 byte data
int SimpleAddCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, uint8_t **out, uint32_t *outLen,
HITLS_CERT_X509 *cert, uint32_t certId, uint32_t *alert, void *addArg)
{
(void)ctx;
(void)extType;
(void)context;
(void)cert;
(void)certId;
(void)alert;
(void)addArg;
*out = malloc(sizeof(uint16_t));
if (*out == NULL) {
return -1;
}
uint32_t bufOffset = 0;
(*out)[bufOffset] = 0xAA;
bufOffset++;
*outLen = bufOffset;
return HITLS_ADD_CUSTOM_EXTENSION_RET_PACK;
}
// Simple free_cb function, frees the allocated data
void SimpleFreeCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, uint8_t *out, void *addArg)
{
(void)ctx;
(void)extType;
(void)context;
(void)addArg;
BSL_SAL_Free(out);
}
// Simple parse_cb function, reads the length and data, checks the data
int SimpleParseCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, const uint8_t **in, uint32_t *inLen,
HITLS_CERT_X509 *cert, uint32_t certId, uint32_t *alert, void *parseArg)
{
(void)ctx;
(void)extType;
(void)context;
(void)cert;
(void)certId;
(void)alert;
(void)parseArg;
if (*inLen <= 0) {
return 0;
}
// Pass the data pointer to BSL_SAL_Dump
uint8_t *dumpedData = BSL_SAL_Dump(*in, *inLen);
if (dumpedData == NULL) {
return 1; // Processing failed
}
// Check the first byte of the returned data
if (dumpedData[0] != 0xAA) {
BSL_SAL_Free(dumpedData); // Free memory
return 1;
}
BSL_SAL_Free(dumpedData); // Free memory
return 0;
}
/* END_HEADER */
/** @
* @test SDV_TLS_PACK_CUSTOM_EXTENSIONS_API_TC001
* @title Test the single extension packing function of the PackCustomExtensions interface
* @precon None
* @brief
* 1. Initialize the TLS context and configure a single custom extension (no callback). Expected result 1.
* 2. Call the PackCustomExtensions interface and verify the packing result. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, packing length is 0 (no data without callback).
@ */
/* BEGIN_CASE */
void SDV_TLS_PACK_CUSTOM_EXTENSIONS_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0};
uint32_t bufLen = sizeof(buf);
uint32_t len = 0;
uint16_t extType = CUSTOM_EXTENTIONS_TYPE_1;
uint32_t context = 1;
// Configure a single custom extension
CustomExtMethods exts = {0};
CustomExtMethod meth = {0};
meth.extType = extType;
meth.context = context;
meth.addCb = NULL; // No callback
meth.freeCb = NULL; // No callback
exts.meths = &meth;
exts.methsCount = 1;
ctx->config.tlsConfig.customExts = &exts;
uint32_t bufOffset = 0;
uint8_t *buffAddr = &buf[0];
PackPacket pkt = {.buf = &buffAddr, .bufLen = &bufLen, .bufOffset = &bufOffset};
// Call the interface under test
// Verify the return value is success
ASSERT_EQ(PackCustomExtensions(ctx, &pkt, context, NULL, 0), HITLS_SUCCESS);
ctx->config.tlsConfig.customExts = NULL;
ASSERT_EQ(len, 0); // No data packed without add_cb
EXIT:
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(tlsConfig);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_PARSE_CUSTOM_EXTENSIONS_API_TC001
* @title Test the single extension parsing function of the ParseCustomExtensions interface
* @precon None
* @brief
* 1. Initialize the TLS context and configure a single custom extension (no callback). Expected result 1.
* 2. Prepare a buffer containing a single extension and call the ParseCustomExtensions interface. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, buffer offset is updated correctly.
@ */
/* BEGIN_CASE */
void SDV_TLS_PARSE_CUSTOM_EXTENSIONS_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0xAA}; // ext_type=1, len=0
uint32_t bufOffset = 0;
uint16_t extType = CUSTOM_EXTENTIONS_TYPE_1;
uint32_t context = 1;
uint32_t extLen = 1;
// Configure a single custom extension
CustomExtMethods exts = {0};
CustomExtMethod meth = {0};
meth.extType = extType;
meth.parseCb = NULL; // No callback
exts.meths = &meth;
exts.methsCount = 1;
ctx->config.tlsConfig.customExts = &exts;
// Call the interface under test
int32_t ret = ParseCustomExtensions(ctx, buf + bufOffset, extType, extLen, context, NULL, 0);
ctx->config.tlsConfig.customExts = NULL;
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
// Note: Current implementation doesn't update bufOffset without parse_cb, adjust expectation if needed
EXIT:
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(tlsConfig);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_PACK_CUSTOM_EXTENSIONS_MULTIPLE_API_TC001
* @title Test the multiple extensions packing function of the PackCustomExtensions interface
* @precon None
* @brief
* 1. Initialize the TLS context and configure two custom extensions. Expected result 1.
* 2. Call the PackCustomExtensions interface and verify the packing result. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, packing length is 0 (no data without callbacks).
@ */
/* BEGIN_CASE */
void SDV_TLS_PACK_CUSTOM_EXTENSIONS_MULTIPLE_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0};
uint32_t bufLen = sizeof(buf);
uint32_t len = 0;
uint32_t context = 1;
uint32_t methsCount = 1;
// Configure multiple custom extensions
CustomExtMethods exts = {0};
CustomExtMethod meths[2] = {{0}, {0}};
meths[0].extType = CUSTOM_EXTENTIONS_TYPE_1;
meths[0].context = context;
meths[0].addCb = NULL; // No callback
meths[0].freeCb = NULL;
meths[1].extType = CUSTOM_EXTENTIONS_TYPE_2;
meths[1].context = context;
meths[1].addCb = NULL; // No callback
meths[1].freeCb = NULL;
exts.meths = meths;
exts.methsCount = methsCount;
ctx->config.tlsConfig.customExts = &exts;
uint32_t bufOffset = 0;
uint8_t *buffAddr = &buf[0];
PackPacket pkt = {.buf = &buffAddr, .bufLen = &bufLen, .bufOffset = &bufOffset};
// Call the interface under test
int32_t ret = PackCustomExtensions(ctx, &pkt, context, NULL, 0);
ctx->config.tlsConfig.customExts = NULL;
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
ASSERT_EQ(len, 0); // No data packed without add_cb
EXIT:
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(tlsConfig);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_PACK_CUSTOM_EXTENSIONS_EMPTY_API_TC001
* @title Test the behavior of the PackCustomExtensions interface when there are no extensions
* @precon None
* @brief
* 1. Initialize the TLS context without configuring any custom extensions. Expected result 1.
* 2. Call the PackCustomExtensions interface and verify the packing result. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, packing length is 0.
@ */
/* BEGIN_CASE */
void SDV_TLS_PACK_CUSTOM_EXTENSIONS_EMPTY_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0};
uint32_t bufLen = sizeof(buf);
uint32_t len = 0;
uint32_t context = 1;
ctx->config.tlsConfig.customExts = NULL; // No extensions
uint32_t bufOffset = 0;
uint8_t *buffAddr = &buf[0];
PackPacket pkt = {.buf = &buffAddr, .bufLen = &bufLen, .bufOffset = &bufOffset};
// Call the interface under test
int32_t ret = PackCustomExtensions(ctx, &pkt, context, NULL, 0);
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
ASSERT_EQ(len, 0); // Verify the packing length is 0
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_PACK_CUSTOM_EXTENSIONS_CALLBACK_API_TC001
* @title Test the PackCustomExtensions interface with callbacks
* @precon None
* @brief
* 1. Initialize the TLS context and configure a single custom extension with add_cb and free_cb. Expected result 1.
* 2. Call the PackCustomExtensions interface and verify the packing result. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, packing length is 3 (ext_type + data), buffer content is correct.
@ */
/* BEGIN_CASE */
void SDV_TLS_PACK_CUSTOM_EXTENSIONS_CALLBACK_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0};
uint32_t bufLen = sizeof(buf);
uint32_t len = 0;
uint16_t extType = CUSTOM_EXTENTIONS_TYPE_1;
uint32_t context = 1;
uint32_t dataLen = 1;
// Configure a single custom extension with callbacks
CustomExtMethods exts = {0};
CustomExtMethod meth = {0};
meth.extType = extType;
meth.context = context;
meth.addCb = SimpleAddCb;
meth.freeCb = SimpleFreeCb;
exts.meths = &meth;
exts.methsCount = 1;
ctx->config.tlsConfig.customExts = &exts;
uint32_t bufOffset = 0;
uint8_t *buffAddr = &buf[0];
PackPacket pkt = {.buf = &buffAddr, .bufLen = &bufLen, .bufOffset = &bufOffset};
// Call the interface under test
int32_t ret = PackCustomExtensions(ctx, &pkt, context, NULL, 0);
ctx->config.tlsConfig.customExts = NULL;
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
len += bufOffset;
ASSERT_EQ(len, sizeof(uint16_t) + sizeof(uint16_t) + dataLen); // ext_type (2 byte) + len (2 byte) + data (1 byte)
// Verify the extension type
uint16_t packedType = BSL_ByteToUint16(buf);
ASSERT_EQ(packedType, extType);
uint16_t packedLen = BSL_ByteToUint16(&buf[sizeof(uint16_t)]);
ASSERT_EQ(packedLen, 1); // Verify the len
ASSERT_EQ(buf[len - 1], 0xAA); // Verify the data
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_PARSE_CUSTOM_EXTENSIONS_CALLBACK_API_TC001
* @title Test the ParseCustomExtensions interface with parse_cb
* @precon None
* @brief
* 1. Initialize the TLS context and configure a single custom extension with parse_cb. Expected result 1.
* 2. Prepare a buffer containing a single extension and call the ParseCustomExtensions interface. Expected result 2.
* @expect
* 1. Initialization successful.
* 2. Returns HITLS_SUCCESS, buffer offset is updated correctly.
@ */
/* BEGIN_CASE */
void SDV_TLS_PARSE_CUSTOM_EXTENSIONS_CALLBACK_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_NE(ctx, NULL);
uint8_t buf[1024] = {0xAA}; // ext_type=1 (big-endian), len=1, data=0xAA
uint32_t bufOffset = 0;
uint16_t extType = CUSTOM_EXTENTIONS_TYPE_1;
uint32_t context = 1;
uint32_t extLen = 1;
// Configure a single custom extension with parse callback
CustomExtMethods exts = {0};
CustomExtMethod meth = {0};
meth.extType = extType;
meth.context = context;
meth.parseCb = SimpleParseCb;
exts.meths = &meth;
exts.methsCount = 1;
ctx->config.tlsConfig.customExts = &exts;
// Call the interface under test
int32_t ret = ParseCustomExtensions(ctx, buf + bufOffset, extType, extLen, context, NULL, 0);
ctx->config.tlsConfig.customExts = NULL;
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_SSLCTX_ADD_CUSTOM_EXTENSION_API_TC002
* @title Test the custom extension addition functionality of the HITLS_AddCustomExtension function
* @precon None
* @brief
* 1. Initialize the TLS context and add a valid custom extension, verify if the addition is successful.
* Expected result 1.
* 2. Attempt to add a duplicate custom extension, verify if the function rejects the duplicate addition.
* Expected result 2.
* 3. Call the function with invalid parameters (add_cb is NULL, free_cb is not NULL), verify if the function correctly
* handles the error. Expected result 3.
* @expect
* 1. Returns HITLS_SUCCESS, the custom extension is correctly added to the context.
* 2. Returns 0, the number of extensions does not increase.
* 3. Returns 0, the number of extensions does not increase.
@ */
/* BEGIN_CASE */
void SDV_HITLS_ADD_CUSTOM_EXTENSION_API_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
ASSERT_NE(tlsConfig, NULL);
uint16_t extType = CUSTOM_EXTENTIONS_TYPE_1;
uint16_t invalidExtType = CUSTOM_EXTENTIONS_TYPE_2;
uint32_t context = 1;
HITLS_AddCustomExtCallback addCb = SimpleAddCb;
HITLS_FreeCustomExtCallback freeCb = SimpleFreeCb;
void *addArg = NULL;
HITLS_ParseCustomExtCallback parseCb = SimpleParseCb;
void *parseArg = NULL;
// Test normal case: Add a custom extension
HITLS_CustomExtParams params = {
.extType = extType,
.context = context,
.addCb = addCb,
.freeCb = freeCb,
.addArg = addArg,
.parseCb = parseCb,
.parseArg = parseArg
};
uint32_t ret = HITLS_CFG_AddCustomExtension(tlsConfig, ¶ms);
ASSERT_EQ(ret, HITLS_SUCCESS); // Verify the return value is success
ASSERT_EQ(tlsConfig->customExts->methsCount, 1); // Verify the number of extensions is 1
CustomExtMethod *meth = &tlsConfig->customExts->meths[0];
ASSERT_EQ(meth->extType, extType); // Verify the extension type
ASSERT_EQ(meth->context, context); // Verify the context
ASSERT_EQ(meth->addCb, addCb); // Verify add_cb
ASSERT_EQ(meth->freeCb, freeCb); // Verify free_cb
ASSERT_EQ(meth->addArg, addArg); // Verify add_arg
ASSERT_EQ(meth->parseCb, parseCb); // Verify parse_cb
ASSERT_EQ(meth->parseArg, parseArg); // Verify parse_arg
// Test boundary case: Attempt to add a duplicate extension
HITLS_CustomExtParams duplicateParams = {
.extType = extType,
.context = context,
.addCb = addCb,
.freeCb = freeCb,
.addArg = addArg,
.parseCb = parseCb,
.parseArg = parseArg
};
ret = HITLS_CFG_AddCustomExtension(tlsConfig, &duplicateParams);
ASSERT_EQ(ret, HITLS_CONFIG_DUP_CUSTOM_EXT); // Verify the return value is failure
ASSERT_EQ(tlsConfig->customExts->methsCount, 1); // Verify the number of extensions does not increase
// Test invalid parameters: add_cb is NULL, free_cb is not NULL
HITLS_CustomExtParams invalidParams = {
.extType = invalidExtType,
.context = context,
.addCb = NULL,
.freeCb = freeCb,
.addArg = addArg,
.parseCb = parseCb,
.parseArg = parseArg
};
ret = HITLS_CFG_AddCustomExtension(tlsConfig, &invalidParams);
ASSERT_EQ(ret, HITLS_INVALID_INPUT); // Verify the return value is failure
ASSERT_EQ(tlsConfig->customExts->methsCount, 1); // Verify the number of extensions does not increase
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
return;
}
/* END_CASE */
typedef struct {
uint32_t parsedContext[10];
uint32_t parsedContextCount;
uint32_t addedContext[10];
uint32_t addedContextCount;
uint32_t alertContext;
uint32_t alert;
bool addEmptyExt;
bool parseEmptyExt;
bool passExt;
} CustomExtensionArg;
int CustomExtensionAddCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, uint8_t **out, uint32_t *outLen,
HITLS_CERT_X509 *cert, uint32_t certId, uint32_t *alert, void *addArg)
{
(void)ctx;
(void)extType;
(void)cert;
(void)certId;
(void)alert;
CustomExtensionArg *arg = (CustomExtensionArg *)addArg;
arg->addedContext[arg->addedContextCount++] = context;
if ((arg->alertContext & context) != 0) {
*alert = arg->alert;
return -1;
}
if (arg->passExt) {
*out = NULL;
*outLen = 0;
return HITLS_ADD_CUSTOM_EXTENSION_RET_PASS;
}
if (arg->addEmptyExt) {
*out = NULL;
*outLen = 0;
return HITLS_ADD_CUSTOM_EXTENSION_RET_PACK;
}
*out = malloc(1);
if (*out == NULL) {
return -1;
}
*outLen = 1;
(*out)[0] = 0xAA;
return HITLS_ADD_CUSTOM_EXTENSION_RET_PACK;
}
// Simple free_cb function, frees the allocated data
void CustomExtensionFreeCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, uint8_t *out, void *addArg)
{
(void)ctx;
(void)extType;
(void)context;
(void)addArg;
BSL_SAL_Free(out);
}
// Simple parse_cb function, reads the length and data, checks the data
int CustomExtensionParseCb(const struct TlsCtx *ctx, uint16_t extType, uint32_t context, const uint8_t **in, uint32_t *inLen,
HITLS_CERT_X509 *cert, uint32_t certId, uint32_t *alert, void *parseArg)
{
(void)ctx;
(void)extType;
(void)context;
(void)cert;
(void)certId;
(void)alert;
CustomExtensionArg *arg = (CustomExtensionArg *)parseArg;
arg->parsedContext[arg->parsedContextCount++] = context;
if ((arg->alertContext & context) != 0) {
*alert = arg->alert;
return -1;
}
if (arg->parseEmptyExt) {
if (*inLen > 0) {
return -1;
}
return 0;
}
if (arg->passExt) {
return -1;
}
if (*inLen != 1 || (*in)[0] != 0xAA) {
return -1;
}
return 0;
}
/**
* @test SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC001
* @title Basic Functionality Test for Custom Extensions
*/
/* BEGIN_CASE */
void SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC001(void)
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *clientConfig = HITLS_CFG_NewTLS13Config();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS13Config();
HITLS_CFG_SetClientVerifySupport(serverConfig, true);
CustomExtensionArg serverArg = {0};
CustomExtensionArg clientArg = {0};
HITLS_CustomExtParams params = {
.extType = CUSTOM_EXTENTIONS_TYPE_2,
.context = HITLS_EX_TYPE_CLIENT_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO | HITLS_EX_TYPE_ENCRYPTED_EXTENSIONS | HITLS_EX_TYPE_TLS1_3_CERTIFICATE | HITLS_EX_TYPE_TLS1_3_CERTIFICATE_REQUEST | HITLS_EX_TYPE_TLS1_3_NEW_SESSION_TICKET,
.addCb = CustomExtensionAddCb,
.freeCb = CustomExtensionFreeCb,
.addArg = &clientArg,
.parseCb = CustomExtensionParseCb,
.parseArg = &clientArg
};
HITLS_CFG_AddCustomExtension(clientConfig, ¶ms);
params.addArg = &serverArg;
params.parseArg = &serverArg;
HITLS_CFG_AddCustomExtension(serverConfig, ¶ms);
FRAME_LinkObj *client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_EQ(clientArg.addedContextCount, 3);
ASSERT_EQ(clientArg.parsedContextCount, 7);
ASSERT_EQ(clientArg.addedContext[0], HITLS_EX_TYPE_CLIENT_HELLO);
ASSERT_EQ(clientArg.addedContext[1], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(clientArg.addedContext[2], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(clientArg.parsedContext[0], HITLS_EX_TYPE_TLS1_2_SERVER_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO | HITLS_EX_TYPE_HELLO_RETRY_REQUEST);
ASSERT_EQ(clientArg.parsedContext[1], HITLS_EX_TYPE_ENCRYPTED_EXTENSIONS);
ASSERT_EQ(clientArg.parsedContext[2], HITLS_EX_TYPE_TLS1_3_CERTIFICATE_REQUEST);
ASSERT_EQ(clientArg.parsedContext[3], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(clientArg.parsedContext[4], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(clientArg.parsedContext[5], HITLS_EX_TYPE_TLS1_3_NEW_SESSION_TICKET);
ASSERT_EQ(clientArg.parsedContext[6], HITLS_EX_TYPE_TLS1_3_NEW_SESSION_TICKET);
ASSERT_EQ(serverArg.addedContextCount, 7);
ASSERT_EQ(serverArg.parsedContextCount, 3);
ASSERT_EQ(serverArg.parsedContext[0], HITLS_EX_TYPE_CLIENT_HELLO);
ASSERT_EQ(serverArg.parsedContext[1], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(serverArg.parsedContext[2], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(serverArg.addedContext[0], HITLS_EX_TYPE_TLS1_3_SERVER_HELLO);
ASSERT_EQ(serverArg.addedContext[1], HITLS_EX_TYPE_ENCRYPTED_EXTENSIONS);
ASSERT_EQ(serverArg.addedContext[2], HITLS_EX_TYPE_TLS1_3_CERTIFICATE_REQUEST);
ASSERT_EQ(serverArg.addedContext[3], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(serverArg.addedContext[4], HITLS_EX_TYPE_TLS1_3_CERTIFICATE);
ASSERT_EQ(serverArg.addedContext[5], HITLS_EX_TYPE_TLS1_3_NEW_SESSION_TICKET);
ASSERT_EQ(serverArg.addedContext[5], HITLS_EX_TYPE_TLS1_3_NEW_SESSION_TICKET);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC002
* @title Alert Scenario Test for Custom Extensions
*/
/* BEGIN_CASE */
void SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC002()
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *clientConfig = HITLS_CFG_NewTLS13Config();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS13Config();
CustomExtensionArg serverArg = {0};
CustomExtensionArg clientArg = {0};
clientArg.alert = ALERT_ILLEGAL_PARAMETER;
clientArg.alertContext = HITLS_EX_TYPE_TLS1_3_SERVER_HELLO;
HITLS_CustomExtParams params = {
.extType = CUSTOM_EXTENTIONS_TYPE_2,
.context = HITLS_EX_TYPE_CLIENT_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO,
.addCb = CustomExtensionAddCb,
.freeCb = CustomExtensionFreeCb,
.addArg = &clientArg,
.parseCb = CustomExtensionParseCb,
.parseArg = &clientArg
};
HITLS_CFG_AddCustomExtension(clientConfig, ¶ms);
params.addArg = &serverArg;
params.parseArg = &serverArg;
HITLS_CFG_AddCustomExtension(serverConfig, ¶ms);
FRAME_LinkObj *client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), -1);
ALERT_Info info = {0};
ALERT_GetInfo(client->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_ILLEGAL_PARAMETER);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC003
* @title Empty Extension Capability Test
*/
/* BEGIN_CASE */
void SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC003()
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *clientConfig = HITLS_CFG_NewTLS13Config();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS13Config();
CustomExtensionArg serverArg = {0};
CustomExtensionArg clientArg = {0};
clientArg.addEmptyExt = true;
clientArg.parseEmptyExt = false;
serverArg.addEmptyExt = false;
serverArg.parseEmptyExt = true;
HITLS_CustomExtParams params = {
.extType = CUSTOM_EXTENTIONS_TYPE_2,
.context = HITLS_EX_TYPE_CLIENT_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO,
.addCb = CustomExtensionAddCb,
.freeCb = CustomExtensionFreeCb,
.addArg = &clientArg,
.parseCb = CustomExtensionParseCb,
.parseArg = &clientArg
};
HITLS_CFG_AddCustomExtension(clientConfig, ¶ms);
params.addArg = &serverArg;
params.parseArg = &serverArg;
HITLS_CFG_AddCustomExtension(serverConfig, ¶ms);
FRAME_LinkObj *client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), 0);
ASSERT_EQ(clientArg.addedContextCount, 1);
ASSERT_EQ(clientArg.parsedContextCount, 1);
ASSERT_EQ(clientArg.addedContext[0], HITLS_EX_TYPE_CLIENT_HELLO);
ASSERT_EQ(clientArg.parsedContext[0], HITLS_EX_TYPE_TLS1_2_SERVER_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO | HITLS_EX_TYPE_HELLO_RETRY_REQUEST);
ASSERT_EQ(serverArg.addedContextCount, 1);
ASSERT_EQ(serverArg.parsedContextCount, 1);
ASSERT_EQ(serverArg.addedContext[0], HITLS_EX_TYPE_TLS1_3_SERVER_HELLO);
ASSERT_EQ(serverArg.parsedContext[0], HITLS_EX_TYPE_CLIENT_HELLO);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC004
* @title Pass Extension Capability Test
*/
/* BEGIN_CASE */
void SDV_HITLS_CUSTOM_EXTENSION_FUNCTION_TC004()
{
FRAME_Init(); // Initialize the test framework
HITLS_Config *clientConfig = HITLS_CFG_NewTLS13Config();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS13Config();
CustomExtensionArg serverArg = {0};
CustomExtensionArg clientArg = {0};
clientArg.passExt = true;
serverArg.passExt = true;
HITLS_CustomExtParams params = {
.extType = CUSTOM_EXTENTIONS_TYPE_2,
.context = HITLS_EX_TYPE_CLIENT_HELLO | HITLS_EX_TYPE_TLS1_3_SERVER_HELLO,
.addCb = CustomExtensionAddCb,
.freeCb = CustomExtensionFreeCb,
.addArg = &clientArg,
.parseCb = CustomExtensionParseCb,
.parseArg = &clientArg
};
HITLS_CFG_AddCustomExtension(clientConfig, ¶ms);
params.addArg = &serverArg;
params.parseArg = &serverArg;
HITLS_CFG_AddCustomExtension(serverConfig, ¶ms);
FRAME_LinkObj *client = FRAME_CreateLink(clientConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(serverConfig, BSL_UIO_TCP);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), 0);
ASSERT_EQ(clientArg.addedContextCount, 1);
ASSERT_EQ(clientArg.parsedContextCount, 0);
ASSERT_EQ(serverArg.addedContextCount, 1);
ASSERT_EQ(serverArg.parsedContextCount, 0);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/custom/test_suite_sdv_custom_extensions.c | C | unknown | 28,516 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* INCLUDE_BASE ../consistency/tls12/test_suite_tls12_consistency_rfc5246_malformed_msg */
/* BEGIN_HEADER */
#include "securec.h"
#include "bsl_bytes.h"
#include "bsl_sal.h"
#include "stub_replace.h"
#include "hitls_error.h"
#include "tls.h"
#include "bsl_uio.h"
#include "rec.h"
#include "crypt.h"
#include "rec_conn.h"
#include "record.h"
#include "bsl_uio.h"
#include "hitls.h"
#include "frame_tls.h"
#include "cert_callback.h"
/* END_HEADER */
/* UserData structure transferred from the server to the alpnCb callback */
static uint8_t S_parsedList[100];
static uint32_t S_parsedListLen;
static TlsAlpnExtCtx alpnServerCtx = {0};
static uint8_t C_parsedList[100];
static uint32_t C_parsedListLen;
static int32_t ConfigAlpn(HITLS_Config *tlsConfig, char *AlpnList, bool isCient)
{
int32_t ret;
char defaultAlpnList[] = "http/1.1,spdy/1,spdy/2,spdy/3";
char *pAlpnList = NULL;
uint32_t AlpnListLen = 0;
if (AlpnList != NULL){
pAlpnList = AlpnList;
AlpnListLen = strlen(pAlpnList);
} else {
pAlpnList = defaultAlpnList;
AlpnListLen = strlen(pAlpnList);
}
/* client set alpn */
if (isCient) {
ret = ExampleAlpnParseProtocolList(C_parsedList, &C_parsedListLen, (uint8_t *)pAlpnList, AlpnListLen);
ASSERT_EQ(ret, HITLS_SUCCESS);
ret = HITLS_CFG_SetAlpnProtos(tlsConfig, C_parsedList, C_parsedListLen);
ASSERT_EQ(ret, HITLS_SUCCESS);
/* server set alpn and alpnSelectCb */
} else {
ret = ExampleAlpnParseProtocolList(S_parsedList, &S_parsedListLen, (uint8_t *)pAlpnList, AlpnListLen);
ASSERT_EQ(ret, HITLS_SUCCESS);
alpnServerCtx = (TlsAlpnExtCtx){ S_parsedList, S_parsedListLen };
ret = HITLS_CFG_SetAlpnProtosSelectCb(tlsConfig, ExampleAlpnCbForLlt, &alpnServerCtx);
ASSERT_EQ(ret, HITLS_SUCCESS);
}
EXIT:
return ret;
}
/**
* @test UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001
* @title ALPN function test
* @precon nan
* @brief server set alpn and alpn callback,client set alpn. The server supports the protocol configured on
the client .Expect result 1
* @expect 1. server returns the protocol supported by the client
*/
/* BEGIN_CASE */
void UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001(int version)
{
FRAME_Init();
RegDefaultMemCallback();
HITLS_Config *s_config = NULL;
HITLS_Config *c_config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
if (version == HITLS_VERSION_TLS12) {
c_config = HITLS_CFG_NewTLS12Config();
s_config = HITLS_CFG_NewTLS12Config();
} else if (version == HITLS_VERSION_TLS13) {
c_config = HITLS_CFG_NewTLS13Config();
s_config = HITLS_CFG_NewTLS13Config();
}
ASSERT_TRUE(c_config != NULL);
ASSERT_TRUE(s_config != NULL);
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetGroups(c_config, groups, sizeof(groups) / sizeof(uint16_t));
HITLS_CFG_SetSignature(c_config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
HITLS_CFG_SetGroups(s_config, groups, sizeof(groups) / sizeof(uint16_t));
HITLS_CFG_SetSignature(s_config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
ConfigAlpn(s_config, NULL, false);
ConfigAlpn(c_config, NULL, true);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
int32_t ret;
ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(memcmp(clientTlsCtx->negotiatedInfo.alpnSelected, "http/1.1", 8) == 0);
ASSERT_TRUE(clientTlsCtx->negotiatedInfo.alpnSelectedSize == 8);
ASSERT_TRUE(memcmp(serverTlsCtx->negotiatedInfo.alpnSelected, "http/1.1", 8) == 0);
ASSERT_TRUE(serverTlsCtx->negotiatedInfo.alpnSelectedSize == 8);
EXIT:
HITLS_CFG_FreeConfig(s_config);
HITLS_CFG_FreeConfig(c_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_alpn_interface.c | C | unknown | 4,793 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include "hitls_session.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "hitls_crypt_reg.h"
#include "crypt_errno.h"
#include "bsl_list.h"
#include "hitls_cert.h"
#include "parse_extensions_client.c"
#include "parse_extensions_server.c"
#include "parse_server_hello.c"
#include "parse_client_hello.c"
#include "uio_udp.c"
/* END_HEADER */
/** @
* @test UT_TLS_CFG_SET_GET_REC_INBUFFER_SIZE_API_TC001
* @title Test the HITLS_CFG_SetRecInbufferSize and HITLS_CFG_GetRecInbufferSize.
* @precon nan
* @brief HITLS_CFG_SetRecInbufferSize
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set recInbufferSize to an invalid value. Expected result 2.
* 3. Transfer a non-empty TLS connection handle information and set recInbufferSize to a valid value. Expected result 3
* HITLS_CFG_GetRecInbufferSize
* 1. Input an empty TLS connection handle or NULL recInbufferSize pointer. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and recInbufferSize pointer.Expected result 3.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_CONFIG_INVALID_LENGTH is returned
* 3. Returns HITLS_SUCCES.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_REC_INBUFFER_SIZE_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
config = HITLS_CFG_NewTLS12Config();
uint32_t recInbufferSize = 18433;
ASSERT_TRUE(HITLS_CFG_SetRecInbufferSize(NULL, recInbufferSize) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetRecInbufferSize(config, recInbufferSize) == HITLS_CONFIG_INVALID_LENGTH);
/* value < 512 */
recInbufferSize = 511;
ASSERT_TRUE(HITLS_CFG_SetRecInbufferSize(config, recInbufferSize) == HITLS_CONFIG_INVALID_LENGTH);
/* 18432 > value > 512 */
recInbufferSize = 1000;
ASSERT_TRUE(HITLS_CFG_SetRecInbufferSize(config, recInbufferSize) == HITLS_SUCCESS);
uint32_t recInbufferSize2 = 0;
ASSERT_TRUE(HITLS_CFG_GetRecInbufferSize(NULL, &recInbufferSize2) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetRecInbufferSize(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetRecInbufferSize(config, &recInbufferSize2) == HITLS_SUCCESS);
ASSERT_EQ(recInbufferSize2, 1000);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_GET_REC_INBUFFER_SIZE_API_TC001
* @title Test the HITLS_SetRecInbufferSize and HITLS_GetRecInbufferSize.
* @precon nan
* @brief HITLS_SetRecInbufferSize
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set recInbufferSize to an invalid value. Expected result 2.
* 3. Transfer a non-empty TLS connection handle information and set recInbufferSize to a valid value. Expected result 3
* HITLS_GetRecInbufferSize
* 1. Input an empty TLS connection handle or NULL recInbufferSize pointer. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and recInbufferSize pointer.Expected result 3.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_CONFIG_INVALID_LENGTH is returned
* 3. Returns HITLS_SUCCES.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_REC_INBUFFER_SIZE_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
config = HITLS_CFG_NewDTLS12Config();
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
/* value > 18432 */
uint32_t recInbufferSize = 18433;
ASSERT_TRUE(HITLS_SetRecInbufferSize(NULL, recInbufferSize) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetRecInbufferSize(ctx, recInbufferSize) == HITLS_CONFIG_INVALID_LENGTH);
/* value < 512 */
recInbufferSize = 511;
ASSERT_TRUE(HITLS_SetRecInbufferSize(ctx, recInbufferSize) == HITLS_CONFIG_INVALID_LENGTH);
/* 18432 > value > 512 */
recInbufferSize = 1000;
ASSERT_TRUE(HITLS_SetRecInbufferSize(ctx, recInbufferSize) == HITLS_SUCCESS);
uint32_t recInbufferSize2 = 0;
ASSERT_TRUE(HITLS_GetRecInbufferSize(NULL, &recInbufferSize2) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetRecInbufferSize(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetRecInbufferSize(ctx, &recInbufferSize2) == HITLS_SUCCESS);
ASSERT_EQ(recInbufferSize2, 1000);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_buffer_minimization.c | C | unknown | 6,441 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "hitls_type.h"
#include "rec_wrapper.h"
#include "hs_ctx.h"
#include "tls.h"
#include "hitls_config.h"
#include "alert.h"
#include "hitls_func.h"
/* END_HEADER */
static void CaListNodeInnerDestroy(void *data)
{
HITLS_TrustedCANode *tmpData = (HITLS_TrustedCANode *)data;
BSL_SAL_FREE(tmpData->data);
BSL_SAL_FREE(tmpData);
return;
}
/**
* @test UT_TLS_TLS13_RECV_CA_LIST_TC001
* @brief 1. Use the default configuration items to configure the client and server, Expect result 1.
* 2. Load the CA file into the configuration, Expect result 1.
* 3. Set the CA list in the configuration, Expect result 1.
* 4. Create the client and server links, Expect result 2.
* @expect 1. HITLS_SUCCESS
* 2. caList->count == 1
*/
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_LoadCAFile_API_TC001(int version, char *certFile, char *userdata)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_TrustedCAList *caList = NULL;
ASSERT_TRUE(HITLS_CFG_SetDefaultPasswordCbUserdata(tlsConfig, userdata) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDefaultPasswordCbUserdata(tlsConfig) == userdata);
ASSERT_TRUE(HITLS_CFG_ParseCAList(tlsConfig, certFile, (uint32_t)strlen(certFile), TLS_PARSE_TYPE_FILE,
TLS_PARSE_FORMAT_ASN1, &caList) == HITLS_SUCCESS);
ASSERT_TRUE(caList != NULL);
ASSERT_TRUE(caList->count == 1);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
BSL_LIST_FREE(caList, CaListNodeInnerDestroy);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS13_RECV_CA_LIST_TC001
* @spec -
* @title The CA list is parsed correctly.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, Expect result 1.
* 2. Load the CA file into the configuration, Expect result 1.
* 3. Set the CA list in the configuration, Expect result 1.
* 4. Create the client and server links, Expect result 1.
* 5. Create the connection between the client and server, Expect result 2.
* 6. Get the peer CA list from the server, Expect result 1.
* 7. Verify that the peer CA list is not NULL and contains one CA, Expect result 3.
* @expect 1. HITLS_SUCCESS
* 2. link established successfully
* 3. peerList != NULL
@ */
/* BEGIN_CASE */
void UT_TLS_TLS13_RECV_CA_LIST_TC001(char *certFile)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
HITLS_TrustedCAList *caList = NULL;
ASSERT_TRUE(HITLS_CFG_ParseCAList(config, certFile, (uint32_t)strlen(certFile), TLS_PARSE_TYPE_FILE,
TLS_PARSE_FORMAT_ASN1, &caList) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCAList(config, caList) == HITLS_SUCCESS);
ASSERT_TRUE(caList != NULL);
ASSERT_TRUE(caList->count == 1);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_TrustedCAList *peerList = HITLS_GetPeerCAList(server->ssl);
ASSERT_TRUE(peerList != NULL);
ASSERT_TRUE(peerList->count == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_TLS12_RECV_CA_LIST_TC001
* @spec -
* @title The CA list is parsed correctly.
* @precon nan
* @brief 1. Use the default configuration items to configure the client and server, Expect result 1.
* 2. Load the CA file into the configuration, Expect result 1.
* 3. Set the CA list in the configuration, Expect result 1.
* 4. set ClientVerifySupport, Expect result 1.
* 4. Create the client and server links, Expect result 1.
* 5. Create the connection between the client and server, Expect result 2.
* 6. Get the peer CA list from the server, Expect result 1.
* 7. Verify that the peer CA list is not NULL and contains one CA, Expect result 3.
* @expect 1. HITLS_SUCCESS
* 2. link established successfully
* 3. peerList != NULL
@ */
/* BEGIN_CASE */
void UT_TLS_TLS12_RECV_CA_LIST_TC001(char *certFile)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_TrustedCAList *caList = NULL;
ASSERT_TRUE(HITLS_CFG_ParseCAList(config, certFile, (uint32_t)strlen(certFile), TLS_PARSE_TYPE_FILE,
TLS_PARSE_FORMAT_ASN1, &caList) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCAList(config, caList) == HITLS_SUCCESS);
HITLS_CFG_SetClientVerifySupport(config, true);
ASSERT_TRUE(caList != NULL);
ASSERT_TRUE(caList->count == 1);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_TrustedCAList *peerList = HITLS_GetPeerCAList(client->ssl);
ASSERT_TRUE(peerList != NULL);
ASSERT_TRUE(peerList->count == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_ca_list.c | C | unknown | 6,453 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include "hitls_session.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "hitls_crypt_reg.h"
#include "crypt_errno.h"
#include "bsl_list.h"
#include "hitls_cert.h"
#include "parse_extensions_client.c"
#include "parse_extensions_server.c"
#include "parse_server_hello.c"
#include "parse_client_hello.c"
#include "uio_udp.c"
/* END_HEADER */
/** @
* @test UT_TLS_CFG_SET_GET_MAX_SEND_FRAGMENT_API_TC001
* @title Test the HITLS_CFG_SetMaxSendFragment and HITLS_CFG_GetMaxSendFragment.
* @precon nan
* @brief HITLS_CFG_SetMaxSendFragment
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set maxSendFragment to an invalid value. Expected result 2.
* 3. Transfer a non-empty TLS connection handle information and set maxSendFragment to a valid value. Expected result 3
* HITLS_CFG_GetMaxSendFragment
* 1. Input an empty TLS connection handle or NULL maxSendFragment pointer. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and maxSendFragment pointer.Expected result 3.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_CONFIG_INVALID_LENGTH is returned
* 3. Returns HITLS_SUCCES.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_MAX_SEND_FRAGMENT_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
config = HITLS_CFG_NewDTLS12Config();
uint16_t maxSendFragment = 16385;
ASSERT_TRUE(HITLS_CFG_SetMaxSendFragment(NULL, maxSendFragment) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetMaxSendFragment(config, maxSendFragment) == HITLS_CONFIG_INVALID_LENGTH);
/* value < 512 */
maxSendFragment = 511;
ASSERT_TRUE(HITLS_CFG_SetMaxSendFragment(config, maxSendFragment) == HITLS_CONFIG_INVALID_LENGTH);
/* 16384 > value > 512 */
maxSendFragment = 1000;
ASSERT_TRUE(HITLS_CFG_SetMaxSendFragment(config, maxSendFragment) == HITLS_SUCCESS);
uint16_t maxSendFragment2 = 0;
ASSERT_TRUE(HITLS_CFG_GetMaxSendFragment(NULL, &maxSendFragment2) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxSendFragment(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxSendFragment(config, &maxSendFragment2) == HITLS_SUCCESS);
ASSERT_EQ(maxSendFragment2, 1000);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_GET_MAX_SEND_FRAGMENT_API_TC001
* @title Test the HITLS_SetMaxSendFragment and HITLS_GetMaxSendFragment.
* @precon nan
* @brief HITLS_SetMaxSendFragment
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set maxSendFragment to an invalid value. Expected result 2.
* 3. Transfer a non-empty TLS connection handle information and set maxSendFragment to a valid value. Expected result 3
* HITLS_GetMaxSendFragment
* 1. Input an empty TLS connection handle or NULL maxSendFragment pointer. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and maxSendFragment pointer.Expected result 3.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_CONFIG_INVALID_LENGTH is returned
* 3. Returns HITLS_SUCCES.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_MAX_SEND_FRAGMENT_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
config = HITLS_CFG_NewDTLS12Config();
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
/* value > 16384 */
uint16_t maxSendFragment = 16385;
ASSERT_TRUE(HITLS_SetMaxSendFragment(NULL, maxSendFragment) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetMaxSendFragment(ctx, maxSendFragment) == HITLS_CONFIG_INVALID_LENGTH);
/* value < 512 */
maxSendFragment = 511;
ASSERT_TRUE(HITLS_SetMaxSendFragment(ctx, maxSendFragment) == HITLS_CONFIG_INVALID_LENGTH);
/* 16384 > value > 512 */
maxSendFragment = 1000;
ASSERT_TRUE(HITLS_SetMaxSendFragment(ctx, maxSendFragment) == HITLS_SUCCESS);
uint16_t maxSendFragment2 = 0;
ASSERT_TRUE(HITLS_GetMaxSendFragment(NULL, &maxSendFragment2) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetMaxSendFragment(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetMaxSendFragment(ctx, &maxSendFragment2) == HITLS_SUCCESS);
ASSERT_EQ(maxSendFragment2, 1000);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_MAX_SEND_FRAGMENT_TC001
* @title Test the HITLS_SetMaxSendFragment
* @precon nan
* @brief HITLS_SetMaxSendFragment
* 1. Create connection. Expected result 1.
* 2. set maxSendFragment to 1000 bytes. Expected result 1.
* 3. Invoke hitls_write to write 1200 bytes. Expected result 2.
* @expect 1. Returns HITLS_SUCCES
* 2. Only 1000 bytes of data is sent
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_MAX_SEND_FRAGMENT_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewDTLS12Config();
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
/* value > 512 */
uint16_t maxSendFragment = 1000;
ASSERT_TRUE(HITLS_SetMaxSendFragment(client->ssl, maxSendFragment) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
const uint8_t sndBuf[1200] = {0};
uint32_t writeLen = 0;
ASSERT_EQ(HITLS_Write(client->ssl, sndBuf, sizeof(sndBuf), &writeLen), HITLS_SUCCESS);
ASSERT_EQ(writeLen, 1000);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_max_send_fragment.c | C | unknown | 7,831 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include "hitls_session.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "hitls_crypt_reg.h"
#include "crypt_errno.h"
#include "bsl_list.h"
#include "hitls_cert.h"
#include "parse_extensions_client.c"
#include "parse_extensions_server.c"
#include "parse_server_hello.c"
#include "parse_client_hello.c"
#include "uio_udp.c"
/* END_HEADER */
/* @
* @test UT_TLS_CFG_SET_DTLS_LINK_MTU_API_TC001
* @title Test HITLS_SetLinkMtu interface
* @brief 1. Create the TLS configuration object config.Expect result 1.
* 2. Use config to create the client and server.Expect result 2.
* 3. Invoke HITLS_SetLinkMtu, Expect result 3.
* @expect 1. The config object is successfully created.
* 2. The client and server are successfully created.
* 3. mtu >= 256, Return HITLS_SUCCESS. mtu < 256, Return HITLS_CONFIG_INVALID_LENGTH.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_DTLS_LINK_MTU_API_TC001(void)
{
FRAME_Init();
uint32_t mtu = 1500;
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(HITLS_SetLinkMtu(client->ssl, mtu) == HITLS_SUCCESS);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_SetLinkMtu(server->ssl, mtu) == HITLS_SUCCESS);
/* value < 256 */
mtu = 200;
ASSERT_TRUE(HITLS_SetLinkMtu(server->ssl, mtu) == HITLS_CONFIG_INVALID_LENGTH);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_NO_QUERY_MTU_API_TC001
* @title Test the HITLS_SetNoQueryMtu interfaces.
* @precon nan
* @brief HITLS_SetNoQueryMtu
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set noQueryMtu to an invalid value. Expected result 2.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_NO_QUERY_MTU_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewDTLS12Config();
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
bool noQueryMtu = false;
ASSERT_TRUE(HITLS_SetNoQueryMtu(NULL, noQueryMtu) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetNoQueryMtu(client->ssl, noQueryMtu) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetNoQueryMtu(server->ssl, noQueryMtu) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_GET_NEED_QUERY_MTU_API_TC001
* @title Test the HITLS_GetNeedQueryMtu interfaces.
* @precon nan
* @brief HITLS_GetNeedQueryMtu
* 1. Input an empty TLS connection handle or NULL needQueryMtu pointer. Expected result 1.
* 2. Input non empty ssl ctx and non empty needQueryMtu pointer. Expected result 2.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_NEED_QUERY_MTU_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewDTLS12Config();
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
bool needQueryMtu = true;
ASSERT_TRUE(HITLS_GetNeedQueryMtu(NULL, &needQueryMtu) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetNeedQueryMtu(NULL, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetNeedQueryMtu(client->ssl, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetNeedQueryMtu(client->ssl, &needQueryMtu) == HITLS_SUCCESS);
ASSERT_EQ(needQueryMtu, false);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
int32_t STUB_REC_Write(TLS_Ctx *ctx, REC_Type recordType, const uint8_t *data, uint32_t num)
{
if ((ctx == NULL) || (ctx->recCtx == NULL) ||
(num != 0 && data == NULL) ||
(num == 0 && recordType != REC_TYPE_APP)) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15537, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"Record write: input null pointer.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
int32_t ret = HITLS_REC_NORMAL_IO_BUSY;
#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
if (ret != HITLS_SUCCESS) {
if (!BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
return ret;
}
bool exceeded = false;
(void)BSL_UIO_Ctrl(ctx->uio, BSL_UIO_UDP_MTU_EXCEEDED, sizeof(bool), &exceeded);
if (exceeded) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17362, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"Record write: get EMSGSIZE error.", 0, 0, 0, 0);
ctx->needQueryMtu = true;
}
}
#endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */
return ret;
}
int32_t STUB_UdpSocketCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg)
{
(void)uio;
(void)cmd;
(void)larg;
*(bool *)parg = true;
return BSL_SUCCESS;
}
/** @
* @test UT_TLS_CM_MTU_EMSGSIZE_TC001
* @title Test the HITLS_SetMaxSendFragment
* @precon nan
* @brief HITLS_SetMaxSendFragment
* 1. Create connection. Expected result 1.
* 2. set maxSendFragment to 1000 bytes. Expected result 1.
* 3. Invoke hitls_write to write 1200 bytes. Expected result 2.
* @expect 1. Returns HITLS_SUCCES
* 2. Only 1000 bytes of data is sent
@ */
/* BEGIN_CASE */
void UT_TLS_CM_MTU_EMSGSIZE_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewDTLS12Config();
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
STUB_Init();
FuncStubInfo tmpStubInfo = {0};
FuncStubInfo tmpStubInfo2 = {0};
STUB_Replace(&tmpStubInfo, REC_Write, STUB_REC_Write);
STUB_Replace(&tmpStubInfo2, FRAME_Ctrl, STUB_UdpSocketCtrl);
ASSERT_TRUE(HITLS_SetMtu(client->ssl, 500) == HITLS_SUCCESS);
ASSERT_EQ(client->ssl->config.pmtu, 500);
const uint8_t sndBuf[1200] = {0};
uint32_t writeLen = 0;
ASSERT_EQ(HITLS_Write(client->ssl, sndBuf, sizeof(sndBuf), &writeLen), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(writeLen, 0);
STUB_Reset(&tmpStubInfo);
STUB_Reset(&tmpStubInfo2);
bool needQueryMtu = false;
ASSERT_TRUE(HITLS_GetNeedQueryMtu(client->ssl, &needQueryMtu) == HITLS_SUCCESS);
ASSERT_EQ(needQueryMtu, true);
ASSERT_EQ(HITLS_Write(client->ssl, sndBuf, sizeof(sndBuf), &writeLen), HITLS_SUCCESS);
/* use min mtu 256, and the encrypt cost is need to be reduced */
ASSERT_TRUE(writeLen < 256);
ASSERT_TRUE(writeLen > 0);
EXIT:
STUB_Reset(&tmpStubInfo);
STUB_Reset(&tmpStubInfo2);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_mtu.c | C | unknown | 10,035 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "frame_tls.h"
#include "frame_link.h"
#include "session.h"
#include "hitls_config.h"
#include "hitls_crypt_init.h"
#include "crypt_provider_local.h"
#include "crypt_eal_implprovider.h"
#include "crypt_provider.h"
#include "crypt_errno.h"
#include "cert_callback.h"
#include "test.h"
#include "crypt_eal_rand.h"
/* END_HEADER */
/* BEGIN_CASE */
void UT_TLS13_LOADPROVIDER_GROUP_TC001(char *path, char *get_cap_test1, int cmd)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)path;
(void)get_cap_test1;
(void)cmd;
SKIP_TEST();
#else
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
CRYPT_EAL_LibCtx *libCtx = NULL;
CRYPT_EAL_ProvMgrCtx *providerMgr = NULL;
HITLS_Config *config = NULL;
int32_t ret = CRYPT_SUCCESS;
libCtx = CRYPT_EAL_LibCtxNew();
ASSERT_TRUE(libCtx != NULL);
ASSERT_EQ(CRYPT_EAL_ProviderSetLoadPath(libCtx, path), CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, BSL_SAL_LIB_FMT_OFF, "default", NULL, NULL);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, cmd, get_cap_test1, NULL, &providerMgr);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ASSERT_TRUE(providerMgr != NULL);
// Random Unloading Test Case
ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(libCtx, GetAvailableRandAlgId(),
"provider=provider_get_cap_test1", NULL, 0, NULL), CRYPT_SUCCESS);
config = HITLS_CFG_ProviderNewTLS13Config(libCtx, NULL);
ASSERT_TRUE(config != NULL);
uint16_t group = 477;
HITLS_CFG_SetGroups(config, &group, 1);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
if (libCtx != NULL) {
CRYPT_EAL_LibCtxFree(libCtx);
}
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void UT_TLS13_LOADPROVIDER_SIGNSCHEME_TC001(char *path, char *get_cap_test1, int cmd)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)path;
(void)get_cap_test1;
(void)cmd;
SKIP_TEST();
#else
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
CRYPT_EAL_LibCtx *libCtx = NULL;
CRYPT_EAL_ProvMgrCtx *providerMgr = NULL;
HITLS_Config *config = NULL;
int32_t ret = CRYPT_SUCCESS;
libCtx = CRYPT_EAL_LibCtxNew();
ASSERT_TRUE(libCtx != NULL);
ASSERT_EQ(CRYPT_EAL_ProviderSetLoadPath(libCtx, path), CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, cmd, get_cap_test1, NULL, &providerMgr);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, BSL_SAL_LIB_FMT_OFF, "default", NULL, NULL);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ASSERT_TRUE(providerMgr != NULL);
// Random Unloading Test Case
ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(libCtx, GetAvailableRandAlgId(),
"provider=provider_get_cap_test1", NULL, 0, NULL), CRYPT_SUCCESS);
config = HITLS_CFG_ProviderNewTLS13Config(libCtx, NULL);
ASSERT_TRUE(config != NULL);
uint16_t signScheme = 23333;
HITLS_CFG_SetSignature(config, &signScheme, 1);
FRAME_CertInfo certInfo = {
"new_signAlg/ca.der",
"new_signAlg/inter.der",
"new_signAlg/client.der",
NULL,
"new_signAlg/client.key.der",
NULL
};
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
if (libCtx != NULL) {
CRYPT_EAL_LibCtxFree(libCtx);
}
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void UT_TLS13_LOADPROVIDER_NEWKEYTYPE_TC001(char *path, char *provider_new_alg_test, int cmd)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)path;
(void)provider_new_alg_test;
(void)cmd;
SKIP_TEST();
#else
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
CRYPT_EAL_LibCtx *libCtx = NULL;
CRYPT_EAL_ProvMgrCtx *providerMgr = NULL;
HITLS_Config *config = NULL;
int32_t ret = CRYPT_SUCCESS;
libCtx = CRYPT_EAL_LibCtxNew();
ASSERT_TRUE(libCtx != NULL);
ASSERT_EQ(CRYPT_EAL_ProviderSetLoadPath(libCtx, path), CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, cmd, provider_new_alg_test, NULL, &providerMgr);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ret = CRYPT_EAL_ProviderLoad(libCtx, BSL_SAL_LIB_FMT_OFF, "default", NULL, NULL);
ASSERT_EQ(ret, CRYPT_SUCCESS);
ASSERT_TRUE(providerMgr != NULL);
// Random Unloading Test Case
ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(libCtx, GetAvailableRandAlgId(),
"provider=default", NULL, 0, NULL), CRYPT_SUCCESS);
config = HITLS_CFG_ProviderNewTLS13Config(libCtx, NULL);
ASSERT_TRUE(config != NULL);
uint16_t signScheme = 24444;
HITLS_CFG_SetSignature(config, &signScheme, 1);
FRAME_CertInfo certInfo = {
"new_keyAlg/ca.der",
"new_keyAlg/inter.der",
"new_keyAlg/client.der",
NULL,
"new_keyAlg/client.key.der",
NULL
};
client = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLinkWithCert(config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
if (libCtx != NULL) {
CRYPT_EAL_LibCtxFree(libCtx);
}
#endif
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_provider.c | C | unknown | 6,469 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stddef.h>
#include "securec.h"
#include "tls_config.h"
#include "tls.h"
#include "hitls_type.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_error.h"
#include "hitls_config.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "transcript_hash.h"
#include "conn_init.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "common_func.h"
#include "hitls_crypt_init.h"
#include "alert.h"
#define TEST_SERVERNAME_LENGTH 20
#define READ_BUF_SIZE 18432
/* END_HEADER */
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
HITLS_TicketKeyCb serverKeyCb;
} SniTestInfo;
typedef struct {
char servername[TEST_SERVERNAME_LENGTH + 1];
int32_t alert;
} SNI_Arg;
static SNI_Arg *sniArg = NULL;
static char *g_serverName = "huawei.com";
static char *g_serverNameErr = "www.huawei.com";
static uint8_t *g_sessionId;
static uint32_t g_sessionIdSize;
int32_t ServernameCbErrOK(HITLS_Ctx *ctx, int *alert, void *arg)
{
(void)ctx;
(void)alert;
(void)arg;
return HITLS_ACCEPT_SNI_ERR_OK;
}
void STUB_SendAlert(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
(void)ctx;
(void)level;
(void)description;
return;
}
typedef struct TEST_SNI_DEAL_CB {
uint32_t sniState;
HITLS_SniDealCb sniDealCb;
} TEST_SNI_DEAL_CB;
typedef struct {
HITLS_Config *clientConfig;
HITLS_Config *serverConfig;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
HITLS_SniDealCb sniDealCb;
HITLS_Session *clientSession;
uint16_t version;
BSL_UIO_TransportType type;
} HandshakeTestInfo;
int32_t TestCreateConfig(HITLS_Config **cfg, uint16_t version)
{
switch (version) {
case HITLS_VERSION_DTLS12:
*cfg = HITLS_CFG_NewDTLS12Config();
break;
case HITLS_VERSION_TLS13:
*cfg = HITLS_CFG_NewTLS13Config();
break;
case HITLS_VERSION_TLS12:
*cfg = HITLS_CFG_NewTLS12Config();
break;
default:
break;
}
if (*cfg == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
void FreeSNIArg(SNI_Arg *sniArg)
{
if (sniArg != NULL) {
BSL_SAL_FREE(sniArg);
}
}
void SetCommonConfig(HITLS_Config **config)
{
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1};
HITLS_CFG_SetGroups(*config, groups, sizeof(groups) / sizeof(uint16_t));
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(*config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
const uint16_t cipherSuites[] = {HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_DHE_DSS_WITH_AES_256_GCM_SHA384,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_DHE_DSS_WITH_AES_128_GCM_SHA256,
HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(*config, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t));
HITLS_CFG_SetClientVerifySupport(*config, true);
HITLS_CFG_SetExtenedMasterSecretSupport(*config, true);
HITLS_CFG_SetNoClientCertSupport(*config, true);
HITLS_CFG_SetRenegotiationSupport(*config, true);
HITLS_CFG_SetPskServerCallback(*config, (HITLS_PskServerCb)ExampleServerCb);
HITLS_CFG_SetPskClientCallback(*config, (HITLS_PskClientCb)ExampleClientCb);
HITLS_CFG_SetSessionTicketSupport(*config, false);
HITLS_CFG_SetCheckKeyUsage(*config, false);
}
static int32_t CreateLink(HandshakeTestInfo *testInfo)
{
testInfo->client = FRAME_CreateLink(testInfo->clientConfig, testInfo->type);
if (testInfo->client == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
if (testInfo->clientSession != NULL) {
int32_t ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
testInfo->server = FRAME_CreateLink(testInfo->serverConfig, testInfo->type);
if (testInfo->server == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
return HITLS_SUCCESS;
}
static int32_t DefaultCfgAndLink(HandshakeTestInfo *testInfo)
{
FRAME_Init();
TestCreateConfig(&(testInfo->clientConfig), testInfo->version);
if (testInfo->clientConfig == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
SetCommonConfig(&(testInfo->clientConfig));
HITLS_CFG_SetServerName(testInfo->clientConfig, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName));
TestCreateConfig(&(testInfo->serverConfig), testInfo->version);
if (testInfo->serverConfig == NULL) {
return HITLS_INTERNAL_EXCEPTION;
}
SetCommonConfig(&(testInfo->serverConfig));
HITLS_CFG_SetServerNameCb(testInfo->serverConfig, testInfo->sniDealCb);
sniArg = (SNI_Arg *)BSL_SAL_Calloc(1, sizeof(SNI_Arg));
snprintf_s(sniArg->servername, sizeof(sniArg->servername), strlen(g_serverName), "%s", g_serverName);
if (HITLS_CFG_SetServerNameArg(testInfo->serverConfig, sniArg) != HITLS_SUCCESS) {
return HITLS_INTERNAL_EXCEPTION;
}
return CreateLink(testInfo);
}
int32_t GetSessionId(HandshakeTestInfo *testInfo)
{
FRAME_Type frameType = {0};
FRAME_Msg recvframeMsg = {0};
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo->client->io);
uint8_t *recMsg = ioUserData->recMsg.msg;
uint32_t recMsgLen = ioUserData->recMsg.len;
frameType.handshakeType = SERVER_HELLO;
frameType.recordType = REC_TYPE_HANDSHAKE;
frameType.versionType = testInfo->version;
uint32_t parseLen = 0;
int32_t ret = FRAME_ParseMsg(&frameType, recMsg, recMsgLen, &recvframeMsg, &parseLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
FRAME_ServerHelloMsg *serverHello = &recvframeMsg.body.hsMsg.body.serverHello;
g_sessionIdSize = serverHello->sessionIdSize.data;
BSL_SAL_FREE(g_sessionId);
g_sessionId = BSL_SAL_Dump(serverHello->sessionId.data, g_sessionIdSize);
FRAME_CleanMsg(&frameType, &recvframeMsg);
return HITLS_SUCCESS;
}
int32_t FirstHandshake(HandshakeTestInfo *testInfo)
{
int32_t ret = FRAME_CreateConnection(testInfo->client, testInfo->server, true, TRY_RECV_SERVER_HELLO);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = GetSessionId(testInfo);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint8_t data[] = "Hello World";
uint32_t writeLen;
ret = HITLS_Write(testInfo->server->ssl, data, sizeof(data), &writeLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ret = FRAME_TrasferMsgBetweenLink(testInfo->server, testInfo->client);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = HITLS_Read(testInfo->client->ssl, readBuf, READ_BUF_SIZE, &readLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
testInfo->clientSession = HITLS_GetDupSession(testInfo->client->ssl);
FRAME_FreeLink(testInfo->client);
testInfo->client = NULL;
FRAME_FreeLink(testInfo->server);
testInfo->server = NULL;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_SNI_RESUME_SERVERNAME_FUNC_TC001
* @title During session resumption, the serverName extension of clientHello
is different from that in first handshake
* @precon nan
* @brief 1. For the first handshake, set serverName to huawei.com in the clientHello.
Expected result 1
2. During session resumption, changed serverName of clientHello to www.sss.com. Expected result 2
3. process the client hello. Expected result 2
* @expect 1. The serverName extension is set successfully and the handshake succeeds
2. return success
@ */
/* BEGIN_CASE */
void UT_TLS_SNI_RESUME_SERVERNAME_FUNC_TC001(int version, int type)
{
g_sessionId = NULL;
g_sessionIdSize = 0;
HandshakeTestInfo testInfo = {0};
testInfo.version = (uint16_t)version;
testInfo.type = (BSL_UIO_TransportType)type;
testInfo.sniDealCb = ServernameCbErrOK;
ASSERT_EQ(DefaultCfgAndLink(&testInfo), HITLS_SUCCESS);
ASSERT_EQ(FirstHandshake(&testInfo), HITLS_SUCCESS);
ASSERT_TRUE(testInfo.clientSession != NULL);
ASSERT_TRUE(CreateLink(&testInfo) == HITLS_SUCCESS);
ASSERT_TRUE(
FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(testInfo.server->ssl->hsCtx->state == TRY_RECV_CLIENT_HELLO);
CONN_Deinit(testInfo.server->ssl);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
uint8_t *buffer = ioUserData->recMsg.msg;
uint32_t len = ioUserData->recMsg.len;
ASSERT_TRUE(len != 0);
FRAME_Msg frameMsg = {0};
uint32_t parseLen = 0;
HS_Init(testInfo.server->ssl);
ASSERT_TRUE(ParserTotalRecord(testInfo.server, &frameMsg, buffer, len, &parseLen) == HITLS_SUCCESS);
ASSERT_TRUE(frameMsg.body.handshakeMsg.type == CLIENT_HELLO);
char *hostName = "www.sss.com";
uint8_t *serverName = frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverName;
uint16_t serverNameSize = frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverNameSize;
frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverNameSize = strlen(hostName) + 1;
frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverName = (uint8_t *)hostName;
testInfo.server->ssl->method.sendAlert = STUB_SendAlert;
CONN_Init(testInfo.server->ssl);
if (testInfo.type == BSL_UIO_TCP) {
ASSERT_TRUE(Tls12ServerRecvClientHelloProcess(testInfo.server->ssl, &frameMsg.body.handshakeMsg, true) ==
HITLS_SUCCESS);
} else {
ASSERT_TRUE(DtlsServerRecvClientHelloProcess(testInfo.server->ssl, &frameMsg.body.handshakeMsg) ==
HITLS_SUCCESS);
}
frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverName = serverName;
frameMsg.body.handshakeMsg.body.clientHello.extension.content.serverNameSize = serverNameSize;
EXIT:
BSL_SAL_FREE(g_sessionId);
CleanRecordBody(&frameMsg);
HITLS_CFG_FreeConfig(testInfo.clientConfig);
HITLS_CFG_FreeConfig(testInfo.serverConfig);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
HITLS_SESS_Free(testInfo.clientSession);
FreeSNIArg(sniArg);
}
/* END_CASE */
void *ExampleServerNameArg1(void)
{
return sniArg;
}
int32_t ExampleServerNameCb1(HITLS_Ctx *ctx, int *alert, void *arg)
{
(void)arg;
(void)alert;
const char *server_servername = "huawei.com";
const char *client_servername = HITLS_GetServerName(ctx, HITLS_SNI_HOSTNAME_TYPE);
if (client_servername != NULL && server_servername != NULL) {
if (strcmp(client_servername, server_servername) == 0){
printf("\nHiTLS ServerNameCb return HITLS_ACCEPT_SNI_ERR_OK\n");
return HITLS_ACCEPT_SNI_ERR_OK;
}
else {
printf("\nHiTLS ServerNameCb return HITLS_ACCEPT_SNI_ERR_ALERT_FATAL\n");
return HITLS_ACCEPT_SNI_ERR_ALERT_FATAL;
}
} else{
if (client_servername == NULL)
{
printf("\nHiTLS Server get client_servername is NULL!\n");
} else if (server_servername == NULL){
printf("\nHiTLS Server get server_servername is NULL!\n");
}
}
printf("\nHiTLS ServerNameCb return HITLS_ACCEPT_SNI_ERR_NOACK\n");
return HITLS_ACCEPT_SNI_ERR_NOACK;
}
/* @
* @test UT_TLS_SNI_RESUME_SERVERNAME_FUNC_TC002
* @title The TLS13 session is resumed. The client hello message carries the SNI, and the SNI value is different from
that of the first connection setup.
* @precon nan
* @brief 1. For the first handshake, set serverName to huawei.com in the clientHello. Expected result 1
2. During session resumption, changed serverName of clientHello to www.huawei.com. Expected result 2
3. process the client hello. Expected result 2
* @expect 1. The serverName extension is set successfully and the handshake succeeds
2. return success
@ */
/* BEGIN_CASE */
void UT_TLS_SNI_RESUME_SERVERNAME_FUNC_TC002()
{
FRAME_Init();
HITLS_Config *clientconfig = HITLS_CFG_NewTLS13Config();
HITLS_Config *serverconfig = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(serverconfig != NULL);
ASSERT_TRUE(clientconfig != NULL);
HITLS_CFG_SetServerNameCb(serverconfig, ExampleServerNameCb1);
HITLS_CFG_SetServerNameArg(serverconfig, ExampleServerNameArg1);
HITLS_CFG_SetServerName(clientconfig, (uint8_t *)g_serverName, strlen(g_serverName));
FRAME_LinkObj *client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *Session = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(Session != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, Session), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetServerName(client->ssl, (uint8_t *)g_serverNameErr, strlen(g_serverNameErr)) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_MSG_HANDLE_SNI_UNRECOGNIZED_NAME);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(server->ssl, &alertInfo);
ASSERT_EQ(alertInfo.flag, ALERT_FLAG_SEND);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_UNRECOGNIZED_NAME);
EXIT:
HITLS_CFG_FreeConfig(clientconfig);
HITLS_CFG_FreeConfig(serverconfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(Session);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_servername_function.c | C | unknown | 15,487 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include "config.h"
#include "hitls.h"
#include "hitls_func.h"
#include "hitls_error.h"
/* END_HEADER */
static char *g_serverName = "www.example.com";
int32_t ServernameCbErrOK(HITLS_Ctx *ctx, int *alert, void *arg)
{
(void)ctx;
(void)alert;
(void)arg;
return HITLS_ACCEPT_SNI_ERR_OK;
}
#define HITLS_CFG_MAX_SIZE 1024
/** @
* @test UT_TLS_CFG_UPREF_FUNC_TC001
* @title test HITLS_CFG_SetServerName/HITLS_CFG_SetServerNameCb/HITLS_CFG_SetServerNameArg/HITLS_GetServernameType
* interface
*
* @brief 1. Apply for and initialize config.Expect result 1
2. Invoke the HITLS_CFG_NewTLS12Config interface and transfer the config parameter.Expect result 2.
3. Set serverNameStrlen HITLS_CFG_MAX_SIZE + 1;Invoke the HITLS_CFG_SetServerName interface Expect result 3.
4. Invoke the HITLS_CFG_NewTLS12Config interface.Expect result 4.
5. input parameters is NULL,Invoke the HITLS_CFG_NewTLS12Config interface and .Expect result 2.
6. Invoke the HITLS_CFG_SetServerNameCb interface ,Expect result 2.
7. Invoke the HITLS_CFG_SetServerNameArg interface ,Expect result 2.
8. Invoke the HITLS_CFG_GetServerNameCb interface ,Expect result 2.
9. Invoke the HITLS_CFG_GetServerNameArg interface ,Expect result 2.
10. Invoke the HITLS_SetServerName interface ,Expect result 2.
11. Invoke the HITLS_SetServerName interface ,Expect result 2.
12. Invoke the HITLS_SetServerName interface ,Expect result 6.
13. Invoke the HITLS_SetServerName interface ,Expect result 6.
14. Invoke the HITLS_GetServernameType interface ,Expect result 1.
* @expect 1. return Not NULL
2. return HITLS_NULL_INPUT
3. return HITLS_CONFIG_INVALID_LENGTH
4. return SUCCESS
5. return HITLS_NULL_INPUT
6. return NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_SERVERNAME_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ASSERT_TRUE(HITLS_CFG_SetServerName(NULL, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName)) ==
HITLS_NULL_INPUT);
uint32_t errLen = HITLS_CFG_MAX_SIZE + 1;
ASSERT_TRUE(HITLS_CFG_SetServerName(config, (uint8_t *)g_serverName, errLen) == HITLS_CONFIG_INVALID_LENGTH);
ASSERT_TRUE(HITLS_CFG_SetServerName(config, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName)) ==
HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetServerName(NULL, NULL, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetServerNameCb(NULL, ServernameCbErrOK) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetServerNameArg(NULL, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetServerNameCb(NULL, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetServerNameArg(NULL, NULL) == HITLS_NULL_INPUT);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetServerName(ctx, NULL, 0) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetServerName(NULL, (uint8_t *)g_serverName, 0) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetServerName(ctx, HITLS_SNI_BUTT) == NULL);
ASSERT_TRUE(HITLS_GetServerName(NULL, HITLS_SNI_HOSTNAME_TYPE) == NULL);
ASSERT_TRUE(HITLS_GetServernameType(ctx) != 0);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_servername_interface.c | C | unknown | 4,055 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "frame_tls.h"
#include "frame_link.h"
#include "session.h"
#include "hitls_config.h"
#include "hitls_crypt_init.h"
/* END_HEADER */
static int32_t ServernameCbErrOK(HITLS_Ctx *ctx, int *alert, void *arg)
{
(void)ctx;
(void)alert;
(void)arg;
return HITLS_ACCEPT_SNI_ERR_OK;
}
/** @
* @test UT_TLS12_RESUME_FUNC_TC001
* @title Test the session resume of tls12.
*
* @brief 1. at first handshake, config serverName, and sessionidCtx. Expect result 1
2. at second handshake, Expect result 2
* @expect 1. connect success
2. resume success
@ */
/* BEGIN_CASE */
void UT_TLS12_RESUME_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
HITLS_CFG_SetServerName(config, (uint8_t *)"www.test.com", (uint32_t)strlen((char *)"www.test.com"));
HITLS_CFG_SetServerNameCb(config, ServernameCbErrOK);
char *sessionIdCtx1 = "123456789";
ASSERT_EQ(HITLS_CFG_SetSessionIdCtx(config, (const uint8_t *)sessionIdCtx1, strlen(sessionIdCtx1)), HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
HITLS_Session *clientSession = HITLS_GetDupSession(client->ssl);
ASSERT_TRUE(clientSession != NULL);
FRAME_FreeLink(client);
client = NULL;
FRAME_FreeLink(server);
server = NULL;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetSession(client->ssl, clientSession), HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_EQ(HITLS_IsSessionReused(client->ssl, &isReused), HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
HITLS_SESS_Free(clientSession);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_session_ticket.c | C | unknown | 2,736 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "securec.h"
#include "hlt.h"
#include "hitls_error.h"
#include "hitls_func.h"
#include "conn_init.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "alert.h"
#include "stub_replace.h"
#include "hs_common.h"
#include "change_cipher_spec.h"
#include "hs.h"
#include "simulate_io.h"
#include "rec_header.h"
#include "rec_wrapper.h"
#include "record.h"
#include "app.c"
/* END_HEADER */
#define READ_BUF_SIZE 18432
#define MAX_DIGEST_SIZE 64UL /* The longest known is SHA512 */
uint32_t g_uiPort = 8890;
static uint32_t g_time = 0;
int32_t STUB_APP_Read(TLS_Ctx *ctx, uint8_t *buf, uint32_t num, uint32_t *readLen)
{
int32_t ret;
uint32_t readbytes;
g_time++;
if(g_time == 2) {
return HITLS_REC_ERR_IO_EXCEPTION;
}
if (ctx == NULL || buf == NULL || num == 0) {
return HITLS_APP_ERR_ZERO_READ_BUF_LEN;
}
// read data to the buffer in non-blocking mode
do {
ret = ReadAppData(ctx, buf, num, &readbytes);
if (ret != HITLS_SUCCESS) {
return ret;
}
} while (readbytes == 0); // do not exit the loop until data is read
*readLen = readbytes;
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC001
* @title UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC001
* @brief
* 1. Create connection. Expected result 1 is obtained.
* 2. Unset the auto retry mode, get keyupdate message. Expected result 2 is obtained.
* @expect
* 1. Successfully created connection.
* 2. After receive keyupdate message, the link will not try to read another app message
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, (uint8_t *)"Hello World", strlen("Hello World"), &len) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
ret = HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_NOT_REQUESTED);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_ClearModeSupport(server->ssl, HITLS_MODE_AUTO_RETRY) == HITLS_SUCCESS);
g_time = 0;
FuncStubInfo tmpRpInfo = {0};
STUB_Replace(&tmpRpInfo, APP_Read, STUB_APP_Read);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
g_time = 0;
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC002
* @title UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC002
* @brief
* 1. Create connection. Expected result 1 is obtained.
* 2. Unset the auto retry mode, Send Hello request. Expected result 2 is obtained.
* @expect
* 1. Successfully created connection.
* 2. After receive Hello request and send no_renegotiation alert, the link will not try to read another app message
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SSL_MODE_AUTO_RETRY_TC002()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, (uint8_t *)"Hello World", strlen("Hello World"), &len) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
ASSERT_EQ(HITLS_SetRenegotiationSupport(client->ssl, false), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Renegotiate(server->ssl), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_ClearModeSupport(client->ssl, HITLS_MODE_AUTO_RETRY) == HITLS_SUCCESS);
g_time = 0;
FuncStubInfo tmpRpInfo = {0};
STUB_Replace(&tmpRpInfo, APP_Read, STUB_APP_Read);
ASSERT_EQ(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
g_time = 0;
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC001
* @title UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC001
* @brief
* 1. Create connection. Expected result 1 is obtained.
* 2. Set moving buffer mode, when io busy, using different buffer retry. Expected result 2 is obtained.
* @expect
* 1. Successfully created connection.
* 2. Retry success.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetModeSupport(client->ssl, HITLS_MODE_ACCEPT_MOVING_WRITE_BUFFER) == HITLS_SUCCESS);
uint8_t data[] = "hello world";
uint8_t data2[] = "hello world";
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, data, sizeof(data), &len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Write(client->ssl, data, sizeof(data), &len) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Write(client->ssl, data2, sizeof(data2), &len), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC002
* @title UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC002
* @brief
* 1. Create connection. Expected result 1 is obtained.
* 2. Set moving buffer mode, when io busy, using shorter buffer retry. Expected result 2 is obtained.
* @expect
* 1. Successfully created connection.
* 2. Send alert. Before send alert, flush the out buffer first.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SSL_MODE_MOVE_BUFFER_TC002()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetModeSupport(client->ssl, HITLS_MODE_ACCEPT_MOVING_WRITE_BUFFER) == HITLS_SUCCESS);
uint8_t data[] = "hello world";
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, data, sizeof(data), &len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Write(client->ssl, data, sizeof(data), &len) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == sizeof(data));
ASSERT_TRUE(memcmp("hello world", readBuf, readLen) == 0);
ASSERT_EQ(HITLS_Write(client->ssl, data, 1, &len), HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(client->ssl->state, CM_STATE_ALERTING);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_EQ(server->ssl->state, CM_STATE_TRANSPORTING);
ASSERT_EQ(HITLS_Write(client->ssl, data, 1, &len), HITLS_CM_LINK_FATAL_ALERTED);
ASSERT_EQ(client->ssl->state, CM_STATE_ALERTED);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(server->ssl->state, CM_STATE_ALERTED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SSL_MODE_RELEASE_BUFFER_TC001
* @title UT_TLS_CM_SSL_MODE_RELEASE_BUFFER_TC001
* @brief
* 1. Set release buffer mode. Create connection. Expected result 1 is obtained.
* @expect
* 1. Successfully created connection.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SSL_MODE_RELEASE_BUFFER_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_SetModeSupport(client->ssl, HITLS_MODE_RELEASE_BUFFERS) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetModeSupport(server->ssl, HITLS_MODE_RELEASE_BUFFERS) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, (uint8_t *)"Hello World", strlen("Hello World"), &len) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen), HITLS_SUCCESS);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_ssl_mode.c | C | unknown | 13,843 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "alert.h"
#include "hitls_type.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "rec_wrapper.h"
#include "common_func.h"
#include "stub_crypt.h"
/* END_HEADER */
/* @
* @test SDV_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC001
* @title Test the behavior of the server when it receives the TLS_FALLBACK_SCSV algorithm suite carried by the lower version of clienthello.
* @brief 1. the client creates the config of tls12, and the server creates the config of tls13.Expect result 1.
* 2. the client sets HITLS_MODE_SEND_FALLBACK_SCSV.expect result 2.
* 3. connection establishment, Expect result 3.
* @expect 1. The config object is successfully created.
* 2. return HITLS_SUCCES.
* 3. Failed to establish connection, send alert ALERT_INAPPROPRIATE_FALLBACK.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC001(int isSetMode)
{
#ifdef HITLS_TLS_FEATURE_MODE
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, 18256, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS_ALL, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
if (isSetMode) {
HLT_SetModeSupport(clientCtxConfig, HITLS_MODE_SEND_FALLBACK_SCSV);
}
clientRes = HLT_ProcessTlsInit(localProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_TlsConnect(clientRes->ssl);
// Wait the remote.
int ret = HLT_GetTlsAcceptResult(serverRes);
if (isSetMode) {
ASSERT_EQ(ret, HITLS_MSG_HANDLE_ERR_INAPPROPRIATE_FALLBACK);
ALERT_Info alertInfo = { 0 };
ALERT_GetInfo(clientRes->ssl, &alertInfo);
ASSERT_EQ(alertInfo.flag, ALERT_FLAG_RECV);
ASSERT_EQ(alertInfo.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(alertInfo.description, ALERT_INAPPROPRIATE_FALLBACK);
} else {
ASSERT_EQ(ret, HITLS_SUCCESS);
}
EXIT:
HLT_FreeAllProcess();
#endif
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC001
* @title Test the behavior of the server when it receives the TLS_FALLBACK_SCSV algorithm suite carried by the
* lower version of clienthello.
* @brief 1. the client creates the config of tls12, and the server creates the config of tls13.Expect result 1.
* 2. the client sets HITLS_MODE_SEND_FALLBACK_SCSV.expect result 2.
* 3. connection establishment, Expect result 3.
* @expect 1. The config object is successfully created.
* 2. return HITLS_SUCCES.
* 3. Failed to establish connection, send alert ALERT_INAPPROPRIATE_FALLBACK.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC001(int isSetMode)
{
#ifdef HITLS_TLS_FEATURE_MODE
FRAME_Init();
HITLS_Config *c_config = NULL;
HITLS_Config *s_config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
c_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(c_config != NULL);
if (isSetMode) {
HITLS_CFG_SetModeSupport(c_config, HITLS_MODE_SEND_FALLBACK_SCSV);
}
s_config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(s_config != NULL);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
if (isSetMode) {
ASSERT_EQ(ret, HITLS_MSG_HANDLE_ERR_INAPPROPRIATE_FALLBACK);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_INAPPROPRIATE_FALLBACK);
} else {
ASSERT_EQ(ret, HITLS_SUCCESS);
}
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC002
* @title Test the behavior of the server when it disables tls13 and receives the TLS_FALLBACK_SCSV algorithm suite
* carried by the lower version of clienthello.
* @brief 1. the client creates the config of tls12, and the server creates the config of tlsall.Expect result 1.
* 2. the client sets HITLS_MODE_SEND_FALLBACK_SCSV. The server disables tls13. expect result 2.
* 3. connection establishment, Expect result 3.
* @expect 1. The config object is successfully created.
* 2. return HITLS_SUCCES.
* 3. return HITLS_SUCCES.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_TLS_FALLBACK_SCSV_TC002()
{
#ifdef HITLS_TLS_FEATURE_MODE
FRAME_Init();
HITLS_Config *c_config = NULL;
HITLS_Config *s_config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
c_config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(c_config != NULL);
HITLS_CFG_SetModeSupport(c_config, HITLS_MODE_SEND_FALLBACK_SCSV);
s_config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(s_config != NULL);
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(s_config, HITLS_VERSION_TLS13) == HITLS_SUCCESS);
client = FRAME_CreateLink(c_config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(s_config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
int32_t ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_EQ(ret, HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(c_config);
HITLS_CFG_FreeConfig(s_config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
#endif
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_frame_tls_fallback_scsv_rfc7507.c | C | unknown | 6,903 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
#include "rec_wrapper.h"
#include "hs_ctx.h"
#include "tls.h"
#include "hitls_config.h"
#include "alert.h"
#define READ_BUF_LEN_18K (18 * 1024)
/* END_HEADER */
static uint32_t g_uiPort = 16888;
static uint32_t retry_count = 0;
int32_t cert_callback(HITLS_Ctx *ctx, void *arg)
{
(void)ctx;
uint32_t *num = arg;
if (*num == 3) {
return HITLS_CERT_CALLBACK_FAILED;
}
if ((*num)++ == 0) {
return HITLS_CERT_CALLBACK_RETRY;
}
return HITLS_CERT_CALLBACK_SUCCESS;
}
/**
* @test SDV_TLS_CERT_CALLBACK_FUNC_TC01
* @title cert Callback Function Test Case 1
* @precon nan
* @brief Server sets the cert callback function, and the cert callback function return HITLS_CERT_CALLBACK_FAILED.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_CALLBACK_CERT_ERROR.
*/
/* BEGIN_CASE */
void SDV_TLS_CERT_CALLBACK_FUNC_TC01(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
int32_t flag = 3;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetCertCb(serverCtxConfig, cert_callback, &flag);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_CALLBACK_CERT_ERROR);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_TLS_CERT_CALLBACK_FUNC_TC02
* @title cert Callback Function Test Case 2
* @precon nan
* @brief Server sets the cert callback function, and the cert callback function return HITLS_CERT_CALLBACK_RETRY.
* The cert callback function is called twice, and the second time it returns HITLS_CERT_CALLBACK_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_TLS_CERT_CALLBACK_FUNC_TC02(int version)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetCertCb(serverCtxConfig, cert_callback, &retry_count);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_SUCCESS);
ASSERT_EQ(retry_count, 2);
EXIT:
HLT_FreeAllProcess();
retry_count = 0;
}
/* END_CASE */
/**
* @test SDV_TLS_CERT_CALLBACK_FUNC_TC03
* @title cert Callback Function Test Case 3
* @precon nan
* @brief Client sets the cert callback function, and the cert callback function return HITLS_CERT_CALLBACK_RETRY.
* Server set client verify support, The cert callback function is called twice,
* and the second time it returns HITLS_CERT_CALLBACK_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The link establishment is successful and cert callback function is called twice.
*/
/* BEGIN_CASE */
void SDV_TLS_CERT_CALLBACK_FUNC_TC03(int version)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetClientVerifySupport(serverCtxConfig, true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetCertCb(clientCtxConfig, cert_callback, &retry_count);
clientRes = HLT_ProcessTlsConnect(localProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(retry_count, 2);
EXIT:
HLT_FreeAllProcess();
retry_count = 0;
}
/* END_CASE */
/**
* @test SDV_TLS_CERT_CALLBACK_FUNC_TC04
* @title cert Callback Function Test Case 4
* @precon nan
* @brief Client sets the cert callback function, and the cert callback function return HITLS_CERT_CALLBACK_RETRY.
* Server set client verify support and post handshake auth, The cert callback function is called twice,
* and the second time it returns HITLS_CERT_CALLBACK_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The link establishment is successful and cert callback function is called twice.
*/
/* BEGIN_CASE */
void SDV_TLS_CERT_CALLBACK_FUNC_TC04(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetClientVerifySupport(serverCtxConfig, true);
HLT_SetPostHandshakeAuth(serverCtxConfig, true);
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetClientVerifySupport(clientCtxConfig, true);
HLT_SetPostHandshakeAuth(clientCtxConfig, true);
HLT_SetCertCb(clientCtxConfig, cert_callback, &retry_count);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(retry_count, 0);
ASSERT_TRUE(HLT_RpcTlsVerifyClientPostHandshake(remoteProcess, serverRes->sslId) == HITLS_SUCCESS);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen), 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
ASSERT_EQ(retry_count, 2);
EXIT:
HLT_FreeAllProcess();
retry_count = 0;
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_hlt_cert_cb.c | C | unknown | 8,648 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
#include "rec_wrapper.h"
#include "hs_ctx.h"
#include "tls.h"
#include "hitls_config.h"
#include "alert.h"
#define READ_BUF_LEN_18K (18 * 1024)
/* END_HEADER */
static uint32_t g_uiPort = 16888;
static uint32_t retry_count = 0;
int32_t client_hello_test_renegotiation_callback(HITLS_Ctx *ctx, int32_t *alert, void *arg)
{
(void)arg;
uint8_t verifyData[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataSize = 0;
HITLS_GetFinishVerifyData(ctx, verifyData, sizeof(verifyData), &verifyDataSize);
if (verifyDataSize != 0) {
*alert = ALERT_NO_RENEGOTIATION;
return HITLS_CLIENT_HELLO_FAILED;
}
HITLS_GetPeerFinishVerifyData(ctx, verifyData, sizeof(verifyData), &verifyDataSize);
if (verifyDataSize != 0) {
*alert = ALERT_NO_RENEGOTIATION;
return HITLS_CLIENT_HELLO_FAILED;
}
return HITLS_CLIENT_HELLO_SUCCESS;
}
int32_t client_hello_callback(HITLS_Ctx *ctx, int32_t *alert, void *arg)
{
(void)ctx;
uint32_t *num = arg;
*alert = ALERT_INTERNAL_ERROR;
if ((*num)++ == 0) {
return HITLS_CLIENT_HELLO_RETRY;
}
return HITLS_CLIENT_HELLO_SUCCESS;
}
int32_t full_client_hello_callback(HITLS_Ctx *ctx, int32_t *alert, void *arg)
{
uint16_t *cipher;
uint16_t cipherLen;
uint16_t *exts;
uint8_t extLen;
uint8_t *random;
uint8_t randomLen;
uint8_t *extBuff;
uint32_t extBuffLen;
uint32_t *num = arg;
const uint16_t expected_ciphers[] = {0xc02c, 0x00ff};
const uint16_t expected_extensions[] = {13, 10, 11, 23, 22};
*alert = ALERT_INTERNAL_ERROR;
if (*num == 0) {
return HITLS_CLIENT_HELLO_RETRY;
}
if (*num == 1) {
return HITLS_CLIENT_HELLO_FAILED;
}
/* Make sure we can defer processing and get called back. */
ASSERT_TRUE(HITLS_ClientHelloGetRandom(ctx, &random, &randomLen) == HITLS_SUCCESS);
ASSERT_TRUE(random != NULL);
ASSERT_TRUE(randomLen == 32);
ASSERT_TRUE(HITLS_ClientHelloGetCiphers(ctx, &cipher, &cipherLen) == HITLS_SUCCESS);
ASSERT_TRUE(cipher != NULL);
ASSERT_TRUE(cipherLen != 0);
// Compare expected_ciphers and cipher
ASSERT_TRUE(cipherLen == sizeof(expected_ciphers) / sizeof(expected_ciphers[0]));
ASSERT_EQ(memcmp(cipher, expected_ciphers, cipherLen * sizeof(uint16_t)), 0);
ASSERT_TRUE(HITLS_ClientHelloGetExtensionsPresent(ctx, &exts, &extLen) == HITLS_SUCCESS);
ASSERT_TRUE(exts != NULL);
ASSERT_TRUE(extLen != 0);
// Compare expected_extensions and exts
ASSERT_TRUE(extLen == sizeof(expected_extensions) / sizeof(expected_extensions[0]));
for (uint16_t i = 0; i < extLen; ++i) {
ASSERT_TRUE(exts[i] == expected_extensions[i]);
}
BSL_SAL_FREE(exts);
ASSERT_TRUE(HITLS_ClientHelloGetExtension(ctx, 13, &extBuff, &extBuffLen) == HITLS_SUCCESS);
ASSERT_TRUE(extBuff != NULL);
ASSERT_TRUE(extBuffLen != 0);
return HITLS_CLIENT_HELLO_SUCCESS;
EXIT:
return HITLS_CLIENT_HELLO_FAILED;
}
/**
* @test SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC01
* @title Client Hello Callback Function Test Case 1
* @precon nan
* @brief Server sets the client hello callback function, and the client hello callback function
* return HITLS_CLIENT_HELLO_FAILED.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_CALLBACK_CLIENT_HELLO_ERROR.
*/
/* BEGIN_CASE */
void SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC01(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
int32_t flag = 1;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
;
HLT_SetClientHelloCb(serverCtxConfig, full_client_hello_callback, &flag);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes == NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_CALLBACK_CLIENT_HELLO_ERROR);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC02
* @title Client Hello Callback Function Test Case 2
* @precon nan
* @brief Server sets the client hello callback function, and the client hello callback function
* return HITLS_CLIENT_HELLO_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC02(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
int32_t flag = 2;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetClientHelloCb(serverCtxConfig, full_client_hello_callback, &flag);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384");
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_SUCCESS);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC03
* @title Client Hello Callback Function Test Case 3
* @precon nan
* @brief Server sets the client hello callback function, and the client hello callback function
* return HITLS_CLIENT_HELLO_RETRY. The client hello callback function is called twice,
* and the second time it returns HITLS_CLIENT_HELLO_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_SUCCESS.
*/
/* BEGIN_CASE */
void SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC03(int version)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
;
HLT_SetClientHelloCb(serverCtxConfig, client_hello_callback, &retry_count);
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_SUCCESS);
ASSERT_EQ(retry_count, 2);
EXIT:
HLT_FreeAllProcess();
retry_count = 0;
}
/* END_CASE */
/**
* @test SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC04
* @title Client Hello Callback Function Test Case 4
* @precon nan
* @brief Server sets the client hello callback function, and the client hello callback function
* return HITLS_CLIENT_HELLO_SUCCESS.
* On renegotiation, the client hello callback returns HITLS_CLIENT_HELLO_FAILED.
* The server supports renegotiation, and the client does not support renegotiation.
* establish a TLS connection between the client and server, expect result 1.
* server starts renegotiation, and the client hello callback function returns HITLS_CLIENT_HELLO_FAILED.
* expect result 2.
* @expect 1. The link establishment is successful.
* 2. The server returns ALERT_NO_RENEGOTIATION.
*/
/* BEGIN_CASE */
void SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC04(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
;
HLT_SetClientHelloCb(serverCtxConfig, client_hello_test_renegotiation_callback, &retry_count);
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetRenegotiationSupport(serverCtxConfig, true);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetRenegotiationSupport(clientCtxConfig, true);
HLT_SetLegacyRenegotiateSupport(clientCtxConfig, true);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
ASSERT_EQ(HITLS_Renegotiate(serverRes->ssl), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(serverRes->ssl), HITLS_SUCCESS);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen),
HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(HLT_ProcessTlsRead(localProcess, serverRes, readBuf, READ_BUF_LEN_18K, &readLen),
HITLS_CALLBACK_CLIENT_HELLO_ERROR);
ALERT_Info info = {0};
ALERT_GetInfo(serverRes->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_NO_RENEGOTIATION);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC05
* @title Client Hello Callback Function Test Case 5
* @precon nan
* @brief Server sets the client hello callback function, and the client hello callback function return
* HITLS_CLIENT_HELLO_RETRY.
* The client hello callback function is called three times, and the third time it returns
* HITLS_CLIENT_HELLO_SUCCESS.
* establish a TLS connection between the client and server, expect result 1.
* @expect 1. The server returns HITLS_SUCCESS.
* 2. The client hello callback function is called three times.
* 3. The retry_count is 3.
*/
/* BEGIN_CASE */
void SDV_TLS_CLIENT_HELLO_CALLBACK_FUNC_TC05(void)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_SetClientHelloCb(serverCtxConfig, client_hello_callback, &retry_count);
HLT_SetGroups(serverCtxConfig, "HITLS_EC_GROUP_SECP256R1");
ASSERT_TRUE(serverCtxConfig != NULL);
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_CURVE25519:HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
ASSERT_TRUE(clientCtxConfig != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResult(serverRes), HITLS_SUCCESS);
ASSERT_EQ(retry_count, 3);
EXIT:
HLT_FreeAllProcess();
retry_count = 0;
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_hlt_clienthello_cb.c | C | unknown | 13,698 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
#include "rec_wrapper.h"
#include "hs_ctx.h"
#include "tls.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10087
static void Test_MultiKeyShare(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)bufSize;
(void)user;
(void)data;
(void)*len;
if (ctx->hsCtx->haveHrr) {
*(bool *)user = true;
} else {
*(bool *)user = false;
}
return;
}
/* BEGIN_CASE */
void SDV_TLS13_MULTI_KEYSHARE_TC001()
{
bool haveHrr = false;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
bool testHrr = true;
RecWrapper wrapper = {
TRY_SEND_FINISH,
REC_TYPE_HANDSHAKE,
false,
&testHrr,
Test_MultiKeyShare
};
RegisterWrapper(wrapper);
HLT_SetGroups(serverCtxConfig, "HITLS_EC_GROUP_SECP256R1");
HLT_SetGroups(clientCtxConfig, "X25519MLKEM768:HITLS_EC_GROUP_SECP256R1");
HLT_Tls_Res *serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Tls_Res *clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_EQ(testHrr, haveHrr);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
ClearWrapper();
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_hlt_multi_keyshare.c | C | unknown | 3,027 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "helper.h"
#include "hitls.h"
#include "frame_tls.h"
#include "hitls_type.h"
/* END_HEADER */
#define READ_BUF_LEN_18K (18 * 1024)
#define PORT 10087
/* BEGIN_CASE */
void SDV_TLS13_PROVIDER_NEW_GROUP_SIGNALG_TC001(char *path, char *providerName, int providerLibFmt, char *group,
char *signAlg, char *rootCa, char *interCa, char *serverCert, char *serverKey, char *clientCert, char *clientKey)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)path;
(void)providerName;
(void)providerLibFmt;
(void)group;
(void)signAlg;
(void)rootCa;
(void)interCa;
(void)serverCert;
(void)serverKey;
(void)clientCert;
(void)clientKey;
SKIP_TEST();
#else
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetProviderPath(serverCtxConfig, path);
HLT_SetProviderAttrName(serverCtxConfig, NULL);
HLT_SetProviderPath(clientCtxConfig, path);
HLT_SetProviderAttrName(clientCtxConfig, NULL);
HLT_AddProviderInfo(serverCtxConfig, providerName, providerLibFmt);
HLT_AddProviderInfo(serverCtxConfig, "default", BSL_SAL_LIB_FMT_OFF);
HLT_AddProviderInfo(clientCtxConfig, providerName, providerLibFmt);
HLT_AddProviderInfo(clientCtxConfig, "default", BSL_SAL_LIB_FMT_OFF);
/* Set Cert */
HLT_SetCertPath(serverCtxConfig, rootCa, interCa, serverCert, serverKey, "NULL", "NULL");
HLT_SetCertPath(clientCtxConfig, rootCa, interCa, clientCert, clientKey, "NULL", "NULL");
HLT_SetGroups(serverCtxConfig, group); // For kex or kem group
HLT_SetGroups(clientCtxConfig, group); // For kex or kem group
HLT_SetSignature(serverCtxConfig, signAlg);
HLT_SetSignature(clientCtxConfig, signAlg);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_AES_128_GCM_SHA256");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS13_PROVIDER_KEM_TC001(char *group)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)group;
SKIP_TEST();
#else
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetGroups(clientCtxConfig, group); // For kex or kem group
HLT_SetGroups(serverCtxConfig, group); // For kex or kem group
serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(localProcess, TLS1_3, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(remoteProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(localProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
#endif
}
/* END_CASE */
/* BEGIN_CASE */
void SDV_TLS13_MULTI_PROVIDER_TC001(char *path, char *providerName, char *attrName, int providerLibFmt)
{
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)path;
(void)providerName;
(void)attrName;
(void)providerLibFmt;
SKIP_TEST();
#else
(void)path;
(void)providerName;
(void)attrName;
(void)providerLibFmt;
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Ctx_Config *serverCtxConfig = NULL;
HLT_Ctx_Config *clientCtxConfig = NULL;
HLT_Process *localProcess = HLT_InitLocalProcess(HITLS);
HLT_Process *remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, true);
ASSERT_TRUE(localProcess != NULL);
ASSERT_TRUE(remoteProcess != NULL);
serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetProviderPath(serverCtxConfig, path);
HLT_SetProviderAttrName(serverCtxConfig, attrName);
HLT_AddProviderInfo(serverCtxConfig, providerName, providerLibFmt);
HLT_AddProviderInfo(serverCtxConfig, "default", BSL_SAL_LIB_FMT_OFF);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_LEN_18K] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, READ_BUF_LEN_18K, &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_FreeAllProcess();
#endif
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_hlt_provider.c | C | unknown | 7,616 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "hlt.h"
#include "securec.h"
#include "process.h"
#include "session.h"
#include "hitls_config.h"
#include "hitls_crypt_init.h"
/* END_HEADER */
#define READ_BUF_SIZE (18 * 1024)
/** @
* @test SDV_TLS12_RESUME_FUNC_TC001
* @title Test the PSK-based session resume of tls12.
*
* @brief 1. at first handshake, config the client does not support tickets,
but the server supports tickets. Expect result 1
2. after first handshake, config the client support tickets, Expect result 2
* @expect 1. connect success
2. resume success
@ */
/* BEGIN_CASE */
void SDV_TLS12_RESUME_FUNC_TC001()
{
int version = TLS1_2;
int connType = TCP;
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
HITLS_Session *session = NULL;
int32_t cnt = 0;
int32_t serverConfigId = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = true;
HLT_SetPsk(clientCtxConfig, "123456789");
HLT_SetPsk(serverCtxConfig, "123456789");
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
do {
DataChannelParam channelParam;
channelParam.port = 18889;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
if (cnt > 0) {
clientCtxConfig->isSupportSessionTicket = true;
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
}
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
if (cnt == 0) {
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
} else {
int ret = HLT_TlsConnect(clientSsl);
ASSERT_EQ(ret, HITLS_SUCCESS);
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_EQ(isReused, 1);
}
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_RpcTlsClose(remoteProcess, serverSslId);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
HITLS_SESS_Free(session);
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */
/* @
* @test SDV_HITLS_TICKET_KEY_CALLBACK_RESUME_FUNC_TC001
* @spec -
* @title Verifying the session resumption scenario after the session ticket key callback function is configured
* @precon nan
* @brief
1. The bottom-layer connection is established.
2. Register the session ticket key callback on the server.
3. After the first handshake is complete, obtain and store the session.
4. Configure the session in the SSL, resume the session, and obtain the session again.
5. Repeat step 4. Expected result 5 is obtained.
* @expect
1. Return success
2. The registration is successful.
3. The handshake has completed and the session is obtained successfully.
4. The session is successfully configured, resumed, and obtained.
5. The session is successfully configured, resumed, and obtained.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void SDV_HITLS_TICKET_KEY_CALLBACK_RESUME_FUNC_TC001(int version, int connType, char *ticketKeyCb)
{
Process *localProcess = NULL;
Process *remoteProcess = NULL;
HLT_FD sockFd = {0};
int32_t serverConfigId = 0;
HITLS_Session *session = NULL;
const char *writeBuf = "Hello world";
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
int32_t cnt = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_CreateRemoteProcess(HITLS);
ASSERT_TRUE(remoteProcess != NULL);
void *clientConfig = HLT_TlsNewCtx(version);
ASSERT_TRUE(clientConfig != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
clientCtxConfig->isSupportSessionTicket = true;
clientCtxConfig->isSupportRenegotiation = false;
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
serverCtxConfig->isSupportSessionTicket = true;
serverCtxConfig->isSupportRenegotiation = false;
#ifdef HITLS_TLS_FEATURE_PROVIDER
serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, version, false, NULL, NULL, NULL, 0, NULL);
#else
serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, version, false);
#endif
memcpy_s(clientCtxConfig->psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
memcpy_s(serverCtxConfig->psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
// Register the session ticket key callback on the server.
HLT_SetTicketKeyCb(serverCtxConfig, ticketKeyCb);
ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
HLT_SetCipherSuites(clientCtxConfig, "HITLS_ECDHE_PSK_WITH_AES_128_CBC_SHA");
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_PSK_WITH_AES_128_CBC_SHA");
do {
DataChannelParam channelParam;
channelParam.port = 18899;
channelParam.type = connType;
channelParam.isBlock = true;
sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
remoteProcess->connFd = sockFd.peerFd;
localProcess->connFd = sockFd.srcFd;
remoteProcess->connType = connType;
localProcess->connType = connType;
int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
HLT_Ssl_Config *serverSslConfig;
serverSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(serverSslConfig != NULL);
serverSslConfig->sockFd = remoteProcess->connFd;
serverSslConfig->connType = connType;
ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
HLT_RpcTlsAccept(remoteProcess, serverSslId);
void *clientSsl = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientSsl != NULL);
HLT_Ssl_Config *clientSslConfig;
clientSslConfig = HLT_NewSslConfig(NULL);
ASSERT_TRUE(clientSslConfig != NULL);
clientSslConfig->sockFd = localProcess->connFd;
clientSslConfig->connType = connType;
// The bottom-layer connection is established.
HLT_TlsSetSsl(clientSsl, clientSslConfig);
if (session != NULL) {
// Configure the session in the SSL, resume the session, and obtain the session again.
ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
}
ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
ASSERT_TRUE(readLen == strlen(writeBuf));
ASSERT_TRUE(memcmp(writeBuf, readBuf, strlen(writeBuf)) == 0);
ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
HLT_CloseFd(sockFd.srcFd, localProcess->connType);
if (cnt != 0) {
if (strcmp(ticketKeyCb, "ExampleTicketKeyRenewCb") == 0) {
SESS_Disable(session);
}
HITLS_SESS_Free(session);
session = NULL;
uint8_t isReused = 0;
ASSERT_TRUE(HITLS_IsSessionReused(clientSsl, &isReused) == HITLS_SUCCESS);
ASSERT_TRUE(isReused == 1);
}
// After the first handshake is complete, obtain and store the session.
session = HITLS_GetDupSession(clientSsl);
ASSERT_TRUE(session != NULL);
ASSERT_TRUE(HITLS_SESS_HasTicket(session) == true);
ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
cnt++;
} while (cnt < 3);
EXIT:
HITLS_SESS_Free(session);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/feature/test_suite_sdv_hlt_session_ticket.c | C | unknown | 10,854 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include "hitls_session.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "hitls_crypt_reg.h"
#include "crypt_errno.h"
#include "bsl_list.h"
#include "hitls_cert.h"
#include "parse_extensions_client.c"
#include "parse_extensions_server.c"
#include "parse_server_hello.c"
#include "parse_client_hello.c"
/* END_HEADER */
static char *g_serverName = "testServer";
uint32_t g_uiPort = 18888;
#define DEFAULT_DESCRIPTION_LEN 128
#define TLS_DHE_PARAM_MAX_LEN 1024
#define GET_GROUPS_CNT (-1)
#define READ_BUF_SIZE (18 * 1024)
#define ALERT_BODY_LEN 2u
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
HITLS_TicketKeyCb serverKeyCb;
} ResumeTestInfo;
int32_t HITLS_RemoveCertAndKey(HITLS_Ctx *ctx);
HITLS_CRYPT_Key *cert_key = NULL;
HITLS_CRYPT_Key *DH_CB(HITLS_Ctx *ctx, int32_t isExport, uint32_t keyLen)
{
(void)ctx;
(void)isExport;
(void)keyLen;
return cert_key;
}
void *STUB_SAL_Calloc(uint32_t num, uint32_t size)
{
(void)num;
(void)size;
return NULL;
}
void *STUB_SAL_Dump(const void *src, uint32_t size)
{
(void)src;
(void)size;
return NULL;
}
FuncStubInfo g_TmpRpInfo = {0};
int32_t STUB_BSL_UIO_Read(BSL_UIO *uio, void *data, uint32_t len, uint32_t *readLen)
{
(void)uio;
(void)data;
(void)len;
(void)readLen;
return 0;
}
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
switch (ver) {
case HITLS_VERSION_TLS12:
return HITLS_CFG_NewTLS12Config();
case HITLS_VERSION_TLS13:
return HITLS_CFG_NewTLS13Config();
case HITLS_VERSION_DTLS12:
return HITLS_CFG_NewDTLS12Config();
default:
return NULL;
}
}
/** @
* @test UT_TLS_CM_IS_DTLS_API_TC001
* @title Test HITLS_IsDtls
* @precon nan
* @brief HITLS_IsDtls
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer the non-empty TLS connection handle information and leave isDtls blank. Expected result 1.
* 3. Transfer the non-empty TLS connection handle information. The isDtls parameter is not empty. Expected result 2 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_DTLS_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t isDtls = 0;
ASSERT_TRUE(HITLS_IsHandShakeDone(ctx, &isDtls) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_IsHandShakeDone(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_IsHandShakeDone(ctx, &isDtls) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_CLEAR_CIPHERSUITES_API_TC001
* @title Test the HITLS_SetCipherSuites and HITLS_ClearTLS13CipherSuites interfaces.
* @precon nan
* @brief HITLS_SetCipherSuites
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer non-empty TLS connection handle information and leave cipherSuites empty. Expected result 1.
* 3. Transfer the non-empty TLS connection handle information. If cipherSuites is not empty and cipherSuitesSize is 0,
* the expected result is 1.
* 4. Transfer the non-empty TLS connection handle information. Set cipherSuites to a value greater than
* HITLS_CFG_MAX_SIZE. Expected result 2.
* 5. The input parameters are valid, and the SAL_CALLOC table is instrumented. Expected result 3.
* 6. Transfer the non-null TLS connection handle information, set cipherSuites to an invalid value, and set
* cipherSuitesSize to a value smaller than HITLS_CFG_MAX_SIZE. Expected result 4 is displayed.
* 7. Transfer valid parameters. Expected result 5.
* HITLS_ClearTLS13CipherSuites
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer the non-empty TLS connection handle information. Expected result 5.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. Return HITLS_HITLS_CM_INVALID_LENGTH
* 3. Returns HITLS_MEMALLOC_FAIL
* 4. Return HITLS_HITLS_CM_NO_SUITABLE_CIPHER_SUITE
* 5. Returns HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_CLEAR_CIPHERSUITES_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint16_t cipherSuites[10] = {
HITLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
HITLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
HITLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
HITLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
};
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_ClearTLS13CipherSuites(ctx) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, NULL, 0) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, cipherSuites, 0) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, cipherSuites, HITLS_CFG_MAX_SIZE + 1) == HITLS_CONFIG_INVALID_LENGTH);
STUB_Init();
FuncStubInfo tmpRpInfo;
STUB_Replace(&tmpRpInfo, BSL_SAL_Calloc, STUB_SAL_Calloc);
ASSERT_TRUE(
HITLS_SetCipherSuites(ctx, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_MEMALLOC_FAIL);
STUB_Reset(&tmpRpInfo);
uint16_t cipherSuites2[10] = {0};
cipherSuites2[0] = 0xFFFF;
cipherSuites2[1] = 0xEFFF;
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, cipherSuites2, sizeof(cipherSuites2) / sizeof(uint16_t)) ==
HITLS_CONFIG_NO_SUITABLE_CIPHER_SUITE);
ASSERT_TRUE(HITLS_SetCipherSuites(ctx, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
if (tlsVersion == HITLS_VERSION_TLS13) {
ASSERT_TRUE(HITLS_ClearTLS13CipherSuites(ctx) == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.tls13cipherSuitesSize == 0);
}
EXIT:
STUB_Reset(&tmpRpInfo);
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_GET_ENCRYPTHENMAC_FUNC_TC001
* @title HITLS_GetEncryptThenMac and HITLS_SetEncryptThenMac interface validation
* @precon nan
* @brief
* 1. After initialization, call the hitls_setencryptthenmac interface to set the value to true and call the
* HITLS_GetEncryptThenMac interface to query the value. Expected result 1.
* 2. Set hitls_setencryptthenmac to true at both ends. After the connection is set up, invoke the HITLS_GetEncryptThenMac
* interface to query the connection. Expected result 2.
* @expect
* 1. The return value is true.
* 2. The return value is true.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_ENCRYPTHENMAC_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
uint32_t encryptThenMacType = 0;
ASSERT_EQ(HITLS_GetEncryptThenMac(server->ssl, &encryptThenMacType), HITLS_SUCCESS);
ASSERT_EQ(encryptThenMacType, 1);
ASSERT_EQ(HITLS_GetEncryptThenMac(client->ssl, &encryptThenMacType), HITLS_SUCCESS);
ASSERT_EQ(encryptThenMacType, 1);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_GetEncryptThenMac(server->ssl, &encryptThenMacType), HITLS_SUCCESS);
ASSERT_EQ(encryptThenMacType, 1);
ASSERT_EQ(HITLS_GetEncryptThenMac(client->ssl, &encryptThenMacType), HITLS_SUCCESS);
ASSERT_EQ(encryptThenMacType, 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_SERVERNAME_FUNC_TC001
* @title HITLS_SetServerName invokes the interface to set the server name.
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. After the initialization, set the servername and run the HITLS_GetServerName command to check the server name.
* Expected result 2 is displayed
* @expect
* 1. Complete initialization
* 2. The returned result is consistent with the settings
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_SERVERNAME_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetServerName(client->ssl, (uint8_t *)g_serverName, (uint32_t)strlen(g_serverName)),
HITLS_SUCCESS);
client->ssl->isClient = true;
const char *server_name = HITLS_GetServerName(client->ssl, HITLS_SNI_HOSTNAME_TYPE);
ASSERT_TRUE(memcmp(server_name, (uint8_t *)g_serverName, strlen(g_serverName)) == 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SET_GET_SESSION_TICKET_SUPPORT_API_TC001
* @title Test the HITLS_SetSessionTicketSupport and HITLS_GetSessionTicketSupport interfaces.
* @precon nan
* @brief HITLS_SetSessionTicketSupport
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set isEnable to an invalid value. Expected result 2.
* 3. Transfer the non-empty TLS connection handle information and set isEnable to a valid value. Expected result 3 is
* obtained.
* HITLS_GetSessionTicketSupport
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Pass an empty getIsSupport pointer. Expected result 1.
* 3. Transfer the non-null TLS connection handle information and ensure that the getIsSupport pointer is not null.
* Expected result 3.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and ctx->config.tlsConfig.isSupportSessionTicket is true.
* 3. Returns HITLS_SUCCES and ctx->config.tlsConfig.isSupportSessionTicket is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_SESSION_TICKET_SUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t isSupport = -1;
uint8_t getIsSupport = -1;
ASSERT_TRUE(HITLS_SetSessionTicketSupport(ctx, isSupport) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetSessionTicketSupport(ctx, &getIsSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetSessionTicketSupport(ctx, NULL) == HITLS_NULL_INPUT);
isSupport = 1;
ASSERT_TRUE(HITLS_SetSessionTicketSupport(ctx, isSupport) == HITLS_SUCCESS);
isSupport = -1;
ASSERT_TRUE(HITLS_SetSessionTicketSupport(ctx, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.isSupportSessionTicket = true);
isSupport = 0;
ASSERT_TRUE(HITLS_SetSessionTicketSupport(ctx, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetSessionTicketSupport(ctx, &getIsSupport) == HITLS_SUCCESS);
ASSERT_TRUE(getIsSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_VERIFY_CLIENT_POST_HANDSHAKE_API_TC001
* @title Invoke the HITLS_VerifyClientPostHandshake interface during connection establishment.
* @precon nan
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1.
* 2. Configure the client and server to support post-handshake extension. Expected result 3.
* 3. When a connection is established, the server is in the Try_RECV_CLIENT_HELLO state, and the
* HITLS_VerifyClientPostHandshake interface is invoked.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. The interface fails to be invoked.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_VERIFY_CLIENT_POST_HANDSHAKE_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
// Apply for and initialize the configuration file
config = HITLS_CFG_NewTLS13Config();
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
// Configure the client and server to support post-handshake extension
client->ssl->config.tlsConfig.isSupportPostHandshakeAuth = true;
server->ssl->config.tlsConfig.isSupportPostHandshakeAuth = true;
ASSERT_TRUE(client->ssl->config.tlsConfig.isSupportPostHandshakeAuth == true);
ASSERT_TRUE(server->ssl->config.tlsConfig.isSupportPostHandshakeAuth == true);
// he server is in the Try_RECV_CLIENT_HELLO state
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(server->ssl->hsCtx->state == TRY_RECV_CLIENT_HELLO);
// the HITLS_VerifyClientPostHandshake interface is invoked
ASSERT_EQ(HITLS_VerifyClientPostHandshake(client->ssl), HITLS_INVALID_INPUT);
ASSERT_EQ(HITLS_VerifyClientPostHandshake(server->ssl), HITLS_MSG_HANDLE_STATE_ILLEGAL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_REMOVE_CERTANDKEY_API_TC001
* @title Test the HITLS_RemoveCertAndKey interface.
* @brief
* 1. Apply for and initialize the configuration file. Expected result 1.
* 2. Invoke the client HITLS_CFG_SetClientVerifySupport and HITLS_CFG_SetNoClientCertSupport. Expected result 2.
* 3. Invoke the HITLS_RemoveCertAndKey, Expected result 3.
* @expect
* 1. The initialization is successful.
* 2. The setting is successful.
* 3. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_REMOVE_CERTANDKEY_API_TC001(void)
{
FRAME_Init();
int32_t ret;
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
ret = HITLS_CFG_SetClientVerifySupport(config, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_CFG_SetNoClientCertSupport(config, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(server != NULL);
ret = HITLS_RemoveCertAndKey(client->ssl);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = FRAME_CreateConnection(client, server, false, HS_STATE_BUTT);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(server->ssl->state == CM_STATE_TRANSPORTING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
static int32_t TestHITLS_PasswordCb(char *buf, int32_t bufLen, int32_t flag, void *userdata)
{
(void)buf;
(void)bufLen;
(void)flag;
(void)userdata;
return 0;
}
/* @
* @test UT_TLS_CM_SET_GET_DEFAULT_API_TC001
* @title Test HITLS_SetDefaultPasswordCb/HITLS_GetDefaultPasswordCb interface
* @brief 1. Invoke the HITLS_SetDefaultPasswordCb interface. Expected result 1.
* 2. Invoke the HITLS_SetDefaultPasswordCb interface. The value of ctx is not empty and the value of password is
* not empty. Expected result 3.
* 3. Invoke the HITLS_GetDefaultPasswordCb interface and leave ctx blank. Expected result 2.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. NULL is returned.
* 3. HITLS_SUCCESS is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_DEFAULT_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetDefaultPasswordCb(NULL, TestHITLS_PasswordCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetDefaultPasswordCb(ctx, TestHITLS_PasswordCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetDefaultPasswordCb(NULL) == NULL);
ASSERT_TRUE(HITLS_GetDefaultPasswordCb(ctx) == TestHITLS_PasswordCb);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_SESSION_API_TC001
* @title Test HITLS_SetSession/HITLS_GetSession interface
* @brief 1. If ctx is NULL, Invoke the HITLS_SetSession interface.Expected result 1.
* 2. Invoke the HITLS_SetSession interface.Expected result 2.
* 3. Invoke the HITLS_GetSession interface. Expected result 2.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. returnes HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_SESSION_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetSession(NULL, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetSession(ctx, NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetSession(ctx) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_PEERSIGNATURE_TYPE_API_TC001
* @title Test HITLS_GetPeerSignatureType interface
* @brief 1. If ctx is NULL, Invoke the HITLS_GetPeerSignatureType interface. Expected result 2.
* 2. Invoke the HITLS_GetPeerSignatureType interface. Expected result 1.
* @expect 1. Returns HITLS_CONFIG_NO_SUITABLE_CIPHER_SUITE
* 2.Returns HITLS_NULL_INPUT
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_PEERSIGNATURE_TYPE_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
HITLS_SignAlgo sigType = {0};
ASSERT_EQ(HITLS_GetPeerSignatureType(NULL, NULL), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_GetPeerSignatureType(ctx, &sigType), HITLS_CONFIG_NO_SUITABLE_CIPHER_SUITE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
static void Test_Fatal_Alert(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
uint32_t bufSize, void *user)
{
(void)bufSize;
(void)user;
(void)len;
(void)data;
uint8_t alertdata[2] = {0x02, 0x29};
REC_Write(ctx, REC_TYPE_ALERT, alertdata, 2);
return;
}
/** @
* @test UT_TLS_CM_FATAL_ALERT_TC001
* @title recv fatal alert brefore client hello need to close connection
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
* 2. After the initialization, send a fetal alert to server, expect reslut 2.
* @expect 1. The initialization is successful.
* 2. The client close the connection
@ */
/* BEGIN_CASE */
void UT_TLS_CM_FATAL_ALERT_TC001(int version)
{
RecWrapper wrapper = {
TRY_SEND_CLIENT_HELLO,
REC_TYPE_HANDSHAKE,
false,
NULL,
Test_Fatal_Alert
};
RegisterWrapper(wrapper);
FRAME_Init();
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
/* Link initialization */
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(client->ssl->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_EQ(server->ssl->state, CM_STATE_ALERTED);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
/* Alert recv means the handshake state is in alerting state and no alert to be sent*/
ASSERT_EQ(info.flag, ALERT_FLAG_RECV);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_NO_CERTIFICATE_RESERVED);
EXIT:
ClearWrapper();
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
return;
}
/* END_CASE */
/* @
* @test UT_TLS_GET_GLOBALCONFIG_TC001
* @spec -
* @title test for HITLS_GetGlobalConfig
* @precon nan
* @brief HITLS_GetGlobalConfig
* 1. Transfer an empty TLS connection handle. Expected result 1 is obtained
* 2. Transfer non-empty TLS connection handle information. Expected result 2 is obtained
* @expect 1. return NULL
* 2. return globalConfig of TLS context
@ */
/* BEGIN_CASE */
void UT_TLS_GET_GLOBALCONFIG_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
ASSERT_TRUE(HITLS_GetGlobalConfig(ctx) == NULL);
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetGlobalConfig(ctx) != NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_HITLS_PEEK_TC001
* @brief 1. Establish connection between server and client
2. client sends a byte
3. server calls HITLS_Peek twice
4. server calls HITLS_Read to read one byte to make IO empty
5. server calls HITLS_Peek
* @expect 1. Return HITLS_SUCCESS
2. Return HITLS_SUCCESS
3. Return HITLS_SUCCESS
4. Return HITLS_SUCCESS
5. Return HITLS_REC_NORMAL_RECV_BUF_EMPTY
@ */
/* BEGIN_CASE */
void UT_TLS_HITLS_PEEK_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t c2s[] = {0};
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, c2s, sizeof(c2s), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t peekBuf[8] = {0};
uint8_t peekBuf1[8] = {0};
uint8_t peekBuf2[8] = {0};
uint8_t readBuf[8] = {0};
uint32_t peekLen = 0;
uint32_t peekLen1 = 0;
uint32_t peekLen2 = 0;
uint32_t readLen = 0;
ASSERT_EQ(HITLS_Peek(server->ssl, peekBuf, sizeof(peekBuf), &peekLen), HITLS_SUCCESS);
ASSERT_EQ(peekLen, sizeof(c2s));
ASSERT_EQ(memcmp(peekBuf, c2s, peekLen), 0);
ASSERT_EQ(HITLS_Peek(server->ssl, peekBuf1, sizeof(peekBuf1), &peekLen1), HITLS_SUCCESS);
ASSERT_EQ(peekLen1, sizeof(c2s));
ASSERT_EQ(memcmp(peekBuf1, c2s, peekLen1), 0);
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, sizeof(readBuf), &readLen), HITLS_SUCCESS);
ASSERT_EQ(readLen, sizeof(c2s));
ASSERT_EQ(memcmp(readBuf, c2s, readLen), 0);
ASSERT_EQ(HITLS_Peek(server->ssl, peekBuf2, sizeof(peekBuf2), &peekLen2), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_EQ(peekLen2, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_SetTmpDhCb_TC001
* @spec -
* @title HITLS_SetTmpDhCb interface test. The config field is empty.
* @precon nan
* @brief 1. If config is empty, expected result 1 occurs.
* @expect 1. HITLS_NULL_INPUT is returned.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SetTmpDhCb_TC001(void)
{
// config is empty
ASSERT_TRUE(HITLS_SetTmpDhCb(NULL, DH_CB) == HITLS_NULL_INPUT);
EXIT:
;
}
/* END_CASE */
/** @
* @test UT_TLS_SET_VERSION_API_TC001
* @title Overwrite the input parameter of the HITLS_SetVersion interface.
* @precon nan
* @brief 1. Invoke the HITLS_SetVersion interface and leave ctx blank. Expected result 2 .
* 2. Invoke the HITLS_SetVersion interface. The ctx parameter is not empty. The minimum version number is
* DTLS1.0, and the maximum version number is DTLS1.2. Expected result 2 .
* 3. Invoke the HITLS_SetVersion interface. The ctx parameter is not empty, the minimum version number is
* DTLS1.2, and the maximum version number is DTLS1.2. Expected result 1 .
* 4. Invoke the HITLS_SetVersion interface, set ctx to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to DTLS1.0. Expected result 2 .
* 5. Invoke the HITLS_SetVersion interface, set ctx to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to TLS1.0. (Expected result 2)
* 6. Invoke the HITLS_SetVersion interface, set ctx to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to TLS1.2. Expected result 2 .
* 7. Invoke the HITLS_SetVersion interface, set ctx to a value, set the minimum version number to TLS1.0, and set
* the maximum version number to DTLS1.2. Expected result 2 .
* 8. Invoke the HITLS_SetVersion interface, set ctx to a value, set the minimum version number to TLS1.2, and set
* the maximum version number to DTLS1.2. Expected result 2 .
* @expect 1. The interface returns a success response, HITLS_SUCCESS.
* 2. The interface returns an error code.
@ */
/* BEGIN_CASE */
void UT_TLS_SET_VERSION_API_TC001(void)
{
HitlsInit();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
int32_t ret;
ret = HITLS_SetVersion(NULL, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_SetVersion(ctx, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_SetVersion(ctx, HITLS_VERSION_DTLS12, HITLS_VERSION_TLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_SET_ServerName_TC001
* @spec -
* @title HITLS_SetServerName invokes the interface to set the server name.
* @precon nan
* @brief
1. Initialize the client and server. Expected result 1
2. After the initialization, set the servername and run the HITLS_GetServerName command to check the server name.
Expected result 2 is displayed
* @expect
1. Complete initialization
2. The returned result is consistent with the settings
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_SET_ServerName_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_SetServerName(client->ssl, (uint8_t *)g_serverName, (uint32_t)strlen((char *)g_serverName)), HITLS_SUCCESS);
client->ssl->isClient = true;
const char *server_name = HITLS_GetServerName(client->ssl, HITLS_SNI_HOSTNAME_TYPE);
ASSERT_TRUE(memcmp(server_name, g_serverName, strlen(g_serverName)) == 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_REQUEST) == HITLS_SUCCESS);
server_name = HS_GetServerName(server->ssl);
ASSERT_TRUE(memcmp(server_name, g_serverName, strlen(g_serverName)) == 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test The interface is invoked in the Idle state. An exception is returned.
* @spec -
* @title UT_TLS_HITLS_READ_WRITE_TC001
* @precon nan
* @brief
1. When the connection is in the Idle state, call the hitls_read/hitls_write interface. Expected result 1 is obtained.
* @expect
1. The connection is not established.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_HITLS_READ_WRITE_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(client->ssl->state == CM_STATE_IDLE);
ASSERT_TRUE(server->ssl->state == CM_STATE_IDLE);
// 1. When the link is in the Idle state, call the hitls_read/hitls_write interface.
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readLen) == HITLS_CM_LINK_UNESTABLISHED);
ASSERT_TRUE(HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen) == HITLS_CM_LINK_UNESTABLISHED);
// 1. When the link is in the Idle state, call the hitls_read/hitls_write interface.
uint8_t writeBuf[] = "abc";
uint32_t writeLen = 4;
uint32_t len = 0;
ASSERT_TRUE(HITLS_Write(client->ssl, writeBuf, writeLen, &len) == HITLS_CM_LINK_UNESTABLISHED);
ASSERT_TRUE(HITLS_Write(server->ssl, writeBuf, writeLen, &len) == HITLS_CM_LINK_UNESTABLISHED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test test HITLS_Close in different cm state
* @spec -
* @title UT_TLS_HITLS_CLOSE_TC001
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
2. Invoke HITLS_Connect to send the message. Expected result 2
3. Invoke HITLS_Close and failed to send the message. Expected result 3
4. Succeeded in invoking HITLS_Connect to resend the failed close_notify message. Expected result 4
5. Invoke HITLS_Close to send the message. Expected result 5
* @expect 1. The connection is not established.
2. The client status is CM_STATE_HANDSHAKING.
3. The client status is CM_STATE_ALERTING.
4. The client status is CM_STATE_ALERTED.
5. The client status is CM_STATE_CLOSED.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_HITLS_CLOSE_TC001(int uioType)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
FRAME_Msg recvframeMsg = {0};
FRAME_Msg sndframeMsg = {0};
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, uioType);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, uioType);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_REQUEST) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_RECV_CERTIFICATE_REQUEST);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
ioUserData->sndMsg.len = 1;
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_REC_NORMAL_IO_BUSY);
ASSERT_EQ(clientTlsCtx->state, CM_STATE_ALERTED);
ioUserData->sndMsg.len = 0;
ASSERT_EQ(HITLS_Close(clientTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
EXIT:
CleanRecordBody(&recvframeMsg);
CleanRecordBody(&sndframeMsg);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test test HITLS_Close in different cm state
* @spec -
* @title UT_TLS_HITLS_CLOSE_TC002
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
2. Invoke HITLS_Close. Expected result 2
* @expect 1. The connection is not established.
2. The client status is CM_STATE_CLOSED.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_HITLS_CLOSE_TC002(int uioType)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
client = FRAME_CreateLink(config, uioType);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, uioType);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(HITLS_Close(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_CLOSED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
int32_t ParseServerCookie(ParsePacket *pkt, ServerHelloMsg *msg);
/* @
* @test test ParseServerCookie and ParseClientCookie
* @spec -
* @title UT_TLS_PARSE_Cookie_TC001
* @precon nan
* @brief 1. Initialize the client. Expected result 1
2. Assemble a message with zero length cookie, invoke ParseServerCookie. Expected result 2
3. Assemble a message with zero length cookie, invoke ParseClientCookie. Expected result 2
* @expect 1. The connection is not established.
2. The return value is HITLS_PARSE_INVALID_MSG_LEN.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_PARSE_Cookie_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
CONN_Init(client->ssl);
ServerHelloMsg svrMsg = { 0 };
ClientHelloMsg cliMsg = { 0 };
uint8_t cookie[] = { 0x00 };
uint32_t bufOffset = 0;
ParsePacket pkt = {.ctx = client->ssl, .buf = cookie, .bufLen = sizeof(cookie), .bufOffset = &bufOffset};
ASSERT_EQ(ParseServerCookie(&pkt, &svrMsg), HITLS_PARSE_INVALID_MSG_LEN);
CleanServerHello(&svrMsg);
ASSERT_EQ(ParseClientCookie(&pkt, &cliMsg), HITLS_PARSE_INVALID_MSG_LEN);
CleanClientHello(&cliMsg);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface/test_suite_sdv_frame_tls_cm_1.c | C | unknown | 37,949 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "hitls.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include <stdlib.h>
#include "securec.h"
#include "bsl_sal.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "bsl_uio.h"
#include "hitls_crypt_reg.h"
#include "hitls_session.h"
#include "cert_method.h"
#include "bsl_list.h"
#include "session_mgr.h"
#include "hitls_x509_verify.h"
#define DEFAULT_DESCRIPTION_LEN 128
#define ERROR_HITLS_GROUP 1
#define ERROR_HITLS_SIGNATURE 0xffffu
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; /* Set the session to the client for session resume. */
} ResumeTestInfo;
HITLS_CERT_X509 *HiTLS_X509_LoadCertFile(HITLS_Config *tlsCfg, const char *file);
void SAL_CERT_X509Free(HITLS_CERT_X509 *cert);
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
switch (ver) {
case TLS1_2:
case HITLS_VERSION_TLS12:
return HITLS_CFG_NewTLS12Config();
case TLS1_3:
case HITLS_VERSION_TLS13:
return HITLS_CFG_NewTLS13Config();
case DTLS1_2:
case HITLS_VERSION_DTLS12:
return HITLS_CFG_NewDTLS12Config();
default:
return NULL;
}
}
int32_t Stub_Write(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
{
(void)uio;
(void)buf;
(void)len;
(void)writeLen;
return HITLS_SUCCESS;
}
int32_t Stub_Read(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen)
{
(void)uio;
(void)buf;
(void)len;
(void)readLen;
return HITLS_SUCCESS;
}
int32_t Stub_Ctrl(BSL_UIO *uio, BSL_UIO_CtrlParameter cmd, void *param)
{
(void)uio;
(void)cmd;
(void)param;
return HITLS_SUCCESS;
}
/* END_HEADER */
/** @
* @test UT_TLS_CFG_SET_VERSION_API_TC001
* @title Overwrite the input parameter of the HITLS_CFG_SetVersion interface.
* @precon nan
* @brief 1. Invoke the HITLS_CFG_SetVersion interface and leave config blank. Expected result 2 .
* 2. Invoke the HITLS_CFG_SetVersion interface. The config parameter is not empty. The minimum version number is
* DTLS1.0, and the maximum version number is DTLS1.2. Expected result 2 .
* 3. Invoke the HITLS_CFG_SetVersion interface. The config parameter is not empty, the minimum version number is
* DTLS1.2, and the maximum version number is DTLS1.2. Expected result 1 .
* 4. Invoke the HITLS_CFG_SetVersion interface, set config to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to DTLS1.0. Expected result 2 .
* 5. Invoke the HITLS_CFG_SetVersion interface, set config to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to TLS1.0. (Expected result 2)
* 6. Invoke the HITLS_CFG_SetVersion interface, set config to a value, set the minimum version number to DTLS1.2, and
* set the maximum version number to TLS1.2. Expected result 2 .
* 7. Invoke the HITLS_CFG_SetVersion interface, set config to a value, set the minimum version number to TLS1.0, and set
* the maximum version number to DTLS1.2. Expected result 2 .
* 8. Invoke the HITLS_CFG_SetVersion interface, set config to a value, set the minimum version number to TLS1.2, and set
* the maximum version number to DTLS1.2. Expected result 2 .
* @expect 1. The interface returns a success response, HITLS_SUCCESS.
* 2. The interface returns an error code.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_VERSION_API_TC001(void)
{
HitlsInit();
HITLS_Config *tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
int32_t ret;
ret = HITLS_CFG_SetVersion(NULL, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_DTLS10, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS10);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_DTLS12, HITLS_VERSION_TLS10);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_DTLS12, HITLS_VERSION_TLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_TLS10, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
ret = HITLS_CFG_SetVersion(tlsConfig, HITLS_VERSION_TLS12, HITLS_VERSION_DTLS12);
ASSERT_TRUE(ret != HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_VERSIONFORBID_API_TC001
* @title Test the HITLS_CFG_SetVersionForbid interface.
* @precon nan
* @brief HITLS_CFG_SetVersionForbid
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer non-empty configuration information and set version to an invalid value. Expected result 2.
* 3. Transfer non-empty configuration information and set version to a valid value. Expected result 3.
* 4. Use HITLS_CFG_GetVersionSupport to view the result.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned, and invalid values in config are filtered out.
* 3. HITLS_SUCCES is returned and config is the expected value.
* 4. The HITLS_SUCCES parameter is returned, and the version parameter is set to the value recorded in the config file.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_VERSIONFORBID_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint32_t version = TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_NULL_INPUT);
version = 0;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS12_VERSION_BIT);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS12);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS_VERSION_MASK);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLS12Config();
version = HITLS_VERSION_TLS12;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS12_VERSION_BIT);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS12);
version = HITLS_VERSION_DTLS12;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS12_VERSION_BIT);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS12);
version = 0x0305u;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS12_VERSION_BIT);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS12);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLSConfig();
version = HITLS_VERSION_DTLS12;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS_VERSION_MASK);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
version = 0x0305u;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS_VERSION_MASK);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLSConfig();
version = HITLS_VERSION_TLS13;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS12_VERSION_BIT);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS12);
HITLS_CFG_FreeConfig(config);
config = HITLS_CFG_NewTLSConfig();
version = HITLS_TLS_ANY_VERSION;
ASSERT_TRUE(HITLS_CFG_SetVersionForbid(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == TLS_VERSION_MASK);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_EXTENEDMASTERSECRETSUPPORT_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetExtenedMasterSecretSupport and HITLS_CFG_GetExtenedMasterSecretSupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetExtenedMasterSecretSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer non-empty configuration information and set support to an invalid value. Expected result 2.
* 3. Transfer non-empty configuration information and set support to a valid value. Expected result 3.
* HITLS_CFG_GetExtenedMasterSecretSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer an empty isSupport pointer. Expected result 1.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and config->isSupportExtendMasterSecret is true.
* 3. Returns HITLS_SUCCES and config->isSupportExtendMasterSecret is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_EXTENEDMASTERSECRETSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_CFG_SetExtenedMasterSecretSupport(config, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetExtenedMasterSecretSupport(config, &isSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetExtenedMasterSecretSupport(config, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_CFG_SetExtenedMasterSecretSupport(config, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_CFG_SetExtenedMasterSecretSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetExtenedMasterSecretSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetExtenedMasterSecretSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetExtenedMasterSecretSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_POSTHANDSHAKEAUTHSUPPORT_API_TC001
* @spec -
* @titleTest the HITLS_CFG_SetPostHandshakeAuthSupport and HITLS_CFG_GetPostHandshakeAuthSupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetPostHandshakeAuthSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer non-empty configuration information and set support to an invalid value. Expected result 2.
* 3. Transfer non-empty configuration information and set support to a valid value. Expected result 3.
* HITLS_CFG_GetPostHandshakeAuthSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer an empty isSupport pointer. Expected result 1.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and the value of config->isSupportPostHandshakeAuth is true.
* 3. HITLS_SUCCES is returned and config->isSupportPostHandshakeAuth is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_POSTHANDSHAKEAUTHSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_CFG_SetPostHandshakeAuthSupport(config, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetPostHandshakeAuthSupport(config, &isSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetPostHandshakeAuthSupport(config, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_CFG_SetPostHandshakeAuthSupport(config, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_CFG_SetPostHandshakeAuthSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetPostHandshakeAuthSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetPostHandshakeAuthSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetPostHandshakeAuthSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_CIPHERSUITES_FUNC_TC001
* @title Test the HITLS_CFG_SetCipherSuites and HITLS_CFG_ClearTLS13CipherSuites interfaces.
* @precon nan
* @brief
* 1. The client invokes the HITLS_CFG_SetCipherSuites interface to set the tls1.3 cipher suite HITLS_AES_128_GCM_SHA256.
* Expected result 1.
* 2. Call HITLS_CFG_ClearTLS13CipherSuites to clear the TLS1.3 algorithm suite. Expected result 2.
* 3. Check whether the value of config->tls13CipherSuites is NULL and whether the value of config->tls13cipherSuitesSize
* is 0. (Expected result 3)
* 4. Establish a connection. Expected result 4.
* @expect
* 1. The setting is successful.
* 2. The interface returns a success message.
* 3. config->tls13CipherSuites, config->tls13cipherSuitesSize = 0
* 4. TLS1.3 initialization fails, and TLS1.2 connection are established.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_CIPHERSUITES_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint16_t cipherSuites[1] = {
HITLS_AES_128_GCM_SHA256
};
config_c = GetHitlsConfigViaVersion(tlsVersion);
config_s = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(config_c, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t))
== HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_ClearTLS13CipherSuites(config_c) == HITLS_SUCCESS);
ASSERT_TRUE(config_c->tls13CipherSuites == NULL);
ASSERT_TRUE(config_c->tls13cipherSuitesSize == 0);
FRAME_CertInfo certInfo = {
"ecdsa/ca-nist521.der:ecdsa/inter-nist521.der:rsa_sha/ca-3072.der:rsa_sha/inter-3072.der",
NULL, NULL, NULL, NULL, NULL,};
client = FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfo);
if (tlsVersion == TLS1_3) {
ASSERT_TRUE(client == NULL);
goto EXIT;
}
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @
* @test UT_TLS_CFG_SET_GET_KEYEXCHMODE_FUNC_TC001
* @title Setting the key exchange mode
* @precon nan
* @brief
* 1. Call HITLS_CFG_SetKeyExchMode to set the key exchange mode to TLS13_KE_MODE_PSK_ONLY. Expected result 1 is
* obtained.
* 2. Invoke the HITLS_CFG_GetKeyExchMode interface. (Expected result 2)
* 3. Call HITLS_CFG_SetKeyExchMode to set the key exchange mode to TLS13_KE_MODE_PSK_WITH_DHE. Expected result 3 is
* obtained.
* 4. Invoke the HITLS_CFG_GetKeyExchMode interface. (Expected result 4)
* @expect
* 1. The setting is successful.
* 2. The returned value is the same as that of TLS13_KE_MODE_PSK_ONLY.
* 3. The setting is successful.
* 4. The return value of the interface is the same as that of TLS13_KE_MODE_PSK_WITH_DHE.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_KEYEXCHMODE_FUNC_TC001()
{
FRAME_Init();
ResumeTestInfo testInfo = {0};
testInfo.version = HITLS_VERSION_TLS13;
testInfo.uioType = BSL_UIO_TCP;
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_EQ(HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_ONLY), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_GetKeyExchMode(testInfo.config), TLS13_KE_MODE_PSK_ONLY);
ASSERT_EQ(HITLS_CFG_SetKeyExchMode(testInfo.config, TLS13_KE_MODE_PSK_WITH_DHE), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_GetKeyExchMode(testInfo.config), TLS13_KE_MODE_PSK_WITH_DHE);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_VERSIONSUPPORT_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetVersionSupport and HITLS_CFG_GetVersionSupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetVersionSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer non-empty configuration information and set version to an invalid value. Expected result 2.
* 3. Transfer non-empty configuration information and set version to a valid value. Expected result 3.
* HITLS_CFG_GetVersionSupport
* 1. Import empty configuration information. Expected result 1.
* 2. Pass the null version pointer. Expected result 1.
* 3. Transfer non-null configuration information and ensure that the version pointer is not null. Expected result 4 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned, and invalid values in config are filtered out.
* 3. HITLS_SUCCES is returned and config is the expected value.
* 4. The HITLS_SUCCES parameter is returned, and the version parameter is set to the value recorded in the config file.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_VERSIONSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint32_t version = 0;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, &version) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, NULL) == HITLS_NULL_INPUT);
version = (TLS13_VERSION_BIT << 1) | TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
version = TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
uint32_t getversion = 0;
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, &getversion) == HITLS_SUCCESS);
ASSERT_TRUE(getversion == config->version);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_ENCRYPTTHENMAC_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetEncryptThenMac and HITLS_CFG_GetEncryptThenMac interfaces.
* @precon nan
* @brief HITLS_CFG_SetEncryptThenMac
* 1. Import empty configuration information. Expected result 1.
* 2. Transfer non-null configuration information and set encryptThenMacType to an invalid value. Expected result 2 is
* obtained.
* 3. Transfer the non-empty configuration information and set encryptThenMacType to a valid value. Expected result 3 is
* obtained.
* HITLS_CFG_GetEncryptThenMac
* 1. Import empty configuration information. Expected result 1.
* 2. Pass the null encryptThenMacType pointer. Expected result 1.
* 3. Transfer non-null configuration information and ensure that the encryptThenMacType pointer is not null. Expected
* result 3.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and config->isEncryptThenMac is true.
* 3. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_ENCRYPTTHENMAC_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint32_t encryptThenMacType = 0;
ASSERT_TRUE(HITLS_CFG_SetEncryptThenMac(config, encryptThenMacType) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetEncryptThenMac(config, &encryptThenMacType) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetEncryptThenMac(config, NULL) == HITLS_NULL_INPUT);
encryptThenMacType = 1;
ASSERT_TRUE(HITLS_CFG_SetEncryptThenMac(config, encryptThenMacType) == HITLS_SUCCESS);
encryptThenMacType = 2;
ASSERT_TRUE(HITLS_CFG_SetEncryptThenMac(config, encryptThenMacType) == HITLS_SUCCESS);
ASSERT_TRUE(config->isEncryptThenMac = true);
uint32_t getencryptThenMacType = -1;
ASSERT_TRUE(HITLS_CFG_GetEncryptThenMac(config, &getencryptThenMacType) == HITLS_SUCCESS);
ASSERT_TRUE(getencryptThenMacType == config->isEncryptThenMac);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_IS_DTLS_API_TC001
* @title Test the HITLS_CFG_IsDtls interface.
* @precon nan
* @brief
* 1. Transfer empty configuration information. Expected result 1.
* 2. Transfer the null pointer isDtls. Expected result 1.
* 3. Transfer the configuration information and ensure that the isDtls pointer is not null. Expected result 2 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. The HITLS_SUCCESS and isDtls information is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_IS_DTLS_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint8_t isDtls = false;
ASSERT_TRUE(HITLS_CFG_IsDtls(config, &isDtls) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_IsDtls(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_IsDtls(config, &isDtls) == HITLS_SUCCESS);
ASSERT_TRUE(isDtls == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *s_config;
HITLS_Config *c_config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession;
HITLS_TicketKeyCb serverKeyCb;
} ResumeTestInfo1;
HITLS_CRYPT_Key *cert_key = NULL;
HITLS_CRYPT_Key* DH_CB(HITLS_Ctx *ctx, int32_t isExport, uint32_t keyLen)
{
(void)ctx;
(void)isExport;
(void)keyLen;
return cert_key;
}
uint64_t RECORDPADDING_CB(HITLS_Ctx *ctx, int32_t type, uint64_t length, void *arg)
{
(void)ctx;
(void)type;
(void)length;
(void)arg;
return 100;
}
int32_t RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType);
int32_t STUB_RecParseInnerPlaintext(TLS_Ctx *ctx, uint8_t *text, uint32_t *textLen, uint8_t *recType)
{
(void)ctx;
(void)text;
(void)textLen;
*recType = (uint8_t)REC_TYPE_APP;
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_CFG_GET_RECORDPADDING_API_TC001
* @title HITLS_CFG_SetRecordPaddingCb Connection
* @precon nan
* @brief 1. If config is empty, expected result 1.
2. RecordPADDING_CB is empty. Expected result 2.
3. RecordPADDING_CB is not empty. Expected result 3.
* @expect 1. The interface returns HITLS_NULL_INPUT.
2. The interface returns HITLS_SUCCESS.
3. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_RECORDPADDING_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// Config is empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(NULL, RECORDPADDING_CB) == HITLS_NULL_INPUT);
// RecordPADDING_CB is empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(config, NULL) == HITLS_SUCCESS);
// RecordPADDING_CB is not empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(config, RECORDPADDING_CB) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetRecordPaddingCb(config) == RECORDPADDING_CB);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(config, RECORDPADDING_CB) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(NULL, RECORDPADDING_CB) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(config, NULL) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_RECORDPADDINGARG_API_TC001
* @title HITLS_CFG_SetRecordPaddingCbArg Connection
* @precon nan
* @brief 1. If config is empty, expected result 1.
2. RecordPADDING_CB is empty. Expected result 2.
3. RecordPADDING_CB is not empty. Expected result 3.
* @expect 1. The interface returns HITLS_NULL_INPUT.
2. The interface returns HITLS_SUCCESS.
3. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_RECORDPADDINGARG_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// Config is empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(NULL, RECORDPADDING_CB) == HITLS_NULL_INPUT);
// RecordPADDING_CB is empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(config, NULL) == HITLS_SUCCESS);
// RecordPADDING_CB is not empty
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(config, RECORDPADDING_CB) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetRecordPaddingCb(config) == RECORDPADDING_CB);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(config, RECORDPADDING_CB) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(NULL, RECORDPADDING_CB) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(config, NULL) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
int32_t EXAMPLE_TicketKeyCallback(
uint8_t *keyName, uint32_t keyNameSize, HITLS_CipherParameters *cipher, uint8_t isEncrypt)
{
(void)keyName;
(void)keyNameSize;
(void)cipher;
(void)isEncrypt;
return 100;
}
/** @
* @test UT_TLS_CFG_SET_TICKET_CB_API_TC001
* @title Test HITLS_CFG_SetTicketKeyCallback interface
* @brief 1. If config is empty, expected result 1.
2. HITLS_CFG_SetTicketKeyCallback is empty. Expected result 2
3. HITLS_CFG_SetTicketKeyCallback is not empty. Expected result 2
* @expect 1. Returns HITLS_NULL_INPUT.
2. Returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_TICKET_CB_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// Config is empty
ASSERT_TRUE(HITLS_CFG_SetTicketKeyCallback(NULL, EXAMPLE_TicketKeyCallback) == HITLS_NULL_INPUT);
// HITLS_TicketKeyCb is empty
ASSERT_TRUE(HITLS_CFG_SetTicketKeyCallback(config, NULL) == HITLS_SUCCESS);
// HITLS_TicketKeyCb is not empty
ASSERT_TRUE(HITLS_CFG_SetTicketKeyCallback(config, EXAMPLE_TicketKeyCallback) == HITLS_SUCCESS);
SESSMGR_SetTicketKeyCb(config->sessMgr, EXAMPLE_TicketKeyCallback);
ASSERT_EQ(SESSMGR_GetTicketKeyCb(config->sessMgr), EXAMPLE_TicketKeyCallback);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_NEW_DTLSCONFIG_API_TC001
* @title Test HITLS_CFG_NewDTLSConfig interface
* @brief 1. Invoke the interface HITLS_CFG_NewTLS12Config, expected result 1.
* @expect 1. Returns not NULL.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_NEW_DTLSCONFIG_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewDTLSConfig();
ASSERT_TRUE(config != NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
#define DATA_MAX_LEN 1024
/** @
* @test UT_TLS_CFG_GET_SET_SESSION_TICKETKEY_API_TC001
* @title Test HITLS_CFG_SetSessionTicketKey interface
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. If ticketKey is null, invoke HITLS_CFG_SetSessionTicketKey. Expected result 2.
* 3. Invoke HITLS_CFG_SetSessionTicketKey. Expected result 3.
* 4. If outSize is null, invoke HITLS_CFG_SetSessionTicketKey. Expected result 2.
* 5. Invoke HITLS_CFG_SetSessionTicketKey. Expected result 3.
* @expect 1. Memory register succeeded.
* 2. Return HITLS_NULL_INPUT.
* 3. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_SET_SESSION_TICKETKEY_API_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
uint8_t getKey[DATA_MAX_LEN] = {0};
uint32_t getKeySize = DATA_MAX_LEN;
uint32_t outSize = 0;
char *ticketKey = "748ab9f3dc1a23748ab9f3dc1a23748ab9f3dc1a23748ab9f3dc1a23748ab9f3dc1a23748ab9f3d";
uint32_t ticketKeyLen = HITLS_TICKET_KEY_NAME_SIZE + HITLS_TICKET_KEY_SIZE + HITLS_TICKET_KEY_SIZE;
ASSERT_TRUE(HITLS_CFG_SetSessionTicketKey(config, NULL, ticketKeyLen) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetSessionTicketKey(config, (uint8_t *)ticketKey, ticketKeyLen) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetSessionTicketKey(config, getKey, getKeySize, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetSessionTicketKey(config, getKey, getKeySize, &outSize) == HITLS_SUCCESS);
ASSERT_TRUE(outSize == ticketKeyLen);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_ADD_CAINDICATION_API_TC001
* @title: Test Add different CA flag indication types.
* @brief
* 1. If data is NULL, Invoke the HITLS_CFG_AddCAIndication.Expected result 1.
* 2. Invoke the HITLS_CFG_AddCAIndication and set the transferred caType to HITLS_TRUSTED_CA_PRE_AGREED.Expected
* result 2.
* 3. Invoke the HITLS_CFG_AddCAIndication and set the transferred caType to HITLS_TRUSTED_CA_KEY_SHA1.Expected
* result 2.
* 4. Invoke the HITLS_CFG_AddCAIndication and set the transferred caType to HITLS_TRUSTED_CA_X509_NAME.Expected
* result 2.
* 5. Invoke the HITLS_CFG_AddCAIndication and set the transferred caType to HITLS_TRUSTED_CA_CERT_SHA1.Expected
* result 2.
* 6. Invoke the HITLS_CFG_AddCAIndication and set the transferred caType to HITLS_TRUSTED_CA_UNKNOWN.Expected
* result 2.
* @expect
* 1. Return HITLS_NULL_INPUT.
* 2. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_ADD_CAINDICATION_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint8_t data[] = {0};
uint32_t len = sizeof(data);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_PRE_AGREED, NULL, len) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_PRE_AGREED, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_KEY_SHA1, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_X509_NAME, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_CERT_SHA1, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_UNKNOWN, data, len) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_CALIST_API_TC001
* @title Test HITLS_CFG_GetCAList interface
* @brief
* 1.Register the memory for config structure. Expected result 1.
* 1.Invoke the interface HITLS_CFG_GetCAList, expected result 2.
* @expect 1. Returns not NULL.
* 2. Returns NULL.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_CALIST_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ASSERT_TRUE(HITLS_CFG_GetCAList(config) == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_GET_VERSION_API_TC001
* @title Test HITLS_CFG_GetMinVersion/HITLS_CFG_GetMaxVersion/HITLS_SetVersion interface
* @brief
* 1.If minVersion is NULL, Invoke the HITLS_CFG_GetMinVersion.Expected result 1.
* 2.If maxVersion is NULL, Invoke the HITLS_CFG_GetMinVersion.Expected result 1.
* 3.Invoke HITLS_CFG_SetVersion.Expected result 2.
* 4.Invoke HITLS_CFG_GetMinVersion.Expected result 2.
* 5.Invoke HITLS_CFG_GetMaxVersion.Expected result 2.
* 6. Check minVersion is HITLS_VERSION_TLS12 and maxVersion is HITLS_VERSION_TLS13
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCES
* 3. Return HITLS_SUCCES,minVersion is HITLS_VERSION_TLS12 and maxVersion is HITLS_VERSION_TLS13
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_VERSION_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
uint16_t minVersion = 0;
uint16_t maxVersion = 0;
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetVersion(config, HITLS_VERSION_TLS12, HITLS_VERSION_TLS13) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_TLS12 && maxVersion == HITLS_VERSION_TLS13);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_SESSION_CACHEMODE_API_TC001
* @title Test ITLS_CFG_GetSessionCacheMoe interface
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. Invoke HITLS_CFG_GetSessionCacheMode. Expected result 2.
* @expect 1. Memory register succeeded.
* 2. Return success and value is 0.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_SESSION_CACHEMODE_API_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_SESS_CACHE_MODE getCacheMode = 0;
ASSERT_EQ(HITLS_CFG_GetSessionCacheMode(config, &getCacheMode), 0);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_GET_SESSIONCACHESIZE_API_TC001
* @title Test HITLS_CFG_SetSessionCacheSize/HITLS_CFG_GetSessionCacheSize interface
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. Invoke HITLS_CFG_SetSessionCacheSize. Expected result 2.
* 3. Invoke HITLS_CFG_GetSessionCacheSize. Expected result 2.
* 4. Check getCacheSize and cacheSize is equal
* @expect 1. Memory register succeeded.
* 2. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_SESSIONCACHESIZE_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint32_t cacheSize = 10;
uint32_t getCacheSize = 0;
ASSERT_TRUE(HITLS_CFG_SetSessionCacheSize(config, cacheSize) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetSessionCacheSize(config, &getCacheSize) == HITLS_SUCCESS);
ASSERT_TRUE(getCacheSize == cacheSize);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_GET_SESSION_TIMEOUT_API_TC001
* @title Test HITLS_CFG_GetSessionTimeout interface
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. Invoke HITLS_CFG_SetSessionTimeout. Expected result 2.
* 3. Invoke HITLS_CFG_GetSessionTimeout. Expected result 2.
* 4. Check timeOut and getTimeOut is equal
* @expect 1. Memory register succeeded.
* 2. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_SESSION_TIMEOUT_API_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
uint64_t timeOut = 10;
uint64_t getTimeOut = 0;
ASSERT_TRUE(HITLS_CFG_SetSessionTimeout(config, timeOut) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetSessionTimeout(config, &getTimeOut) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_VERSIONFORBID_API_TC001
* @title Test HITLS_SetVersionForbid interface
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. If context is NULL, invoke HITLS_SetVersionForbid. Expected result 3.
* 3. If context is NULL, invoke HITLS_SetVersionForbid. Expected result 2.
* @expect 1. Memory register succeeded.
* 2. Return HITLS_SUCCESS.
* 3. Return HITLS_NULL_INPUT
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_VERSIONFORBID_API_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetVersionForbid(NULL, HITLS_VERSION_TLS12) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetVersionForbid(ctx, HITLS_VERSION_TLS12) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_CONFIGUSEDATA_API_TC001
* @title Test HITLS_CFG_SetConfigUserData/HITLS_CFG_GetConfigUserData interfaces
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. If config is NULL, invoke HITLS_CFG_SetConfigUserData. Expected result 2.
* 3. Invoke HITLS_CFG_SetConfigUserData. Expected result 3.
* 3. Invoke HITLS_CFG_SetConfigUserData. Expected result 4.
* @expect 1. Memory register succeeded.
* 2. Return HITLS_NULL_INPUT.
* 3. Return HITLS_SUCCESS.
* 4. Return not NULL.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_CONFIGUSEDATA_API_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
char *userData = "123456";
ASSERT_TRUE(HITLS_CFG_SetConfigUserData(NULL, userData) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetConfigUserData(config, userData) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetConfigUserData(config) != NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
void EXAMPLE_HITLS_ConfigUserDataFreeCb(
void* data)
{
(void)data;
return;
}
/** @
* @test UT_TLS_CFG_SET_CONFIG_USERDATA_FREECB_API_TC001
* @title Test HITLS_CFG_SetConfigUserDataFreeCb interfaces
* @brief 1. Register the memory for config structure. Expected result 1.
* 2. If config is NULL, invoke HITLS_CFG_SetConfigUserDataFreeCb. Expected result 2.
* 3. Invoke HITLS_CFG_SetConfigUserDataFreeCb. Expected result 3.
* @expect 1. Memory register succeeded.
* 2. Return HITLS_NULL_INPUT.
* 3. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_CONFIG_USERDATA_FREECB_API_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
ASSERT_TRUE(HITLS_CFG_SetConfigUserDataFreeCb(NULL, EXAMPLE_HITLS_ConfigUserDataFreeCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetConfigUserDataFreeCb(config, EXAMPLE_HITLS_ConfigUserDataFreeCb) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_CERTIFICATE_API_TC001
* @title Test HITLS_CFG_SetCertificate interface
* @brief 1. Invoke the HITLS_CFG_SetCertificate interface, set tlsConfig to null, and set cert for the device
* certificate. (Expected result 1)
* 2. Invoke the HITLS_CFG_SetCertificate interface. Set tlsConfig and cert to an empty value for the device
* certificate.(Expected result 1)
* 3. Invoke the HITLS_CFG_SetCertificate interface. Ensure that tlsConfig and cert are not empty. Perform deep
* copy. (Expected result 3)
* 4. Invoke the HITLS_CFG_GetCertificate interface. The value of tlsConfig->certMgrCtx->currentCertKeyType is
* greater than the value of TLS_CERT_KEY_TYPE_UNKNOWN, Expected result 4 is obtained.
* 5. Invoke the HITLS_CFG_GetCertificate interface and leave tlsConfig empty. Expected result 4 is obtained.
* 6. Invoke the HITLS_CFG_SetCertificate interface, set tlsConfig->certMgrCtx to null, and set cert to a non-empty
* device certificate. (Expected result 2)
* 7. Invoke HITLS_CFG_GetCertificate
* Run the tlsConfig command to set certMgrCtx to null. Expected result 4 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Return HITLS_CERT_ERR_X509_DUP
* 3. HITLS_SUCCESS is returned.
* 4. NULL is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_CERTIFICATE_API_TC001(int version, char *certFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_CERT_X509 *cert = HiTLS_X509_LoadCertFile(tlsConfig, certFile);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetCertificate(NULL, cert, false) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetCertificate(tlsConfig, NULL, true) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetCertificate(tlsConfig, cert, true), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetCertificate(tlsConfig) != NULL);
tlsConfig->certMgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
ASSERT_TRUE(HITLS_CFG_GetCertificate(tlsConfig) == NULL);
ASSERT_TRUE(HITLS_CFG_GetCertificate(NULL) == NULL);
SAL_CERT_MgrCtxFree(tlsConfig->certMgrCtx);
tlsConfig->certMgrCtx = NULL;
ASSERT_EQ(HITLS_CFG_SetCertificate(tlsConfig, cert, true), HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetCertificate(tlsConfig) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
SAL_CERT_X509Free(cert);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_CHECK_PRIVATEKEY_API_TC001
* @title Test HITLS_CFG_CheckPrivateKey interface
* @brief 1. Invoke the HITLS_CFG_CheckPrivateKey interface and leave tlsConfig blank. Expected result 1
* 2. Invoke the HITLS_CFG_CheckPrivateKey interface. The tlsConfig parameter is not empty,
* The value of tlsConfig->certMgrCtx->currentCertKeyType is greater than or equal to the maximum value
* TLS_CERT_KEY_TYPE_UNKNOWN. Expected result 2
* 3. Invoke the HITLS_CFG_CheckPrivateKey interface and leave tlsConfig->certMgrCtx empty. Expected result 3
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_CONFIG_NO_CERT is returned.
* 3. The HITLS_UNREGISTERED_CALLBACK message is returned.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_CHECK_PRIVATEKEY_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_CheckPrivateKey(NULL) == HITLS_NULL_INPUT);
tlsConfig->certMgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
ASSERT_TRUE(HITLS_CFG_CheckPrivateKey(tlsConfig) == HITLS_CONFIG_NO_CERT);
SAL_CERT_MgrCtxFree(tlsConfig->certMgrCtx);
tlsConfig->certMgrCtx = NULL;
ASSERT_TRUE(HITLS_CFG_CheckPrivateKey(tlsConfig) == HITLS_UNREGISTERED_CALLBACK);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_ADD_CHAINCERT_API_TC001
* @title Test HITLS_CFG_GetChainCerts interface
* @brief 1. Invoke the HITLS_CFG_AddChainCert interface, set tlsConfig to null, and set addCert to a certificate to be
* added. Perform shallow copy. Expected result 1 .
* 2. Invoke the HITLS_CFG_AddChainCert interface. The tlsConfig parameter is not empty and the addCert parameter
* is empty.Perform deep copy. Expected result 1 .
* 3. Invoke the HITLS_CFG_AddChainCert interface. Ensure that tlsConfig is not empty and addCert is not empty.
* Perform shallow copy. Expected result 2 .
* 4. Invoke the HITLS_CFG_AddChainCert interface. The value of tlsConfig is not empty and the value of
* tlsConfig->certMgrCtx->currentCertKeyType is greater than or equal to the maximum value TLS_CERT_KEY_TYPE_UNKNOWN.
* Expected result 4 .
* 5. Invoke the HITLS_CFG_GetChainCerts interface. Set tlsConfig to a value greater than or equal to the maximum
* value TLS_CERT_KEY_TYPE_UNKNOWN. (Expected result 3)
* 6. Invoke the HITLS_CFG_GetChainCerts interface and leave tlsConfig blank. Expected result 3 .
* 7. Invoke the HITLS_CFG_LoadKeyBuffer interface. Set tlsConfig->certMgrCtx to null and addCert to the
* certificate to be added. Perform deep copy. Expected result 5 .
* 8. Invoke the HITLS_CFG_GetChainCerts interface and leave tlsConfig->certMgrCtx empty. Expected result 3.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCESS is returned.
* 3. NULL is returned.
* 4. Return ITLS_CERT_ERR_ADD_CHAIN_CERT
* 5. Return HITLS_CERT_ERR_X509_DUP
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_ADD_CHAINCERT_API_TC001(int version, char *certFile, char *addCertFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_CERT_X509 *cert = HITLS_CFG_ParseCert(tlsConfig, (const uint8_t *)certFile, strlen(certFile) + 1, TLS_PARSE_TYPE_FILE,
TLS_PARSE_FORMAT_ASN1);
cert = HiTLS_X509_LoadCertFile(tlsConfig, certFile);
HITLS_CERT_X509 *addCert = HiTLS_X509_LoadCertFile(tlsConfig, addCertFile);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetCertificate(tlsConfig, cert, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddChainCert(NULL, addCert, false) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_AddChainCert(tlsConfig, NULL, true) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_AddChainCert(tlsConfig, addCert, false), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetChainCerts(tlsConfig) != NULL);
tlsConfig->certMgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
ASSERT_EQ(HITLS_CFG_AddChainCert(tlsConfig, cert, true), HITLS_CERT_ERR_ADD_CHAIN_CERT);
ASSERT_TRUE(HITLS_CFG_GetChainCerts(tlsConfig) == NULL);
ASSERT_TRUE(HITLS_CFG_GetChainCerts(NULL) == NULL);
SAL_CERT_MgrCtxFree(tlsConfig->certMgrCtx);
tlsConfig->certMgrCtx = NULL;
ASSERT_EQ(HITLS_CFG_AddChainCert(tlsConfig, cert, true), HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetChainCerts(tlsConfig) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/** @
* @test UT_HITLS_CFG_REMOVE_CERTANDKEY_API_TC001
* @title Test HITLS_CFG_RemoveCertAndKey interface
* @brief
* 1. Register the memory for config structure. Expected result 1.
* 2. Invoke HITLS_CFG_RemoveCertAndKey interface, expected result 3.
* 3. Invoke HITLS_CFG_SetCertificate interface, expected result 3.
* 4. Invoke HITLS_CFG_LoadKeyFile interface, expected result 3.
* 5. Invoke HITLS_CFG_GetCertificate interface, expected result 2.
* 6. Invoke HITLS_CFG_GetPrivateKey interface, expected result 2.
* 7. Invoke HITLS_CFG_CheckPrivateKey interface, expected result 3.
* 8. Invoke HITLS_CFG_RemoveCertAndKey interface, expected result 3.
* 9. Invoke HITLS_CFG_GetCertificate interface, expected result 4.
* 10. Invoke HITLS_CFG_GetPrivateKey interface, expected result 4.
* @expect 1. Create successful.
* 2. Return not NULL
* 3. Return HITLS_SUCCESS
* 4.Return NULL
@ */
/* BEGIN_CASE */
void UT_HITLS_CFG_REMOVE_CERTANDKEY_API_TC001(int version, char *certFile, char *keyFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_CERT_X509 *cert = HiTLS_X509_LoadCertFile(tlsConfig, certFile);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_EQ(HITLS_CFG_RemoveCertAndKey(tlsConfig), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetCertificate(tlsConfig, cert, true), HITLS_SUCCESS);
#ifdef HITLS_TLS_FEATURE_PROVIDER
ASSERT_EQ(HITLS_CFG_ProviderLoadKeyFile(tlsConfig, keyFile, "ASN1", NULL), HITLS_SUCCESS);
#else
ASSERT_EQ(HITLS_CFG_LoadKeyFile(tlsConfig, keyFile, TLS_PARSE_FORMAT_ASN1), HITLS_SUCCESS);
#endif
ASSERT_TRUE(HITLS_CFG_GetCertificate(tlsConfig) != NULL);
ASSERT_TRUE(HITLS_CFG_GetPrivateKey(tlsConfig) != NULL);
ASSERT_EQ(HITLS_CFG_CheckPrivateKey(tlsConfig), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_RemoveCertAndKey(tlsConfig), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetCertificate(tlsConfig) == NULL);
ASSERT_TRUE(HITLS_CFG_GetPrivateKey(tlsConfig) == NULL);
EXIT:
HITLS_CFG_FreeCert(tlsConfig, cert);
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
void StubListDataDestroy(void *data)
{
BSL_SAL_FREE(data);
return;
}
/** @
* @test UT_HITLS_CFG_ADD_EXTRA_CHAINCERT_API_TC001
* @title Test HITLS_CFG_AddExtraChainCert interface
* @brief
* 1. Create a config object. Expected result 1 .
* 2. If the input value of config is null, invoke HITLS_CFG_GetExtraChainCerts to obtain the configured additional
* certificate chain. Expected result 2 .
* 3. Call the interface to add a certificate to the additional certificate chain and call HITLS_CFG_GetExtraChainCerts
* to obtain the configured additional certificate chain. Expected result 3 .
* 4. Call the API again to add certificate 2 to the additional certificate chain and call HITLS_CFG_GetExtraChainCerts
* to obtain the configured additional certificate chain. Expected result 4 .
5. Invoke HITLS_CFG_ClearChainCerts to clear the attached certificate chain. Expected result 5 .
* @expect
* 1. The config object is created successfully.
* 2. Failed to set the additional certificate chain. The obtained additional certificate chain is empty.
* 3. The additional certificate chain is successfully set and obtained.
* 4. The additional certificate chain is successfully set and obtained.
* 5. The STORE for obtaining the attached certificate chain does not change.
@ */
/* BEGIN_CASE */
void UT_HITLS_CFG_ADD_EXTRA_CHAINCERT_API_TC001(int version, char *certFile1, char *certFile2)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_CERT_X509 *cert1 = HiTLS_X509_LoadCertFile(tlsConfig, certFile1);
HITLS_CERT_X509 *cert2 = HiTLS_X509_LoadCertFile(tlsConfig, certFile2);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_AddExtraChainCert(NULL, cert1) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_AddExtraChainCert(tlsConfig, cert1) == HITLS_SUCCESS);
HITLS_CERT_Chain *extraChainCert = HITLS_CFG_GetExtraChainCerts(tlsConfig);
ASSERT_TRUE(extraChainCert->count == 1);
ASSERT_TRUE(HITLS_CFG_GetExtraChainCerts(tlsConfig) != NULL);
ASSERT_TRUE(HITLS_CFG_AddExtraChainCert(tlsConfig, cert2) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetExtraChainCerts(tlsConfig) != NULL);
ASSERT_TRUE(HITLS_CFG_ClearChainCerts(tlsConfig) == HITLS_SUCCESS);
HITLS_CERT_Chain *extraChainCert1 = HITLS_CFG_GetExtraChainCerts(tlsConfig);
ASSERT_TRUE(extraChainCert1->count == 2);
ASSERT_TRUE(HITLS_CFG_GetExtraChainCerts(tlsConfig) != NULL);
ASSERT_TRUE(HITLS_CFG_ClearExtraChainCerts(NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_ClearExtraChainCerts(tlsConfig) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetExtraChainCerts(tlsConfig) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_DTLS_MTU_API_TC001
* @title Test HITLS_SetMtu interface
* @brief 1. Create the TLS configuration object config.Expect result 1.
* 2. Use config to create the client and server.Expect result 2.
* 3. Invoke HITLS_SetMtu, Expect result 3.
* @expect 1. The config object is successfully created.
* 2. The client and server are successfully created.
* 3. Return HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_DTLS_MTU_API_TC001(void)
{
FRAME_Init();
uint32_t mtu = 1500;
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(HITLS_SetMtu(client->ssl, mtu) == HITLS_SUCCESS);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_SetMtu(server->ssl, mtu) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
void Test_HITLS_KeyLogCb(HITLS_Ctx *ctx, const char *line)
{
(void)ctx;
(void)line;
printf("there is Test_HITLS_KeyLogCb\n");
}
/* @
* @test UT_TLS_CFG_LogSecret_TC001
* @spec -
* @title Test the HITLS_LogSecret interface.
* @precon nan
* @brief
* 1. Transfer an empty context. The label and secret are not empty, and the secret length is not 0.
* Expected result 1 is obtained.
* 2. Transfer a non-empty context. The label is empty, the secret is not empty,
* and the secret length is not 0. Expected result 1 is obtained.
* 3. Transfer a non-empty context. The label is not empty, the secret is empty,
* and the secret length is not 0. Expected result 1 is obtained.
* 4. Transfer a non-empty context. The label and secret are not empty, and the secret length is 0.
* Expected result 1 is obtained.
* 5. Transfer a non-empty context. The label and secret are not empty, and the secret length is not 0.
* Expected result 2 is obtained.
* @expect 1. return HITLS_NULL_INPUT
* 2. return HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_LogSecret_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = NULL;
HITLS_CFG_SetKeyLogCb(config, Test_HITLS_KeyLogCb);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
const char label[] = "hello";
const char secret[] = "hello123";
ASSERT_EQ(HITLS_LogSecret(NULL, label, (const uint8_t *)secret, strlen(secret)), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_LogSecret(ctx, NULL, (const uint8_t *)secret, strlen(secret)), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_LogSecret(ctx, label, NULL, strlen(secret)), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_LogSecret(ctx, label, (const uint8_t *)secret, 0), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_LogSecret(ctx, label, (const uint8_t *)secret, strlen(secret)), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SetTmpDhCb_TC001
* @spec -
* @title HITLS_CFG_SetTmpDhCb interface test. The config field is empty.
* @precon nan
* @brief 1. If config is empty, expected result 1 is obtained.
* @expect 1. HITLS_NULL_INPUT is returned.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SetTmpDhCb_TC001()
{
// config is empty
ASSERT_TRUE(HITLS_CFG_SetTmpDhCb(NULL, DH_CB) == HITLS_NULL_INPUT);
EXIT:
;
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_GET_CIPHERSUITESBYSTDNAME_TC001
* @spec -
* @title HITLS_CFG_GetCipherSuiteByStdName connection
* @precon nan
* @brief 1. Transfer a null pointer. Expected result 1 is obtained.
* 2. Transfer the "TLS_RSA_WITH_AES_128_CBC_SHA" character string. Expected result 2 is obtained.
* 3. Input the character string x. Expected result 3 is obtained.
* @expect 1. return NULL
* 2. return HITLS_RSA_WITH_AES_128_CBC_SHA
* 3. return NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_CIPHERSUITESBYSTDNAME_TC001(void)
{
const char *StdName = NULL;
ASSERT_TRUE(HITLS_CFG_GetCipherSuiteByStdName((const uint8_t *)StdName) == NULL);
const char StdName2[] = "TLS_RSA_WITH_AES_128_CBC_SHA";
const HITLS_Cipher* Cipher2 = HITLS_CFG_GetCipherSuiteByStdName((const uint8_t *)StdName2);
ASSERT_TRUE(Cipher2->cipherSuite == HITLS_RSA_WITH_AES_128_CBC_SHA);
const char StdName3[] = "x";
ASSERT_TRUE(HITLS_CFG_GetCipherSuiteByStdName((const uint8_t *)StdName3) == NULL);
EXIT:
return;
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_CLEAR_CALIST_TC001
* @title HITLS_CFG_ClearCAList interface test
* @precon nan
* @brief 1. pass NULL parameter, expect result 1
* 2. pass config with NULL caList, expect result 1
* 3. pass normal config, expect result 1
* @expect 1. void function has no return value
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_CLEAR_CALIST_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Config *config2 = {0};
HITLS_CFG_ClearCAList(NULL);
HITLS_CFG_ClearCAList(config2);
HITLS_CFG_ClearCAList(config);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_GET_DHAUTOSUPPORT_TC001
* @spec -
* @title HITLS_CFG_SetDhAutoSupport and HITLS_CFG_GetDhAutoSupport contact
* @precon nan
* @brief HITLS_CFG_SetDhAutoSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set support to an invalid value. Expected result 2 is obtained.
* 3. Transfer non-empty configuration information and set support to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetDhAutoSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty isSupport pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is obtained.
* @expect 1. return HITLS_NULL_INPUT
* 2. return HITLS_SUCCES,and config->isSupportDhAuto is True
* 3. return HITLS_SUCCES,and config->isSupportDhAuto is False or True
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_DHAUTOSUPPORT_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_CFG_SetDhAutoSupport(config, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetDhAutoSupport(config, &isSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetDhAutoSupport(config, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_CFG_SetDhAutoSupport(config, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_CFG_SetDhAutoSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDhAutoSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetDhAutoSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDhAutoSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_GET_READ_AHEAD_TC001
* @title HITLS_CFG_GetReadAhead interface test
* @precon nan
* @brief 1. pass NULL config, expect result 1
* 2. pass NULL onOff, expect result 1
* 3. pass normal parameters, expect result 2
* @expect 1. return HITLS_NULL_INPUT
* 2. return HITLS_SUCCESS
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_READ_AHEAD_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
int32_t onOff = 0;
ASSERT_TRUE(HITLS_CFG_GetReadAhead(NULL, &onOff) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetReadAhead(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetReadAhead(config, &onOff) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_CONFIG_SET_KeyLogCb_TC001
* @spec -
* @title Test the HITLS_CFG_SetKeyLogCb and HITLS_CFG_GetKeyLogCb interfaces.
* @precon nan
* @brief HITLS_CFG_SetKeyLogCb and HITLS_CFG_GetKeyLogCb
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set callback to a non-empty value. Expected result 2 is obtained.
* @expect 1. return HITLS_NULL_INPUT
* 2. return HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_KeyLogCb_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ASSERT_TRUE(HITLS_CFG_SetKeyLogCb(NULL, Test_HITLS_KeyLogCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetKeyLogCb(config, Test_HITLS_KeyLogCb) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_GetKeyLogCb(NULL), NULL);
ASSERT_EQ(HITLS_CFG_GetKeyLogCb(config), Test_HITLS_KeyLogCb);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
int g_recordPaddingCbArg = 1;
uint64_t RecordPaddingCb(HITLS_Ctx *ctx, int32_t type, uint64_t length, void *arg)
{
(void)ctx;
(void)type;
(void)length;
ASSERT_TRUE(g_recordPaddingCbArg == (*(int *)arg));
ASSERT_TRUE(&g_recordPaddingCbArg == arg);
EXIT:
return 0;
}
/** @
* @test UT_TLS_CFG_SET_RECORDPADDINGARG_API_TC002
* @title HITLS_CFG_SetRecordPaddingCbArg Connection
* @precon nan
* @brief 1. Create tls13 config, expected result 1.
2. Set RecordPaddingCb and RecordPaddingCbArg to 1 for the client, Expected result 2.
3. Establish a connection, Verify that the arg passed in RecordPaddingCb matches the set arg.
Expected result 3.
* @expect
* 1. The creating is successful.
* 2. The setting is successful.
* 3. The arg value is the same,TLS1.3 connection are established.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_RECORDPADDINGARG_API_TC002()
{
HitlsInit();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLS13Config();
config_s = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCb(config_c, RecordPaddingCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetRecordPaddingCbArg(config_c, &g_recordPaddingCbArg) == HITLS_SUCCESS);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_LOADVERIFYDIR_MULTI_PATH_TC001
* @title Test HITLS_CFG_LoadVerifyDir with multiple CA paths
* @brief
* 1. Create a config object.
* 2. Pass in a string containing multiple paths (such as "/tmp/ca1:/tmp/ca2:/tmp/ca3").
* 3. Call HITLS_CFG_LoadVerifyDir.
* 4. Check that the number and content of caPaths in the cert store are consistent with the input.
* @expect
* 1. The interface returns success.
* 2. The number and content of paths in the cert store are consistent with the input.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_LOADVERIFYDIR_MULTI_PATH_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *multi_path = "/tmp/ca1:/tmp/ca2:/tmp/ca3:/tmp/ca3";
int32_t ret = HITLS_CFG_LoadVerifyDir(config, multi_path);
ASSERT_TRUE(ret == HITLS_SUCCESS);
HITLS_CERT_Store *store = SAL_CERT_GetCertStore(config->certMgrCtx);
ASSERT_TRUE(store != NULL);
HITLS_X509_StoreCtx *storeCtx = (HITLS_X509_StoreCtx *)store;
BslList *caPaths = storeCtx->caPaths;
ASSERT_TRUE(caPaths != NULL);
int expect_count = 3;
int actual_count = BSL_LIST_COUNT(caPaths);
ASSERT_TRUE(actual_count == expect_count);
const char *expect_paths[] = {"/tmp/ca1", "/tmp/ca2", "/tmp/ca3"};
for (int i = 0; i < expect_count; ++i) {
const char *path = (const char *)BSL_LIST_GetIndexNode(i, caPaths);
ASSERT_TRUE(path != NULL);
ASSERT_TRUE(strcmp(path, expect_paths[i]) == 0);
}
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_LOADVERIFYFILE_TC001
* @title Test HITLS_CFG_LoadVerifyFile with a single CA path
* @brief
* 1. Create a config object.
* 2. Pass in a string containing a single path.
* 3. Call HITLS_CFG_LoadVerifyFile.
* 4. Load a client certificate signed by the CA in the specified path.
* 5. Call HITLS_CFG_BuildCertChain to verify the client certificate.
* @expect
* 1. The interface returns success.
* 2. The client certificate is successfully verified.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_LOADVERIFYFILE_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *path = "../testdata/tls/certificate/pem/rsa_sha256/inter.pem";
int32_t ret = HITLS_CFG_LoadVerifyFile(config, path);
ASSERT_EQ(ret, HITLS_SUCCESS);
const char *path1 = "../testdata/tls/certificate/pem/rsa_sha256/ca.pem";
ret = HITLS_CFG_LoadVerifyFile(config, path1);
ASSERT_EQ(ret, HITLS_SUCCESS);
const char *certToVerify = "../testdata/tls/certificate/pem/rsa_sha256/client.pem";
ret = HITLS_CFG_LoadCertFile(config, certToVerify, TLS_PARSE_FORMAT_PEM);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_BuildCertChain(config, HITLS_BUILD_CHAIN_FLAG_NO_ROOT), HITLS_SUCCESS);
HITLS_CERT_Chain *chain = HITLS_CFG_GetChainCerts(config);
ASSERT_TRUE(chain != NULL);
ASSERT_TRUE(chain->count == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_LOADVERIFYFILE_TC002
* @title Test HITLS_CFG_LoadVerifyFile with a single CA path
* @brief
* 1. Create a config object.
* 2. Pass in a string containing a single path.
* 3. Call HITLS_CFG_LoadVerifyFile.
* 4. Load a client certificate signed by the CA in the specified path.
* 5. Call HITLS_CFG_BuildCertChain to verify the client certificate.
* @expect
* 1. The interface returns success.
* 2. The client certificate verification fails.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_LOADVERIFYFILE_TC002(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *path = "../testdata/tls/certificate/pem/ecdsa_sha256/inter.pem";
int32_t ret = HITLS_CFG_LoadVerifyFile(config, path);
ASSERT_EQ(ret, HITLS_SUCCESS);
const char *certToVerify = "../testdata/tls/certificate/pem/rsa_sha256/client.pem";
ret = HITLS_CFG_LoadCertFile(config, certToVerify, TLS_PARSE_FORMAT_PEM);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_BuildCertChain(config, HITLS_BUILD_CHAIN_FLAG_NO_ROOT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetChainCerts(config) == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_USECERTCHAINFILE_TC001
* @title Test HITLS_CFG_UseCertificateChainFile with a single file path
* @brief
* 1. Create a config object.
* 2. Pass in a string containing a single path.
* 3. Call HITLS_CFG_UseCertificateChainFile.
* 4. Load a client certificate signed by the CA in the specified path.
* 5. Call HITLS_CFG_BuildCertChain to verify the client certificate.
* @expect
* 1. The interface returns success.
* 2. The client certificate verification verified.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_USECERTCHAINFILE_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *path = "../testdata/tls/certificate/pem/rsa_sha256/cert_chain.pem";
int32_t ret = HITLS_CFG_UseCertificateChainFile(config, path);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_BuildCertChain(config, HITLS_BUILD_CHAIN_FLAG_CHECK), HITLS_SUCCESS);
HITLS_CERT_Chain *chain = HITLS_CFG_GetChainCerts(config);
ASSERT_TRUE(chain != NULL);
ASSERT_TRUE(chain->count == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_USECERTCHAINFILE_TC002
* @title Test HITLS_CFG_UseCertificateChainFile with a single CA path
* @brief
* 1. Create a config object.
* 2. Pass in a string containing a single path.
* 3. Call HITLS_CFG_UseCertificateChainFile.
* 4. Load a client certificate signed by the CA in the specified path.
* 5. Call HITLS_CFG_BuildCertChain to verify the client certificate.
* @expect
* 1. The interface returns success.
* 2. The client certificate verification fails.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_USECERTCHAINFILE_TC002(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *path = "../testdata/tls/certificate/pem/rsa_sha256/cert_chain_damaged_ca.pem";
int32_t ret = HITLS_CFG_UseCertificateChainFile(config, path);
ASSERT_EQ(ret, HITLS_CFG_ERR_LOAD_CERT_FILE);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_USECERTCHAINFILE_TC003
* @title Test HITLS_CFG_LoadVerifyFile with a single CA path
* @brief
* 1. Create a config object.
* 2. Pass in a string containing a single path.
* 3. Call HITLS_CFG_UseCertificateChainFile.
* 4. Load a client certificate signed by the CA in the specified path.
* 5. Call HITLS_CFG_BuildCertChain to verify the client certificate.
* @expect
* 1. The interface returns success.
* 2. The client certificate verification fails.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_USECERTCHAINFILE_TC003(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
const char *path = "../testdata/tls/certificate/pem/rsa_sha256/cert_chain_duplicate_ca.pem";
int32_t ret = HITLS_CFG_UseCertificateChainFile(config, path);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_BuildCertChain(config, HITLS_BUILD_CHAIN_FLAG_CHECK), HITLS_CERT_STORE_CTRL_ERR_ADD_CERT_LIST);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface/test_suite_sdv_frame_tls_config_1.c | C | unknown | 73,676 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "securec.h"
#include "hlt.h"
#include "hitls_error.h"
#include "hitls_func.h"
#include "conn_init.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "alert.h"
#include "stub_replace.h"
#include "hs_common.h"
#include "change_cipher_spec.h"
#include "hs.h"
#include "simulate_io.h"
#include "rec_header.h"
#include "rec_wrapper.h"
#include "recv_client_hello.c"
#include "record.h"
#define READ_BUF_SIZE 18432
#define MAX_DIGEST_SIZE 64UL /* The longest known is SHA512 */
uint32_t g_uiPort = 8890;
/* END_HEADER */
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
HITLS_Config *config;
int32_t ret;
switch (ver) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
case HITLS_VERSION_DTLS12:
config = HITLS_CFG_NewDTLS12Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
default:
return NULL;
}
}
int32_t STUB_BSL_UIO_Write(BSL_UIO *uio, const void *data, uint32_t len, uint32_t *writeLen)
{
(void)uio;
(void)data;
(void)len;
(void)writeLen;
return BSL_INTERNAL_EXCEPTION;
}
/** @
* @test SDV_TLS_CM_KEYUPDATE_FUNC_TC001
* @title HITLS_TLS_Interface_SDV_23_0_5_102
* @precon nan
* @brief
* 1. Set the version number to tls1.3. After the connection is established, invoke the HITLS_GetKeyUpdateType interface.
* Expected result 1 is obtained.
* 2. Set the version number to tls1.3. After the connection is created, call hitls_keyupdate successfully, and then call the
* HITLS_GetKeyUpdateType interface. Expected result 2 is obtained.
* 3. Set the version number to tls1.3. After the connection is created, call the hitls_keyupdate interface to construct an
* I/O exception. If the interface fails to be called, call the HITLS_GetKeyUpdateType interface again. Expected
* result 3 is obtained.
* @expect
* 1. The return value is 255.
* 2. The return value is 255.
* 3. The return value is the configured keyupdate type.
@ */
/* BEGIN_CASE */
void SDV_TLS_CM_KEYUPDATE_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetKeyUpdateType(client->ssl);
ASSERT_EQ(ret, HITLS_KEY_UPDATE_REQ_END);
ret = HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_NOT_REQUESTED);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_SUCCESS);
ret = HITLS_GetKeyUpdateType(client->ssl);
ASSERT_EQ(ret, HITLS_KEY_UPDATE_REQ_END);
FuncStubInfo tmpRpInfo = {0};
STUB_Replace(&tmpRpInfo, BSL_UIO_Write, STUB_BSL_UIO_Write);
ret = HITLS_KeyUpdate(client->ssl, HITLS_UPDATE_REQUESTED);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_ERR_IO_EXCEPTION);
ret = HITLS_GetKeyUpdateType(client->ssl);
ASSERT_EQ(ret, HITLS_UPDATE_REQUESTED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
STUB_Reset(&tmpRpInfo);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface/test_suite_sdv_hlt_tls_cm_1.c | C | unknown | 4,979 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include "crypt.h"
#include "hitls_crypt_type.h"
#include "hitls_crypt_init.h"
#define PRF_OUT_LEN 48
/* END_HEADER */
/* BEGIN_CASE */
void SDV_TLS_CRYPT_PRF_TC001(int hashAlgo, Hex *secret, Hex *label, Hex *seed, Hex *expect)
{
CRYPT_KeyDeriveParameters input = {0};
input.hashAlgo = hashAlgo;
input.secret = (uint8_t *)secret->x;
input.secretLen = secret->len;
input.label = (uint8_t *)label->x;
input.labelLen = label->len;
input.seed = (uint8_t *)seed->x;
input.seedLen = seed->len;
input.libCtx = NULL;
input.attrName = NULL;
uint8_t out[PRF_OUT_LEN] = {0};
HITLS_CryptMethodInit();
ASSERT_TRUE(PRF_OUT_LEN <= expect->len);
ASSERT_EQ(SAL_CRYPT_PRF(&input, out, PRF_OUT_LEN), HITLS_SUCCESS);
ASSERT_COMPARE("result cmp", out, PRF_OUT_LEN, expect->x, PRF_OUT_LEN);
EXIT:
return;
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface/test_suite_sdv_tls_crypt.c | C | unknown | 1,433 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "hitls.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "tls.h"
#include "hs.h"
#include "hs_ctx.h"
#include "hs_state_recv.h"
#include "conn_init.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "recv_process.h"
#include "stub_replace.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "simulate_io.h"
#include "parser_frame_msg.h"
#include "pack_frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "cert.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "hlt.h"
#include "sctp_channel.h"
#include "logger.h"
#define READ_BUF_SIZE (18 * 1024) /* Maximum length of the read message buffer */
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isServerExtendMasterSecret;
bool isSupportRenegotiation; /* Renegotiation support flag */
bool needStopBeforeRecvCCS; /* CCS test, so that the TRY_RECV_FINISH stops before the CCS message is received */
} HandshakeTestInfo;
int32_t SendHelloReq(HITLS_Ctx *ctx)
{
/** Initialize the message buffer. */
uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
size_t len = HS_MSG_HEADER_SIZE;
/** Write records. */
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
#define TEST_CLIENT_SEND_FAIL 1
void TestSetCertPath(HLT_Ctx_Config *ctxConfig, char *SignatureType)
{
if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA1", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA1")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA256")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA384")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA384_EE_PATH, RSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA512")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA512_EE_PATH, RSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
strlen("CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
strlen("CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
strlen("CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_ECDSA_SHA1", strlen("CERT_SIG_SCHEME_ECDSA_SHA1")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA1_CA_PATH,
ECDSA_SHA1_CHAIN_PATH,
ECDSA_SHA1_EE_PATH,
ECDSA_SHA1_PRIV_PATH,
"NULL",
"NULL");
}
}
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_interface.base.c | C | unknown | 5,477 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_interface */
#include <stdio.h>
#include "hitls_error.h"
#include "hitls_cert.h"
#include "hitls.h"
#include "hitls_func.h"
#include "securec.h"
#include "cert_method.h"
#include "cert_mgr.h"
#include "cert_mgr_ctx.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "session.h"
#include "bsl_list.h"
#include "bsl_sal.h"
#include "bsl_uio.h"
#include "alert.h"
#include "stub_replace.h"
#include "cert_callback.h"
#include "crypt_eal_rand.h"
#include "hitls_crypt_reg.h"
#include "hitls_crypt_init.h"
#include "uio_base.h"
/* END_HEADER */
#define BUF_MAX_SIZE 4096
int32_t g_uiPort = 18886;
static int TestHITLS_VerifyCb(int32_t isPreverifyOk, HITLS_CERT_StoreCtx *storeCtx)
{
(void)isPreverifyOk;
(void)storeCtx;
return 0;
}
static int32_t TestPasswordCb(char *buf, int32_t bufLen, int32_t flag, void *userdata)
{
(void)flag;
char *passwd = NULL;
static char pass[] = "123456";
if (userdata != NULL) {
passwd = userdata;
} else {
passwd = pass;
}
int32_t len = strlen(passwd);
if (len > bufLen) {
return -1;
}
memcpy(buf, passwd, len);
return len;
}
static uint32_t ReadFileBuffer(const char *filePath, char *data)
{
FILE *fd;
uint32_t size;
uint32_t bytes;
fd = fopen(filePath, "rb");
if (fd == NULL) {
return 0;
}
(void)fseek(fd, 0, SEEK_END);
size = (uint32_t)ftell(fd);
rewind(fd);
bytes = (uint32_t)fread(data, 1, size, fd);
(void)fclose(fd);
if (bytes != size) {
return 0;
}
return bytes;
}
/* @
* @test UT_TLS_CERT_CM_SetVerifyDepth_API_TC001
* @title The input parameter of the HITLS_SetVerifyDepth interface is replaced.
* @precon This test case covers the HITLS_SetVerifyDepth, HITLS_GetVerifyDepth
* @brief 1.Invoke the HITLS_SetVerifyDepth interface. The value of ctx is empty and the value of depth is not empty.
* Expected result 1 is obtained.
* 2.Invoke the HITLS_SetVerifyDepth interface. The values of ctx and depth are not empty.
* Expected result 2 is obtained.
* 3.Invoke the HITLS_GetVerifyDepth interface. The ctx field is empty and the depth address is not empty.
* Expected result 1 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns HITLS_NULL_INPUT
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetVerifyDepth_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t depth = 5;
int32_t dep = 0;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetVerifyDepth(NULL, depth) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetVerifyDepth(ctx, depth), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyDepth(ctx, &dep) == HITLS_SUCCESS);
ASSERT_EQ(depth, dep);
ASSERT_TRUE(HITLS_GetVerifyDepth(NULL, &dep) == HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_SetDefaultPasswordCb_FUNC_001
* @title Set the password callback and set the user data defaultPasswdCbUserdata.
* @precon This test case covers the HITLS_CFG_SetDefaultPasswordCb, HITLS_CFG_GetDefaultPasswordCb,
* HITLS_CFG_SetDefaultPasswordCbUserdata, HITLS_CFG_GetDefaultPasswordCbUserdata
* @brief 1. Create a CTX object. Expected result 1 is obtained.
* 2. Set the password callback and set the incorrect user data defaultPasswdCbUserdata.
* Expected result 2 is obtained.
* @expect 1. Created successfully.
* 2. Failed to load the encrypted private key file.
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_SetDefaultPasswordCb_FUNC_001(int version, char *keyFile, char *userdata)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetDefaultPasswordCb(tlsConfig, TestPasswordCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDefaultPasswordCb(tlsConfig) == TestPasswordCb);
ASSERT_TRUE(HITLS_CFG_SetDefaultPasswordCbUserdata(tlsConfig, userdata)== HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDefaultPasswordCbUserdata(tlsConfig) == userdata);
#ifdef HITLS_TLS_FEATURE_PROVIDER
ASSERT_EQ(HITLS_CFG_ProviderLoadKeyFile(tlsConfig, keyFile, "ASN1", NULL),
HITLS_CFG_ERR_LOAD_KEY_FILE);
#else
ASSERT_EQ(HITLS_CFG_LoadKeyFile(tlsConfig, keyFile, TLS_PARSE_FORMAT_ASN1), HITLS_CFG_ERR_LOAD_KEY_FILE);
#endif
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetDefaultPasswordCbUserdata_API_TC001
* @title The input parameter of the HITLS_SetDefaultPasswordCbUserdata interface is replaced.
* @precon This test case covers the HITLS_SetDefaultPasswordCbUserdata, HITLS_GetDefaultPasswordCbUserdata
* @brief 1.Invoke the HITLS_SetDefaultPasswordCbUserdata interface. The value of ctx is empty and the value of
* userdata is not empty. Expected result 1 is obtained.
* 2.Invoke the HITLS_SetDefaultPasswordCbUserdata interface. The values of ctx and userdata are not empty.
* Expected result 2 is obtained.
* 3.Invoke the HITLS_GetDefaultPasswordCbUserdata interface and leave ctx blank. Expected result 3 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetDefaultPasswordCbUserdata_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
char *userData = "123456";
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetDefaultPasswordCbUserdata(NULL, userData) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetDefaultPasswordCbUserdata(ctx, userData) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetDefaultPasswordCbUserdata(NULL) == NULL);
ASSERT_TRUE(HITLS_GetDefaultPasswordCbUserdata(ctx) == userData);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_LoadCertFile_API_TC001
* @title HITLS_CFG_LoadCertFile Loading a Device Certificate from a File
* @precon This test case covers the HITLS_CFG_LoadCertFile, HITLS_CFG_SetDefaultPasswordCbUserdata,
* HITLS_CFG_GetDefaultPasswordCbUserdata, HITLS_CFG_LoadKeyFile
* @brief 1. Apply for a configuration file. Expected result 1 is obtained.
* 2. Load an incorrect path. Expected result 2 is obtained.
* 3. Use the same keyword "123456" for mac word and pass word. Expected result 3 is obtained.
* @expect 1. The application is successful.
* 2. Failed to load the certificate.
* 3. The certificate is loaded successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_LoadCertFile_API_TC001(int version, char *certFile1, char *certFile2, char *keyFile2, char *userdata)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_EQ(
HITLS_CFG_LoadCertFile(tlsConfig, certFile1, TLS_PARSE_FORMAT_ASN1), HITLS_CFG_ERR_LOAD_CERT_FILE);
ASSERT_TRUE(HITLS_CFG_SetDefaultPasswordCbUserdata(tlsConfig, userdata) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDefaultPasswordCbUserdata(tlsConfig) == userdata);
ASSERT_TRUE(HITLS_CFG_LoadCertFile(tlsConfig, certFile2, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
#ifdef HITLS_TLS_FEATURE_PROVIDER
ASSERT_TRUE(HITLS_CFG_ProviderLoadKeyFile(tlsConfig, keyFile2, "ASN1", NULL) == HITLS_SUCCESS);
#else
ASSERT_TRUE(HITLS_CFG_LoadKeyFile(tlsConfig, keyFile2, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
#endif
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_LoadCertBuffer_FUNC_001
* @title HITLS_CFG_LoadCertBuffer Loads and Obtains the Device Certificate from the Buffer
* @precon nan
* @brief 1. Create a CTX object. Expected result 1 is obtained.
* 2. In the local context, the store is not initialized. Invoke HITLS_CFG_GetCertificate to obtain the device
* certificate. Expected result 2 is obtained.
* 3. Call the interface to convert the certificate file into a buffer. Expected result 3 is obtained.
* 4. Delete one byte from the buffer, that is, buffer1. Expected result 4 is obtained.
* 5. Add one byte to the buffer, that is, buffer2. Expected result 5 is obtained.
* 6. Call the interface to set the device certificate through buffer1. Expected result 6 is obtained.
* 7. Call the interface to set the device certificate through buffer2. Expected result 7 is obtained.
* 8. Call the interface to set the device certificate through the buffer. Expected result 8 is obtained.
* 9. Call the interface repeatedly to set the device certificate through the buffer. Expected result 9 is
* obtained.
* @expect 1. Created successfully.
* 2. The obtained content is empty.
* 3. The file is converted successfully.
* 4. Deleted successfully.
* 5. Adding succeeded.
* 6. Failed to load the device certificate.
* 7. Failed to load the device certificate.
* 8. Succeeded in loading the device certificate.
* 9. Failed to load the device certificate.
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_LoadCertBuffer_FUNC_001(int version, char *certPath)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
uint8_t buf[BUF_MAX_SIZE] = {0};
uint32_t bufLen = ReadFileBuffer(certPath, (char *)buf);
ASSERT_TRUE(buf != NULL);
ASSERT_TRUE(bufLen <= BUF_MAX_SIZE);
uint8_t buf2[BUF_MAX_SIZE] = {0};
(void)memcpy_s(buf2, bufLen, buf, bufLen);
buf2[bufLen - 1] = 'b';
buf2[bufLen] = 0;
uint8_t buf1[BUF_MAX_SIZE] = {0};
(void)memcpy_s(buf1, bufLen, buf, bufLen);
buf1[bufLen - 2] = 0;
ASSERT_TRUE(HITLS_CFG_LoadCertBuffer(tlsConfig, buf, bufLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
ASSERT_EQ(
HITLS_CFG_LoadCertBuffer(tlsConfig, buf1, bufLen - 1, TLS_PARSE_FORMAT_ASN1), HITLS_CFG_ERR_LOAD_CERT_BUFFER);
ASSERT_TRUE(HITLS_CFG_LoadCertBuffer(tlsConfig, buf2, bufLen + 1, TLS_PARSE_FORMAT_ASN1) != HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_LoadCertBuffer(tlsConfig, buf, bufLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_LoadCertFile_API_TC001
* @title The input parameter of the HITLS_LoadCertFile interface is replaced.
* @precon nan
* @brief 1.Invoke the HITLS_LoadCertFile interface. The ctx field is empty, the device certificate file name is not
* empty, and the certificate format is PEM. Expected result 1 is obtained.
* 2.Invoke the HITLS_LoadCertFile interface. The ctx parameter is not empty, the device certificate file name
* is not empty, and the certificate format is PEM. Expected result 2 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_LoadCertFile_API_TC001(int version, char *certFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_LoadCertFile(NULL, NULL, TLS_PARSE_FORMAT_ASN1) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_LoadCertFile(ctx, certFile, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_LoadCertBuffer_API_TC001
* @title The input parameter of the HITLS_LoadCertBuffer interface is replaced.
* @precon nan
* @brief 1.Invoke the HITLS_LoadCertBuffer interface. The ctx field is empty, the certificate buffer is not empty, the
* buffer length is the actual buffer length, and the certificate format is PEM. Expected result 1 is
* displayed.
* 2.Invoke the HITLS_LoadCertBuffer interface. Ensure that ctx is not empty, the device certificate file name
* is not empty, and the certificate format is PEM. Expected result 2 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_LoadCertBuffer_API_TC001(int version, char *certFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t certBuffer[BUF_MAX_SIZE] = {0};
uint32_t certBuffLen = ReadFileBuffer(certFile, (char *)certBuffer);
ASSERT_TRUE(certBuffLen <= BUF_MAX_SIZE);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_LoadCertBuffer(NULL, certBuffer, certBuffLen, TLS_PARSE_FORMAT_ASN1) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_LoadCertBuffer(ctx, certBuffer, certBuffLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_LoadKeyBuffer_FUNC_TC001
* @title Load the private key from buffer by using HITLS_CFG_LoadKeyBuffer interface
* @precon nan
* @brief 1. Apply for a configuration file. Expected result 1 is obtained
* 2. Call the API to convert the certificate file into a buffer. Expected result 2 is displayed
* 3. Delete one byte from the buffer, that is, buf1. Expected result 3 is obtained
* 4. Add one byte to the buffer, that is, buf2. Expected result 4
* 5. Call the interface to load the private key through buf1. Expected result 5
* 6. Call the interface to load the private key through buf2. Expected result 6
* 7. Invoke the interface to load the private key through the buffer. Expected result 7
* 8. Invoke the interface repeatedly to load the private key through the buffer. Expected result 8 is obtained
* @expect 1. The application is successful.
* 2. The file is converted successfully.
* 3. The deletion is successful.
* 4. The addition is successful.
* 5. The private key fails to be loaded.
* 6. The private key success to be loaded.
* 7. The private key is loaded.
* 8. The private key fails to be loaded
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_LoadKeyBuffer_FUNC_TC001(int version, char *keyPath)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
uint8_t buf[BUF_MAX_SIZE] = {0};
uint32_t bufLen = ReadFileBuffer(keyPath, (char *)buf);
ASSERT_TRUE(buf != NULL);
ASSERT_TRUE(bufLen <= BUF_MAX_SIZE);
uint8_t buf2[BUF_MAX_SIZE] = {0};
memcpy_s(buf2, bufLen, buf, bufLen);
buf2[bufLen - 1] = 'a';
buf2[bufLen] = 0;
uint8_t buf1[BUF_MAX_SIZE] = {0};
memcpy_s(buf1, bufLen, buf, bufLen);
buf1[bufLen - 2] = 0;
ASSERT_TRUE(HITLS_CFG_LoadKeyBuffer(tlsConfig, buf, bufLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
ASSERT_EQ(
HITLS_CFG_LoadKeyBuffer(tlsConfig, buf1, bufLen - 1, TLS_PARSE_FORMAT_ASN1), HITLS_CFG_ERR_LOAD_KEY_BUFFER);
ASSERT_EQ(
HITLS_CFG_LoadKeyBuffer(tlsConfig, buf2, bufLen + 1, TLS_PARSE_FORMAT_ASN1), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_LoadKeyBuffer(tlsConfig, buf, bufLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_LoadKeyFile_API_TC001
* @title The error input parameter for HITLS_LoadKeyFile
* @precon nan
* @brief 1.Invoke the HITLS_LoadKeyFile interface. The ctx field is empty, the private key file name is not empty,
* and the private key format is PEM. Expected result 1
* 2.Invoke the HITLS_LoadKeyFile interface. The ctx field is not empty. The private key file name is not empty
* and the private key is in PEM format. Expected result 2 is obtained
* @expect 1.Back HITLS_NULL_INPUT
* 2.Back HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_LoadKeyFile_API_TC001(int version, char *keyFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_LoadKeyFile(NULL, keyFile, TLS_PARSE_FORMAT_ASN1) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_LoadKeyFile(ctx, keyFile, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_SetAndGetCert_FUNC_TC001
* @title Set and get verify result
* @precon nan
* @brief 1. Construct the CTX configuration and initialize the session and certificate management. Expected results 1
* 2. Call HITLS_GetVerifyResult to query the peer certificate verification result of the current context. Expected result 2
* 3. Call HITLS_SetVerifyResult to set the peer certificate verification result of the current context. Expected result 3
* 4. Call HITLS_GetVerifyResult to query the peer certificate verification result of the current context. Expected result 4 is obtained
* @expect 1. Initialization succeeded.
* 2. The verification result is 0.
* 3. The setting result is successful.
* 4. The verification result is the set value
@ */
/* BEGIN_CASE */
void UT_TLS_SetAndGetCert_FUNC_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_ERROR result;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetVerifyResult(ctx, &result) == HITLS_SUCCESS);
ASSERT_EQ(result, HITLS_X509_V_OK);
ASSERT_TRUE(HITLS_SetVerifyResult(ctx, HITLS_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyResult(ctx, &result) == HITLS_SUCCESS);
ASSERT_TRUE(result == HITLS_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_LoadKeyBuffer_API_TC001
* @title The error input parameter for HITLS_LoadKeyBuffer
* @precon nan
* @brief 1. Invoke the HITLS_LoadKeyBuffer interface. The ctx field is empty, the private key buffer is not empty,
* the buffer length is the actual buffer length, and the private key format is PEM. Expected result 1 is
* displayed.
* 2. Invoke the HITLS_LoadKeyBuffer interface. The ctx and private key buffer are not empty, the buffer length
* is the actual buffer length, and the private key format is pem. The expected result is 1
* @expect 1. HITLS_NULL_INPUT is returned
* 2. HITLS_SUCCESS is returned
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_LoadKeyBuffer_API_TC001(int version, char *keyFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t keyBuffer[BUF_MAX_SIZE] = {0};
uint32_t keyBuffLen = ReadFileBuffer(keyFile, (char *)keyBuffer);
ASSERT_TRUE(keyBuffLen <= BUF_MAX_SIZE);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_LoadKeyBuffer(NULL, keyBuffer, keyBuffLen, TLS_PARSE_FORMAT_ASN1) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_LoadKeyBuffer(ctx, keyBuffer, keyBuffLen, TLS_PARSE_FORMAT_ASN1) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_SetTlcpCertificate_FUNC_001
* If an unrecognized record type is received, ignore it.
* @title There are only four types of record layers.
* @precon Test Content: Record layer protocols include: handshake, alarm, and password specification change.
* To support protocol extensions, the record layer protocol may support other record types.
* Any new record types should be deassigned in addition to the Content Type values assigned for the types
* described above.
* In this test case, interface HITLS_CFG_SetTlcpCertificate, HITLS_CFG_SetTlcpPrivateKey is invoked at the
* bottom layer.
* @brief After the link is set up, the server receives abnormal messages (the recordType is 99) after receiving
* app data. The server is expected to return an alert.
* @expect 1. HITLS_REC_ERR_RECV_UNEXPECTED_MSG is returned
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_SetTlcpCertificate_FUNC_001(void)
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(tlsConfig != NULL);
uint16_t cipherSuite[] = {HITLS_ECDHE_SM4_CBC_SM3, HITLS_ECC_SM4_CBC_SM3};
HITLS_CFG_SetCipherSuites(tlsConfig, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, true);
server = FRAME_CreateTLCPLink(tlsConfig, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t dataBuf[] = "Hello World!";
uint8_t readBuf[READ_BUF_SIZE];
uint32_t readbytes;
uint32_t writeLen;
ASSERT_EQ(HITLS_Write(client->ssl, dataBuf, sizeof(dataBuf), &writeLen), HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
ioServerData->recMsg.msg[0] = 0x99u;
ASSERT_EQ(HITLS_Read(server->ssl, readBuf, READ_BUF_SIZE, &readbytes), HITLS_REC_ERR_RECV_UNEXPECTED_MSG);
ALERT_Info info = { 0 };
ALERT_GetInfo(server->ssl, &info);
ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CFG_SetVerifyCb_API_TC001
* @title HITLS_CFG_SetVerifyCb interface input parameter test
* @precon This test case covers the HITLS_CFG_SetVerifyCb, HITLS_CFG_GetVerifyCb
* @brief 1. Invoke the HITLS_CFG_SetVerifyCb interface. Input empty tlsConfig and non-empty certificate verification
* callback. Expected result 1
* 2. Invoke the HITLS_CFG_SetVerifyCb interface. Input non-empty tlsConfig and non-empty certificate
* verification callback. Expected result 3
* 3. Invoke the HITLS_CFG_GetVerifyCb interface. Input empty tlsConfig, Expected result 2
* 4. Invoke the HITLS_CFG_SetVerifyCb interface. Input empty tlsConfig->certMgrCtx, and non-empty certificate
* verification callback, Expected result 1
* 5. Invoke the HITLS_CFG_GetVerifyCb interface. Input empty tlsConfig->certMgrCtx, Expected result 2
* Expected result 2
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return NULL
* 3. Return HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CFG_SetVerifyCb_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetVerifyCb(NULL, TestHITLS_VerifyCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetVerifyCb(tlsConfig, TestHITLS_VerifyCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetVerifyCb(tlsConfig) == TestHITLS_VerifyCb);
ASSERT_TRUE(HITLS_CFG_GetVerifyCb(NULL) == NULL);
SAL_CERT_MgrCtxFree(tlsConfig->certMgrCtx);
tlsConfig->certMgrCtx = NULL;
ASSERT_TRUE(HITLS_CFG_SetVerifyCb(tlsConfig, TestHITLS_VerifyCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetVerifyCb(tlsConfig) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetVerifyCb_API_TC001
* @title HITLS_SetVerifyCb interface input parameter test
* @precon This test case covers the HITLS_SetVerifyCb, HITLS_GetVerifyCb
* @brief 1.Invoke the HITLS_SetVerifyCb interface. Input empty ctx and non-empty certificate verification
* callback. Expected result 1
* 2.Invoke the HITLS_SetVerifyCb interface. Input non-empty ctx and non-empty certificate verification
* callback. Expected result 2
* 3.Invoke the HITLS_GetVerifyCb interface. Input empty ctx, Expected result 3
* @expect 1.Return HITLS_NULL_INPUT
* 2.Return HITLS_SUCCESS
* 3.Return NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetVerifyCb_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetVerifyCb(NULL, TestHITLS_VerifyCb) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetVerifyCb(ctx, TestHITLS_VerifyCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyCb(NULL) == NULL);
ASSERT_TRUE(HITLS_GetVerifyCb(ctx) == TestHITLS_VerifyCb);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/*
* @test UT_TLS_CERT_GET_CERTIFICATE_API_TC001
*
* @title Overwrite the input parameter of the HITLS_GetCertificate interface.
*
* @brief
* 1. Invoke the HITLS_GetCertificate interface and leave ctx blank. Expected result 1.
* 2. Invoke the HITLS_GetPeerCertificate interface and leave ctx blank. Expected result 1.
* 3. Invoke the HITLS_GetPeerCertificate interface. The value of ctx is not empty and the value of ctx->session is empty.
* Expected result 1.
* 4. Invoke the HITLS_GetPeerCertChain interface and leave ctx blank. Expected result 1.
* 5. Invoke the HITLS_GetPeerCertChain interface. The value of ctx is not empty and the value of ctx->session is empty.
* Expected result 1 .
* @expect 1. Return NULL.
* @prior Level 1
* @auto TRUE
*/
/* BEGIN_CASE */
void UT_TLS_CERT_GET_CERTIFICATE_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetCertificate(NULL) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertificate(NULL) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertChain(NULL) == NULL);
ctx->session = NULL;
ASSERT_TRUE(HITLS_GetPeerCertificate(ctx) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertChain(ctx) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
void StubListDataDestroy(void *data)
{
BSL_SAL_FREE(data);
return;
}
/* @
* @test UT_TLS_CERT_GET_CALIST_FUNC_TC001
*
* @title Obtain the peer certificate chain and trusted CA list.
*
* @brief
* 1. Construct the CTX configuration. Expected result 1.
* 2. Invoke HITLS_GetPeerCertChain to obtain the peer certificate chain. Expected result 2.
* 3. Configure a certificate management instance for the session instance. Expected result 3.
* 4. Add the session instance to the SSL instance. Expected result 4.
* 5. If no certificate is loaded to the peer end, call HITLS_GetPeerCertificate to obtain the peer certificate.
* Expected result 5.
* 6. Create a peer certificate management instance and a certificate chain. Expected result 6.
* 7. Add the created certificates to the certificate linked list one by one. Expected result 7.
* 8. Invoke HITLS_GetPeerCertChain to obtain the peer certificate chain. Expected result 8.
* 9. Invoke the HITLS_GetPeerCAList client certificate authority (CA) list. Expected result 9.
* @expect
* 1. The creation is successful.
* 2. Obtaining failed. The session is empty.
* 3. The setting is successful, and the interface returns 0.
* 4. If the setting is successful, the interface returns 0.
* 5. Failed to obtain the certificate. The certificate is empty.
* 6. The peerCert and certificate chain are successfully created.
* 7. The interface returns 0.
* 8. The certificate successfully. The obtained peer certificate chain is correct. The obtained cert is
* correct.
* 9. The obtained CA certificate list is correct. The obtained cert is correct.
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_GET_CALIST_FUNC_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ctx->isClient = true;
HITLS_Session *session = HITLS_SESS_New();
ASSERT_TRUE(session != NULL);
CERT_Pair *peerCert = (CERT_Pair *)BSL_SAL_Calloc(1u, sizeof(CERT_Pair));
HITLS_CERT_X509 *cert1 = (HITLS_CERT_X509 *)BSL_SAL_Calloc(1u, sizeof(HITLS_CERT_X509));
HITLS_CERT_X509 *cert2 = (HITLS_CERT_X509 *)BSL_SAL_Calloc(1u, sizeof(HITLS_CERT_X509));
HITLS_CERT_X509 *cert3 = (HITLS_CERT_X509 *)BSL_SAL_Calloc(1u, sizeof(HITLS_CERT_X509));
peerCert->chain = (HITLS_CERT_Chain *)BSL_LIST_New(sizeof(HITLS_CERT_X509 *));
ASSERT_TRUE(peerCert->chain != NULL);
HITLS_CERT_Chain *certChain = peerCert->chain;
int32_t ret = BSL_LIST_AddElement((BslList *)certChain, cert1, BSL_LIST_POS_END);
ASSERT_TRUE(ret == 0);
ret = BSL_LIST_AddElement((BslList *)certChain, cert2, BSL_LIST_POS_END);
ASSERT_TRUE(ret == 0);
ret = BSL_LIST_AddElement((BslList *)certChain, cert3, BSL_LIST_POS_END);
ASSERT_TRUE(ret == 0);
ret = SESS_SetPeerCert(session, peerCert, false);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetSession(ctx, session) == HITLS_SUCCESS);
HITLS_CERT_Chain *getCertChain = HITLS_GetPeerCertChain(ctx);
ASSERT_TRUE(getCertChain != NULL);
HITLS_TrustedCAList *tmpCAList = ctx->peerInfo.caList;
HITLS_TrustedCANode *newNode1 = (HITLS_TrustedCANode *)BSL_SAL_Calloc(1, sizeof(HITLS_TrustedCANode));
ASSERT_TRUE(newNode1 != NULL);
newNode1->caType = HITLS_TRUSTED_CA_X509_NAME;
newNode1->data = NULL;
newNode1->dataSize = 0;
HITLS_TrustedCANode *newNode2 = (HITLS_TrustedCANode *)BSL_SAL_Calloc(1, sizeof(HITLS_TrustedCANode));
ASSERT_TRUE(newNode2 != NULL);
newNode2->caType = HITLS_TRUSTED_CA_X509_NAME;
newNode2->data = NULL;
newNode2->dataSize = 0;
ret = BSL_LIST_AddElement((BslList *)tmpCAList, newNode1, BSL_LIST_POS_END);
ASSERT_TRUE(ret == 0);
ret = BSL_LIST_AddElement((BslList *)tmpCAList, newNode2, BSL_LIST_POS_END);
ASSERT_TRUE(ret == 0);
HITLS_TrustedCAList *caList = HITLS_GetPeerCAList(ctx);
ASSERT_TRUE(caList != NULL);
ASSERT_EQ(caList->count, 2);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
BSL_LIST_DeleteAll((BslList *)peerCert->chain, StubListDataDestroy);
HITLS_SESS_Free(session);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_LOAD_FILE_FUNC_TC001
* @title HITLS_CFG_LoadCrlFile interface functional test
* @precon This test case covers the HITLS_CFG_LoadCrlFile interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_LOAD_FILE_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Test successful CRL file loading
int32_t ret = HITLS_CFG_LoadCrlFile(config, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameters
ret = HITLS_CFG_LoadCrlFile(NULL, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
ret = HITLS_CFG_LoadCrlFile(config, NULL, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
ret = HITLS_CFG_LoadCrlFile(config, "", TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_LOAD_BUFFER_FUNC_TC001
* @title HITLS_CFG_LoadCrlBuffer interface functional test
* @precon This test case covers the HITLS_CFG_LoadCrlBuffer interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_LOAD_BUFFER_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Read CRL file content
FILE *file = fopen(crlPath, "rb");
ASSERT_TRUE(file != NULL);
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
ASSERT_TRUE(fileSize > 0);
uint8_t *crlData = (uint8_t *)BSL_SAL_Malloc(fileSize);
ASSERT_TRUE(crlData != NULL);
size_t bytesRead = fread(crlData, 1, fileSize, file);
fclose(file);
ASSERT_EQ(bytesRead, (size_t)fileSize);
// Test successful CRL buffer loading
int32_t ret = HITLS_CFG_LoadCrlBuffer(config, crlData, fileSize, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameters
ret = HITLS_CFG_LoadCrlBuffer(NULL, crlData, fileSize, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
ret = HITLS_CFG_LoadCrlBuffer(config, NULL, fileSize, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
ret = HITLS_CFG_LoadCrlBuffer(config, crlData, 0, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
BSL_SAL_Free(crlData);
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_CTX_LOAD_FILE_FUNC_TC001
* @title HITLS_LoadCrlFile interface functional test
* @precon This test case covers the HITLS_LoadCrlFile runtime context interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_CTX_LOAD_FILE_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Test successful CRL file loading in context
int32_t ret = HITLS_LoadCrlFile(ctx, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameters
ret = HITLS_LoadCrlFile(NULL, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_CTX_LOAD_BUFFER_FUNC_TC001
* @title HITLS_LoadCrlBuffer interface functional test
* @precon This test case covers the HITLS_LoadCrlBuffer runtime context interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_CTX_LOAD_BUFFER_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Read CRL file content
FILE *file = fopen(crlPath, "rb");
ASSERT_TRUE(file != NULL);
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
ASSERT_TRUE(fileSize > 0);
uint8_t *crlData = (uint8_t *)BSL_SAL_Malloc(fileSize);
ASSERT_TRUE(crlData != NULL);
size_t bytesRead = fread(crlData, 1, fileSize, file);
fclose(file);
ASSERT_EQ(bytesRead, (size_t)fileSize);
// Test successful CRL buffer loading in context
int32_t ret = HITLS_LoadCrlBuffer(ctx, crlData, fileSize, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameters
ret = HITLS_LoadCrlBuffer(NULL, crlData, fileSize, TLS_PARSE_FORMAT_ASN1);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
BSL_SAL_Free(crlData);
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_CFG_CLEAR_FUNC_TC001
* @title HITLS_CFG_ClearVerifyCrls interface functional test
* @precon This test case covers the HITLS_CFG_ClearVerifyCrls interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_CFG_CLEAR_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Load CRL file first
int32_t ret = HITLS_CFG_LoadCrlFile(config, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test successful CRL clearing
ret = HITLS_CFG_ClearVerifyCrls(config);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Load CRL again to verify clearing worked
ret = HITLS_CFG_LoadCrlFile(config, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameter
ret = HITLS_CFG_ClearVerifyCrls(NULL);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_CTX_CLEAR_FUNC_TC001
* @title HITLS_ClearVerifyCrls interface functional test
* @precon This test case covers the HITLS_ClearVerifyCrls runtime context interface
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_CTX_CLEAR_FUNC_TC001(void)
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLSConfig();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Load CRL file first
int32_t ret = HITLS_LoadCrlFile(ctx, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test successful CRL clearing
ret = HITLS_ClearVerifyCrls(ctx);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Load CRL again to verify clearing worked
ret = HITLS_LoadCrlFile(ctx, crlPath, TLS_PARSE_FORMAT_ASN1);
ASSERT_EQ(ret, HITLS_SUCCESS);
// Test invalid parameter
ret = HITLS_ClearVerifyCrls(NULL);
ASSERT_NE(ret, HITLS_SUCCESS);
EXIT:
HITLS_Free(ctx);
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CRL_VERIFICATION_HANDSHAKE_TC001
* @title CRL verification in TLS handshake functional test
* @precon This test case covers CRL functionality during TLS handshake process
@ */
/* BEGIN_CASE */
void UT_TLS_CRL_VERIFICATION_HANDSHAKE_TC001(void)
{
HitlsInit();
FRAME_Init();
// Test data paths
const char *serverCertPath = "../testdata/tls/certificate/der/ed25519/ed25519.end.der";
const char *serverKeyPath = "../testdata/tls/certificate/der/ed25519/ed25519.end.key.der";
const char *intCaPath = "../testdata/tls/certificate/der/ed25519/ed25519.intca.der";
const char *caCertPath = "../testdata/tls/certificate/der/ed25519/ed25519.ca.der";
const char *crlPath = "../testdata/tls/certificate/der/ed25519/ed25519.crl.der";
// Test 1: Handshake without CRL - should succeed
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
// Configure server with certificate and key
ASSERT_EQ(HITLS_CFG_LoadCertFile(config, serverCertPath, TLS_PARSE_FORMAT_ASN1), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_LoadKeyFile(config, serverKeyPath, TLS_PARSE_FORMAT_ASN1), HITLS_SUCCESS);
HITLS_CERT_X509 *caCert = HITLS_CFG_ParseCert(config, (const uint8_t *)caCertPath, strlen(caCertPath), TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(caCert != NULL);
ASSERT_EQ(HITLS_CFG_AddCertToStore(config, caCert, TLS_CERT_STORE_TYPE_DEFAULT, false), HITLS_SUCCESS);
caCert = HITLS_CFG_ParseCert(config, (const uint8_t *)intCaPath, strlen(intCaPath), TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(caCert != NULL);
ASSERT_EQ(HITLS_CFG_AddCertToStore(config, caCert, TLS_CERT_STORE_TYPE_DEFAULT, false), HITLS_SUCCESS);
FRAME_LinkObj *client = FRAME_CreateLinkBase(config, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLinkBase(config, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
// Attempt handshake without CRL - should succeed
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
server = FRAME_CreateLinkBase(config, BSL_UIO_TCP, false);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(HITLS_CFG_LoadCrlFile(config, crlPath, TLS_PARSE_FORMAT_ASN1), HITLS_SUCCESS);
client = FRAME_CreateLinkBase(config, BSL_UIO_TCP, false);
ASSERT_TRUE(client != NULL);
ASSERT_NE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetVerifyFlags_API_TC001
* @title The input parameter of the HITLS_CFG_SetVerifyFlags interface is replaced.
* @precon This test case covers the HITLS_CFG_SetVerifyFlags, HITLS_CFG_GetVerifyFlags
* @brief 1.Invoke the HITLS_CFG_SetVerifyFlags interface. The value of ctx is empty and the value of flags is 5.
* Expected result 1 is obtained.
* 2.Invoke the HITLS_CFG_SetVerifyFlags interface. The values of ctx and flags are not empty.
* Expected result 2 is obtained.
* 3.Invoke the HITLS_CFG_GetVerifyFlags interface. The ctx field is empty and the ff address is not empty.
* Expected result 1 is obtained.
* 4.Invoke the HITLS_CFG_GetVerifyFlags interface. The values of ctx and ff address are not empty.
* Expected result 2 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetVerifyFlags_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t flags = 5;
uint32_t ff = 0;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetVerifyFlags(NULL, flags) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetVerifyFlags(tlsConfig, flags), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetVerifyFlags(tlsConfig, &ff) == HITLS_SUCCESS);
ASSERT_EQ(flags, ff);
ASSERT_TRUE(HITLS_CFG_GetVerifyFlags(NULL, &ff) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetVerifyFlags(tlsConfig, NULL) == HITLS_NULL_INPUT);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
flags = 10;
uint32_t ff2 = 0;
ASSERT_TRUE(HITLS_SetVerifyFlags(NULL, flags) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetVerifyFlags(ctx, flags), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyFlags(ctx, &ff2) == HITLS_SUCCESS);
ASSERT_EQ((flags | ff), ff2);
ASSERT_TRUE(HITLS_GetVerifyFlags(NULL, &ff2) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetVerifyFlags(ctx, NULL) == HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_frame_cert_interface.c | C | unknown | 43,911 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
/* INCLUDE_BASE test_suite_interface */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <linux/limits.h>
#include <unistd.h>
#include <stdbool.h>
#include "hitls_error.h"
#include "hitls_cert.h"
#include "hitls.h"
#include "hitls_func.h"
#include "securec.h"
#include "cert_method.h"
#include "cert_mgr.h"
#include "cert_mgr_ctx.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "session.h"
#include "bsl_sal.h"
#include "bsl_uio.h"
#include "alert.h"
#include "stub_replace.h"
#include "cert_callback.h"
#include "crypt_eal_rand.h"
#include "hitls_crypt_reg.h"
#include "hitls_crypt_init.h"
#include "uio_base.h"
#include "hlt_type.h"
#include "hlt.h"
#include "hitls_cert_type.h"
#include "hitls_type.h"
#include "hitls_cert_reg.h"
#include "hitls_config.h"
#include "hitls_cert_init.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "logger.h"
#include "tls_config.h"
#include "tls.h"
#include "crypt_algid.h"
#include "crypt_errno.h"
#include "bsl_obj.h"
#include "bsl_errno.h"
#include "hitls_x509_adapt.h"
#include "hitls_pki_x509.h"
/* END_HEADER */
#define BUF_MAX_SIZE 4096
int32_t g_uiPort = 18886;
HITLS_CERT_X509 *HiTLS_X509_LoadCertFile(HITLS_Config *tlsCfg, const char *file);
/* @
* @test UT_TLS_CERT_CM_SetVerifyStore_API_TC001
* @title The input parameters of the HITLS_SetVerifyStore and HITLS_GetVerifyStore interfaces are replaced.
* @precon nan
* @brief 1.Invoke the HITLS_SetVerifyStore interface. The value of ctx is empty and the value of store for the CA
* certificate is not empty. Perform shallow copy. Expected result 1 is obtained.
* 2.Invoke the HITLS_SetVerifyStore interface. Set ctx and CA certificate store to a value that is not empty.
* Expected result 2 is obtained.
* 3.Invoke the HITLS_GetVerifyStore interface and leave tlsConfig blank. Expected result 3 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetVerifyStore_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_CERT_Store *verifyStore = HITLS_X509_Adapt_StoreNew();
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetVerifyStore(NULL, verifyStore, false) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetVerifyStore(ctx, verifyStore, true), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyStore(ctx) == verifyStore);
ASSERT_TRUE(HITLS_GetVerifyStore(NULL) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
HITLS_X509_StoreCtxFree(verifyStore);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetChainStore_API_TC001
* @title The input parameters of the HITLS_SetChainStore and HITLS_GetChainStore interfaces are replaced.
* @precon This test case covers the HITLS_SetChainStore、HITLS_GetChainStore
* @brief 1.Invoke the HITLS_SetChainStore interface. The ctx field is empty and the certificate chain store is not
* empty. Perform shallow copy. Expected result 1 is obtained.
* 2.Invoke the HITLS_SetChainStore interface. The value of ctx is not empty and the value of store in the
* certificate chain is not empty. Perform shallow copy. Expected result 2 is obtained.
* 3.Invoke the HITLS_GetChainStore interface and leave tlsConfig empty. Expected result 3 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetChainStore_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_CERT_Store *chainStore = HITLS_X509_Adapt_StoreNew();
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetChainStore(NULL, chainStore, false) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetChainStore(ctx, chainStore, false), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetChainStore(ctx) == chainStore);
ASSERT_TRUE(HITLS_GetChainStore(NULL) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetCertStore_API_TC001
* @title The input parameter of the HITLS_SetCertStore interface is replaced.
* @precon This test case covers the HITLS_SetCertStore、HITLS_GetCertStore
* @brief 1.Invoke the HITLS_SetCertStore interface. The value of ctx is empty, and the value of store for the trust
* certificate is not empty. Perform shallow copy. Expected result 1 is obtained.
* 2.Invoke the HITLS_SetCertStore interface. Ensure that ctx and store of the trust certificate are not empty.
* Perform shallow copy. Expected result 2 is obtained.
* 3.Invoke the HITLS_GetCertStore interface and leave ctx blank. Expected result 3 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetCertStore_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_CERT_Store *certStore = HITLS_X509_Adapt_StoreNew();
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetCertStore(NULL, certStore, false) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetCertStore(ctx, certStore, false), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetCertStore(ctx) == certStore);
ASSERT_TRUE(HITLS_GetCertStore(NULL) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_CM_SetDefaultPasswordCbUserdata_API_TC001
* @title The input parameter of the HITLS_SetDefaultPasswordCbUserdata interface is replaced.
* @precon This test case covers the HITLS_SetDefaultPasswordCbUserdata、HITLS_GetDefaultPasswordCbUserdata
* @brief 1.Invoke the HITLS_SetDefaultPasswordCbUserdata interface. The value of ctx is empty and the value of
* userdata is not empty. Expected result 1 is obtained.
* 2.Invoke the HITLS_SetDefaultPasswordCbUserdata interface. The values of ctx and userdata are not empty.
* Expected result 2 is obtained.
* 3.Invoke the HITLS_GetDefaultPasswordCbUserdata interface and leave ctx blank. Expected result 3 is obtained.
* @expect 1.Returns HITLS_NULL_INPUT
* 2.Returns HITLS_SUCCESS
* 3.Returns NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_CM_SetDefaultPasswordCbUserdata_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_CERT_Store *certStore = HITLS_X509_Adapt_StoreNew();
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetCertStore(NULL, certStore, false) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetCertStore(ctx, certStore, false), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetCertStore(ctx) == certStore);
ASSERT_TRUE(HITLS_GetCertStore(NULL) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CERT_SetGetAndCheckPrivateKey_API_TC001
* @title The error input parameter for HITLS_SetPrivateKey
* @precon nan
* @brief 1.Invoke the HITLS_SetPrivateKey interface. Ensure that ctx is empty and privatekey is not empty.
* Perform deep copy. Expected result 1
* 2.Invoke the HITLS_SetPrivateKey interface. Ensure that ctx is not empty and privatekey is not empty.
* In shallow copy mode, expected result 2
* 3.Invoke the HITLS_GetPrivateKey interface. If ctx is empty, expected result 3
* 4.Invoke the HITLS_CheckPrivateKey interface. If ctx is empty, expected result 1 is obtained
* @expect 1.Back HITLS_NULL_INPUT
* 2.Back HITLS_SUCCESS
* 3.Back HITLS_NULL_INPUT
@ */
/* BEGIN_CASE */
void UT_TLS_CERT_SetGetAndCheckPrivateKey_API_TC001(int version, char *keyFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
#ifdef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_Key *privatekey = HITLS_X509_Adapt_ProviderKeyParse(tlsConfig, (const uint8_t *)keyFile, sizeof(keyFile),
TLS_PARSE_TYPE_FILE, "ASN1", NULL);
#else
HITLS_CERT_Key *privatekey = HITLS_X509_Adapt_KeyParse(tlsConfig, (const uint8_t *)keyFile, sizeof(keyFile),
TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
#endif
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetPrivateKey(NULL, privatekey, true) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetPrivateKey(ctx, privatekey, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetPrivateKey(NULL) == NULL);
ASSERT_TRUE(HITLS_GetPrivateKey(ctx) != NULL);
ASSERT_TRUE(HITLS_CheckPrivateKey(NULL) == HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_HITLS_CERT_ClearChainCerts_API_TC001
* @title HITLS_ClearChainCerts interface input parameter test
* @precon nan
* @brief 1. Invoke HITLS_ClearChainCerts interface. Input empty ctx. Expected result 1
* 2. Invoke HITLS_ClearChainCerts interface. Input non-empty ctx. Expected result 2
* 3. Invoke HITLS_ClearChainCerts interface. Input non-empty ctx and empty tlsConfig->certMgrCtx,
* Expected result 1
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_HITLS_CERT_ClearChainCerts_API_TC001(int version, char *certFile, char *addCertFile)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
HITLS_CERT_X509 *cert = HiTLS_X509_LoadCertFile(tlsConfig, certFile);
ASSERT_TRUE(cert != NULL);
HITLS_CERT_X509 *addCert = HiTLS_X509_LoadCertFile(tlsConfig, addCertFile);
ASSERT_TRUE(addCert != NULL);
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_TRUE(HITLS_CFG_SetCertificate(tlsConfig, cert, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddChainCert(tlsConfig, addCert, false) == HITLS_SUCCESS);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_ClearChainCerts(NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_ClearChainCerts(ctx) == HITLS_SUCCESS);
SAL_CERT_MgrCtxFree(ctx->config.tlsConfig.certMgrCtx);
ctx->config.tlsConfig.certMgrCtx = NULL;
ASSERT_EQ(HITLS_ClearChainCerts(ctx), HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_frame_cert_interface_2.c | C | unknown | 11,572 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdlib.h>
#include <unistd.h>
#include "securec.h"
#include "bsl_sal.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "bsl_uio.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_reg.h"
#include "hitls_config.h"
#include "tls_config.h"
#include "hitls.h"
#include "hs_common.h"
#include "hitls_func.h"
#include "tls.h"
#include "conn_init.h"
#include "crypt_errno.h"
#include "stub_replace.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "rec_wrapper.h"
#include "hlt_type.h"
#include "hlt.h"
#include "process.h"
#include "hitls_crypt_init.h"
#include "bsl_list.h"
#include "simulate_io.h"
#include "alert.h"
#include "crypt_default.h"
#include "stub_crypt.h"
#include "hitls_crypt.h"
#define READ_BUF_SIZE 18432
#define MAX_CERT_LIST 4294967295
#define MIN_CERT_LIST 0
#define DEFAULT_SECURITYLEVEL 0
/* END_HEADER */
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
HITLS_Config *config;
int32_t ret;
switch (ver) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
case HITLS_VERSION_DTLS12:
config = HITLS_CFG_NewDTLS12Config();
ret = HITLS_CFG_SetCheckKeyUsage(config, false);
if (ret != HITLS_SUCCESS) {
return NULL;
}
return config;
default:
return NULL;
}
}
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isSupportSessionTicket;
bool needStopBeforeRecvCCS;
} HandshakeTestInfo;
static uint8_t g_clientRandom[RANDOM_SIZE];
static uint8_t g_serverRandom[RANDOM_SIZE];
/* @
* @test UT_TLS_CM_SET_GET_UIO_API_TC001
* @title Test the HITLS_SetUio and HITLS_GetUio interfaces
* @precon nan
* @brief HITLS_SetUio
* 1. Input an empty connection context and a non-empty UIO. Expected result 1 is obtained
* 2. Input an empty connection context and an empty UIO. Expected result 1 is obtained
* 3. Input a non-empty connection context and an empty UIO. Expected result 1 is obtained
* 4. Input a non-empty connection context and a non-empty UIO. Expected result 2 is obtained
* HITLS_GetUio
* 1. Input an empty connection context. Expected result 3 is obtained
* 2. Input a non-empty connection context. Expected result 4 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
* 3. Return a null pointer
* 4. Return connection uio
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_UIO_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx* ctx = HITLS_New(tlsConfig);
BSL_UIO *uio = NULL;
BSL_UIO *uio2;
int32_t ret;
uio = BSL_UIO_New(BSL_UIO_TcpMethod());
ret = HITLS_SetUio(NULL, uio);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetUio(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetUio(ctx, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetUio(ctx, uio);
ASSERT_TRUE(ret == HITLS_SUCCESS);
uio2 = HITLS_GetUio(NULL);
ASSERT_TRUE(uio2 == NULL);
uio2 = HITLS_GetUio(ctx);
ASSERT_TRUE(uio2 != NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
BSL_UIO_Free(uio);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_READ_UIO_API_TC001
* @title Test the HITLS_SetReadUio, HITLS_GetReadUio interfaces
* @precon nan
* @brief HITLS_SetReadUio
* 1. Input an empty connection context and a non-empty UIO. Expected result 1 is obtained
* 2. Input an empty connection context and an empty UIO. Expected result 1 is obtained
* 2. Input a non-empty connection context and an empty UIO. Expected result 1 is obtained
* 4. Input a non-empty connection context and a non-empty UIO. Expected result 2 is obtained
* HITLS_GetReadUio
* 1. Input an empty connection context. Expected result 3 is obtained
* 2. Input a non-empty connection context. Expected result 4 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
* 3. Return a null pointer
* 4. Return connection uio
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_READ_UIO_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL) ;
HITLS_Ctx* ctx = HITLS_New(tlsConfig);
BSL_UIO *uio = NULL;
BSL_UIO *uio2 = NULL;
int32_t ret;
uio = BSL_UIO_New(BSL_UIO_TcpMethod());
ret = HITLS_SetReadUio(NULL, uio);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetReadUio(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetReadUio(ctx, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetReadUio(ctx, uio);
ASSERT_TRUE(ret == HITLS_SUCCESS);
uio2 = HITLS_GetReadUio(NULL);
ASSERT_TRUE(uio2 == NULL);
uio2 = HITLS_GetReadUio(ctx);
ASSERT_TRUE(uio2 != NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
BSL_UIO_Free(uio);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_ENDPOINT_FUNC_TC001
* @title Invoke HITLS_SetEndPoint after initialization, check whether the state is handshaking
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. After initialization, call HITLS_SetEndPoint and check the state status. Expected result 2 is obtained
* @expect 1. Complete initialization
* 2. state is handshaking
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_ENDPOINT_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
uint32_t ret = HITLS_SetEndPoint(server->ssl, true);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(server->ssl->state, CM_STATE_HANDSHAKING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test The HITLS_SetEndPoint function fails to be invoked during link establishment
* @title UT_TLS_CM_SET_ENDPOINT_FUNC_TC002
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. Invoke HITLS_SetEndPoint during link establishment. Expected result 2 is obtained
* @expect 1. Complete initialization
* 2. Invoking failed
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_ENDPOINT_FUNC_TC002(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_HELLO) == HITLS_SUCCESS);
uint32_t ret = HITLS_SetEndPoint(server->ssl, true);
ASSERT_EQ(ret, HITLS_MSG_HANDLE_STATE_ILLEGAL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test Obtains the maximum writable plaintext length after initialization
* @title UT_TLS_CM_GET_MAXWRITESIZE_FUNC_TC001
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. Invoke HITLS_GetMaxWriteSize to obtain the maximum writable plaintext length.
* Expected result 2 is obtained
* @expect 1. Complete initialization
* 2. Obtain the length successfully, the length is equal to REC_MAX_PLAIN_LENGTH
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_MAXWRITESIZE_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
uint32_t len = 0;
uint32_t ret = CONN_Init(client->ssl);
ASSERT_EQ(ret, HITLS_SUCCESS);
ret = HITLS_GetMaxWriteSize(client->ssl, &len);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(len, REC_MAX_PLAIN_LENGTH);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_USR_DATA_TC001
* @title test HITLS_SetUserData, HITLS_GetUserData interfaces
* @precon nan
* @brief HITLS_SetUserData
* 1. Input an empty connection context and a non-empty userData. Expected result 1 is obtained
* 2. Input an empty connection context and an empty userData. Expected result 1 is obtained
* 3. Input a non-empty connection context and an empty userData. Expected result 2 is obtained
* 4. Input a non-empty connection context and a non-empty userData. Expected result 2 is obtained
* HITLS_GetUserData
* 1. Input an empty connection context. Expected result 4 is obtained
* 2. Input a non-empty connection context. Expected result 3 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
* 3. Return userData
* 4. Return a null pointer
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_USR_DATA_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL) ;
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
int32_t ret;
uint8_t userData[5] = {0};
void *ret2 = HITLS_GetUserData(NULL);
ASSERT_TRUE(ret2 == NULL);
ret = HITLS_SetUserData(NULL, &userData);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetUserData(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetUserData(ctx, NULL);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_SetUserData(ctx, &userData);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_SetUserData(ctx, "userdata");
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret2 = HITLS_GetUserData(ctx);
ASSERT_TRUE(strcmp(ret2, "userdata") == 0);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_USR_DATA_TC001
* @title test HITLS_SESS_SetUserData, HITLS_SESS_GetUserData interfaces
* @precon nan
* @brief HITLS_SESS_GetUserData
* 1. Input an empty connection context and a non-empty userData. Expected result 1 is obtained
* 2. Input an empty connection context and an empty userData. Expected result 1 is obtained
* 3. Input a non-empty connection context and an empty userData. Expected result 2 is obtained
* 4. Input a non-empty connection context and a non-empty userData. Expected result 2 is obtained
* HITLS_SESS_GetUserData
* 1. Input an empty connection context. Expected result 4 is obtained
* 2. Input a non-empty connection context. Expected result 3 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
* 3. Return userData
* 4. Return a null pointer
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SESSION_SET_GET_USR_DATA_API_TC001(void)
{
HitlsInit();
HITLS_Session *session = HITLS_SESS_New();
int32_t ret;
uint8_t userData[5] = {0};
void *ret2 = HITLS_SESS_GetUserData(NULL);
ASSERT_TRUE(ret2 == NULL);
ret = HITLS_SESS_SetUserData(NULL, &userData);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SESS_SetUserData(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SESS_SetUserData(session, NULL);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_SESS_SetUserData(session, &userData);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_SESS_SetUserData(session, "userdata");
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret2 = HITLS_SESS_GetUserData(session);
ASSERT_TRUE(strcmp(ret2, "userdata") == 0);
EXIT:
HITLS_SESS_Free(session);
}
/* END_CASE */
/* @
* @test HITLS_SetShutdownState Set HITLS_SENT_SHUTDOWN to 1 and do not send the close_notify message.
* @title UT_TLS_CM_SET_SHUTDOWN_FUNC_TC001
* @precon nan
* @brief 1. Set HITLS_SENT_SHUTDOWN to 1 and invoke the Hitls_Close interface. Expected result 1 is obtained
* @expect 1. The interface is successfully invoked and the close_notify message is not sent
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_SHUTDOWN_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetShutdownState(client->ssl, 1) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Close(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_CLOSED);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
uint32_t readLen = ioUserData->recMsg.len;
ASSERT_TRUE(readLen == 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_SHUTDOWN_FUNC_TC001
* @title Use HITLS_GetShutdownState to obtain the configured value
* @precon nan
* @brief 1. Set HITLS_SENT_SHUTDOWN to 1 and invoke the HITLS_GetShutdownState interface. Expected result 1
* 2. Set HITLS_SENT_SHUTDOWN to 2 and invoke the HITLS_GetShutdownState interface. Expected result 2
* 3. Set HITLS_SENT_SHUTDOWN to 0 and invoke the HITLS_GetShutdownState interface. Expected result 3
* @expect 1. Obtain value 1
* 2. Obtain value 2
* 3. Obtain value 0.
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_SHUTDOWN_FUNC_TC001(int version)
{
int32_t ret;
uint32_t mode;
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
CONN_Init(ctx);
ASSERT_TRUE(ctx != NULL);
for (uint32_t i = 0; i <= 2; i++) {
ret = HITLS_SetShutdownState(ctx, i);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_GetShutdownState(ctx, &mode);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(mode == i);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_NEGOTIATED_VERSION_FUNC_TC001
* @title HITLS_GetNegotiatedVersion Interface in TLS1.2 Scenario and TLS1.3 Scenario
* @precon nan
* @brief 1. Set the protocol version to TLS1.2 or TLS1.3. After initialization, invoke the HITLS_GetNegotiatedVersion
* interface to obtain the negotiated version number. Expected result 1 is obtained
* 2. Set the protocol version to TLS1.2 or TLS1.3. After the connection is established, invoke the
* HITLS_GetNegotiatedVersion interface to obtain the negotiated version number. Expected result 2 is obtained
* @expect 1. obtained value is 0
* 2. obtained value is tls1.2/tls1.3
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_NEGOTIATED_VERSION_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
uint16_t negoVersion = HITLS_VERSION_TLCP_DTLCP11;
ret = HITLS_GetNegotiatedVersion(client->ssl, &negoVersion);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(negoVersion, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetNegotiatedVersion(client->ssl, &negoVersion);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(negoVersion, version);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_MAX_PROTO_VERSION_API_TC001
* @title test HITLS_SetMaxProtoVersion, HITLS_GetMaxProtoVersion interfaces
* @precon nan
* @brief HITLS_SetMaxProtoVersion
* 1. Input an empty connection context. Expected result 1 is obtained
* 2. Input a non-empty connection context and version is too low. Expected result 2 is obtained
* 3. Input a non-empty connection context and normal version. Expected result 3 is obtained
* HITLS_GetMaxProtoVersion
* 1. Input an empty connection context and a null pointer. Expected result 1 is obtained
* 2. Input an empty connection context and a non-empty pointer. Expected result 1 is obtained
* 3. Input a non-empty connection context and a non-empty pointer. Expected result 3 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_CONFIG_INVALID_VERSION
3. Return HITLS_SUCCESS
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_MAX_PROTO_VERSION_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
int32_t ret;
uint16_t maxVersion = 0;
ret = HITLS_SetMaxProtoVersion(NULL, HITLS_VERSION_TLS10);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetMaxProtoVersion(ctx, HITLS_VERSION_TLS10);
ASSERT_TRUE(ret == HITLS_CONFIG_INVALID_VERSION);
ret = HITLS_SetMaxProtoVersion(ctx, HITLS_VERSION_TLS13);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_GetMaxProtoVersion(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_GetMaxProtoVersion(NULL, &maxVersion);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_GetMaxProtoVersion(ctx, &maxVersion);
ASSERT_TRUE(ret == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_GET_MIN_PROTO_VERSION_API_TC001
* @title test HITLS_SetMinProtoVersion, HITLS_GetMinProtoVersion interfaces
* @precon nan
* @brief HITLS_SetMaxProtoVersion
* 1. Input an empty connection context. Expected result 1 is obtained
* 2. Input a non-empty connection context and version is too high. Expected result 2 is obtained
* 3. Input a non-empty connection context and normal version. Expected result 3 is obtained
* HITLS_GetMinProtoVersion
* 1. Input an empty connection context and a null pointer. Expected result 1 is obtained
* 2. Input an empty connection context and a non-empty pointer. Expected result 1 is obtained
* 3. Input a non-empty connection context and a non-empty pointer. Expected result 3 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_CONFIG_INVALID_VERSION
* 3. Return HITLS_SUCCESS
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_GET_MIN_PROTO_VERSION_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL) ;
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
int32_t ret;
uint16_t minVersion = 0;
ret = HITLS_SetMinProtoVersion(NULL, HITLS_VERSION_TLS12);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetMinProtoVersion(ctx, HITLS_VERSION_TLS13);
ASSERT_TRUE(ret == HITLS_CONFIG_INVALID_VERSION);
ret = HITLS_SetMinProtoVersion(ctx, HITLS_VERSION_TLS12);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_GetMinProtoVersion(NULL, NULL);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_GetMinProtoVersion(NULL, &minVersion);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_GetMinProtoVersion(ctx, &minVersion);
ASSERT_TRUE(ret == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_IS_AEAD_FUNC_TC001
* @title HITLS_IsAead Obtains whether to use the AEAD algorithm after negotiation
* @precon TLS12, HITLS_RSA_with_AES_128_CBC_SHA256 (not AEAD), TLS13, HITLS_CHACHA20_POLY1305_SHA256 /
* HITLS_AES_128_GCM_SHA256 (AEAD)
* @brief 1. Initialize the client and server and set the cipherSuite. Expected result 1
* 2. After connection is established, invoke HITLS_IsAead to check whether
* the AEAD algorithm is negotiated. Expected result 2
* @expect 1. Initialization is complete.
* 2. Value of isAEAD
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_AEAD_FUNC_TC001(int version, int ciphersuite)
{
FRAME_Init();
int ret;
uint8_t isAEAD = 0;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t cipherSuites[] = {(uint16_t)ciphersuite};
HITLS_CFG_SetCipherSuites(config_c, cipherSuites, sizeof(cipherSuites) / sizeof(uint16_t));
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ret = HITLS_IsAead(client->ssl, &isAEAD);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isAEAD == (version == HITLS_VERSION_TLS13));
ret = HITLS_IsAead(server->ssl, &isAEAD);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isAEAD == (version == HITLS_VERSION_TLS13));
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test HITLS_IsHandShakeDone Check whether the handshake is complete during connection establishment
* @title UT_TLS_CM_IS_HSDONE_FUNC_TC001
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
* 2. During connection establishment, invoke HITLS_IsHandShakeDone to check whether the handshake is complete.
* Expected result 2
* @expect 1. Initialization is complete
* 2. The interface returns 0 and the handshake is not done
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_HSDONE_FUNC_TC001(int version, int state)
{
FRAME_Init();
int ret;
uint8_t isDone;
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_HandshakeState curState = (HITLS_HandshakeState)state;
ret = FRAME_CreateConnection(client, server, true, curState);
ret = HITLS_IsHandShakeDone(client->ssl, &isDone);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isDone == 0);
ret = HITLS_IsHandShakeDone(server->ssl, &isDone);
ASSERT_TRUE(ret == HITLS_SUCCESS);
if (version == HITLS_VERSION_TLS12 && curState == TRY_RECV_FINISH) {
ASSERT_TRUE(isDone == 1);
} else {
ASSERT_TRUE(isDone == 0);
}
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test HITLS_IsHandShakeDone Check whether the handshake is complete after connection establishment
* @title UT_TLS_CM_IS_HSDONE_FUNC_TC002
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
* 2. After the connection is established, invoke HITLS_IsHandShakeDone to check whether the handshake
* is complete. Expected result 2
* @expect 1. Initialization is complete
* 2. The interface returns 1 and the handshake is done
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_HSDONE_FUNC_TC002(int version)
{
FRAME_Init();
int ret;
uint8_t isDone;
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ret = HITLS_IsHandShakeDone(client->ssl, &isDone);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isDone == 1);
ret = HITLS_IsHandShakeDone(server->ssl, &isDone);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isDone == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_IS_SERVER_FUNC_TC001
* @title HITLS_IsServer The client invokes the interface to determine whether the current server is the server
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
* 2. The client invokes the HITLS_IsServer interface to determine whether the current client is a server.
* Expected result 2
* 3. The server invokes the HITLS_IsServer interface to determine whether the current server is a server.
* Expected result 3
* @expect 1. Initialization is complete
* 2. The interface returns false
* 3. The interface returns true
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_SERVER_FUNC_TC001(int version)
{
FRAME_Init();
int ret;
uint8_t isServer;
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ret = HITLS_IsServer(client->ssl, &isServer);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isServer == false);
ret = HITLS_IsServer(server->ssl, &isServer);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(isServer == true);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_READHASPENDING_FUNC_TC001
* @title HITLS_ReadHasPending Interface test
* @precon nan
* @brief 1. After initialization, invoke the hitls_readhaspending interface. Expected result 1 is obtained.
* 2. After the connection is established, the peer sends data and the local
* invokes the hitls_readhaspending interface. Expected result 2 is obtained.
* @expect 1. Return 0
* 2. Return 1
@ */
/* BEGIN_CASE */
void UT_TLS_CM_READHASPENDING_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isPending = 0;
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_ReadHasPending(client->ssl, &isPending) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_ReadHasPending(server->ssl, &isPending) == HITLS_SUCCESS);
ASSERT_EQ(isPending, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(client->ssl, data, sizeof(data), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
uint8_t readBuf[5] = {0};
uint32_t readLen = 0;
ASSERT_TRUE(HITLS_Read(server->ssl, readBuf, 5, &readLen) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_ReadHasPending(server->ssl, &isPending) == HITLS_SUCCESS);
ASSERT_EQ(isPending, 1);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_READPENDING_FUNC_TC001
* @title HITLS_GetReadPendingBytes interfaces test
* @precon nan
* @brief 1. After initialization, invoke the HITLS_GetReadPendingBytes interface to query data.
* Expected result 1 is obtained.
* 2. Simulate a scenario where the peer end sends app data during renegotiation to generate app data cache,
* and invoke HITLS_GetReadPendingBytes to obtain the cache value. Expected result 2 is obtained.
* 3. When the buffer length of the HITLS_Read read data is less than 16 KB, some data is left.
* Invoke the HITLS_GetReadPendingBytes interface to query the data. Expected result 3 is obtained.
* @expect 1. The return value is 0.
* 2. Returns the size of the cached value.
* 3. Returns the size of the left value.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_READPENDING_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_GetReadPendingBytes(server->ssl) == 0);
ASSERT_TRUE(HITLS_GetReadPendingBytes(client->ssl) == 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
uint8_t data[] = "Hello World";
uint32_t writeLen;
ASSERT_TRUE(HITLS_Write(server->ssl, data, sizeof(data), &writeLen) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_IO_BUSY);
client->ssl->state = CM_STATE_ALERTING;
ASSERT_TRUE(HITLS_GetReadPendingBytes(client->ssl) == sizeof("Hello World"));
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test HITLS_GetPeerSignScheme Unidirectional authentication on the client
* @title UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC001
* @precon nan
* @brief 1. Configure unidirectional authentication. After the negotiation is complete,
* call the interface to obtain the local signature hash algorithm. Expected result 1 is displayed.
* 2. Call the interface to obtain the peer signature hash algorithm. Expected result 2 is obtained.
* @expect 1. Return 0
* 2. Return 0
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, false);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
HITLS_SignHashAlgo peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
uint32_t ret = HITLS_GetPeerSignScheme(server->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(peerSignScheme, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test HITLS_GetPeerSignScheme Client two-way authentication Verification
* @title UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC002
* @precon nan
* @brief 1. Set two-way authentication. Before the client receives the certificate request, call the interface to
* obtain the local signature hash algorithm. Expected result 1 is obtained.
* 2. After receiving the certificate request, the client invokes the interface to obtain the negotiated
8 signature hash algorithm. Expected result 2 is displayed.
* @expect 1. Return 0
* 2. The returned value is the negotiated algorithm
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC002(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_REQUEST) == HITLS_SUCCESS);
HITLS_SignHashAlgo peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
//
uint32_t ret = HITLS_GetPeerSignScheme(client->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
if (version == HITLS_VERSION_TLS13) {
ASSERT_EQ(peerSignScheme, 0);
} else {
ASSERT_NE(peerSignScheme, 0);
}
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
ret = HITLS_GetPeerSignScheme(client->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_NE(peerSignScheme, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC003
* @title HITLS_GetPeerSignScheme Client Verification
* @precon nan
* @brief 1. Before the client receives the serverkeyexchange message, call the interface to obtain the peer signature
* hash algorithm. Expected result 1 is displayed.
* 2. After receiving the serverkeyexchange message, the client invokes the interface to obtain the signature
* hash algorithm of the peer end. Expected result 2 is obtained.
* @expect 1. Return 0
* 2. The return value is the algorithm used by the server
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC003(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
HITLS_CFG_SetClientVerifySupport(config, true);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SignHashAlgo peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
uint32_t ret = HITLS_GetPeerSignScheme(server->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(peerSignScheme, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
ret = HITLS_GetPeerSignScheme(client->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(peerSignScheme, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC004
* @title HITLS_GetPeerSignScheme two-way authentication verification on the server
* @precon nan
* @brief 1. Set two-way authentication. Before the server receives the certificate verify message,
* call the API to obtain the peer signature hash algorithm. Expected result 1 is obtained.
* 2. After receiving the certificate verify message, the server invokes the API to obtain the signature hash
* algorithm of the peer end. Expected result 2 is obtained.
* @expect 1. Return 0
* 2. The returned value is the algorithm used by the client
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_PEER_SIGN_SCHEME_FUNC_TC004(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetClientVerifySupport(config, true);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
HITLS_SignHashAlgo peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
uint32_t ret = HITLS_GetPeerSignScheme(server->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(peerSignScheme, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
peerSignScheme = CERT_SIG_SCHEME_UNKNOWN;
ret = HITLS_GetPeerSignScheme(server->ssl, &peerSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_NE(peerSignScheme, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_LOCAL_SIGN_SCHEME_FUNC_TC001
* @title HITLS_GetLocalSignScheme Server-side verification
* @precon nan
* @brief 1. Before the server receives the client hello message, call the interface to obtain the negotiated signature
* hash algorithm. Expected result 1 is displayed
* 2. After receiving the client hello message, the server invokes the interface to obtain the negotiated
* signature hash algorithm. Expected result 2 is displayed
* @expect 1. Return 0
* 2. The return value is the algorithm used by the server
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_LOCAL_SIGN_SCHEME_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SignHashAlgo localSignScheme = CERT_SIG_SCHEME_UNKNOWN;
ret = HITLS_GetLocalSignScheme(server->ssl, &localSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(localSignScheme, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ret = HITLS_GetLocalSignScheme(server->ssl, &localSignScheme);
ASSERT_EQ(ret, HITLS_SUCCESS);
switch (version) {
case HITLS_VERSION_TLS12:
ASSERT_EQ(localSignScheme, CERT_SIG_SCHEME_RSA_PKCS1_SHA256);
break;
case HITLS_VERSION_TLS13:
ASSERT_EQ(localSignScheme, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256);
break;
default:
config = NULL;
break;
}
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_EC_GROUPS_FUNC_TC001
* @title test HITLS_SetEcGroups interface
* @precon nan
* @brief 1. Input an empty link context and a non-empty group. Normal groupsize. Expected result 1 is obtained
* 2. Input a non-empty link context, empty group, and normal groupsize. Expected result 1 is obtained.
* 3. Input a non-empty link context, a non-empty group, and groupsize 0. Expected result 1 is obtained
* 4. Transfer a non-empty link context, a non-empty group, and normal groupsize. Expected result 2 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_EC_GROUPS_FUNC_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
uint16_t groups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP521R1};
uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
int32_t ret;
ret = HITLS_SetEcGroups(NULL, groups, groupsSize);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetEcGroups(ctx, NULL, groupsSize);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetEcGroups(ctx, groups, 0);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetEcGroups(ctx, groups, groupsSize);
ASSERT_TRUE(ret == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_SIGAL_LIST_FUNC_TC001
* @title test HITLS_SetSigalgsList interface
* @precon nan
* @brief 1. Input an empty link context and a non-empty signAlg. Normal signAlgsSize. Expected result 1 is obtained
* 2. Input an non-empty link context and an empty signAlg. Normal signAlgsSize. Expected result 1 is obtained
* 2. Input a non-empty link context and a non-empty signAlg. 0 signAlgsSize. Expected result 1 is obtained
* 2. Input a non-empty link context and a non-empty signAlg. Normal signAlgsSize. Expected result 2 is obtained
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
@*/
/* BEGIN_CASE */
void UT_TLS_CM_SET_SIGAL_LIST_FUNC_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL) ;
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
uint32_t signAlgsSize = sizeof(signAlgs) / sizeof(uint16_t);
int32_t ret;
ret = HITLS_SetSigalgsList(NULL, signAlgs, signAlgsSize);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetSigalgsList(ctx, NULL, signAlgsSize);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetSigalgsList(ctx, signAlgs, 0);
ASSERT_TRUE(ret == HITLS_NULL_INPUT);
ret = HITLS_SetSigalgsList(ctx, signAlgs, signAlgsSize);
ASSERT_TRUE(ret == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_EC_POINT_FUNC_TC001
* @title Set the normal dot format value.
* @precon nan
* @brief 1. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED and invoke the HITLS_CFG_SetEcPointFormats interface.
* Expected result 1 is obtained.
* 2. Set pointFormats to HITLS_POINT_FORMAT_BUTT and invoke the HITLS_CFG_SetEcPointFormats interface.
* Expected result 2
* 3. Use config to generate ctx, due to the result 3
* 4. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED again and generate ctx again. Expected result 4 is
* obtained. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED and invoke the HITLS_SetEcPointFormats
* interface. Expected result 2
* @expect 1. Interface return value, HITLS_SUCCESS
* 2. Interface return value: HITLS_SUCCESS
* 3. Failed to generate the file.
* 4. The file is generated successfully.
* 5. The setting is successful.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_EC_POINT_FUNC_TC001(int version)
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *Config = NULL;
HITLS_Ctx *ctx = NULL;
Config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(Config != NULL);
const uint8_t pointFormats[] = {HITLS_POINT_FORMAT_UNCOMPRESSED};
uint32_t pointFormatsSize = sizeof(pointFormats) / sizeof(uint8_t);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
const uint8_t pointFormats2[] = {HITLS_POINT_FORMAT_BUTT};
uint32_t pointFormatsSize2 = sizeof(pointFormats2) / sizeof(uint8_t);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats2, pointFormatsSize2) == HITLS_SUCCESS);
ctx = HITLS_New(Config);
if(version == TLS1_2){
ASSERT_TRUE(ctx == NULL);
}
HITLS_Free(ctx);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
ctx = HITLS_New(Config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetEcPointFormats(ctx, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
client = FRAME_CreateLink(Config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(Config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_CONFIG_FUNC_TC001
* @title After the initialization is complete, obtain the config file and check whether the configuration is consistent
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. After the initialization is complete, obtain hitlsConfig and check whether the main configurations are
* consistent with the settings. Expected result 2 is obtained.
* @expect 1. Complete initialization
* 2. Consistent results
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_CONFIG_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
const HITLS_Config *cfgFromCtx = NULL;
cfgFromCtx = HITLS_GetConfig(client->ssl);
ASSERT_TRUE(cfgFromCtx != NULL);
ASSERT_EQ(cfgFromCtx->signAlgorithmsSize, sizeof(signAlgs) / sizeof(uint16_t));
ASSERT_TRUE(memcmp(cfgFromCtx->signAlgorithms, signAlgs, cfgFromCtx->signAlgorithmsSize) == 0);
ASSERT_EQ(cfgFromCtx->isSupportRenegotiation, true);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_CURRENT_CIPHER_FUNC_TC001
* @title HITLS_GetCurrentCipher Obtain the negotiated cipher suite pointer after initialization and before negotiation
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. Before link establishment, call HITLS_GetCurrentCipher to obtain the negotiated cipher suite pointer.
* Expected result 2 is returned.
* @expect 1. Complete initialization
* 2. Return NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_CURRENT_CIPHER_FUNC_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
CONN_Init(ctx);
ASSERT_TRUE(ctx != NULL);
const HITLS_Cipher *hitlsCipher = HITLS_GetCurrentCipher(ctx);
ASSERT_EQ(hitlsCipher->cipherSuite, 0);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GET_RANDOM_FUNC_TC001
* @title tls1.3 Obtain clientRandom and serverRandom
* @precon nan
* @brief 1. establish connection
* 2. Obtain and compare clientRandom and serverRandom.
* @expect 1. Return success
* 2. The clientRandom stored on the server is the same as that sent by the client, and the serverRandom stored
8 on the client is the same as that sent by the server.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_RANDOM_FUNC_TC001(void)
{
HandshakeTestInfo testInfo = {0};
FRAME_Init();
testInfo.config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(testInfo.config != NULL);
testInfo.client = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLink(testInfo.config, BSL_UIO_TCP);
ASSERT_TRUE(testInfo.server != NULL);
FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT);
uint8_t clientRandom[RANDOM_SIZE];
uint8_t serverRandom[RANDOM_SIZE];
uint32_t randomSize = RANDOM_SIZE;
ASSERT_TRUE(HITLS_GetHsRandom(testInfo.client->ssl, g_clientRandom, &randomSize, true) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetHsRandom(testInfo.server->ssl, clientRandom, &randomSize, true) == HITLS_SUCCESS);
ASSERT_TRUE(randomSize == RANDOM_SIZE);
ASSERT_TRUE(memcmp(g_clientRandom, clientRandom, RANDOM_SIZE) == 0);
ASSERT_TRUE(HITLS_GetHsRandom(testInfo.server->ssl, g_serverRandom, &randomSize, false) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetHsRandom(testInfo.client->ssl, serverRandom, &randomSize, false) == HITLS_SUCCESS);
ASSERT_TRUE(randomSize == RANDOM_SIZE);
ASSERT_TRUE(memcmp(g_serverRandom, serverRandom, RANDOM_SIZE) == 0);
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/* @
* @test HITLS_GetHandShakeState change state to alerting, obtain the state
* @title UT_TLS_CM_GET_HANDSHAKE_STATE_FUNC_TC001
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1 is obtained
* 2. When an alerting message is generated during data transmission, invoke HITLS_GetHandShakeState to stop
* sending the alerting message and obtain the current status. Expected result 2 is obtained
* @expect 1. Complete initialization
* 2. Return TLS_CONNECTED
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_HANDSHAKE_STATE_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
client->ssl->method.sendAlert(client->ssl, ALERT_LEVEL_WARNING, ALERT_NO_CERTIFICATE_RESERVED);
ret = ALERT_Flush(client->ssl);
ASSERT_EQ(ret, HITLS_SUCCESS);
uint32_t state = 0;
ret = HITLS_GetHandShakeState(client->ssl, &state);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(state, TLS_CONNECTED);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test HITLS_GetStateString Query the handshake status in sequence.
* @title UT_TLS_CM_GET_STATE_STRING_FUNC_TC001
* @precon nan
* @brief 1. Invoke the HITLS_GetStateString interface and transfer values 0-30 and 255 at a time. Expected result 1.
* @expect 1. The interface returns the corresponding handshake status.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_STATE_STRING_FUNC_TC001()
{
const char goalStr[34][32] = {
"idle",
"connected",
"send hello request",
"send client hello",
"send hello retry request",
"send server hello",
"send hello verify request",
"send encrypted extensions",
"send certificate",
"send server key exchange",
"send certificate request",
"send server hello done",
"send client key exchange",
"send certificate verify",
"send new session ticket",
"send change cipher spec",
"send end of early data",
"send finished",
"send keyupdate",
"recv client hello",
"recv server hello",
"recv hello verify request",
"recv encrypted extensions",
"recv certificate",
"recv server key exchange",
"recv certificate request",
"recv server hello done",
"recv client key exchange",
"recv certificate verify",
"recv new session ticket",
"recv end of early data",
"recv finished",
"recv keyupdate",
"recv hello request",
};
int32_t ret;
for (uint32_t i = 0; i <= 30; i++) {
ret = strcmp(HITLS_GetStateString(i), goalStr[i]);
ASSERT_TRUE(strcmp(HITLS_GetStateString(i), goalStr[i]) == 0);
}
ASSERT_TRUE(strcmp(HITLS_GetStateString(255), "unknown") == 0);
EXIT:
return;
}
/* END_CASE */
/* @
* @test HITLS_IsHandShaking function point test
* @title UT_TLS_CM_IS_HANDSHAKING_FUNC_TC001
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1.
* 2. Invoke the HITLS_IsHandShaking interface to check whether handshake is in progress. Expected result 2.
* 3. Initiate a connection establishment request and invoke the HITLS_IsHandShaking interface during connection establishment. (Expected result 3)
* 4. Invoke HITLS_IsHandShaking to complete connection establishment. Expected result 4.
* 5. Invoke the HITLS_Renegotiate interface to initiate renegotiation. (Expected result 5.)
* 6. Invoke the HITLS_IsHandShaking interface to check whether the handshake is in progress. (Expected result 6)
* 7. After the renegotiation is complete, invoke the HITLS_IsHandShaking interface to check whether handshake is in progress. Expected result 7.
*@expect 1. Initialization is complete.
* 2. The interface output parameter is 0.
* 3. The interface output parameter is 1.
* 4. The interface output parameter is 0.
* 5. The state changes to the renegotiation state.
* 6. The output parameter of the interface is 1.
* 7. The interface output parameter is 0.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_HANDSHAKING_FUNC_TC001(int version)
{
FRAME_Init();
uint8_t isHandShaking = 0;
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(tlsConfig != NULL);
tlsConfig->isSupportRenegotiation = true;
FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
ASSERT_TRUE(HITLS_IsHandShaking(clientTlsCtx, &isHandShaking) == HITLS_SUCCESS);
ASSERT_TRUE(isHandShaking == 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(HITLS_IsHandShaking(clientTlsCtx, &isHandShaking) == HITLS_SUCCESS);
ASSERT_TRUE(isHandShaking == 1);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_IsHandShaking(clientTlsCtx, &isHandShaking) == HITLS_SUCCESS);
ASSERT_TRUE(isHandShaking == 0);
if (version == HITLS_VERSION_TLS12) {
ASSERT_EQ(HITLS_Renegotiate(clientTlsCtx), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Renegotiate(serverTlsCtx), HITLS_SUCCESS);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_RENEGOTIATION);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_RENEGOTIATION);
ASSERT_TRUE(HITLS_IsHandShaking(clientTlsCtx, &isHandShaking) == HITLS_SUCCESS);
ASSERT_TRUE(isHandShaking == 1);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_IsHandShaking(clientTlsCtx, &isHandShaking) == HITLS_SUCCESS);
ASSERT_TRUE(isHandShaking == 0);
}
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_IsBeforeHandShake_FUNC_TC001
* @title HITLS_IsBeforeHandShake Check whether the handshake is not performed
* @percon NA
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Call HITLS_IsBeforeHandShake to check whether the handshake has not been performed. Expected result 2
* 3. During transporting, call HITLS_IsBeforeHandShake to check whether the handshake has not been performed
* Expected result 3
* 4. Establish a connection and invoke the HITLS_IsBeforeHandShake interface to check whether the handshake
* has not been performed.Expected result 4
* @expect
* 1. Initialization is complete
* 2. isBefore will be 1
* 3. isBefore will be 0
* 4. isBefore will be 0
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_IsBeforeHandShake_FUNC_TC001(int version)
{
FRAME_Init();
int ret = 0;
uint8_t isBefore = 0;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ret = HITLS_IsBeforeHandShake(client->ssl, &isBefore);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_IDLE);
ASSERT_TRUE(isBefore == 1);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
ret = HITLS_IsBeforeHandShake(client->ssl, &isBefore);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_HANDSHAKING);
ASSERT_TRUE(isBefore == 0);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(client->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(isBefore == 0);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_GetClientVersion_FUNC_TC001
* @title HITLS_GetClientVersion Obtains the client and server version numbers
* after initialization and before negotiation.
* @percon NA
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Call HITLS_GetClientVersion to obtain the client and server version numbers. Expected result 2
* @expect
* 1. Completing the initialization
* 2. The interface returns all 0s
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetClientVersion_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
uint16_t clientVersion = HITLS_VERSION_TLS12;
ASSERT_TRUE(HITLS_GetClientVersion(client->ssl, &clientVersion) == HITLS_SUCCESS);
uint16_t serverVersion = HITLS_VERSION_TLS12;
ASSERT_TRUE(HITLS_GetClientVersion(server->ssl, &serverVersion) == HITLS_SUCCESS);
ASSERT_EQ(clientVersion, 0);
ASSERT_EQ(serverVersion, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_IsClient_FUNC_TC001
* @title Testing the HITLS_IsClient
* @percon NA
* @brief
* 1. Enter an empty TLS connection handle. Expected result 1
* 2. Enter a non-empty TLS connection handle and leave isClient empty. Expected result 1
* 3. Enter a non-empty TLS connection handle and leave isClient not empty. Expected result 2
* @expect
* 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCESS
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_IsClient_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool isClient = 0;
ASSERT_TRUE(HITLS_IsClient(ctx, &isClient) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_IsClient(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_IsClient(ctx, &isClient) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test HITLS_GetSharedGroup Obtain the first supported peer group when only one curve is matched on the client and
server.
* @title UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC001
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Configure the client and server to support only one elliptic curve (the number of intersection groups is 1).
Expected result 2
* 3. Establish a connection and invoke the HITLS_GetSharedGroup interface to obtain the first supported peer group
(Expected result 3)
* @expect
* 1. Initialization is complete
* 2. The setting is successful
* 3. The interface returns the supported elliptic curve
@ */
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC001(int version)
{
FRAME_Init();
int ret;
uint16_t groupId;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetSharedGroup(server->ssl, 1, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP256R1);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetSharedGroup Obtain the second supported peer group if only one matching curve exists on the client and
* server.
* @title UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC002
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Configure only one elliptic curve supported by the client and server. Expected result 2
* 3. Establish a connection and invoke the HITLS_GetSharedGroup interface to obtain the second supported peer group,
* Expected result 3
* @expect
* 1. Initialization is complete
* 2. The setting is successful
* 3. The interface returns 0
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC002(int version)
{
FRAME_Init();
int ret;
uint16_t groupId;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetSharedGroup(server->ssl, 2, &groupId);
ASSERT_TRUE(ret == HITLS_INVALID_INPUT);
ASSERT_TRUE(groupId == 0);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetSharedGroup Obtain the second and third supported peer groups when the client and server have two
* matching curves.
* @title UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC003
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Configure two elliptic curves supported by the client and server. Expected result 2
* 3. Establish a connection. Invoke HITLS_GetSharedGroup to obtain the second supported peer group and the third
* supported peer group, Expected result 3
* @expect
* 1. Initialization is complete
* 2. The setting is successful
* 3. The corresponding curve is returned for the first call and 0 is returned for the second call
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC003(int version)
{
FRAME_Init();
int ret;
uint16_t groupId;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetSharedGroup(server->ssl, 2, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP384R1);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetSharedGroup Obtain the first supported peer group when there is no matching curve on the client and
* server
* @title UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC004
* @precon In the current framework, the TLS13 client and server do not have a matching curve. The TLS12 client and
* server can successfully establish a connection. The TLS13 framework capability needs to be supplemented
* @brief
* 1. Initialize the client and server, Expected result 1
* 2. Configure the client and server not to support the elliptic curve. Expected result 2
* 3. Establish a connection. After the parameters are negotiated, invoke the HITLS_GetSharedGroup interface to obtain
* the supported peer group. Expected result 3
* @expect
* 1. Initialization is complete
* 2. The setting is successful
* 3. The interface returns 0
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC004(int version)
{
FRAME_Init();
int ret;
uint16_t groupId;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
uint16_t signWrtVersion =
(version == HITLS_VERSION_TLS12) ? CERT_SIG_SCHEME_RSA_PKCS1_SHA256 : CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {signWrtVersion, CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
HITLS_CFG_SetDhAutoSupport(config_c, true);
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {
signWrtVersion, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
HITLS_CFG_SetDhAutoSupport(config_s, true);
FRAME_CertInfo certInfo = {
"rsa_pss_sha256/rsa_pss_root.crt",
"rsa_pss_sha256/rsa_pss_intCa.crt",
"rsa_pss_sha256/rsa_pss_dev.crt",
0,
"rsa_pss_sha256/rsa_pss_dev.key",
0,
};
client = (version == HITLS_VERSION_TLS12) ? FRAME_CreateLink(config_c, BSL_UIO_TCP)
: FRAME_CreateLinkWithCert(config_c, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
server = (version == HITLS_VERSION_TLS12) ? FRAME_CreateLink(config_s, BSL_UIO_TCP)
: FRAME_CreateLinkWithCert(config_s, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(server != NULL);
ret = FRAME_CreateConnection(client, server, true, HS_STATE_BUTT);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ret = HITLS_GetSharedGroup(server->ssl, 1, &groupId);
ASSERT_TRUE(ret == HITLS_INVALID_INPUT);
ASSERT_TRUE(groupId == 0);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetSharedGroup If the matched elliptic curve exists, obtain the (-1)th supported peer group.
* @title UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC005
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Configure two elliptic curves supported by the client and server. Expected result 2
* 3. Establish a connection. Invoke HITLS_GetSharedGroup to obtain the (-1)th supported peer group. Expected result 3
* @expect
* 1. Initialization is complete
* 2. The setting is successful
* 3. The interface returns 2
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC005(int version)
{
FRAME_Init();
int ret;
uint16_t groupId;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groups_c[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_RSA_PKCS1_SHA256};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t groups_s[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256,
CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384,
CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetSharedGroup(server->ssl, -1, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(groupId == 2);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_SetVersionSupport_HITLS_GetVersionSupport_API_TC001
* @title Test the HITLS_SetVersionSupport and HITLS_GetVersionSupport interfaces.
* @precon nan
* @brief
* HITLS_SetVersionSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input a non-empty TLS connection handle and set the version to an invalid value. Expected result 2
* 3. Input a non-empty TLS connection handle and set the version to a valid value. Expected result 3
* HITLS_GetVersionSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input an empty version pointer. Expected result 1
* 3. Input a non-empty TLS connection handle and ensure that the version pointer is not empty. Expected result 4
* @expect
* 1. Return HITLS_NULL_INPUT.
* 2. Return HITLS_SUCCESS, and invalid values in ctx->config.tlsConfig are filtered out
* 3. Return HITLS_SUCCESS is returned, and the value of ctx->config.tlsConfig is the expected value
* 4. Return HITLS_SUCCESS is returned and the value of version is the same as that recorded in config
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_SetVersionSupport_HITLS_GetVersionSupport_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t version = 0;
ASSERT_TRUE(HITLS_SetVersionSupport(ctx, version) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetVersionSupport(ctx, &version) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetVersionSupport(ctx, NULL) == HITLS_NULL_INPUT);
version = (TLS13_VERSION_BIT << 1) | TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_SetVersionSupport(ctx, version) == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.minVersion == HITLS_VERSION_TLS12 &&
ctx->config.tlsConfig.maxVersion == HITLS_VERSION_TLS13);
version = TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_SetVersionSupport(ctx, version) == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.minVersion == HITLS_VERSION_TLS12 &&
ctx->config.tlsConfig.maxVersion == HITLS_VERSION_TLS13);
uint32_t getversion = 0;
ASSERT_TRUE(HITLS_GetVersionSupport(ctx, &getversion) == HITLS_SUCCESS);
ASSERT_TRUE(getversion == ctx->config.tlsConfig.version);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_SetQuietShutdown_HITLS_GetQuietShutdown_API_TC001
* @title Test the HITLS_SetQuietShutdown and HITLS_GetQuietShutdown interfaces
* @precon nan
* @brief
* HITLS_SetQuietShutdown
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input a non-empty TLS connection handle and set mode to an invalid value. Expected result 2
* 3. Input a non-empty TLS connection handle and set mode to a valid value. Expected result 3
* HITLS_GetQuietShutdown
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input an empty mode pointer. Expected result 1
* 3. Input a non-empty TLS connection handle and ensure that the mode pointer is not empty. Expected result 3
* @expect
* 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_CONFIG_INVALID_SET
* 3. Return HITLS_SUCCES
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_SetQuietShutdown_HITLS_GetQuietShutdown_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
int32_t mode = 0;
ASSERT_TRUE(HITLS_SetQuietShutdown(ctx, mode) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetQuietShutdown(ctx, &mode) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetQuietShutdown(ctx, NULL) == HITLS_NULL_INPUT);
mode = 1;
ASSERT_TRUE(HITLS_SetQuietShutdown(ctx, mode) == HITLS_SUCCESS);
mode = -1;
ASSERT_TRUE(HITLS_SetQuietShutdown(ctx, mode) == HITLS_CONFIG_INVALID_SET);
int32_t getMode = -1;
ASSERT_TRUE(HITLS_GetQuietShutdown(ctx, &getMode) == HITLS_SUCCESS);
ASSERT_TRUE(getMode == true);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_TLS_CM_HITLS_SetDhAutoSupport_API_TC001
* @title Test HITLS_SetDhAutoSupport
* @precon nan
* @brief
* HITLS_SetDhAutoSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input a non-empty TLS connection handle and set support to an invalid value. Expected result 2
* 3. Input a non-empty TLS connection handle and set support to a valid value. Expected result 3 is displayed.
* @expect
* 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCES, and isSupportDhAuto is ture
* 3. Return HITLS_SUCCES, and isSupportDhAuto is ture or false
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_SetDhAutoSupport_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
ASSERT_TRUE(HITLS_SetDhAutoSupport(ctx, support) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
support = true;
ASSERT_TRUE(HITLS_SetDhAutoSupport(ctx, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_SetDhAutoSupport(ctx, support) == HITLS_SUCCESS);
support = false;
ASSERT_TRUE(HITLS_SetDhAutoSupport(ctx, support) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_TMPDH_TC001
* @spec -
* @title Test HITLS_SetTmpDh interface
* @precon nan
* @brief
* HITLS_SetTmpDh
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Input non-empty TLS connection handle information and leave dhPkey empty. Expected result 1
* 3. Input the non-empty TLS connection handle information and ensure that dhPkey is not empty. Expected result 2
* @expect
* 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCES
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_SetTmpDh_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
HITLS_CRYPT_Key *dhPkey = HITLS_CRYPT_GenerateDhKeyBySecbits(LIBCTX_FROM_CONFIG(config),
ATTRIBUTE_FROM_CONFIG(config), config, HITLS_SECURITY_LEVEL_THREE_SECBITS);
ASSERT_TRUE(HITLS_SetTmpDh(ctx, dhPkey) == HITLS_NULL_INPUT);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetTmpDh(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetTmpDh(ctx, dhPkey) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test HITLS_GetPeerFinishVerifyData Obtaining Peer VerifyDATA After Link Establishment and Renegotiation Are Complete
* @title UT_TLS_CM_HITLS_GetPeerFinishVerifyData_FUNC_TC001
* @precon nan
* @brief
* 1. Initialize the client and server and obtain the verifyDATA. Expected result 1.
* 2. Send a connection setup request. Expected result 2
* 3. Call HITLS_GetPeerFinishVerifyData to obtain and store VerifyData. Expected result 3
* 4. Perform renegotiation. Expected result 4
* 5. Call HITLS_GetPeerFinishVerifyData to obtain VerifyData. Expected result 5
* @expect
* 1. Return HITLS_SUCCESS and the len of verifyDATA is 0
* 2. The connection is established.
* 3. Return HITLS_SUCCESS and the len of verifyDATA is not 0
* 4. Renegotiation succeeded.
* 5. Return HITLS_SUCCESS and the verifyDATA is different from result 1
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetPeerFinishVerifyData_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
uint8_t verifyDataOld[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataOldSize = 0;
uint32_t ret =
HITLS_GetPeerFinishVerifyData(serverTlsCtx, verifyDataOld, sizeof(verifyDataOld), &verifyDataOldSize);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(verifyDataOldSize, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ret = HITLS_GetPeerFinishVerifyData(serverTlsCtx, verifyDataOld, sizeof(verifyDataOld), &verifyDataOldSize);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_NE(verifyDataOldSize, 0);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ret = HITLS_GetPeerFinishVerifyData(serverTlsCtx, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(verifyDataNewSize, verifyDataOldSize);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetFinishVerifyData: Obtains the verification data before a connection is established
* @title UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC001
* @precon nan
* @brief
*1. Initialize the client and server. Expected result 1
*2. Call HITLS_GetFinishVerifyData to obtain VerifyData. Expected result 2
* @expect
*1. Completing the initialization
*2. The interface returns 0.
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
ASSERT_TRUE(HITLS_GetFinishVerifyData(server->ssl, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_EQ(verifyDataNewSize, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetFinishVerifyData Obtain VerifyDATA after connection establishment
* @title UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC002
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result 1
* 2. Send a link setup request. Expected result 2
* 3. Call HITLS_GetFinishVerifyData to obtain VerifyData. Expected result 3
* @expect
* 1. Completing the initialization
* 2. The connection is established
* 3. The value returned by the interface is not 0
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC002(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
ASSERT_TRUE(HITLS_GetFinishVerifyData(server->ssl, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_NE(verifyDataNewSize, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetFinishVerifyData Obtains VerifyDATA after link establishment and renegotiation.
* @title UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC003
* @precon nan
* @brief
* 1. Initialize the client and server. Expected result
* 2. Send a connection setup request. Expected result 2
* 3. Call HITLS_GetFinishVerifyData to obtain and store VerifyData. Expected result 3
* 4. Perform renegotiation. Expected result 4
* 5. Call HITLS_GetFinishVerifyData to obtain VerifyData. Expected result 5
* @expect
* 1. Complete the initialization
* 2. The link is established
* 3. The interface returns a value other than 0
* 4. Renegotiation succeeded
* 5. Inconsistent with the first link establishmen
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetFinishVerifyData_FUNC_TC003(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
config->isSupportRenegotiation = true;
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config, 1), HITLS_SUCCESS);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
uint16_t cipherSuite = HITLS_RSA_WITH_AES_128_CBC_SHA256;
int32_t ret = HITLS_CFG_SetCipherSuites(config, &cipherSuite, 1);
ASSERT_EQ(ret, HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
uint8_t verifyDataOld[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataOldSize = 0;
ASSERT_TRUE(HITLS_GetFinishVerifyData(server->ssl, verifyDataOld, sizeof(verifyDataOld), &verifyDataOldSize) ==
HITLS_SUCCESS);
ASSERT_NE(verifyDataOldSize, 0);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_GetFinishVerifyData(serverTlsCtx, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_TRUE(verifyDataNewSize == verifyDataOldSize);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
int32_t SendHelloReq(HITLS_Ctx *ctx)
{
uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
size_t len = HS_MSG_HEADER_SIZE;
return REC_Write(ctx, REC_TYPE_HANDSHAKE, buf, len);
}
/**
* @test UT_TLS_CM_HITLS_GetRenegotiationState_FUNC_TC001
* @title Verifying the HITLS_GetRenegotiationState Interface
* @precon nan
* @brief
* 1. After the client and server are initialized, initiate a connection establishment request. Expected result 1
* 2. Call the HITLS_GetRenegotiationState interface to query the renegotiation status. Expected result 2
* 3. The server invokes the hitls_renegotiate interface to initiate renegotiation and invokes the
* HITLS_GetRenegotiationState interface to query the renegotiation status on the server. Expected result 3
* 4. After receiving the hello request, the client invokes the HITLS_GetRenegotiationState interface
* to query the renegotiation status. Expected result 4
* 5. After receiving the client hello message, the server invokes the HITLS_GetRenegotiationState interface to query
* the renegotiation status. Expected result 5
* 6. After the renegotiation is complete, call the HITLS_GetRenegotiationState interface to query the renegotiation
* status on the client and server. Expected result 6
* 7. The client invokes the hitls_renegotiate interface to initiate renegotiation and invokes the
* HITLS_GetRenegotiationState interface to query the renegotiation status. Expected result 7
* @expect
* 1. The connection is successfully established
* 2. The return value is false
* 3. The return value is true
* 4. The return value is true
* 5. The return value is true
* 6. The return value is flase
* 7. The return value is true
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetRenegotiationState_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t isRenegotiation = true;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_SetRenegotiationSupport(server->ssl, true);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(client->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(server->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == true);
ASSERT_TRUE(SendHelloReq(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
ASSERT_TRUE(HITLS_GetRenegotiationState(server->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == true);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, TRY_SEND_SERVER_HELLO), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(server->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == true);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(server->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == false);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationState(server->ssl, &isRenegotiation) == HITLS_SUCCESS);
ASSERT_TRUE(isRenegotiation == true);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test Verifying the HITLS_GetRwstate Interface
* @title UT_TLS_CM_HITLS_GetRwstate_FUNC_TC001
* @precon nan
* @brief
* 1. After the initialization, invoke the HITLS_GetRwstate interface to query data. Expected result 1
* 2. When reading data, set ruio to null, construct a read exception, and call the HITLS_GetRwstate interface for
* query. Expected result 2
* 3. When writing data, set uio to null, construct a write exception, and call the HITLS_GetRwstate interface for
* query. Expected result 3
* @expect
* 1. The returned status is nothing
* 2. The returned status is reading
* 3. The returned status is writing
*/
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetRwstate_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint8_t rwstate = HITLS_READING;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
uint32_t ret = HITLS_GetRwstate(client->ssl, &rwstate);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(rwstate, HITLS_NOTHING);
void *tmpUio = client->ssl->rUio;
client->ssl->rUio = NULL;
BSL_UIO_Free(tmpUio);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen = 0;
ret = HITLS_Read(client->ssl, readBuf, READ_BUF_SIZE, &readLen);
ret = HITLS_GetRwstate(client->ssl, &rwstate);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(rwstate, HITLS_READING);
tmpUio = client->ssl->uio;
client->ssl->uio = NULL;
BSL_UIO_Free(tmpUio);
FRAME_TrasferMsgBetweenLink(client, server);
HITLS_Accept(server->ssl);
ASSERT_EQ(ret, HITLS_SUCCESS);
uint8_t writeBuf[100] = {0};
uint32_t writeLen;
ret = HITLS_Write(client->ssl, writeBuf, sizeof(writeBuf), &writeLen);
ret = HITLS_GetRwstate(client->ssl, &rwstate);
ASSERT_EQ(ret, HITLS_SUCCESS);
ASSERT_EQ(rwstate, HITLS_WRITING);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_CLIENTVERIFYSUPPORT_API_TC001
* @title Test the HITLS_SetClientVerifySupport and HITLS_GetClientVerifySupport interfaces.
* @precon nan
* @brief
* HITLS_SetClientVerifySupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer non-empty TLS connection handle information and set support to an invalid value. Expected result 2
* 3. Transfer the non-empty TLS connection handle information and set support to a valid value. Expected result 3
* HITLS_GetClientVerifySupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null.
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. Returns HITLS_SUCCES, isSupportClientVerify is true, and isSupportVerifyNone is false
* 3. HITLS_SUCCES is returned, isSupportClientVerify is true or false, and isSupportVerifyNone and isSupportVerifyNone
* are mutually exclusive, but can be false at the same time
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_CLIENTVERIFYSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_SetClientVerifySupport(ctx, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetClientVerifySupport(ctx, &isSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetClientVerifySupport(ctx, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_SetClientVerifySupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportVerifyNone == false);
support = -1;
ASSERT_TRUE(HITLS_SetClientVerifySupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetClientVerifySupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_SetClientVerifySupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetClientVerifySupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_NOCLIENTCERTSUPPORT_API_TC001
* @title Test the HITLS_SetNoClientCertSupport and HITLS_GetClientVerifySupport interfaces
* @precon nan
* @brief
* HITLS_SetNoClientCertSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer non-empty TLS connection handle information and set support to an invalid value. Expected result 2
* 3. Transfer the non-empty TLS connection handle information and set support to a valid value. Expected result 3
* HITLS_GetNoClientCertSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned.
* 2. HITLS_SUCCES is returned and isSupportNoClientCert is true
* 3. Returns HITLS_SUCCES and isSupportNoClientCert is true or false
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_NOCLIENTCERTSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_SetNoClientCertSupport(ctx, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetNoClientCertSupport(ctx, &isSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetNoClientCertSupport(ctx, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_SetNoClientCertSupport(ctx, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_SetNoClientCertSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetNoClientCertSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_SetNoClientCertSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetNoClientCertSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_VERIFYNONESUPPORT_API_TC001
* @title Test the HITLS_SetVerifyNoneSupport and HITLS_GetVerifyNoneSupport interfaces
* @precon nan
* @brief
* HITLS_SetVerifyNoneSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer non-empty TLS connection handle information and set support to an invalid value. Expected result 2
* 3. Transfer the non-empty TLS connection handle information and set support to a valid value. Expected result 3
* HITLS_GetVerifyNoneSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. Returns HITLS_SUCCES, isSupportVerifyNone is true, and isSupportClientVerify is false
* 3. HITLS_SUCCES is returned, isSupportVerifyNone is true or false, isSupportClientVerify and isSupportClientVerify
* are mutually exclusive, but can be false at the same time
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_VERIFYNONESUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_SetVerifyNoneSupport(ctx, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetVerifyNoneSupport(ctx, &isSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetVerifyNoneSupport(ctx, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_SetVerifyNoneSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportClientVerify == false);
support = -1;
ASSERT_TRUE(HITLS_SetVerifyNoneSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyNoneSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_SetVerifyNoneSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetVerifyNoneSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_CLIENTONCEVERIFYSUPPORT_API_TC001
* @title Test the HITLS_SetClientOnceVerifySupport and HITLS_GetClientOnceVerifySupport interfaces.
* @precon nan
* @brief
* HITLS_SetClientOnceVerifySupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer non-empty TLS connection handle information and set support to an invalid value. Expected result 2
* 3. Transfer the non-empty TLS connection handle information and set support to a valid value. Expected result 3
* HITLS_GetClientOnceVerifySupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null.
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. HITLS_SUCCES is returned and isSupportPostHandshakeAuth is true
* 3. HITLS_SUCCES is returned and isSupportPostHandshakeAuth is true or false
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_CLIENTONCEVERIFYSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_SetClientOnceVerifySupport(ctx, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetClientOnceVerifySupport(ctx, &isSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetClientOnceVerifySupport(ctx, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_SetClientOnceVerifySupport(ctx, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_SetClientOnceVerifySupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetClientOnceVerifySupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_SetClientOnceVerifySupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetClientOnceVerifySupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test Verifying the HITLS_ClearRenegotiationNum Interface
* @title UT_HITLS_CM_HITLS_ClearRenegotiationNum_FUNC_TC001
* @precon nan
* @brief
* 1. After initialization, invoke the HITLS_ClearRenegotiationNum interface to query data. Expected result 1
* 2. After the link is set up, invoke the HITLS_ClearRenegotiationNum interface to query the link. Expected result 2
* 3. Initiate renegotiation. After the renegotiation is successful, invoke the HITLS_ClearRenegotiationNum interface to
check the values of the client and server. Expected result 3
* 4. Initiate another five renegotiations. After the negotiation is complete, invoke the HITLS_ClearRenegotiationNum
interface to query the values of the client and server. Expected result 4
* 5. Invoke the HITLS_ClearRenegotiationNum interface again to query the values on the client and server. Expected
result 5
* 6. The client initiates renegotiation. The server rejects the renegotiation. Invoke the HITLS_ClearRenegotiationNum
interface to query the values of the client and server. Expected result 6
* @expect
* 1. The value is 0
* 2. The value is 0
* 3. The value is 1
* 4. The value is 0
* 5. The value is 0
* 6. The value is 1 for the client and 0 for the server
*/
/* BEGIN_CASE */
void UT_HITLS_CM_HITLS_ClearRenegotiationNum_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {CERT_SIG_SCHEME_RSA_PKCS1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256};
HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t));
config->isSupportRenegotiation = true;
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
HITLS_SetClientRenegotiateSupport(server->ssl, true);
uint32_t renegotiationNum = 0;
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
uint8_t verifyDataOld[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataOldSize = 0;
ASSERT_TRUE(HITLS_GetFinishVerifyData(serverTlsCtx, verifyDataOld, sizeof(verifyDataOld), &verifyDataOldSize) ==
HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_GetFinishVerifyData(serverTlsCtx, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_TRUE(verifyDataNewSize == verifyDataOldSize);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 1);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 1);
for (int i = 0; i < 5; ++i) {
ASSERT_TRUE(HITLS_Renegotiate(serverTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_GetFinishVerifyData(serverTlsCtx, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_TRUE(verifyDataNewSize == verifyDataOldSize);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
}
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 5);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 5);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
ASSERT_TRUE(HITLS_Renegotiate(clientTlsCtx) == HITLS_SUCCESS);
serverTlsCtx->negotiatedInfo.isSecureRenegotiation = false;
ASSERT_EQ(FRAME_CreateRenegotiation(client, server), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
ASSERT_TRUE(clientTlsCtx->state == CM_STATE_ALERTED);
ASSERT_TRUE(serverTlsCtx->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_GetFinishVerifyData(serverTlsCtx, verifyDataNew, sizeof(verifyDataNew), &verifyDataNewSize) ==
HITLS_SUCCESS);
ASSERT_TRUE(verifyDataNewSize == verifyDataOldSize);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) == 0);
ASSERT_TRUE(memcpy_s(verifyDataOld, sizeof(verifyDataOld), verifyDataNew, verifyDataNewSize) == EOK);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(clientTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 1);
ASSERT_TRUE(HITLS_ClearRenegotiationNum(serverTlsCtx, &renegotiationNum) == HITLS_SUCCESS);
ASSERT_EQ(renegotiationNum, 0);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetNegotiateGroup: EC cipher suite
* @spec -
* @title UT_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC001
* @precon nan
* @brief
* 1. Configure the two ends to use the EC cipher suite. Before connection establishment is complete, invoke the
* HITLS_GetNegotiateGroup interface to query the negotiated value. Expected result 1
* 2. Configure the EC cipher suite to be used at both ends. After the connection is established, invoke the
* HITLS_GetNegotiateGroup interface to query the negotiated value. Expected result 2
* @expect
* 1. The return value is 0
* 2. The returned value is the negotiated value
*/
/* BEGIN_CASE */
void UT_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC001(int version)
{
FRAME_Init();
int ret;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint16_t groupId;
uint16_t expectedGroupId = HITLS_EC_GROUP_SECP256R1;
uint16_t groups_c[] = {expectedGroupId, HITLS_EC_GROUP_SECP384R1};
uint16_t signAlgs_c[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384};
HITLS_CFG_SetGroups(config_c, groups_c, sizeof(groups_c) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_c, signAlgs_c, sizeof(signAlgs_c) / sizeof(uint16_t));
uint16_t groups_s[] = {expectedGroupId, HITLS_EC_GROUP_SECP521R1};
uint16_t signAlgs_s[] = {CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256, CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512};
HITLS_CFG_SetGroups(config_s, groups_s, sizeof(groups_s) / sizeof(uint16_t));
HITLS_CFG_SetSignature(config_s, signAlgs_s, sizeof(signAlgs_s) / sizeof(uint16_t));
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
ret = HITLS_GetNegotiateGroup(client->ssl, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_EQ(groupId, 0);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
ret = HITLS_GetNegotiateGroup(client->ssl, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_EQ(groupId, expectedGroupId);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test HITLS_GetNegotiateGroup interface uses the RSA cipher suite.
* @spec -
* @title UT_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC002
* @precon nan
* @brief
* 1. Set the RSA cipher suite to be used at both ends. After the connection is established, invoke the
HITLS_GetNegotiateGroup interface to query the negotiated value. Expected result 1
* @expect
* 1. The return value of tls12 is 0. The prerequisite is that the cipher suite does not contain the (EC)DHE. The cipher
suite involved in key exchange must have the same group. tls13 is the negotiated group. The current framework supports
only ECDHE. Therefore, the connection can be successfully established only when the same EC group exists, The default
common curve is HITLS_EC_GROUP_CURVE25519.
*/
/* BEGIN_CASE */
void UT_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC002(int version)
{
FRAME_Init();
int ret;
HITLS_Config *config_c = GetHitlsConfigViaVersion(version);
HITLS_Config *config_s = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
if (version == HITLS_VERSION_TLS12) {
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config_c, true), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetEncryptThenMac(config_s, true), HITLS_SUCCESS);
uint16_t cipherSuite = HITLS_RSA_WITH_AES_256_CBC_SHA;
ASSERT_EQ(HITLS_CFG_SetCipherSuites(config_c, &cipherSuite, 1), HITLS_SUCCESS);
}
FRAME_LinkObj *client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
FRAME_LinkObj *server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint16_t groupId;
uint16_t expectedGroupId = (version == HITLS_VERSION_TLS12) ? 0 : HITLS_EC_GROUP_CURVE25519;
ret = HITLS_GetNegotiateGroup(server->ssl, &groupId);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_EQ(groupId, expectedGroupId);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_CIPHERSERVERPREFERENCE_FUNC_TC001
* @title Test the HITLS_SetCipherServerPreference and HITLS_GetCipherServerPreference interfaces
* @precon nan
* @brief
* HITLS_SetCipherServerPreference
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer a non-empty TLS connection handle and set isSupport to an invalid value. Expected result 2
* 3. Transfer a non-empty TLS connection handle and set isSupport to a valid value. Expected result 3
* HITLS_GetCipherServerPreference
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null.
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. HITLS_SUCCES is returned and isSupportServerPreference is true
* 3. Returns HITLS_SUCCES and isSupportServerPreference is true or false
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_CIPHERSERVERPREFERENCE_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool isSupport = false;
bool getIsSupport = false;
ASSERT_TRUE(HITLS_SetCipherServerPreference(ctx, isSupport) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetCipherServerPreference(ctx, &getIsSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetCipherServerPreference(ctx, NULL) == HITLS_NULL_INPUT);
isSupport = true;
ASSERT_TRUE(HITLS_SetCipherServerPreference(ctx, isSupport) == HITLS_SUCCESS);
isSupport = -1;
ASSERT_TRUE(HITLS_SetCipherServerPreference(ctx, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportServerPreference = true);
isSupport = false;
ASSERT_TRUE(HITLS_SetCipherServerPreference(ctx, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetCipherServerPreference(ctx, &getIsSupport) == HITLS_SUCCESS);
ASSERT_TRUE(getIsSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_RENEGOTIATIONSUPPORT_FUNC_TC001
* @title Test the HITLS_SetRenegotiationSupport and HITLS_GetRenegotiationSupport interfaces.
* @precon nan
* @brief
* HITLS_SetRenegotiationSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer non-empty TLS connection handle information and set support to an invalid value. Expected result 2
* 3. Transfer the non-empty TLS connection handle information and set support to a valid value. Expected result 3
* HITLS_GetRenegotiationSupport
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer an empty isSupport pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the isSupport pointer is not null.
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. HITLS_SUCCES is returned and isSupportRenegotiation is true
* 3. HITLS_SUCCES is returned and isSupportRenegotiation is true or false
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_RENEGOTIATIONSUPPORT_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_SetRenegotiationSupport(ctx, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetRenegotiationSupport(ctx, &isSupport) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetRenegotiationSupport(ctx, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_SetRenegotiationSupport(ctx, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_SetRenegotiationSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_SetRenegotiationSupport(ctx, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRenegotiationSupport(ctx, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_FLIGHTTRANSMITSWITCH_FUNC_TC001
* @title Test the HITLS_SetFlightTransmitSwitch and HITLS_GetFlightTransmitSwitch interfaces
* @precon nan
* @brief
* HITLS_SetFlightTransmitSwitch
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Transfer a non-empty TLS connection handle and set isEnable to an invalid value. Expected result 2
* 3. Transfer a non-empty TLS connection handle and set isEnable to a valid value. Expected result 3
* GetFlightTransmitSwitch
* 1. Input an empty TLS connection handle. Expected result 1
* 2. Pass an empty getIsEnable pointer. Expected result 1
* 3. Transfer the non-null TLS connection handle information and ensure that the getIsEnable pointer is not null.
* Expected result 3
* @expect
* 1. HITLS_NULL_INPUT is returned
* 2. HITLS_SUCCES is returned and ctx->config.tlsConfig.isFlightTransmitEnable is true
* 3. Returns HITLS_SUCCES and ctx->config.tlsConfig.isFlightTransmitEnable is true or false
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_FLIGHTTRANSMITSWITCH_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t isEnable = -1;
uint8_t getIsEnable = -1;
ASSERT_TRUE(HITLS_SetFlightTransmitSwitch(ctx, isEnable) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetFlightTransmitSwitch(ctx, &getIsEnable) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetFlightTransmitSwitch(ctx, NULL) == HITLS_NULL_INPUT);
isEnable = 1;
ASSERT_TRUE(HITLS_SetFlightTransmitSwitch(ctx, isEnable) == HITLS_SUCCESS);
isEnable = -1;
ASSERT_TRUE(HITLS_SetFlightTransmitSwitch(ctx, isEnable) == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.isFlightTransmitEnable = true);
isEnable = 0;
ASSERT_TRUE(HITLS_SetFlightTransmitSwitch(ctx, isEnable) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetFlightTransmitSwitch(ctx, &getIsEnable) == HITLS_SUCCESS);
ASSERT_TRUE(getIsEnable == false);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/**
* @test UT_HITLS_CM_SET_GET_SetMaxCertList_FUNC_TC001
* @title HTLS_CFG_SetMaxCertList, HITLS_CFG_GetMaxCertList, HITLS_SetMaxCertList, and HITLS_GetMaxCertList APIs
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx
* 2. Set the certificate chain length config to null and invoke the HITLS_CFG_SetMaxCertList interface
* 3. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value
* 4. Set the maximum length of the certificate chain by calling the HITLS_CFG_SetMaxCertList interface
* 5. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value
* 6. Set the minimum certificate chain length by calling the HITLS_CFG_SetMaxCertList interface
* 7. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value
* 8. Use the HITLS_SetMaxCertList and HITLS_GetMaxCertList interfaces to repeat the preceding test
* @expect
* 1. Initialization succeeds
* 2. HITLS_NULL_INPUT is returned
* 3. HITLS_NULL_INPUT is returned
* 4. The interface returns HITLS_SUCCESS
* 5. The value of MaxCertList returned by the interface is 2 ^ 32 - 1
* 6. The interface returns the HITLS_SUCCESS
* 7. The value of MaxCertList returned by the interface is 0
* 8. Same as above
*/
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_SetMaxCertList_FUNC_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig;
HITLS_Ctx *ctx = NULL;
tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
uint32_t maxSize;
// The config parameter is empty.
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(NULL, MAX_CERT_LIST) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(NULL, &maxSize) == HITLS_NULL_INPUT);
// Set the maximum value to 2 ^ 32 - 1.
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(tlsConfig, MAX_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(tlsConfig, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MAX_CERT_LIST);
// Set the minimum value to 0.
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(tlsConfig, MIN_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(tlsConfig, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MIN_CERT_LIST);
// The config parameter is empty.
ASSERT_TRUE(HITLS_SetMaxCertList(NULL, MAX_CERT_LIST) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetMaxCertList(NULL, &maxSize) == HITLS_NULL_INPUT);
// Set the maximum value to 2 ^ 32 - 1.
ASSERT_TRUE(HITLS_SetMaxCertList(ctx, MAX_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetMaxCertList(ctx, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MAX_CERT_LIST);
// Set the minimum value to 0.
ASSERT_TRUE(HITLS_SetMaxCertList(ctx, MIN_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetMaxCertList(ctx, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MIN_CERT_LIST);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
void ExampleInfoCallback(const HITLS_Ctx *ctx, int32_t eventType, int32_t value)
{
(void)ctx;
(void)eventType;
(void)value;
}
uint64_t Test_RecordPaddingCb(HITLS_Ctx *ctx, int32_t type, uint64_t length, void *arg)
{
(void)ctx;
(void)type;
(void)length;
(void)arg;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_CM_InfoCb_API_TC001
* @title InfoCb Interface Parameter Test
* @precon nan
* @brief
1. Use the HITLS_GetInfoCb without HITLS_CFG_SetInfoCb. Expected result 1 is obtained.
2. Use the HITLS_SetInfoCb interface to set callback. Expected result 2
3. Use the HITLS_GetInfoCb . Expected result 3
4. Use the HITLS_GetInfoCb with the parameter is NULL . Expected result 4
* @expect
1. Return the NULL.
2. Return the HITLS_SUCCESS
3. Return value is not NULL.
4. Return the NULL.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_InfoCb_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
FRAME_LinkObj *client = FRAME_CreateLink(config, BSL_UIO_UDP);
ASSERT_TRUE(client != NULL);
HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
ASSERT_TRUE(clientTlsCtx != NULL);
HITLS_InfoCb infoCallBack = HITLS_GetInfoCb(clientTlsCtx);
ASSERT_TRUE(infoCallBack == NULL);
int32_t ret = HITLS_SetInfoCb(clientTlsCtx, ExampleInfoCallback);
ASSERT_TRUE(ret == HITLS_SUCCESS);
infoCallBack = HITLS_GetInfoCb(clientTlsCtx);
ASSERT_TRUE(infoCallBack != NULL);
infoCallBack = HITLS_GetInfoCb(NULL);
ASSERT_TRUE(infoCallBack == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
}
/* END_CASE */
#define MSG_CB_PRINT_LEN 500
void msg_callback(int32_t writePoint, int32_t tlsVersion, int32_t contentType, const void *msg,
uint32_t msgLen, HITLS_Ctx *ctx, void *arg)
{
(void)writePoint;
(void)tlsVersion;
(void)contentType;
(void)msg;
(void)msgLen;
(void)ctx;
(void)arg;
}
/* @
* @test UT_TLS_CM_SetMsgCb_API_TC001
* @title HITLS_SetMsgCb Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetMsgCb interface to set callback. (Expected result 2)
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetMsgCb_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetMsgCb(NULL, msg_callback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetMsgCb(ctx, msg_callback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GetError_API_TC001
* @title HITLS_GetError Interface Parameter Test
* @precon nan
* @brief 1. Set ctx to NULL. Expected result 1 is obtained.
2.Invoke the HITLS_GetError interface and send the return value. The expected result 2 is obtained
* @expect 1. Return the HITLS_ERR_SYSCALL message.
2. Link error codes are returned
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GetError_API_TC001(void)
{
FRAME_Init();
HITLS_Ctx *ctx = NULL;
HITLS_Config *config = NULL;
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetError(NULL, HITLS_SUCCESS) == HITLS_ERR_SYSCALL);
ASSERT_TRUE(HITLS_GetError(ctx, HITLS_SUCCESS) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SetAlpnProtos_API_TC001
* @title HITLS_SetAlpnProtos Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
* @expect
1. Return the HITLS_NULL_INPUT message.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetAlpnProtos_API_TC001()
{
HitlsInit();
uint8_t * alpnProtosname = (uint8_t *)"vpn|http";
uint32_t alpnProtosnameLen = sizeof(alpnProtosname);
ASSERT_TRUE(HITLS_SetAlpnProtos(NULL, alpnProtosname, alpnProtosnameLen) == HITLS_NULL_INPUT);
EXIT:
return;
}
/* END_CASE */
uint32_t SetPskClientCallback(HITLS_Ctx *ctx, const uint8_t *hint, uint8_t *identity, uint32_t maxIdentityLen,
uint8_t *psk, uint32_t maxPskLen)
{
(void)ctx;
(void)hint;
(void)identity;
(void)maxIdentityLen;
(void)psk;
(void)maxPskLen;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_CM_SetPskClientCallback_API_TC001
* @title HITLS_SetPskClientCallback Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetPskClientCallback interface to set callback. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetPskClientCallback_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetPskClientCallback(NULL, SetPskClientCallback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetPskClientCallback(ctx, SetPskClientCallback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
static uint32_t SetPskServerCallback(HITLS_Ctx *ctx, const uint8_t *identity, uint8_t *psk, uint32_t maxPskLen)
{
(void)ctx;
(void)identity;
(void)psk;
(void)maxPskLen;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_CM_SetPskServerCallback_API_TC001
* @title HITLS_SetPskServerCallback Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetPskServerCallback interface to set callback. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetPskServerCallback_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetPskServerCallback(NULL, SetPskServerCallback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetPskServerCallback(ctx, SetPskServerCallback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
static int32_t SetPskUsePsksessionCallback(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
uint32_t *idLen, HITLS_Session **session)
{
(void)ctx;
(void)hashAlgo;
(void)id;
(void)idLen;
(void)session;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_CM_SetPskUseSessionCallback_API_TC001
* @title HITLS_SetPskUseSessionCallback Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetPskUseSessionCallback interface to set callback. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetPskUseSessionCallback_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetPskUseSessionCallback(NULL, SetPskUsePsksessionCallback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetPskUseSessionCallback(ctx, SetPskUsePsksessionCallback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
int32_t SetPskFindSessionCallback(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
HITLS_Session **session)
{
(void)ctx;
(void)identity;
(void)identityLen;
(void)session;
return HITLS_SUCCESS;
}
/* @
* @test UT_TLS_CM_SetPskFindSessionCallback_API_TC001
* @title HITLS_SetPskFindSessionCallback Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetPskFindSessionCallback interface to set callback. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetPskFindSessionCallback_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetPskFindSessionCallback(NULL, SetPskFindSessionCallback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetPskFindSessionCallback(ctx, SetPskFindSessionCallback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SetNeedCheckPmsVersion_API_TC001
* @title HITLS_SetNeedCheckPmsVersion Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetNeedCheckPmsVersion interface to set parameter true. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetNeedCheckPmsVersion_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = NULL;
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetNeedCheckPmsVersion(NULL, true), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetNeedCheckPmsVersion(ctx, true), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SetPskIdentityHint_API_TC001
* @title HITLS_SetPskIdentityHint Interface Parameter Test
* @precon nan
* @brief
1. Set ctx to NULL. Expected result 1 is obtained.
2. Use the HITLS_SetPskIdentityHint interface to set parameter. Expected result 2
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SetPskIdentityHint_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
uint8_t * identityH = (uint8_t *)"123456";
uint32_t identityHintLen = strlen((char *)identityH);
ASSERT_TRUE(HITLS_SetPskIdentityHint(ctx, identityH, identityHintLen) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
return;
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SETTICKETNUMS_API_TC001
* @title HITLS_SetTicketNums interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_SetTicketNums interface and set the input parameter of the HITLS_Ctx to NULL.
* 3. Invoke the HITLS_SetTicketNums interface and set the input parameter of the ticketNums to 0.
* 4. Invoke the HITLS_SetTicketNums interface and set normal ticketNums.
* @expect
* 1. Initialization succeeded.
* 2. The interface returns HITLS_NULL_INPUT.
* 3. The interface returns HITLS_SUCCESS.
* 4. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SETTICKETNUMS_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetTicketNums(NULL, 0), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetTicketNums(ctx, 0), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetTicketNums(ctx, 3), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetTicketNums(ctx, 100), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GETTICKETNUMS_API_TC001
* @title HITLS_GetTicketNums interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_GetRecordPaddingCb interface and set the input parameter of the HITLS_Ctx to NULL.
* 3. Invoke the HITLS_GetRecordPaddingCb interface to get the default ticketNums.
* 4. Invoke the HITLS_GetRecordPaddingCb interface and set normal ticketNums.
* 5. Invoke the HITLS_GetRecordPaddingCb interface to get the ticketNums
* @expect
* 1. Initialization succeeded.
* 2. The interface returns ticketNums is 2.
* 3. The interface returns HITLS_NULL_INPUT.
* 4. The interface returns HITLS_SUCCESS.
* 5. Consistent with the configured ticketNums.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GETTICKETNUMS_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS13Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
const int defaultNum = 2;
int TicketNum = 3;
ASSERT_EQ(HITLS_GetTicketNums(NULL), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_GetTicketNums(ctx), defaultNum);
ASSERT_EQ(HITLS_SetTicketNums(ctx, TicketNum), HITLS_SUCCESS);
ASSERT_EQ(HITLS_GetTicketNums(ctx), TicketNum);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SETRECORDPADDINGCB_API_TC001
* @title HITLS_SetRecordPaddingCb interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_SetRecordPaddingCb interface and set the input parameter of the HITLS_Ctx to NULL.
* 3. Invoke the HITLS_SetRecordPaddingCb interface and set the input parameter of the callback to NULL.
* 4. Invoke the HITLS_SetRecordPaddingCb interface and set normal callback.
* @expect
* 1. Initialization succeeded.
* 2. The interface returns HITLS_NULL_INPUT.
* 3. The interface returns HITLS_SUCCESS.
* 4. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SETRECORDPADDINGCB_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetRecordPaddingCb(NULL, 0), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetRecordPaddingCb(ctx, NULL), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetRecordPaddingCb(ctx, Test_RecordPaddingCb), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GETRECORDPADDINGCB_API_TC001
* @title HITLS_GetRecordPaddingCb interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_GetRecordPaddingCb interface to get the default callback.
* 3. Invoke the HITLS_GetRecordPaddingCb interface and set the input parameter of the HITLS_Ctx to NULL.
* 4. Invoke the HITLS_GetRecordPaddingCb interface and set normal callback.
* 5. Invoke the HITLS_GetRecordPaddingCb interface to get the callback
* @expect
* 1. Initialization succeeded.
* 2. The interface returns NULL.
* 3. The interface returns HITLS_NULL_INPUT.
* 4. The interface returns HITLS_SUCCESS.
* 5. Consistent with the configured callback.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GETRECORDPADDINGCB_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_GetRecordPaddingCb(ctx), NULL);
ASSERT_EQ(HITLS_GetRecordPaddingCb(NULL), NULL);
ASSERT_TRUE(HITLS_SetRecordPaddingCb(ctx, Test_RecordPaddingCb) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetRecordPaddingCb(ctx) == Test_RecordPaddingCb);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SETRECORDPADDINGCBARG_API_TC001
* @title HITLS_SetRecordPaddingCbArg interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_SetRecordPaddingCbArg interface and set the input parameter of the HITLS_Ctx to NULL.
* 3. Invoke the HITLS_SetRecordPaddingCbArg interface and set the input parameter of the RecordPaddingArg to NULL.
* 4. Invoke the HITLS_SetRecordPaddingCbArg interface and set normal recordPaddingArg.
* @expect
* 1. Initialization succeeded.
* 2. The interface returns HITLS_NULL_INPUT.
* 3. The interface returns HITLS_SUCCESS.
* 4. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SETRECORDPADDINGCBARG_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
uint32_t arg = 1;
ASSERT_EQ(HITLS_SetRecordPaddingCbArg(NULL, &arg), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetRecordPaddingCbArg(ctx, NULL), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetRecordPaddingCbArg(ctx, &arg), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_GETRECORDPADDINGCBARG_API_TC001
* @title HITLS_GetRecordPaddingCbArg interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_GetRecordPaddingCbArg interface to get the default recordPaddingArg.
* 3. Invoke the HITLS_GetRecordPaddingCbArg interface and set the input parameter of the HITLS_Ctx to NULL.
* 4. Invoke the HITLS_GetRecordPaddingCbArg interface and set normal recordPaddingArg.
* 5. Invoke the HITLS_GetRecordPaddingCbArg interface to get the recordPaddingArg.
* @expect
* 1. Initialization succeeded.
* 2. The interface returns NULL.
* 3. The interface returns HITLS_NULL_INPUT.
* 4. The interface returns HITLS_SUCCESS.
* 5. Consistent with the configured recordPaddingArg.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GETRECORDPADDINGCBARG_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
int arg = 1;
ASSERT_EQ(HITLS_GetRecordPaddingCbArg(ctx), NULL);
ASSERT_EQ(HITLS_GetRecordPaddingCbArg(NULL), NULL);
ASSERT_EQ(HITLS_SetRecordPaddingCbArg(ctx, &arg), HITLS_SUCCESS);
ASSERT_EQ(*(int*)HITLS_GetRecordPaddingCbArg(ctx) , arg);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SETCLOSECHECKKEYUSAGE_API_TC001
* @title HITLS_SetCheckKeyUsage interface test
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Invoke the HITLS_SetCheckKeyUsage interface and set the input parameter of the HITLS_Ctx to NULL.
* 3. Invoke the HITLS_SetCheckKeyUsage interface and set the input parameter of the isClose to true.
* 4. Invoke the HITLS_SetCheckKeyUsage interface and set the input parameter of the isClose to false.
* @expect
* 1. Initialization succeeded.
* 2. The interface returns HITLS_NULL_INPUT.
* 3. The interface returns HITLS_SUCCESS.
* 4. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SETCLOSECHECKKEYUSAGE_API_TC001()
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_EQ(HITLS_SetCheckKeyUsage(NULL, true), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetCheckKeyUsage(ctx, true), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetCheckKeyUsage(ctx, false), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
static int32_t STUB_ChangeState(TLS_Ctx *ctx, uint32_t nextState)
{
int32_t ret = HITLS_SUCCESS;
if (HS_STATE_BUTT == nextState) {
if (true == ctx->isClient) {
ctx->hsCtx->hsMsg = NULL;
ret = HITLS_REC_NORMAL_RECV_BUF_EMPTY;
}
}
HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
hsCtx->state = nextState;
return ret;
}
static bool StateCompare(FRAME_LinkObj *link, HITLS_HandshakeState state)
{
if ((link->ssl->hsCtx != NULL) && (link->ssl->hsCtx->state == state)) {
if (state != TRY_RECV_FINISH) {
return true;
}
}
return false;
}
/** @
* @test UT_TLS_CM_HITLS_DOHANDSHAKE_API_TC001
* @title HITLS_DoHandShake Interface Test
* @precon nan
* @brief 1、Invoke the HITLS_DoHandShake to create tls connect. The expected result 1 is obtained
* @expect 1、connect success
@ */
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_DOHANDSHAKE_API_TC001()
{
FRAME_Init();
HITLS_Config *config = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
int32_t clientRet;
int32_t serverRet;
int32_t ret;
uint32_t count = 0;
FuncStubInfo tmpRpInfo = { 0 };
STUB_Init();
STUB_Replace(&tmpRpInfo, HS_ChangeState, STUB_ChangeState);
HITLS_SetEndPoint(client->ssl, true);
do {
if (StateCompare(client, HS_STATE_BUTT)) {
ret = HITLS_SUCCESS;
break;
}
clientRet = HITLS_DoHandShake(client->ssl);
if (clientRet != HITLS_SUCCESS) {
ret = clientRet;
if ((clientRet != HITLS_REC_NORMAL_IO_BUSY) && (clientRet != HITLS_REC_NORMAL_RECV_BUF_EMPTY)) {
break;
}
}
ret = FRAME_TrasferMsgBetweenLink(client, server);
if (ret != HITLS_SUCCESS) {
break;
}
if (StateCompare(server, HS_STATE_BUTT)) {
ret = HITLS_SUCCESS;
break;
}
serverRet = HITLS_DoHandShake(server->ssl);
if (serverRet != HITLS_SUCCESS) {
ret = serverRet;
if ((serverRet != HITLS_REC_NORMAL_IO_BUSY) && (serverRet != HITLS_REC_NORMAL_RECV_BUF_EMPTY)) {
break;
}
}
ret = FRAME_TrasferMsgBetweenLink(server, client);
if (ret != HITLS_SUCCESS) {
break;
}
if (clientRet == HITLS_SUCCESS && serverRet == HITLS_SUCCESS) {
ret = HITLS_SUCCESS;
break;
}
count++;
ret = HITLS_INTERNAL_EXCEPTION;
} while (count < 40);
ASSERT_EQ(ret, HITLS_SUCCESS);
EXIT:
STUB_Reset(&tmpRpInfo);
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SECURITY_SECURITYLEVEL_API_TC001
* @title HITLS_GetSecurityLevel and HITLS_CFG_GetSecurityLevel Interface Test
* @precon nan
* @brief HITLS_GetSecurityLevel
1、Invoke HITLS_GetSecurityLevel to obtain the default security level. The expected result 1 is obtained
2、Check the obtained security level. The expected result 2 is obtained
HITLS_CFG_GetSecurityLevel
3、Invoke HITLS_CFG_GetSecurityLevel to obtain the default security level. The expected result 1 is obtained
4、Check the obtained security level. The expected result 2 is obtained
* @expect 1、return HITLS_SUCCESS
2、The security level is 1
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SECURITY_SECURITYLEVEL_API_TC001()
{
HitlsInit();
HITLS_Ctx *ctx = NULL;
HITLS_Config *Config;
int32_t level;
Config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(Config != NULL);
ctx = HITLS_New(Config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetSecurityLevel(ctx, &level) == HITLS_SUCCESS);
ASSERT_TRUE(level == 1);
ASSERT_TRUE(HITLS_CFG_GetSecurityLevel(Config, &level) == HITLS_SUCCESS);
ASSERT_TRUE(level == 1);
EXIT:
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SECURITY_SECURITYLEVEL_API_TC002
* @title HITLS_SetSecurityLevel and HITLS_CFG_SetSecurityLevel Interface Parameter Test
* @precon nan
* @brief HITLS_SetSecurityLevel
1、Invoke the HITLS_SetSecurityLevel to configure the security level.The expected result 1 is obtained
2、Invoke HITLS_GetSecurityLevel to obtain the default security level. The expected result 2 is obtained
3、Check the obtained security level. The expected result 3 is obtained
HITLS_CFG_SetSecurityLevel
4、Invoke the HITLS_CFG_SetSecurityLevel to configure the security level.The expected result 1 is obtained
5、Invoke HITLS_GetSecurityLevel to obtain the default security level. The expected result 2 is obtained
6、Check the obtained security level. The expected result 3 is obtained
* @expect 1、return HITLS_SUCCESS
2、return HITLS_SUCCESS
3、The security level is equal to the configured security level.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SECURITY_SECURITYLEVEL_API_TC002()
{
HitlsInit();
HITLS_Ctx *ctx = NULL;
HITLS_Config *Config;
int32_t level;
Config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(Config != NULL);
ctx = HITLS_New(Config);
ASSERT_TRUE(ctx != NULL);
for(int32_t i = 0; i <= 5; i++){
ASSERT_TRUE(HITLS_SetSecurityLevel(ctx, i) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetSecurityLevel(ctx, &level) == HITLS_SUCCESS);
ASSERT_TRUE(level == i);
ASSERT_TRUE(HITLS_CFG_SetSecurityLevel(Config, i) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetSecurityLevel(Config, &level) == HITLS_SUCCESS);
ASSERT_TRUE(level == i);
}
EXIT:
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
}
/* END_CASE */
int32_t TEST_HITLS_SecurityCb(const HITLS_Ctx *ctx, const HITLS_Config *config, int32_t option,
int32_t bits, int32_t id, void *other, void *exData)
{
(void)ctx;
(void)config;
(void)option;
(void)bits;
(void)id;
(void)other;
(void)exData;
return HITLS_SUCCESS;
}
/** @
* @test UT_TLS_CM_SECURITY_SECURITYCB_API_TC001
* @title HITLS_SetSecurityCb HITLS_SetSecurityExData HITLS_GetSecurityCb and HITLS_GetSecurityExData Interface Test
* @precon nan
* @brief 1、Invoke the HITLS_SetSecurityCb interface and set the first parameter to NULL.
The expected result 1 is obtained
2、Invoke the HITLS_SetSecurityCb interface and transfer the first parameter to a normal parameter.
The expected result 2 is obtained
3、Invoke the HITLS_SetSecurityExData interface and set the first parameter to NULL. The expected result 3 is obtained
4、Invoke the HITLS_SetSecurityExData interface and transfer the first parameter to a normal parameter.
The expected result 4 is obtained
5、Invoke the HITLS_GetSecurityCb interface and set the parameter to NULL. The expected result 5 is obtained
6、Invoke the HITLS_GetSecurityCb interface and transfer the parameter to a normal parameter.
The expected result 5 is obtained
7、Invoke the HITLS_GetSecurityExData interface and set the parameter to NULL. The expected result 7 is obtained
8、Invoke the HITLS_GetSecurityExData interface and transfer the parameter to a normal parameter.
The expected result 8 is obtained
* @expect 1、return HITLS_NULL_INPUT
2、 return HITLS_SUCCESS
3、 return HITLS_NULL_INPUT
4、 return HITLS_SUCCESS
5、 return NULL
6、 The returned value is equal to the configured value TEST_HITLS_SecurityCb.
7、 return NULL
8、 The returned value is equal to the configured value securityExData.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SECURITY_SECURITYCB_API_TC001()
{
HitlsInit();
HITLS_Ctx *ctx = NULL;
HITLS_Config *Config;
Config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(Config != NULL);
ctx = HITLS_New(Config);
int32_t userdata = 0;
void *securityExData = &userdata;
ASSERT_EQ(HITLS_SetSecurityCb(NULL, TEST_HITLS_SecurityCb), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetSecurityCb(ctx, TEST_HITLS_SecurityCb), HITLS_SUCCESS);
ASSERT_EQ(HITLS_SetSecurityExData(NULL, securityExData), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_SetSecurityExData(ctx, securityExData), HITLS_SUCCESS);
ASSERT_EQ(HITLS_GetSecurityCb(NULL), NULL);
ASSERT_EQ(HITLS_GetSecurityCb(ctx), TEST_HITLS_SecurityCb);
ASSERT_EQ(HITLS_GetSecurityExData(NULL), NULL);
ASSERT_EQ(HITLS_GetSecurityExData(ctx), securityExData);
EXIT:
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CM_SECURITY_SECURITYCB_API_TC002
* @title HITLS_CFG_SetSecurityCb HITLS_CFG_SetSecurityExData HITLS_CFG_GetSecurityCb and
HITLS_CFG_GetSecurityExData Interface Test
* @precon nan
* @brief 1、Invoke the HITLS_CFG_SetSecurityCb interface and set the first parameter to NULL.
The expected result 1 is obtained
2、Invoke the HITLS_CFG_SetSecurityCb interface and transfer the first parameter to a normal parameter.
The expected result 2 is obtained
3、Invoke the HITLS_CFG_SetSecurityExData interface and set the first parameter to NULL.
The expected result 3 is obtained
4、Invoke the HITLS_CFG_SetSecurityExData interface and transfer the first parameter to a normal parameter.
The expected result 4 is obtained
5、Invoke the HITLS_CFG_GetSecurityCb interface and set the parameter to NULL. The expected result 5 is obtained
6、Invoke the HITLS_CFG_GetSecurityCb interface and transfer the parameter to a normal parameter.
The expected result 5 is obtained
7、Invoke the HITLS_CFG_GetSecurityExData interface and set the parameter to NULL. The expected result 7 is obtained
8、Invoke the HITLS_CFG_GetSecurityExData interface and transfer the parameter to a normal parameter.
The expected result 8 is obtained
* @expect 1、return HITLS_NULL_INPUT
2、 return HITLS_SUCCESS
3、 return HITLS_NULL_INPUT
4、 return HITLS_SUCCESS
5、 return NULL
6、 The returned value is equal to the configured value TEST_HITLS_SecurityCb.
7、 return NULL
8、 The returned value is equal to the configured value securityExData.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SECURITY_SECURITYCB_API_TC002()
{
HitlsInit();
HITLS_Config *Config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(Config != NULL);
int32_t userdata = 0;
void *securityExData = &userdata;
ASSERT_EQ(HITLS_CFG_SetSecurityCb(NULL, TEST_HITLS_SecurityCb), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetSecurityCb(Config, TEST_HITLS_SecurityCb), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_SetSecurityExData(NULL, securityExData), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetSecurityExData(Config, securityExData), HITLS_SUCCESS);
ASSERT_EQ(HITLS_CFG_GetSecurityCb(NULL), NULL);
ASSERT_EQ(HITLS_CFG_GetSecurityCb(Config), TEST_HITLS_SecurityCb);
ASSERT_EQ(HITLS_CFG_GetSecurityExData(NULL), NULL);
ASSERT_EQ(HITLS_CFG_GetSecurityExData(Config), securityExData);
EXIT:
HITLS_CFG_FreeConfig(Config);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_IS_DTLS_API_TC001
* @title Test HITLS_IsDtls
* @precon nan
* @brief HITLS_IsDtls
* 1. Input an empty TLS connection handle. Expected result 1.
* 2. Transfer the non-empty TLS connection handle information and leave isDtls blank. Expected result 1.
* 3. Transfer the non-empty TLS connection handle information. The isDtls parameter is not empty. Expected result 2.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CM_IS_DTLS_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t isDtls = 0;
ASSERT_TRUE(HITLS_IsDtls(ctx, &isDtls) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_IsDtls(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_IsDtls(ctx, &isDtls) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/*@
* @test UT_TLS_CM_GET_SELECTEDALPNPROTO_API_TC001
*
* @title test HITLS_GetSelectedAlpnProto interface
* @brief
* 1. Construct the CTX configuration. Expected result 1.
* 2. Construct the CTX connection handle. Expected result 1.
* 3. tls connection handle is NULL, Invoke the HITLS_GetSelectedAlpnProto interface. Expected result 2.
* 4. proto is NULL ,invoke the HITLS_CFG_SetSessionIdCtx interface.Expected result 2.
* 5. protoLen is NULL, Invoke the HITLS_GetSessionTicketKey interface.Expected result 2
* @expect 1. Return not NULL.
* 2. Return not HITLS_NULL_INPUT.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_SELECTEDALPNPROTO_API_TC001()
{
HitlsInit();
HITLS_Config *tlsConfig;
HITLS_Ctx *ctx = NULL;
tlsConfig = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
uint8_t * alpnProtosname = (uint8_t *)"vpn|http";
uint32_t alpnProtosnameLen = sizeof(alpnProtosname);
ASSERT_TRUE(HITLS_GetSelectedAlpnProto(NULL, &alpnProtosname, &alpnProtosnameLen) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_GetSelectedAlpnProto(ctx, NULL, &alpnProtosnameLen), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_GetSelectedAlpnProto(ctx, &alpnProtosname, NULL), HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
return;
}
/* END_CASE */
#define DATA_MAX_LEN 1024
/*@
* @test UT_TLS_CM_GET_SET_SESSIONTICKETKEY_API_TC001
*
* @title test HITLS_SetSessionTicketKey/HITLS_GetSessionTicketKey interface
* @brief 1. Construct the CTX connection handle. Expected result 1.
* 2. tls connection handle is NULL, Invoke the HITLS_SetSessionTicketKey interface. Expected result 2.
* 3. tls connection handle is NULL, Invoke the HITLS_GetSessionTicketKey interface.Expected result 2.
* @expect 1. Return not NULL.
* 2. Return HITLS_NULL_INPUT.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_GET_SET_SESSIONTICKETKEY_API_TC001(int version)
{
FRAME_Init();
uint8_t key[] = "748ab9f3dc1a23";
HITLS_Config *config = GetHitlsConfigViaVersion(version);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
uint8_t getKey[DATA_MAX_LEN] = {0};
uint32_t getKeySize = DATA_MAX_LEN;
uint32_t outSize = 0;
uint32_t ticketKeyRandLen = HITLS_TICKET_KEY_NAME_SIZE + HITLS_TICKET_KEY_SIZE + HITLS_TICKET_KEY_SIZE;
ASSERT_TRUE(HITLS_SetSessionTicketKey(NULL, key, ticketKeyRandLen) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetSessionTicketKey(NULL, getKey, getKeySize, &outSize) == HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_SET_SESSIONIDCTX_API_TC001
*
* @title test HITLS_CFG_SetSessionIdCtx interface
* @precon nan
* @brief 1. Construct the CTX connection handle. Expected result 1.
* 2. config is NULL, Invoke the HITLS_CFG_SetSessionIdCtx interface. Expected result 4.
* 3. invoke the HITLS_CFG_SetSessionIdCtx interface.Expected result 2.
* 4. tls connection handle is NULL, Invoke the HITLS_SetSessionIdCtx interface.Expected
* result 4.
* 5. Invoke the HITLS_SetSessionIdCtx interface. Expected result 2.
* @expect 1. Return not NULL.
* 2. Return not HITLS_NULL_INPUT.
* 3. Return NULL.
* 4. Return HITLS_NULL_INPUT.
@ */
/* BEGIN_CASE */
void UT_TLS_CM_SET_SESSIONIDCTX_API_TC001(int version)
{
FRAME_Init();
char *key = "748ab9f3dc1a23";
HITLS_Config *config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
HITLS_Ctx *ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
uint32_t keyLen = strlen(key);
ASSERT_TRUE(HITLS_CFG_SetSessionIdCtx(NULL, (const uint8_t *)key, keyLen)== HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetSessionIdCtx(config, (const uint8_t *)key, keyLen)!= HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetSessionIdCtx(NULL, (const uint8_t *)key, keyLen)== HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetSessionIdCtx(ctx, (const uint8_t *)key, keyLen)!= HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_TLS_CM_HITLS_GetCertificate_API_TC001
* @title Cover the input parameter of the HITLS_GetCertificate interface.
* @precon nan
* @brief 1. Invoke the HITLS_GetCertificate interface and leave ctx blank. Expected result 1.
* 2. Invoke the HITLS_GetPeerCertificate interface and leave ctx blank. Expected result 1.
* 3. Invoke the HITLS_GetPeerCertificate interface. The value of ctx is not empty and the value of ctx->session
* is empty. Expected result 1.
* 4. Invoke the HITLS_GetPeerCertChain interface and leave ctx blank. Expected result 1.
* 5. Invoke the HITLS_GetPeerCertChain interface. The value of ctx is not empty and the value of ctx->session is
* empty. Expected result 1.
* @expect 1.Return NULL
@ */
/* BEGIN_CASE */
void UT_TLS_CM_HITLS_GetCertificate_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetCertificate(NULL) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertificate(NULL) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertChain(NULL) == NULL);
ctx->session = NULL;
ASSERT_TRUE(HITLS_GetPeerCertificate(ctx) == NULL);
ASSERT_TRUE(HITLS_GetPeerCertChain(ctx) == NULL);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_HITLS_CM_HITLS_Set_and_Get_ErrorCode_API_TC001
* @spec -
* @title Cover the input parameter of HITLS_SetChainStore and HITLS_GetChainStore interface
* @precon nan
* @brief 1. Invoke the HITLS_GetErrorCode interface and leave ctx blank. Expected result 1.
2. Invoke the HITLS_GetErrorCode interface with ctx not empty. Expected result 3.
3. Invoke the HITLS_SetErrorCode interface and leave ctx blank. Expected result 1.
4. Invoke the HITLS_SetErrorCode interface. The value of ctx is not empty. Expected result 2.
* @expect 1. Return HITLS_NULL_INPUT
2. Return HITLS_SUCCESS
3. Return errorCode
* @prior Level 1
* @auto TRUE
@ */
/* BEGIN_CASE */
void UT_HITLS_CM_HITLS_Set_and_Get_ErrorCode_API_TC001(int version)
{
HitlsInit();
HITLS_Config *tlsConfig = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t errorCode = 0;
tlsConfig = HitlsNewCtx(version);
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
HITLS_SetErrorCode(ctx, errorCode);
ASSERT_TRUE(HITLS_GetErrorCode(NULL) == HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_GetErrorCode(ctx), errorCode);
ASSERT_TRUE(HITLS_SetErrorCode(ctx, errorCode) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_SetErrorCode(NULL, errorCode) == HITLS_NULL_INPUT);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_HITLS_CM_SET_GET_ENCRYPTTHENMAC_TC001
* @title Test the HITLS_SetEncryptThenMac and HITLS_GetEncryptThenMac interfaces.
* @precon nan
* @brief HITLS_SetEncryptThenMac
* 1. Transfer an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set encryptThenMacType to an invalid value. Expected result 2.
* 3. Transfer a non-empty TLS connection handle and set encryptThenMacType to a valid value. Expected result 3.
* HITLS_GetEncryptThenMac
* 1. Transfer an empty TLS connection handle. Expected result 1.
* 2. Transfer an empty encryptThenMacType pointer. Expected result 1.
* 3. Transfer the non-null TLS connection handle information and ensure that the encryptThenMacType pointer is not null. Expected result 3.
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCES and isEncryptThenMac is True
* 3. Return HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_HITLS_CM_SET_GET_ENCRYPTTHENMAC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t encryptThenMacType = 0;
ASSERT_TRUE(HITLS_SetEncryptThenMac(ctx, encryptThenMacType) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetEncryptThenMac(ctx, &encryptThenMacType) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetEncryptThenMac(ctx, NULL) == HITLS_NULL_INPUT);
encryptThenMacType = 1;
ASSERT_TRUE(HITLS_SetEncryptThenMac(ctx, encryptThenMacType) == HITLS_SUCCESS);
encryptThenMacType = -1;
ASSERT_TRUE(HITLS_SetEncryptThenMac(ctx, encryptThenMacType) == HITLS_SUCCESS);
ASSERT_TRUE(config->isEncryptThenMac = true);
uint32_t getencryptThenMacType = -1;
ASSERT_TRUE(HITLS_GetEncryptThenMac(ctx, &getencryptThenMacType) == HITLS_SUCCESS);
ASSERT_TRUE(getencryptThenMacType == true);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/* @
* @test UT_HITLS_CM_HITLS_GetPostHandshakeAuthSupport_TC001
* @title Test the HITLS_GetPostHandshakeAuthSupport interfaces.
* @precon nan
* @brief 1. Transfer an empty TLS connection handle. Expected result 1.
* 2. Transfer a non-empty TLS connection handle and set isSupport to to NULL. Expected result 1.
* 3. Transfer a non-empty TLS connection handle and set isSupport to a valid value. Expected result 2.
* @expect 1. Return HITLS_NULL_INPUT
* 2. Return HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_HITLS_CM_HITLS_GetPostHandshakeAuthSupport_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint32_t isSupport = 0;
ASSERT_TRUE(HITLS_GetPostHandshakeAuthSupport(NULL, NULL) == HITLS_NULL_INPUT);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetEncryptThenMac(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetEncryptThenMac(ctx, isSupport) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_frame_cm_interface.c | C | unknown | 172,126 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <sys/types.h>
#include <regex.h>
#include "securec.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "hitls.h"
#include "frame_tls.h"
#include "cert_callback.h"
#include "hitls_config.h"
#include "hitls_error.h"
#include "bsl_errno.h"
#include "bsl_uio.h"
#include "frame_io.h"
#include "uio_abstraction.h"
#include "tls.h"
#include "tls_config.h"
#include "logger.h"
#include "process.h"
#include "hs_ctx.h"
#include "hlt.h"
#include "stub_replace.h"
#include "hitls_type.h"
#include "frame_link.h"
#include "session_type.h"
#include "common_func.h"
#include "hitls_func.h"
#include "hitls_cert_type.h"
#include "cert_mgr_ctx.h"
#include "parser_frame_msg.h"
#include "recv_process.h"
#include "simulate_io.h"
#include "rec_wrapper.h"
#include "cipher_suite.h"
#include "alert.h"
#include "conn_init.h"
#include "pack.h"
#include "send_process.h"
#include "cert.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_type.h"
#include "hs.h"
#include "hs_state_recv.h"
#include "app.h"
#include "record.h"
#include "rec_conn.h"
#include "session.h"
#include "frame_msg.h"
#include "pack_frame_msg.h"
#include "cert_mgr.h"
#include "hs_extensions.h"
#include "hlt_type.h"
#include "sctp_channel.h"
#include "hitls_crypt_init.h"
#include "crypt_default.h"
#include "stub_crypt.h"
#include "hitls_crypt.h"
#include "security.h"
/* END_HEADER */
#define DEFAULT_DESCRIPTION_LEN 128
#define ERROR_HITLS_GROUP 1
#define ERROR_HITLS_SIGNATURE 0xffffu
typedef struct {
uint16_t version;
BSL_UIO_TransportType uioType;
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_Session *clientSession; /* Set the session to the client for session resume. */
} ResumeTestInfo;
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
switch (ver) {
case TLS1_2:
case HITLS_VERSION_TLS12:
return HITLS_CFG_NewTLS12Config();
case TLS1_3:
case HITLS_VERSION_TLS13:
return HITLS_CFG_NewTLS13Config();
case DTLS1_2:
case HITLS_VERSION_DTLS12:
return HITLS_CFG_NewDTLS12Config();
default:
return NULL;
}
}
static int32_t UT_ClientHelloCb(HITLS_Ctx *ctx, int32_t *alert, void *arg)
{
(void)ctx;
(void)alert;
return *(int32_t *)arg;
}
static int32_t UT_CookieGenerateCb(HITLS_Ctx *ctx, uint8_t *cookie, uint32_t *cookie_len)
{
(void)ctx;
(void)cookie;
(void)cookie_len;
return 0;
}
static int32_t UT_CookieVerifyCb(HITLS_Ctx *ctx, const uint8_t *cookie, uint32_t cookie_len)
{
(void)ctx;
(void)cookie;
(void)cookie_len;
return 1;
}
/** @
* @test UT_TLS_CFG_UPREF_FUNC_TC001
* @spec -
* @title Invoke the HITLS_CFG_UpRef interface to change the number of config reference times.
* @precon nan
* @brief 1. Apply for and initialize config.
2. Invoke the HITLS_CFG_UpRef interface and transfer the config parameter.
3. Check the number of times the config file is referenced.
* @expect 1. The application is successful.
2. The invoking is successful.
3. The number of references is 2.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_UPREF_FUNC_TC001()
{
HitlsInit();
HITLS_Config *config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ASSERT_TRUE(HITLS_CFG_UpRef(config) == HITLS_SUCCESS);
ASSERT_TRUE(config->references.count == 2);
HITLS_CFG_FreeConfig(config);
ASSERT_TRUE(config->references.count == 1);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_RESUMPTIONONRENEGOSUPPORT_API_TC001
* @Specifications-
* @title Test the HITLS_CFG_SetResumptionOnRenegoSupport interface for setting ResumptionOnRenegoSupport.
* @preppynan
* @brief HITLS_CFG_Setting negotiation support
* 1. Transfer empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and support invalid values. Expected result 2 is displayed.
* 3. The input configuration information is not empty and the value can be valid. Expected result 3 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and isResumptionOnRenego is set to true.
* 3. HITLS_SUCCES is returned and isResumptionOnRenego is set to true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_RESUMPTIONONRENEGOSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
ASSERT_TRUE(HITLS_CFG_SetResumptionOnRenegoSupport(config, support) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_SetResumptionOnRenegoSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(config->isResumptionOnRenego == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetResumptionOnRenegoSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(config->isResumptionOnRenego == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_NOCLIENTCERTSUPPORT_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetNoClientCertSupport and HITLS_CFG_GetNoClientVerifySupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetNoClientCertSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set support to an invalid value. Expected result 2 is obtained.
* 3. Transfer non-empty configuration information and set support to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetNoClientCertSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty isSupport pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and config->isSupportNoClientCert is true.
* 3. Returns HITLS_SUCCES and config->isSupportNoClientCert is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_NOCLIENTCERTSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_CFG_SetNoClientCertSupport(config, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetNoClientCertSupport(config, &isSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetNoClientCertSupport(config, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_CFG_SetNoClientCertSupport(config, support) == HITLS_SUCCESS);
support = -1;
ASSERT_TRUE(HITLS_CFG_SetNoClientCertSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetNoClientCertSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetNoClientCertSupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetNoClientCertSupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_CLIENTVERIFYSUPPORT_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetClientVerifySupport and HITLS_CFG_GetClientVerifySupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetClientVerifySupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set support to an invalid value. Expected result 2 is obtained.
* 3. Transfer non-empty configuration information and set support to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetClientVerifySupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty isSupport pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned, and config->isSupportClientVerify is true and isSupportVerifyNone is false.
* 3. HITLS_SUCCES is returned, and config->isSupportClientVerify is true or false. isSupportVerifyNone and
isSupportVerifyNone are mutually exclusive, but can be false at the same time.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_CLIENTVERIFYSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool support = -1;
uint8_t isSupport = -1;
ASSERT_TRUE(HITLS_CFG_SetClientVerifySupport(config, support) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetClientVerifySupport(config, &isSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetClientVerifySupport(config, NULL) == HITLS_NULL_INPUT);
support = true;
ASSERT_TRUE(HITLS_CFG_SetClientVerifySupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportVerifyNone == false);
support = -1;
ASSERT_TRUE(HITLS_CFG_SetClientVerifySupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetClientVerifySupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == true);
support = false;
ASSERT_TRUE(HITLS_CFG_SetClientVerifySupport(config, support) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetClientVerifySupport(config, &isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(isSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_TMPDH_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetTmpDh interface.
* @precon nan
* @brief HITLS_CFG_SetTmpDh
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and leave dhPkey empty. Expected result 1 is obtained.
* 3. Transfer non-empty configuration information and set dhPkey to a non-empty value. Expected result 2 is displayed.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_TMPDH_API_TC001(int tlsVersion, int securityBits, int securityLevel)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_CRYPT_Key *dhPkey = NULL;
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
dhPkey = HITLS_CRYPT_GenerateDhKeyBySecbits(LIBCTX_FROM_CONFIG(config), ATTRIBUTE_FROM_CONFIG(config), config,
securityBits);
ASSERT_TRUE(HITLS_CFG_SetTmpDh(NULL, dhPkey) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetSecurityLevel(config, securityLevel) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetTmpDh(config, NULL) == HITLS_NULL_INPUT);
if (SECURITY_GetSecbits(securityLevel) <= securityBits) {
ASSERT_TRUE(HITLS_CFG_SetTmpDh(config, dhPkey) == HITLS_SUCCESS);
} else {
ASSERT_TRUE(HITLS_CFG_SetTmpDh(config, dhPkey) == HITLS_CRYPT_ERR_DH);
}
EXIT:
if (SECURITY_GetSecbits(securityLevel) > securityBits) {
SAL_CRYPT_FreeDhKey(dhPkey);
}
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
int32_t g_securityBits = 0;
static HITLS_CRYPT_Key *UT_TmpDhCb(HITLS_Ctx *ctx, int32_t isExport, uint32_t keyLen)
{
(void)isExport;
(void)keyLen;
HITLS_CRYPT_Key *dhPkey = NULL;
HITLS_Config *config = &ctx->config.tlsConfig;
dhPkey = HITLS_CRYPT_GenerateDhKeyBySecbits(LIBCTX_FROM_CONFIG(config), ATTRIBUTE_FROM_CONFIG(config), config,
g_securityBits);
return dhPkey;
}
/* @
* @test UT_TLS_CFG_SET_TMPDHCB_API_TC001
* @title Set tmpdhcallback. The link setup status varies according to the security level.
* @precon nan
* @brief
* 1. Set the RSA certificate and algorithm suite.
* 2. Set dh callback and generate dhkey through dhcb.
* 3. Set security level to 2, set tmpdh key to 80 bits, and set up a link.
* 4. Set security level to 2, set tmpdh key to 112 bits, and set up a link.
* 5. Set security level to 1, set tmpdh key to 128 bits, and set up a link.
* 6. Set security level to 2, set tmpdh key to 128 bits, and set up a link.
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The link fails to be set up.
* 4. The link is set up successfully.
* 5. The link is set up successfully.
* 6. The link is set up successfully.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_TMPDHCB_API_TC001(int securityBits, int securityLevel)
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint16_t pfsCipherSuites[] = {HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256};
HITLS_Config *clientConfig = HITLS_CFG_NewTLS12Config();
HITLS_Config *serverConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(clientConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, NULL,
RSA_SHA256_PRIV_PATH3, NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(serverConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, NULL,
RSA_SHA256_PRIV_PATH3, NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(clientConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) ==
HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(serverConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) ==
HITLS_SUCCESS);
HITLS_CFG_SetDhAutoSupport(serverConfig, false);
g_securityBits = securityBits;
ASSERT_TRUE(HITLS_CFG_SetTmpDhCb(serverConfig, UT_TmpDhCb) == HITLS_SUCCESS);
FRAME_CertInfo certInfo = {0, 0, 0, 0, 0, 0};
client = FRAME_CreateLinkWithCert(clientConfig, BSL_UIO_TCP, &certInfo);
server = FRAME_CreateLinkWithCert(serverConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_TRUE(HITLS_SetSecurityLevel(server->ssl, securityLevel) == HITLS_SUCCESS);
if (SECURITY_GetSecbits(securityLevel) > securityBits) {
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_KEY_EXCHANGE), HITLS_SUCCESS);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_ERR_GET_DH_KEY);
} else {
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
}
EXIT:
HITLS_CFG_FreeConfig(serverConfig);
HITLS_CFG_FreeConfig(clientConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_CLIENTHELLOCB_API_TC001
* @title Test the HITLS_CFG_SetClientHelloCb interface.
* @precon nan
* @brief HITLS_CFG_SetClientHelloCb
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and leave callback empty. Expected result 1 is obtained.
* 3. Transfer non-empty configuration information and set callback to a non-empty value. Expected result 2 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_CLIENTHELLOCB_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
int32_t cbRetVal = 0;
ASSERT_TRUE(HITLS_CFG_SetClientHelloCb(config, UT_ClientHelloCb, &cbRetVal) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_SetClientHelloCb(config, NULL, &cbRetVal) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetClientHelloCb(config, UT_ClientHelloCb, &cbRetVal) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_COOKIEGENERATECB_API_TC001
* @title Test the HITLS_CFG_SetCookieGenCb interface.
* @precon nan
* @brief HITLS_CFG_SetCookieGenCb
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and leave callback empty. Expected result 1 is obtained.
* 3. Transfer non-empty configuration information and set callback to a non-empty value. Expected result 2 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_COOKIEGENERATECB_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, UT_CookieGenerateCb) == HITLS_NULL_INPUT);
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, UT_CookieGenerateCb) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_COOKIEVERIFYCB_API_TC001
* @title Test the HITLS_CFG_SetCookieVerifyCb interface.
* @precon nan
* @brief HITLS_CFG_SetCookieVerifyCb
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and leave callback empty. Expected result 1 is obtained.
* 3. Transfer non-empty configuration information and set callback to a non-empty value. Expected result 2 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_COOKIEVERIFYCB_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, UT_CookieVerifyCb) == HITLS_NULL_INPUT);
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, UT_CookieVerifyCb) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_VERSION_API_TC001
* @title Test the HITLS_CFG_SetVersion, HITLS_CFG_GetMinVersion, and HITLS_CFG_GetMaxVersion interfaces.
* @precon nan
* @brief HITLS_CFG_SetVersion
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information, set minVersion to 0, and set maxVersion to a value other than 0.
* Expected result 2 is obtained.
* 3. Transfer non-empty configuration information, set minVersion to 0, and set maxVersion to 0. Expected result 3 is
* obtained.
* 4. Transfer non-empty configuration information, set minVersion to 0, and set maxVersion to 0. Expected result 4 is
* obtained.
* 5. Transfer non-empty configuration information, and set both minVersion and maxVersion to 0. Expected result 5 is
* obtained.
* 5. Transfer non-empty configuration information, set minVersion to dtls, and set maxVersion to dtls. Expected result 5
* is obtained.
* 6. Transfer non-empty configuration information, set minVersion to TLCP, and set maxVersion to TLCP. Expected result 5
* is obtained.
* HITLS_CFG_GetMinVersion
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty MinVersion pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the MinVersion pointer is not null. Expected result 5 is
* obtained.
* HITLS_CFG_GetMaxVersion
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Pass an empty MaxVersion pointer. Expected result 1 is obtained.
* 3. Transfer non-null configuration information and ensure that the MaxVersion pointer is not null. Expected result 5
* is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES and minVersion is HITLS_VERSION_SSL30.
* 3. Returns HITLS_SUCCES and maxVersion is HITLS_VERSION_TLS13.
* 4. The HITLS_SUCCES table is returned, and the version value is 0, which is cleared.
* 5. Returns HITLS_SUCCES with minVersion and maxVersion set to the configured values.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_VERSION_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
HITLS_Config *dtlsConfig = NULL;
HITLS_Config *tlcpConfig = NULL;
uint16_t minVersion = 0;
uint16_t maxVersion = 0;
ASSERT_TRUE(HITLS_CFG_SetVersion(config, minVersion, maxVersion) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, &minVersion) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, &maxVersion) == HITLS_NULL_INPUT);
config = HITLS_CFG_NewTLSConfig();
dtlsConfig = HITLS_CFG_NewDTLSConfig();
tlcpConfig = HITLS_CFG_NewTLCPConfig();
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetVersion(config, 0, 0) == HITLS_SUCCESS);
ASSERT_TRUE(config->version == 0);
ASSERT_TRUE(HITLS_CFG_SetVersion(config, HITLS_VERSION_TLS12, HITLS_VERSION_TLS13) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_TLS12 && maxVersion == HITLS_VERSION_TLS13);
ASSERT_TRUE(HITLS_CFG_SetVersion(config, 0, HITLS_VERSION_TLS13) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_TLS12 && maxVersion == HITLS_VERSION_TLS13);
ASSERT_TRUE(HITLS_CFG_SetVersion(config, HITLS_VERSION_TLS12, 0) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(config, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(config, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_TLS12 && maxVersion == HITLS_VERSION_TLS13);
ASSERT_TRUE(HITLS_CFG_SetVersion(dtlsConfig, HITLS_VERSION_DTLS12, HITLS_VERSION_DTLS12) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(dtlsConfig, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(dtlsConfig, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_DTLS12 && maxVersion == HITLS_VERSION_DTLS12);
ASSERT_TRUE(HITLS_CFG_SetVersion(tlcpConfig, HITLS_VERSION_TLCP_DTLCP11, HITLS_VERSION_TLCP_DTLCP11) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMinVersion(tlcpConfig, &minVersion) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxVersion(tlcpConfig, &maxVersion) == HITLS_SUCCESS);
ASSERT_TRUE(minVersion == HITLS_VERSION_TLCP_DTLCP11 && maxVersion == HITLS_VERSION_TLCP_DTLCP11);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_CFG_FreeConfig(dtlsConfig);
HITLS_CFG_FreeConfig(tlcpConfig);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_HASHID_API_TC001
* @title Test the HITLS_CFG_GetHashId interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2. Transfer an empty hashId. Expected result 1 is obtained.
* 3. Import the HITLS_RSA_WITH_AES_128_CBC_SHA cipher suite and set hashAlg to HITLS_HASH_BUTT. Expected result 2 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCESS is returned and HashId is HITLS_HASH_SHA1.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_HASHID_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
HITLS_HashAlgo hashId = HITLS_HASH_BUTT;
ASSERT_TRUE(HITLS_CFG_GetHashId(cipher, &hashId) == HITLS_NULL_INPUT);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_CBC_SHA;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(HITLS_CFG_GetHashId(cipher, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetHashId(cipher, &hashId) == HITLS_SUCCESS);
ASSERT_TRUE(hashId == HITLS_HASH_SHA1);
EXIT:
return;
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_MACID_API_TC001
* @title Test the HITLS_CFG_GetMacId interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2. Input an empty macAlg. Expected result 1
* 3. Input the HITLS_RSA_WITH_AES_128_CBC_SHA cipher suite and set macAlg to HITLS_MAC_BUTT. Expected result 2 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCESS and macAlg is HITLS_MAC_1.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_MACID_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
HITLS_MacAlgo macAlg = HITLS_MAC_BUTT;
ASSERT_TRUE(HITLS_CFG_GetMacId(cipher, &macAlg) == HITLS_NULL_INPUT);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_CBC_SHA;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(HITLS_CFG_GetMacId(cipher, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMacId(cipher, &macAlg) == HITLS_SUCCESS);
ASSERT_TRUE(macAlg == HITLS_MAC_1);
EXIT:
return;
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_KEYEXCHID_API_TC001
* @title Test the HITLS_CFG_GetKeyExchId interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2. Input null kxAlg. Expected result 1
* 3. Input the HITLS_RSA_WITH_AES_128_CBC_SHA cipher suite and set kxAlg to HITLS_KEY_EXCH_BUTT. Expected result 2 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCESS and kxAlg is HITLS_KEY_EXCH_RSA.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_KEYEXCHID_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
HITLS_KeyExchAlgo kxAlg = HITLS_KEY_EXCH_BUTT;
ASSERT_TRUE(HITLS_CFG_GetKeyExchId(cipher, &kxAlg) == HITLS_NULL_INPUT);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_CBC_SHA;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(HITLS_CFG_GetKeyExchId(cipher, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetKeyExchId(cipher, &kxAlg) == HITLS_SUCCESS);
ASSERT_TRUE(kxAlg == HITLS_KEY_EXCH_RSA);
EXIT:
return;
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_CIPHERSUITESTDNAME_API_TC001
* @title Test the HITLS_CFG_GetCipherSuiteStdName interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2.Import the HITLS_RSA_WITH_AES_128_CBC_SHA cipher suite. Expected result 2 is obtained.
* @expect
* 1. Return "(NONE)"
* 2. Return "TLS_RSA_WITH_AES_128_CBC_SHA256"
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_CIPHERSUITESTDNAME_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
ASSERT_TRUE(strcmp((char *)HITLS_CFG_GetCipherSuiteStdName(cipher),"(NONE)") == 0);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_CBC_SHA;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(strcmp((char *)HITLS_CFG_GetCipherSuiteStdName(cipher),"TLS_RSA_WITH_AES_128_CBC_SHA") == 0);
EXIT:
return;
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_DESCRIPTION_API_TC001
* @title Test the HITLS_CFG_GetDescription interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2. Input an empty buff. Expected result 1 is obtained.
* 3. Transfer a buff whose length is less than the length of CIPHERSUITE_DESCRIPTION_MAXLEN. Expected result 1 is
* obtained.
* 4. Transfer the abnormal algorithm name cipher suite. Expected result 2 is obtained.
* 5. Import the HITLS_RSA_WITH_AES_128_CBC_SHA cipher suite whose buff size is DEFAULT_DESCRIPTION_LEN. Expected result
* 3 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_CONFIG_INVALID_LENGTH.
* 3. Returns HITLS_SUCCESS, and buff is Description.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_DESCRIPTION_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
char buff[DEFAULT_DESCRIPTION_LEN] = {0};
ASSERT_TRUE(HITLS_CFG_GetDescription(cipher, (uint8_t *)buff, sizeof(buff)) == HITLS_NULL_INPUT);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_CBC_SHA;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(HITLS_CFG_GetDescription(cipher, NULL, sizeof(buff)) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetDescription(cipher, (uint8_t *)buff, 0) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetDescription(cipher, (uint8_t *)buff, sizeof(buff)) == HITLS_SUCCESS);
HITLS_Cipher *newCipher = (HITLS_Cipher *)malloc(sizeof(HITLS_Cipher));
memcpy(newCipher, cipher, sizeof(HITLS_Cipher));
newCipher->name =
"************************************************************************************************************";
ASSERT_TRUE(HITLS_CFG_GetDescription(newCipher, (uint8_t *)buff, sizeof(buff)) == HITLS_CONFIG_INVALID_LENGTH);
EXIT:
free(newCipher);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_CIPHER_ISAEAD_API_TC001
* @title Test the HITLS_CIPHER_IsAead interface.
* @precon nan
* @brief
* 1. Input an empty cipher suite. Expected result 1 is obtained.
* 2. Import the HITLS_RSA_WITH_AES_128_GCM_SHA256 cipher suite. Expected result 2 is obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCESS and isAead is true.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_CIPHER_ISAEAD_API_TC001(void)
{
const HITLS_Cipher *cipher = NULL;
uint8_t isAead = false;
ASSERT_TRUE(HITLS_CIPHER_IsAead(cipher, &isAead) == HITLS_NULL_INPUT);
const uint16_t cipherID = HITLS_RSA_WITH_AES_128_GCM_SHA256;
cipher = HITLS_CFG_GetCipherByID(cipherID);
ASSERT_TRUE(HITLS_CIPHER_IsAead(cipher, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CIPHER_IsAead(cipher, &isAead) == HITLS_SUCCESS);
ASSERT_TRUE(isAead == true);
EXIT:
return;
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_VERSIONSUPPORT_API_TC001
* @spec -
* @title Test the HITLS_CFG_SetVersionSupport and HITLS_CFG_GetVersionSupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetVersionSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set version to an invalid value. Expected result 2 is obtained.
* 3. Transfer non-empty configuration information and set version to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetVersionSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Pass the null version pointer. Expected result 1 is obtained.
* 3. Transfer non-null configuration information and ensure that the version pointer is not null. Expected result 4 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned, and invalid values in config are filtered out.
* 3. HITLS_SUCCES is returned and config is the expected value.
* 4. The HITLS_SUCCES parameter is returned, and the version parameter is set to the value recorded in the config file.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_VERSIONSUPPORT_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint32_t version = 0;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, &version) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, NULL) == HITLS_NULL_INPUT);
version = (TLS13_VERSION_BIT << 1) | TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
version = TLS13_VERSION_BIT | TLS12_VERSION_BIT;
ASSERT_TRUE(HITLS_CFG_SetVersionSupport(config, version) == HITLS_SUCCESS);
ASSERT_TRUE(config->minVersion == HITLS_VERSION_TLS12 && config->maxVersion == HITLS_VERSION_TLS13);
uint32_t getversion = 0;
ASSERT_TRUE(HITLS_CFG_GetVersionSupport(config, &getversion) == HITLS_SUCCESS);
ASSERT_TRUE(getversion == config->version);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_QUIETSHUTDOWN_API_TC001
* @title Test the HITLS_CFG_SetQuietShutdown and HITLS_CFG_GetQuietShutdown interfaces.
* @precon nan
* @brief HITLS_CFG_SetQuietShutdown
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set mode to an invalid value. Expected result 2 is obtained.
* 3. Transfer non-empty configuration information and set mode to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetQuietShutdown
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer a null mode pointer. Expected result 1 is obtained.
* 3. Transfer non-null configuration information and ensure that the mode pointer is not null. Expected result 3 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_CONFIG_INVALID_SET
* 3. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_QUIETSHUTDOWN_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
int32_t mode = 0;
ASSERT_TRUE(HITLS_CFG_SetQuietShutdown(config, mode) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetQuietShutdown(config, &mode) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetQuietShutdown(config, NULL) == HITLS_NULL_INPUT);
mode = 1;
ASSERT_TRUE(HITLS_CFG_SetQuietShutdown(config, mode) == HITLS_SUCCESS);
mode = 2;
ASSERT_TRUE(HITLS_CFG_SetQuietShutdown(config, mode) == HITLS_CONFIG_INVALID_SET);
int32_t getMode = -1;
ASSERT_TRUE(HITLS_CFG_GetQuietShutdown(config, &getMode) == HITLS_SUCCESS);
ASSERT_TRUE(getMode == config->isQuietShutdown);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_CIPHERSERVERPREFERENCE_API_TC001
* @title Test the HITLS_CFG_SetCipherServerPreference and HITLS_CFG_GetCipherServerPreference interfaces.
* @precon nan
* @brief HITLS_CFG_SetCipherServerPreference
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set isSupport to an invalid value. Expected result 2 is obtained.
* 3. Transfer a non-empty configuration information and set isSupport to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetCipherServerPreference
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty isSupport pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and config->isSupportServerPreference is set to true.
* 3. Returns HITLS_SUCCES, and config->isSupportServerPreference is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_CIPHERSERVERPREFERENCE_API_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool isSupport = false;
bool getIsSupport = false;
ASSERT_TRUE(HITLS_CFG_SetCipherServerPreference(config, isSupport) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetCipherServerPreference(config, &getIsSupport) == HITLS_NULL_INPUT);
switch (tlsVersion) {
case HITLS_VERSION_TLS12:
config = HITLS_CFG_NewTLS12Config();
break;
case HITLS_VERSION_TLS13:
config = HITLS_CFG_NewTLS13Config();
break;
default:
config = NULL;
break;
}
ASSERT_TRUE(HITLS_CFG_GetCipherServerPreference(config, NULL) == HITLS_NULL_INPUT);
isSupport = true;
ASSERT_TRUE(HITLS_CFG_SetCipherServerPreference(config, isSupport) == HITLS_SUCCESS);
isSupport = 2;
ASSERT_TRUE(HITLS_CFG_SetCipherServerPreference(config, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportServerPreference = true);
isSupport = false;
ASSERT_TRUE(HITLS_CFG_SetCipherServerPreference(config, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetCipherServerPreference(config, &getIsSupport) == HITLS_SUCCESS);
ASSERT_TRUE(getIsSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_HELLO_VERIFY_REQ_API_TC001
* @title Test the HITLS_CFG_SetDtlsCookieExchangeSupport and HITLS_CFG_GetDtlsCookieExchangeSupport interfaces.
* @precon nan
* @brief HITLS_CFG_SetDtlsCookieExchangeSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer non-empty configuration information and set isSupport to an invalid value. Expected result 2 is obtained.
* 3. Transfer a non-empty configuration information and set isSupport to a valid value. Expected result 3 is obtained.
* HITLS_CFG_GetDtlsCookieExchangeSupport
* 1. Import empty configuration information. Expected result 1 is obtained.
* 2. Transfer an empty isSupport pointer. Expected result 1 is obtained.
* 3. Transfer the non-null configuration information and the isSupport pointer is not null. Expected result 3 is
* obtained.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. HITLS_SUCCES is returned and config->isSupportDtlsCookieExchange is set to true.
* 3. Returns HITLS_SUCCES, and config->isSupportDtlsCookieExchange is true or false.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_HELLO_VERIFY_REQ_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = NULL;
bool isSupport = false;
bool getIsSupport = false;
ASSERT_TRUE(HITLS_CFG_SetDtlsCookieExchangeSupport(config, isSupport) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetDtlsCookieExchangeSupport(config, &getIsSupport) == HITLS_NULL_INPUT);
config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(HITLS_CFG_GetDtlsCookieExchangeSupport(config, NULL) == HITLS_NULL_INPUT);
isSupport = true;
ASSERT_TRUE(HITLS_CFG_SetDtlsCookieExchangeSupport(config, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(config->isSupportDtlsCookieExchange = true);
isSupport = false;
ASSERT_TRUE(HITLS_CFG_SetDtlsCookieExchangeSupport(config, isSupport) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetDtlsCookieExchangeSupport(config, &getIsSupport) == HITLS_SUCCESS);
ASSERT_TRUE(getIsSupport == false);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_RENEGOTIATIONSUPPORT_FUNC_TC001
* @title Test the function of supporting the renegotiation function by setting the HITLS_CFG_SetRenegotiationSupport and
* obtaining the function of supporting the renegotiation function by the HITLS_CFG_GetRenegotiationSupport.
* @precon nan
* @brief 1. Call HITLS_CFG_SetRenegotiationSupport to disable renegotiation. Expected result 1 is obtained.
* 2. Invoke the HITLS_CFG_GetRenegotiationSupport interface to obtain the configured value. (Expected result
* 2)
* 3. Invoke the HITLS_SetRenegotiationSupport interface to support renegotiation. Expected result 3 is
* obtained.
* 4. Invoke the HITLS_GetRenegotiationSupport interface to obtain the configured value. Expected result 4 is
* obtained.
* 5. Establish a connection. and check whether the value of isSecureRenegotiation in the
* negotiation information is true. Expected result 5 is obtained.
* 6. Perform renegotiation. Expected result 6 is obtained.
* @expect 1. Setting succeeded.
* 2. The interface returns false.
* 3. The setting is successful.
* 4. The interface returns true.
* 5. The value of isSecureRenegotiation is true.
* 6. The renegotiation succeeds.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_RENEGOTIATIONSUPPORT_FUNC_TC001()
{
FRAME_Init();
FRAME_LinkObj *clientRes;
FRAME_LinkObj *serverRes;
HITLS_Config *config = NULL;
uint8_t supportrenegotiation;
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_CFG_SetRenegotiationSupport(config, false);
HITLS_CFG_GetRenegotiationSupport(config, &supportrenegotiation);
ASSERT_TRUE(supportrenegotiation == false);
clientRes = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(clientRes != NULL);
serverRes = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(serverRes != NULL);
HITLS_SetRenegotiationSupport(clientRes->ssl, true);
HITLS_SetRenegotiationSupport(serverRes->ssl, true);
HITLS_GetRenegotiationSupport(clientRes->ssl, &supportrenegotiation);
ASSERT_TRUE(supportrenegotiation == true);
FRAME_CreateConnection(clientRes, serverRes, true, HS_STATE_BUTT);
ASSERT_TRUE(clientRes->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(serverRes->ssl->state == CM_STATE_TRANSPORTING);
ASSERT_TRUE(HITLS_Renegotiate(serverRes->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(clientRes->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(FRAME_CreateRenegotiationState(clientRes, serverRes, true, HS_STATE_BUTT) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(clientRes);
FRAME_FreeLink(serverRes);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_ECPOINTFORMATS_FUNC_TC001
* @title Set the normal dot format value.
* @precon nan
* @brief 1. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED and invoke the HITLS_CFG_SetEcPointFormats interface.
* Expected result 1 is obtained.
* 2. Set pointFormats to HITLS_POINT_FORMAT_BUTT and invoke the HITLS_CFG_SetEcPointFormats interface.
* (Expected result 2)
* 3. Use config to generate ctx, due to the result 3
* 4. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED again and generate ctx again. Expected result 4 is
* obtained. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED and invoke the HITLS_SetEcPointFormats
* interface. (Expected result 2)
* 5. Set pointFormats to HITLS_POINT_FORMAT_UNCOMPRESSED and invoke the HITLS_SetEcPointFormats interface.
* Expected result 2 is obtained.
* @expect 1. Interface return value, HITLS_SUCCESS
* 2. Interface return value: HITLS_SUCCESS
* 3. Failed to generate the file.
* 4. The file is generated successfully.
* 5. The setting is successful.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_ECPOINTFORMATS_FUNC_TC001(int version)
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *Config = NULL;
HITLS_Ctx *ctx = NULL;
Config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(Config != NULL);
const uint8_t pointFormats[] = {HITLS_POINT_FORMAT_UNCOMPRESSED};
uint32_t pointFormatsSize = sizeof(pointFormats) / sizeof(uint8_t);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
const uint8_t pointFormats2[] = {HITLS_POINT_FORMAT_BUTT};
uint32_t pointFormatsSize2 = sizeof(pointFormats2) / sizeof(uint8_t);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats2, pointFormatsSize2) == HITLS_SUCCESS);
ctx = HITLS_New(Config);
if(version == TLS1_2){
ASSERT_TRUE(ctx == NULL);
}
HITLS_Free(ctx);
ASSERT_TRUE(HITLS_CFG_SetEcPointFormats(Config, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
ctx = HITLS_New(Config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_SetEcPointFormats(ctx, pointFormats, pointFormatsSize) == HITLS_SUCCESS);
client = FRAME_CreateLink(Config, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(Config, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
typedef struct {
HITLS_Config *config;
FRAME_LinkObj *client;
FRAME_LinkObj *server;
HITLS_HandshakeState state;
bool isClient;
bool isSupportExtendMasterSecret;
bool isSupportClientVerify;
bool isSupportNoClientCert;
bool isSupportRenegotiation;
bool isSupportSessionTicket;
bool needStopBeforeRecvCCS;
} HandshakeTestInfo;
/** @
* @test UT_TLS_CFG_SET_GROUPS_FUNC_TC001
* @title Sets the elliptic curve that does not exist.
* @precon nan
* @brief 1. Set group to 0x0001 and invoke the HITLS_CFG_SetGroups interface. Expected result 1 is obtained.
* 2. Establish a connection. Check whether the group value in the client hello message sent by the client is
* 0x0001.Expected result 2 is obtained.
* 3. Establish a connection and check whether the connection is successfully established. (Expected result 3)
* @expect 1. Interface HITLS_SUCCESS
* 2. The value of group in the client hello message is 0x0001.
* 3. connection establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GROUPS_FUNC_TC001(int version)
{
FRAME_Init();
HandshakeTestInfo testInfo = {0};
uint16_t group[] = {ERROR_HITLS_GROUP};
uint32_t grouplength = sizeof(group) / sizeof(uint16_t);
testInfo.isClient = false;
testInfo.config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(testInfo.config != NULL);
ASSERT_TRUE(HITLS_CFG_SetGroups(testInfo.config, group, grouplength) == HITLS_SUCCESS);
if (version == TLS1_2) {
uint16_t cipherSuite[] = {HITLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256};
HITLS_CFG_SetCipherSuites(testInfo.config, cipherSuite, sizeof(cipherSuite) / sizeof(uint16_t));
}
FRAME_CertInfo certInfo = {
"rsa_sha/ca-3072.der:rsa_sha/inter-3072.der",
"rsa_sha/inter-3072.der",
"rsa_sha/end-sha256.der",
NULL,
"rsa_sha/end-sha256.key.der",
NULL,
};
testInfo.client = FRAME_CreateLinkWithCert(testInfo.config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(testInfo.client != NULL);
testInfo.server = FRAME_CreateLinkWithCert(testInfo.config, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(testInfo.server != NULL);
if (version == TLS1_2) {
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, HS_STATE_BUTT),
HITLS_MSG_HANDLE_CIPHER_SUITE_ERR);
} else {
ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, testInfo.isClient, HS_STATE_BUTT),
HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
}
EXIT:
HITLS_CFG_FreeConfig(testInfo.config);
FRAME_FreeLink(testInfo.client);
FRAME_FreeLink(testInfo.server);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_SIGNATURE_FUNC_TC001
* @title Set a nonexistent signature algorithm.
* @precon nan
* @brief
* 1. Set Signature to 0xffff and call the HITLS_CFG_SetSignature interface. (Expected result 1)
* @expect
* 1. Interface return value: HITLS_CONFIG_INVALID_LENGTH
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_SIGNATURE_FUNC_TC001(int version)
{
FRAME_Init();
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_Config *config = NULL;
config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(config != NULL);
uint16_t signAlgs[] = {ERROR_HITLS_SIGNATURE};
ASSERT_TRUE(HITLS_CFG_SetSignature(config, signAlgs, sizeof(signAlgs) / sizeof(uint16_t)) == HITLS_SUCCESS);
client = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(client == NULL);
server = FRAME_CreateLink(config, BSL_UIO_TCP);
ASSERT_TRUE(server == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
void ExampleInfoCallback(const HITLS_Ctx *ctx, int32_t eventType, int32_t value)
{
(void)ctx;
(void)eventType;
(void)value;
}
#define MSG_CB_PRINT_LEN 500
void msg_callback(int32_t writePoint, int32_t tlsVersion, int32_t contentType, const void *msg,
uint32_t msgLen, HITLS_Ctx *ctx, void *arg)
{
(void)writePoint;
(void)tlsVersion;
(void)contentType;
(void)msg;
(void)msgLen;
(void)ctx;
(void)arg;
}
/* @
* @test UT_TLS_CFG_InfoCb_API_TC001
* @title InfoCb Interface Parameter Test
* @precon nan
* @brief
1. Use the HITLS_CFG_GetInfoCb without HITLS_CFG_SetInfoCb. Expected result 1 is obtained.
2. Use the HITLS_CFG_SetInfoCb interface to set callback. Expected result 2
3. Use the HITLS_CFG_GetInfoCb . Expected result 3
4. Use the HITLS_CFG_GetInfoCb with the parameter is NULL . Expected result 4
* @expect
1. Return the NULL.
2. Return the HITLS_SUCCESS
3. Return value is not NULL.
4. Return the NULL.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_InfoCb_API_TC001(void)
{
FRAME_Init();
HITLS_Config *config = HITLS_CFG_NewDTLS12Config();
ASSERT_TRUE(config != NULL);
HITLS_InfoCb infoCallBack = HITLS_CFG_GetInfoCb(config);
ASSERT_TRUE(infoCallBack == NULL);
int32_t ret = HITLS_CFG_SetInfoCb(config, ExampleInfoCallback);
ASSERT_TRUE(ret == HITLS_SUCCESS);
infoCallBack = HITLS_CFG_GetInfoCb(config);
ASSERT_TRUE(infoCallBack != NULL);
infoCallBack = HITLS_CFG_GetInfoCb(NULL);
ASSERT_TRUE(infoCallBack == NULL);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SetMsgCb_API_TC001
* @title HITLS_CFG_SetMsgCb Interface Parameter Test
* @precon nan
* @brief
1. Set config to NULL. Expected result 1 is obtained.
2. Invoke the HITLS_CFG_SetMsgCb interface to set callback. (Expected result 2)
* @expect
1. Return the HITLS_NULL_INPUT message.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SetMsgCb_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_EQ(HITLS_CFG_SetMsgCb(NULL, msg_callback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetMsgCb(tlsConfig, msg_callback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SetMsgCbArg_API_TC001
* @title HITLS_CFG_SetMsgCbArg Interface Parameter Test
* @precon nan
* @brief 1. Set config to NULL. Expected result 1 is obtained.
2. Use the HITLS_CFG_SetMsgCbArg interface to set Arg. Expected result 2 is obtained.
* @expect 1. The HITLS_NULL_INPUT message is returned.
2. Return the HITLS_SUCCESS
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SetMsgCbArg_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig;
tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
ASSERT_EQ(HITLS_CFG_SetMsgCbArg(NULL, msg_callback), HITLS_NULL_INPUT);
ASSERT_EQ(HITLS_CFG_SetMsgCbArg(tlsConfig, msg_callback), HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SETTMPDH_FUNC_TC001
* @title Set tmpdhkey. The link setup status varies according to the security level.
* @precon nan
* @brief
* 1. Set the RSA certificate and algorithm suite.
* 2. Set the dh key to not follow the certificate, and set the tmpdh key with 80 security bits.
* 3. Set the security level to 0 and set up a link.
* 4. Set the security level to 2 and set up a link.
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The link is set up successfully.
* 4. The link fails to be set up.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SETTMPDH_FUNC_TC001(int level)
{
(void)level;
FRAME_Init();
HITLS_Config *clientConfig = NULL;
HITLS_Config *serverConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
HITLS_CRYPT_Key *key = NULL;
uint16_t pfsCipherSuites[] = {HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256};
clientConfig = HITLS_CFG_NewTLS12Config();
serverConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(clientConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH,
RSA_SHA256_EE_PATH3, NULL, RSA_SHA256_PRIV_PATH3,NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(serverConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH,
RSA_SHA256_EE_PATH3, NULL, RSA_SHA256_PRIV_PATH3,NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(clientConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(serverConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) == HITLS_SUCCESS);
HITLS_CFG_SetSecurityLevel(serverConfig, level);
HITLS_CFG_SetSecurityLevel(clientConfig, level);
HITLS_CFG_SetDhAutoSupport(serverConfig, false);
int32_t secBits = 80;
key = HITLS_CRYPT_GenerateDhKeyBySecbits(LIBCTX_FROM_CONFIG(serverConfig), ATTRIBUTE_FROM_CONFIG(serverConfig),
serverConfig, secBits);
HITLS_CFG_SetTmpDh(serverConfig, key);
FRAME_CertInfo certInfo = {0, 0, 0, 0, 0, 0};
client = FRAME_CreateLinkWithCert(clientConfig, BSL_UIO_TCP, &certInfo);
server = FRAME_CreateLinkWithCert(serverConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
if (level > 1) {
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_KEY_EXCHANGE), HITLS_SUCCESS);
FRAME_TrasferMsgBetweenLink(server, client);
HITLS_Connect(client->ssl);
ASSERT_EQ(HITLS_Accept(server->ssl) , HITLS_MSG_HANDLE_ERR_GET_DH_KEY);
} else {
ASSERT_EQ(FRAME_CreateConnection(client, server, false, HS_STATE_BUTT), HITLS_SUCCESS);
}
EXIT:
if (SECURITY_GetSecbits(level) > secBits) {
SAL_CRYPT_FreeDhKey(key);
}
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/* @
* @test UT_TLS_CFG_SET_POSTHANDSHAKEAUTHSUPPORT_API_TC001
*
* @title Test the HITLS_SetPostHandshakeAuthSupport interface.
*
* @brief
* 1. The default value of the TLS connection handle isSupportPostHandshakeAuth is fasle. Expected result 1。
* 2. Run the HITLS_SetPostHandshakeAuthSupport command to set a handle. The value of isSupportPostHandshakeAuth is true.
* Expected result 2.
* @expect
* 1. isSupportPostHandshakeAuth is false.
* 2. isSupportPostHandshakeAuth is true.
@*/
/* BEGIN_CASE */
void UT_TLS_CFG_SET_POSTHANDSHAKEAUTHSUPPORT_API_TC001(int tlsVersion)
{
HitlsInit();
HITLS_Config *tlsConfig = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(tlsConfig != NULL);
HITLS_Ctx *ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(ctx->config.tlsConfig.isSupportPostHandshakeAuth == false);
int ret = HITLS_SetPostHandshakeAuthSupport(ctx, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
ASSERT_TRUE(ctx->config.tlsConfig.isSupportPostHandshakeAuth == true);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_GET_SECURE_RENEGOTIATIONSUPPORET_FUNC_TC001
* @title HITLS_GetSecureRenegotationSupport The client does not support security renegotiation,
* but the server supports security renegotiation. Obtains whether security renegotiation is supported.
* @precon nan
* @brief HITLS_GetSecureRenegotationSupport
* 1. Transfer an empty TLS connection handle. Expected result 1.
* 2. Transfer the non-empty TLS connection handle information and leave isSecureRenegotiation blank. Expected result 1.
* 3. Transfer the non-empty TLS connection handle information. The isSecureRenegotiation parameter is not empty.
* Expected result 2.
* @expect
* 1. Returns HITLS_NULL_INPUT
* 2. Returns HITLS_SUCCES
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_GET_SECURE_RENEGOTIATIONSUPPORET_API_TC001(void)
{
HitlsInit();
HITLS_Config *config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t isSecureRenegotiation = 0;
ASSERT_TRUE(HITLS_GetSecureRenegotiationSupport(NULL, &isSecureRenegotiation) == HITLS_NULL_INPUT);
config = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config != NULL);
ctx = HITLS_New(config);
ASSERT_TRUE(ctx != NULL);
ASSERT_TRUE(HITLS_GetSecureRenegotiationSupport(ctx, NULL) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetSecureRenegotiationSupport(ctx, &isSecureRenegotiation) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
HITLS_Free(ctx);
}
/* END_CASE */
/** @
* @test UT_TLS_CFG_SET_GET_DHAUTOSUPPORT_FUNC_TC001
* @title Test the HITLS_SetDhAutoSupport and HITLS_CFG_GetDhAutoSupport interfaces.
* @precon nan
* @brief
* 1. Invoke the HITLS_CFG_SetDhAutoSupport interface to set the parameter to false. Expected result 1 is obtained.
* 2. Establish a connection. Expected result 2 is obtained.
* @expect
* 1. The setting is successful.
* 2. connection establishment fails.
@ */
/* BEGIN_CASE */
void UT_TLS_CFG_SET_GET_DHAUTOSUPPORT_FUNC_TC001(void)
{
FRAME_Init();
HITLS_Config *clientConfig = NULL;
HITLS_Config *serverConfig = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
uint16_t pfsCipherSuites[] = {HITLS_DHE_RSA_WITH_AES_128_GCM_SHA256};
clientConfig = HITLS_CFG_NewTLS12Config();
serverConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(clientConfig != NULL);
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(clientConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH,
RSA_SHA256_EE_PATH3, NULL, RSA_SHA256_PRIV_PATH3,NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HiTLS_X509_LoadCertAndKey(serverConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH,
RSA_SHA256_EE_PATH3, NULL, RSA_SHA256_PRIV_PATH3,NULL) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(clientConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) ==
HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_SetCipherSuites(serverConfig, pfsCipherSuites, sizeof(pfsCipherSuites) / sizeof(uint16_t)) ==
HITLS_SUCCESS);
HITLS_CFG_SetDhAutoSupport(serverConfig, false);
FRAME_CertInfo certInfo = {0, 0, 0, 0, 0, 0};
client = FRAME_CreateLinkWithCert(clientConfig, BSL_UIO_TCP, &certInfo);
server = FRAME_CreateLinkWithCert(serverConfig, BSL_UIO_TCP, &certInfo);
ASSERT_TRUE(client != NULL);
ASSERT_TRUE(server != NULL);
ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_SEND_SERVER_KEY_EXCHANGE), HITLS_SUCCESS);
FRAME_TrasferMsgBetweenLink(server, client);
HITLS_Connect(client->ssl);
ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_MSG_HANDLE_ERR_GET_DH_KEY);
EXIT:
HITLS_CFG_FreeConfig(clientConfig);
HITLS_CFG_FreeConfig(serverConfig);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_frame_config_interface.c | C | unknown | 60,914 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <linux/limits.h>
#include <unistd.h>
#include <stdbool.h>
#include "hitls_error.h"
#include "hitls_cert.h"
#include "hitls.h"
#include "hitls_func.h"
#include "securec.h"
#include "cert_method.h"
#include "cert_mgr.h"
#include "cert_mgr_ctx.h"
#include "frame_tls.h"
#include "frame_link.h"
#include "frame_io.h"
#include "hlt_type.h"
#include "process.h"
#include "hlt.h"
#include "session.h"
#include "bsl_sal.h"
#include "alert.h"
#include "stub_replace.h"
#include "cert_callback.h"
#include "crypt_eal_rand.h"
#include "hitls_crypt_reg.h"
#include "hitls_crypt_init.h"
#include "logger.h"
#include "uio_base.h"
#include "hitls_cert_type.h"
#include "hitls_type.h"
#include "hitls_cert_reg.h"
#include "hitls_config.h"
#include "hitls_cert_init.h"
#include "bsl_log.h"
#include "bsl_err.h"
#include "tls_config.h"
#include "tls.h"
#include "crypt_algid.h"
#include "crypt_errno.h"
#include "bsl_uio.h"
#include "bsl_obj.h"
#include "bsl_errno.h"
#include "hitls_x509_adapt.h"
/* END_HEADER */
#define BUF_MAX_SIZE 4096
int32_t g_uiPort = 18886;
#define DEFAULT_CERT_PATH "../../testcode/testdata/tls/certificate/der/"
#define CERT_PATH_LEN 120
#define SUCCESS (0)
#define ERROR (1)
#define MAX_BUFFER (8192)
#define READ_BUF_LEN_18K (18 * 1024)
#define READ_DATA_18432 18432
#define PASSWDLEN (10)
#define CERT_PATH_BUFFER (100)
#define RSA_ROOT_CERT_DER "rsa_sha/ca-3072.der"
#define RSA_CA_CERT_DER "rsa_sha/inter-3072.der"
#define RSA_EE_CERT_DER "rsa_sha/end-sha1.der"
#define RSA_PRIV_KEY_DER "rsa_sha/end-sha1.key.der"
#define RSA_EE_CERT_DER "rsa_sha/end-sha1.der"
#define RSA_PRIV_KEY_DER "rsa_sha/end-sha1.key.der"
#define RSA_ROOT_CERT2_DER "rsa_sha256/ca.der"
#define RSA_CA_CERT2_DER "rsa_sha256/inter.der"
#define RSA_EE_CERT2_DER "rsa_sha256/server.der"
#define RSA_PRIV_KEY2_DER "rsa_sha256/server.key.der"
#define ECDSA_ROOT_CERT_DER "ecdsa/ca-nist521.der"
#define ECDSA_CA_CERT_DER "ecdsa/inter-nist521.der"
#define ECDSA_EE_CERT_DER "ecdsa/end256-sha256.der"
#define ECDSA_PRIV_KEY_DER "ecdsa/end256-sha256.key.der"
typedef enum {
SHALLOW_COPY = 0,
DEEP_COPY,
} COPY_WAY;
typedef enum {
ECDSA_CERT,
ED25519_CERT,
RSA_CERT,
RSA_CERT_TWO,
RSA_CERT_THREE,
} EE_CERT_TYPE;
typedef enum {
FROM_CONFIG,
FROM_CTX,
FROM_BUFFER_TO_CONFIG,
FROM_BUFFER_TO_CTX
} LOAD_CERT_WAY;
typedef LOAD_CERT_WAY LOAD_KEY_WAY;
int GetCertPathFrom(int eeCertType, char **rootCA, char **ca, char **ee, char **prvKey)
{
switch (eeCertType) {
case RSA_CERT:
*rootCA = RSA_ROOT_CERT_DER;
*ca = RSA_CA_CERT_DER;
*ee = RSA_EE_CERT_DER;
*prvKey = RSA_PRIV_KEY_DER;
return SUCCESS;
case RSA_CERT_TWO:
*rootCA = RSA_ROOT_CERT2_DER;
*ca = RSA_CA_CERT2_DER;
*ee = RSA_EE_CERT2_DER;
*prvKey = RSA_PRIV_KEY2_DER;
return SUCCESS;
case RSA_CERT_THREE:
*rootCA = RSA_ROOT_CERT_DER;
*ca = RSA_CA_CERT_DER;
*ee = RSA_EE_CERT_DER;
*prvKey = RSA_PRIV_KEY_DER;
return SUCCESS;
case ECDSA_CERT:
*rootCA = ECDSA_ROOT_CERT_DER;
*ca = ECDSA_CA_CERT_DER;
*ee = ECDSA_EE_CERT_DER;
*prvKey = ECDSA_PRIV_KEY_DER;
return SUCCESS;
default:
return ERROR;
}
}
int NormalizePath(char* normalizedPath, const char* path) {
int ret;
ret = sprintf_s(normalizedPath, CERT_PATH_LEN, "%s%s", DEFAULT_CERT_PATH, path);
if (ret <= 0) {
LOG_ERROR("sprintf_s Error");
return ERROR;
}
return SUCCESS;
}
int Dtls_DataTransfer(HITLS_Ctx *clientCtx, HLT_Process *remoteProcess, HLT_Tls_Res *serverRes)
{
uint8_t *writeBuf = (uint8_t *)"hello world";
uint32_t writeLen = strlen((char *)writeBuf);
uint8_t readBuf[READ_DATA_18432] = { 0 };
uint32_t readLen = 0;
ASSERT_EQ(HLT_TlsWrite(clientCtx, writeBuf, writeLen), SUCCESS);
ASSERT_EQ(HLT_ProcessTlsRead(remoteProcess, serverRes, readBuf, READ_DATA_18432, &readLen), 0);
ASSERT_COMPARE("COMPARE DATA", writeBuf, writeLen, readBuf, readLen);
return SUCCESS;
EXIT:
return ERROR;
}
HITLS_Ctx *Dtls_New_Ctx(HLT_Process *localProcess, HITLS_Config* clientConfig)
{
HITLS_Ctx *clientCtx = HLT_TlsNewSsl(clientConfig);
ASSERT_TRUE(clientCtx != NULL);
HLT_Ssl_Config clientCtxConfig;
clientCtxConfig.sockFd = localProcess->connFd;
clientCtxConfig.connType = SCTP;
ASSERT_TRUE_AND_LOG("HLT_TlsSetSsl", HLT_TlsSetSsl(clientCtx, &clientCtxConfig) == 0);
return clientCtx;
EXIT:
return NULL;
}
void TestSetCertPath(HLT_Ctx_Config *ctxConfig, char *SignatureType)
{
if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA1", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA1")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA256", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA256")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA256_EE_PATH3, RSA_SHA256_PRIV_PATH3, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA384")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA384_EE_PATH, RSA_SHA384_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512", strlen("CERT_SIG_SCHEME_RSA_PKCS1_SHA512")) ==
0 ||
strncmp(SignatureType,
"CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512",
strlen("CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512")) == 0) {
HLT_SetCertPath(
ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA512_EE_PATH, RSA_SHA512_PRIV_PATH, "NULL", "NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256",
strlen("CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA256_EE_PATH,
ECDSA_SHA256_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384",
strlen("CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA384_EE_PATH,
ECDSA_SHA384_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType,
"CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512",
strlen("CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA_CA_PATH,
ECDSA_SHA_CHAIN_PATH,
ECDSA_SHA512_EE_PATH,
ECDSA_SHA512_PRIV_PATH,
"NULL",
"NULL");
} else if (strncmp(SignatureType, "CERT_SIG_SCHEME_ECDSA_SHA1", strlen("CERT_SIG_SCHEME_ECDSA_SHA1")) == 0) {
HLT_SetCertPath(ctxConfig,
ECDSA_SHA1_CA_PATH,
ECDSA_SHA1_CHAIN_PATH,
ECDSA_SHA1_EE_PATH,
ECDSA_SHA1_PRIV_PATH,
"NULL",
"NULL");
}
}
HITLS_CERT_X509 *HiTLS_X509_LoadCertFile(HITLS_Config *tlsCfg, const char *file);
/* @
* @test SDV_TLS_LoadAndDelCert_FUNC_TC001
* @title Loading and Deleting Certificates
* @precon nan
* @brief 1. Initialize the client and server. Expected result 1
2. Load the certificate to the certificate chain. Expected result 2
3. Load the first certificate and private key to the certificate chain. Expected result 2
4. Load the second certificate to the certificate chain. Expected result 2
5. Run the config command to remove all certificates. Expected result 3
6. Load the third certificate to the certificate chain. Expected result 2
7. Remove all certificates in CTX mode. Expected result 3
8. Load the third certificate to the certificate chain. Expected result 2
9. Initiate link establishment. Expected result 4 is obtained
* @expect 1. Initialization succeeded.
2. Loading succeeded.
3. Removing succeeded.
4. Link setup succeeded
@ */
/* BEGIN_CASE */
void SDV_TLS_CERT_LoadAndDelCert_FUNC_TC001(int delWay)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HITLS_Config* serverConfig = NULL;
// Stores the path where the certificate is loaded for the first time.
char *rootCAFilePath1 = NULL;
char *caFilePath1 = NULL;
char *eeFilePath1 = NULL;
char *eeKeyPath1 = NULL;
// Stores the path where the certificate is loaded for the second time.
char *rootCAFilePath2 = NULL;
char *caFilePath2 = NULL;
char *eeFilePath2 = NULL;
char *eeKeyPath2 = NULL;
HITLS_CERT_X509 *eeCert3 = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, false);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
TestSetCertPath(clientCtxConfig, "CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256");
rootCAFilePath1 = DEFAULT_CERT_PATH""RSA_ROOT_CERT_DER;
caFilePath1 = DEFAULT_CERT_PATH""RSA_CA_CERT_DER;
eeFilePath1 = DEFAULT_CERT_PATH""RSA_EE_CERT_DER;
eeKeyPath1 = DEFAULT_CERT_PATH""RSA_PRIV_KEY_DER;
rootCAFilePath2 = DEFAULT_CERT_PATH""ECDSA_ROOT_CERT_DER;
caFilePath2 = DEFAULT_CERT_PATH""ECDSA_CA_CERT_DER;
eeFilePath2 = DEFAULT_CERT_PATH""ECDSA_EE_CERT_DER;
eeKeyPath2 = DEFAULT_CERT_PATH""ECDSA_PRIV_KEY_DER;
ASSERT_EQ(HLT_TlsRegCallback(HITLS_CALLBACK_DEFAULT), SUCCESS);
serverConfig = HLT_TlsNewCtx(DTLS1_2);
ASSERT_TRUE(serverConfig != NULL);
uint16_t group = HITLS_EC_GROUP_SECP256R1;
ASSERT_EQ(HITLS_CFG_SetGroups(serverConfig, &group, 1), SUCCESS);
// Load the certificate to the Chain Store.
HITLS_CERT_Store *chainStore = HITLS_X509_Adapt_StoreNew();
ASSERT_TRUE(chainStore != NULL);
ASSERT_EQ(HITLS_CFG_SetVerifyStore(serverConfig, chainStore, SHALLOW_COPY), SUCCESS);
HITLS_CERT_X509 *rootCACert2 = HiTLS_X509_LoadCertFile(serverConfig, rootCAFilePath2);
ASSERT_TRUE(rootCACert2 != NULL);
ASSERT_EQ(HITLS_CFG_AddCertToStore(serverConfig, rootCACert2, TLS_CERT_STORE_TYPE_VERIFY, false), HITLS_SUCCESS);
HITLS_CERT_X509 *caCert2 = HiTLS_X509_LoadCertFile(serverConfig, caFilePath2);
ASSERT_TRUE(caCert2 != NULL);
ASSERT_EQ(HITLS_CFG_AddCertToStore(serverConfig, caCert2, TLS_CERT_STORE_TYPE_VERIFY, false), HITLS_SUCCESS);
// Loading the device certificate and corresponding private key for the first time
HITLS_CERT_X509 *eeCert1 = HiTLS_X509_LoadCertFile(serverConfig, eeFilePath2);
ASSERT_TRUE(eeCert1 != NULL);
ASSERT_EQ(HITLS_CFG_SetCertificate(serverConfig, eeCert1, SHALLOW_COPY), SUCCESS);
HITLS_CERT_Key *prvKey1 = HITLS_CFG_ParseKey(serverConfig, (const uint8_t *)eeKeyPath2, strlen(eeKeyPath1),
TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
ASSERT_TRUE(prvKey1 != NULL);
ASSERT_EQ(HITLS_CFG_SetPrivateKey(serverConfig, prvKey1, SHALLOW_COPY), SUCCESS);
// The private key is not loaded when the certificate is loaded for the second time.
HITLS_CERT_X509 *eeCert2 = HiTLS_X509_LoadCertFile(serverConfig, eeFilePath2);
ASSERT_TRUE(eeCert2 != NULL);
ASSERT_EQ(HITLS_CFG_SetCertificate(serverConfig, eeCert2, SHALLOW_COPY), SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetCertificate(serverConfig) == eeCert2);
if (delWay == FROM_CONFIG) {
ASSERT_EQ(HITLS_CFG_RemoveCertAndKey(serverConfig), SUCCESS);
// Reload the certificate without loading the private key.
eeCert3 = HiTLS_X509_LoadCertFile(serverConfig, eeFilePath2);
ASSERT_TRUE(eeCert3 != NULL);
ASSERT_EQ(HITLS_CFG_SetCertificate(serverConfig, eeCert3, SHALLOW_COPY), SUCCESS);
#ifdef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_Key *prvKey2 = HITLS_X509_Adapt_ProviderKeyParse(serverConfig, (const uint8_t *)eeKeyPath2,
strlen(eeKeyPath2), TLS_PARSE_TYPE_FILE, "ASN1", NULL);
#else
HITLS_CERT_Key *prvKey2 = HITLS_X509_Adapt_KeyParse(serverConfig, (const uint8_t *)eeKeyPath2,
strlen(eeKeyPath2), TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
#endif
ASSERT_TRUE(prvKey2 != NULL);
ASSERT_EQ(HITLS_CFG_SetPrivateKey(serverConfig, prvKey2, SHALLOW_COPY), SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetPrivateKey(serverConfig) == prvKey2);
}
HITLS_Ctx *serverCtx = Dtls_New_Ctx(localProcess, serverConfig);
ASSERT_TRUE(serverCtx != NULL);
if (delWay == FROM_CTX) {
// After the certificate is loaded from Config, the certificate is copied to CTX.
ASSERT_TRUE(HITLS_GetCertificate(serverCtx) != eeCert2);
ASSERT_EQ(HITLS_RemoveCertAndKey(serverCtx), SUCCESS);
// Reload the certificate without loading the private key.
eeCert3 = HiTLS_X509_LoadCertFile(serverConfig, eeFilePath2);
ASSERT_TRUE(eeCert3 != NULL);
ASSERT_EQ(HITLS_SetCertificate(serverCtx, eeCert3, SHALLOW_COPY), SUCCESS);
#ifdef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_Key *prvKey2 = HITLS_X509_Adapt_ProviderKeyParse(serverConfig, (const uint8_t *)eeKeyPath2,
strlen(eeKeyPath2), TLS_PARSE_TYPE_FILE, "ASN1", NULL);
#else
HITLS_CERT_Key *prvKey2 = HITLS_X509_Adapt_KeyParse(serverConfig, (const uint8_t *)eeKeyPath2,
strlen(eeKeyPath2), TLS_PARSE_TYPE_FILE, TLS_PARSE_FORMAT_ASN1);
#endif
ASSERT_TRUE(prvKey2 != NULL);
ASSERT_EQ(HITLS_SetPrivateKey(serverCtx, prvKey2, SHALLOW_COPY), SUCCESS);
ASSERT_TRUE(HITLS_GetCertificate(serverCtx) == eeCert3);
ASSERT_TRUE(HITLS_GetPrivateKey(serverCtx) == prvKey2);
}
unsigned long int tlsAcceptId = HLT_TlsAccept(serverCtx);
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_GetTlsAcceptResultFromId(tlsAcceptId), 0);
ASSERT_EQ(Dtls_DataTransfer(serverCtx, remoteProcess, clientRes), SUCCESS);
EXIT:
HLT_FreeAllProcess();
return;
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_hlt_cert_interface.c | C | unknown | 15,849 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <unistd.h>
#include <semaphore.h>
#include "securec.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_config.h"
#include "hitls_cert_type.h"
#include "hitls.h"
#include "process.h"
#include "hitls_error.h"
#include "hitls_type.h"
#include "hitls_func.h"
#include "hitls.h"
#include "conn_init.h"
#include "frame_tls.h"
#include "frame_msg.h"
#include "frame_io.h"
#include "frame_link.h"
#include "hs_common.h"
#include "change_cipher_spec.h"
#include "stub_replace.h"
#define READ_BUF_SIZE 18432
#define Port 7788
#define ROOT_DER "%s/ca.der:%s/inter.der"
#define INTCA_DER "%s/inter.der"
#define SERVER_DER "%s/server.der"
#define SERVER_KEY_DER "%s/server.key.der"
#define CLIENT_DER "%s/client.der"
#define CLIENT_KEY_DER "%s/client.key.der"
#define BUF_SZIE 18432
/* END_HEADER */
static uint32_t g_uiPort = 18889;
static void SetCertPath_2(HLT_Ctx_Config *ctxConfig, char *cipherSuite)
{
if (strstr(cipherSuite, "RSA") != NULL) {
HLT_SetCertPath(ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, NULL, NULL);
} else if (strstr(cipherSuite, "ECDSA") != NULL) {
HLT_SetCertPath(ctxConfig, ECDSA_SHA_CA_PATH, ECDSA_SHA_CHAIN_PATH, ECDSA_SHA1_EE_PATH, ECDSA_SHA1_PRIV_PATH, NULL, NULL);
} else {
HLT_SetCertPath(ctxConfig, RSA_SHA_CA_PATH, RSA_SHA_CHAIN_PATH, RSA_SHA1_EE_PATH, RSA_SHA1_PRIV_PATH, NULL, NULL);
}
}
/**
* @test SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC001
* @title To test the setting of the HITLS_SetCipherServerPreference interface of the dtls.
* @precon By default, the algorithm preferred by the client is preferred.
* @brief
* 1. Initialize the hitls.
* 2. Create an SSL ctx object.
* 3. Create an SSL object.
* 4. Connect
* 5. Check for connection errors.
* 6. Check whether the negotiated cipher suite is the client preference.
* 7. Check whether the negotiated group is the client preference.
* 8. Check that the negotiated signature algorithm is the client preference.
* @expect
* 1. successful
* 2. successful
* 3. successful
* 4. successful
* 5. successful
* 6. successful
* 7. successful
* 8. successful
*/
/* BEGIN_CASE */
void SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC001(char *serverCipherSuite, char *clientCipherSuite, int expectResult)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath_2(serverCtxConfig, serverCipherSuite);
HLT_SetGroups(serverCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetCipherSuites(serverCtxConfig, serverCipherSuite);
HLT_SetSignature(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384:CERT_SIG_SCHEME_RSA_PKCS1_SHA512");
serverRes = HLT_ProcessTlsAccept(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath_2(clientCtxConfig, clientCipherSuite);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP384R1:HITLS_EC_GROUP_SECP256R1");
HLT_SetCipherSuites(clientCtxConfig, clientCipherSuite);
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512:CERT_SIG_SCHEME_RSA_PKCS1_SHA384");
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[BUF_SZIE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
HITLS_Ctx *testCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(testCtx->negotiatedInfo.cipherSuiteInfo.cipherSuite == expectResult);
ASSERT_TRUE(testCtx->negotiatedInfo.negotiatedGroup == HITLS_EC_GROUP_SECP384R1);
ASSERT_TRUE(testCtx->negotiatedInfo.signScheme == CERT_SIG_SCHEME_RSA_PKCS1_SHA512);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC002
* @title To test the setting of the HITLS_SetCipherServerPreference interface of the dtls.
* @precon Set the algorithm for preferentially selecting the server-side preference.
* @brief
* 1. Initialize the hitls.
* 2. Create an SSL ctx object.
* 3. Create an SSL object.
* 4. Connect
* 5. Check for connection errors.
* 6. Check whether the negotiated algorithm is the server preference.
* @expect
* 1. successful
* 2. successful
* 3. successful
* 4. successful
* 5. successful
* 6. successful
*/
/* BEGIN_CASE */
void SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC002(char *serverCipherSuite, char *clientCipherSuite, int expectResult)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath_2(serverCtxConfig, serverCipherSuite);
HLT_SetGroups(serverCtxConfig, "NULL");
HLT_SetCipherSuites(serverCtxConfig, serverCipherSuite);
HLT_SetSignature(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384:CERT_SIG_SCHEME_RSA_PKCS1_SHA512");
serverRes = HLT_ProcessTlsAccept(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
int32_t ret = HITLS_SetCipherServerPreference(serverRes->ssl, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath_2(clientCtxConfig, clientCipherSuite);
HLT_SetGroups(clientCtxConfig, "NULL");
HLT_SetCipherSuites(clientCtxConfig, clientCipherSuite);
HLT_SetSignature(clientCtxConfig, "NULL");
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[BUF_SZIE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
HITLS_Ctx *testCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(testCtx->negotiatedInfo.cipherSuiteInfo.cipherSuite == expectResult);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC003
* @title To test the setting of the HITLS_SetCipherServerPreference interface of the dtls.
* @precon Set the signature algorithm preferred by the server.
* @brief
* 1. Initialize the hitls.
* 2. Create an SSL ctx object.
* 3. Create an SSL object.
* 4. Connect
* 5. Check for connection errors.
* 6. Check whether the negotiated signature algorithm is the server preference.
* @expect
* 1. successful
* 2. successful
* 3. successful
* 4. successful
* 5. successful
* 6. successful
*/
/* BEGIN_CASE */
void SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC003(char *serverCipherSuite, char *clientCipherSuite)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath_2(serverCtxConfig, serverCipherSuite);
HLT_SetCipherSuites(serverCtxConfig, serverCipherSuite);
HLT_SetGroups(serverCtxConfig, "NULL");
HLT_SetSignature(serverCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA384:CERT_SIG_SCHEME_RSA_PKCS1_SHA512");
serverRes = HLT_ProcessTlsAccept(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
int32_t ret = HITLS_SetCipherServerPreference(serverRes->ssl, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath_2(clientCtxConfig, clientCipherSuite);
HLT_SetCipherSuites(clientCtxConfig, clientCipherSuite);
HLT_SetGroups(clientCtxConfig, "NULL");
HLT_SetSignature(clientCtxConfig, "CERT_SIG_SCHEME_RSA_PKCS1_SHA512:CERT_SIG_SCHEME_RSA_PKCS1_SHA384");
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[BUF_SZIE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
HITLS_Ctx *testCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(testCtx->negotiatedInfo.signScheme == CERT_SIG_SCHEME_RSA_PKCS1_SHA384);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
/**
* @test SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC004
* @title To test the setting of the HITLS_SetCipherServerPreference interface of the dtls.
* @precon Set the preference group of the server.
* @brief
* 1. Initialize the hitls.
* 2. Create an SSL ctx object.
* 3. Create an SSL object.
* 4. Connect
* 5. Check for connection errors.
* 6. Check whether the negotiated group is the server preference.
* @expect
* 1. successful
* 2. successful
* 3. successful
* 4. successful
* 5. successful
* 6. successful
*/
/* BEGIN_CASE */
void SDV_HITLS_CM_HITLS_GetNegotiateGroup_FUNC_TC004(char *serverCipherSuite, char *clientCipherSuite)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, g_uiPort, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
SetCertPath_2(serverCtxConfig, serverCipherSuite);
HLT_SetCipherSuites(serverCtxConfig, serverCipherSuite);
HLT_SetGroups(serverCtxConfig, "HITLS_EC_GROUP_SECP256R1:HITLS_EC_GROUP_SECP384R1");
HLT_SetSignature(serverCtxConfig, "NULL");
serverRes = HLT_ProcessTlsAccept(localProcess, DTLS1_2, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
int32_t ret = HITLS_SetCipherServerPreference(serverRes->ssl, true);
ASSERT_TRUE(ret == HITLS_SUCCESS);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
SetCertPath_2(clientCtxConfig, clientCipherSuite);
HLT_SetCipherSuites(clientCtxConfig, clientCipherSuite);
HLT_SetGroups(clientCtxConfig, "HITLS_EC_GROUP_SECP384R1:HITLS_EC_GROUP_SECP256R1");
HLT_SetSignature(clientCtxConfig, "NULL");
clientRes = HLT_ProcessTlsConnect(remoteProcess, DTLS1_2, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[BUF_SZIE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
HITLS_Ctx *testCtx = (HITLS_Ctx *)serverRes->ssl;
ASSERT_TRUE(testCtx->negotiatedInfo.negotiatedGroup == HITLS_EC_GROUP_SECP256R1);
EXIT:
HLT_FreeAllProcess();
}
/* END_CASE */
int32_t REC_GetMaxWriteSize(const TLS_Ctx *ctx, uint32_t *len);
int32_t STUB_REC_GetMaxWriteSize(const TLS_Ctx *ctx, uint32_t *len)
{
(void)ctx;
*len = 100;
return HITLS_SUCCESS;
}
/* @
* @test SDV_TLS_CM_FRAGMENTATION_FUNC_TC001
* @title DTLS Message Fragmentation
* @precon nan
* @brief
* 1. Initialize the client and server processes.
* 2. The interface for obtaining the maximum message length is stubbed and the maximum message length is changed to 100.
* 3. Creat and connect linck.
* @expect
* 1. The initialization is successful.
* 2. The stub is successful.
* 3. The link is set up successfully.
@ */
/* BEGIN_CASE */
void SDV_TLS_CM_FRAGMENTATION_FUNC_TC001(void)
{
if (!IsEnableSctpAuth()) {
return;
}
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
HLT_Ctx_Config *serverConfig = NULL;
HLT_Ctx_Config *clientConfig = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
HILT_TransportType connType = SCTP;
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, Port, false);
ASSERT_TRUE(remoteProcess != NULL);
serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(serverConfig != NULL);
ASSERT_TRUE(clientConfig != NULL);
serverRes = HLT_ProcessTlsAccept(remoteProcess, DTLS1_2, serverConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
STUB_Init();
FuncStubInfo stubInfo = {0};
STUB_Replace(&stubInfo, REC_GetMaxWriteSize, STUB_REC_GetMaxWriteSize);
clientRes = HLT_ProcessTlsInit(localProcess, DTLS1_2, clientConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) == 0);
EXIT:
STUB_Reset(&stubInfo);
HLT_FreeAllProcess();
}
/* END_CASE */ | 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_hlt_cm_interface.c | C | unknown | 15,727 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/* BEGIN_HEADER */
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include "securec.h"
#include "bsl_sal.h"
#include "alert.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_crypt_reg.h"
#include "hitls_config.h"
#include "tls_config.h"
#include "hitls.h"
#include "hitls_func.h"
#include "pack_frame_msg.h"
#include "hlt.h"
#include "logger.h"
#include "hitls_cert_type.h"
#include "crypt_util_rand.h"
#include "common_func.h"
#include "frame_tls.h"
#include "conn_init.h"
#include "tls.h"
#include "simulate_io.h"
#include "frame_io.h"
#include "frame_link.h"
#include "stub_replace.h"
#include "session_type.h"
#include "cert_callback.h"
#include "bsl_sal.h"
#include "sal_net.h"
#include "parse_msg.h"
#include "hs_msg.h"
#include "hitls_crypt_init.h"
#include "uio_abstraction.h"
#include "process.h"
#include "rec_wrapper.h"
#include "hs_ctx.h"
#include "hitls_type.h"
/* END_HEADER */
#define Port 7788
#define READ_BUF_SIZE 18432
#define ROOT_DER "%s/ca.der:%s/inter.der"
#define INTCA_DER "%s/inter.der"
#define SERVER_DER "%s/server.der"
#define SERVER_KEY_DER "%s/server.key.der"
#define CLIENT_DER "%s/client.der"
#define CLIENT_KEY_DER "%s/client.key.der"
#define RENEGOTIATE_FAIL 1
#define MAX_CERT_LIST 4294967295
#define MIN_CERT_LIST 0
static uint32_t g_useFlight = 0; /* Range required in the test case */
static uint32_t g_flag; /* Used to record the number of handshake messages in the current flight. */
static uint32_t g_flight = 0; /* is used to record the number of the current flight */
static HLT_FrameHandle g_frameHandle;
static HITLS_Config *GetHitlsConfigViaVersion(int ver)
{
switch (ver) {
case TLS1_2:
case HITLS_VERSION_TLS12:
return HITLS_CFG_NewTLS12Config();
case TLS1_3:
case HITLS_VERSION_TLS13:
return HITLS_CFG_NewTLS13Config();
case DTLS1_2:
case HITLS_VERSION_DTLS12:
return HITLS_CFG_NewDTLS12Config();
default:
return NULL;
}
}
static void TEST_MsgHandle(void *msg, void *data)
{
(void)data;
(void)msg;
}
/* Verify whether the parsed msg meets the requirements. Restrict the msg input parameter. */
static bool CheckHandleType(FRAME_Msg *msg)
{
if (msg->recType.data != REC_TYPE_HANDSHAKE) {
if (msg->recType.data == (uint64_t)g_frameHandle.expectReType) {
return true;
}
} else {
if (msg->recType.data == (uint64_t)g_frameHandle.expectReType &&
msg->body.hsMsg.type.data == (uint64_t)g_frameHandle.expectHsType) {
return true;
}
}
return false;
}
/* Obtain the frameType. The input parameters frameHandle and frameType must not be empty. */
static int32_t GetFrameType(HLT_FrameHandle *frameHandle, FRAME_Type *frameType)
{
if (frameHandle->ctx == NULL) {
return HITLS_NULL_INPUT;
}
TLS_Ctx *tmpCtx = (TLS_Ctx *)frameHandle->ctx;
frameType->versionType = tmpCtx->negotiatedInfo.version > 0 ?
tmpCtx->negotiatedInfo.version : tmpCtx->config.tlsConfig.maxVersion;
frameType->keyExType = tmpCtx->hsCtx->kxCtx->keyExchAlgo;
frameType->recordType = frameHandle->expectReType;
frameType->handshakeType = frameHandle->expectHsType;
return HITLS_SUCCESS;
}
static int32_t STUB_Write(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
{
FRAME_Msg msg = {0};
uint32_t parseLen = 0;
uint32_t offset = 0;
uint32_t msgCnt = 0;
FRAME_Type frameType = { 0 };
(void)GetFrameType(&g_frameHandle, &frameType);
g_flight++;
while (offset < len) {
(void)FRAME_ParseMsgHeader(&frameType, &((uint8_t*)buf)[offset], len - offset, &msg, &parseLen);
offset += parseLen + msg.length.data;
if (g_flight == g_useFlight) {
msgCnt++;
}
FRAME_CleanMsg(&frameType, &msg);
}
if (CheckHandleType(&msg) && g_flight == g_useFlight) {
g_flag = msgCnt;
}
return BSL_UIO_TcpMethod()->uioWrite(uio, buf, len, writeLen);
}
static int SetCertPath(HLT_Ctx_Config *ctxConfig, const char *certStr, bool isServer)
{
char caCertPath[50];
char chainCertPath[30];
char eeCertPath[30];
char privKeyPath[30];
int32_t ret = sprintf_s(caCertPath, sizeof(caCertPath), ROOT_DER, certStr, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(chainCertPath, sizeof(chainCertPath), INTCA_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(eeCertPath, sizeof(eeCertPath), isServer ? SERVER_DER : CLIENT_DER, certStr);
ASSERT_TRUE(ret > 0);
ret = sprintf_s(privKeyPath, sizeof(privKeyPath), isServer ? SERVER_KEY_DER : CLIENT_KEY_DER, certStr);
ASSERT_TRUE(ret > 0);
HLT_SetCaCertPath(ctxConfig, (char *)caCertPath);
HLT_SetChainCertPath(ctxConfig, (char *)chainCertPath);
HLT_SetEeCertPath(ctxConfig, (char *)eeCertPath);
HLT_SetPrivKeyPath(ctxConfig, (char *)privKeyPath);
return 0;
EXIT:
return -1;
}
/* @
* @test SDV_TLS_CFG_SET_GET_VERIFYNONESUPPORT_FUNC_TC001
* @title The server does not verify the client certificate.
* @precon nan
* @brief
* 1. The server invokes the HITLS_CFG_SetVerifyNoneSupport and sets the parameter to false. Expected result 1 is
* obtained.
* 2. The server invokes the HITLS_CFG_SetVerifyNoneSupport interface to obtain the configuration result. (Expected
* result 2)
* 3. The server invokes the HITLS_SetVerifyNoneSupport interface and sets it to true. Expected result 3 is obtained.
* 4. The server invokes the HITLS_GetVerifyNoneSupport interface to obtain the configuration result. (Expected result 4)
* 4. Establish a connection. Expected result 4 is obtained.
* @expect
* 1. The setting is successful.
* 2. The setting is successful.
* 3. The setting is successful.
* 4. The connection is successfully established.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_SET_GET_VERIFYNONESUPPORT_FUNC_TC001(int version, int connType)
{
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
uint8_t c_flag = 0;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, connType, Port, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
ASSERT_EQ(SetCertPath(serverCtxConfig, "ecdsa_sha256", true), 0);
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
serverRes = HLT_ProcessTlsAccept(localProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HITLS_SetVerifyNoneSupport(serverRes->ssl, true);
HITLS_GetVerifyNoneSupport(serverRes->ssl, &c_flag);
ASSERT_TRUE(c_flag == 1);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetCertPath(clientCtxConfig, "ecdsa_sha256/ca.der", "NULL", "NULL", "NULL", "NULL", "NULL");
HLT_SetCipherSuites(serverCtxConfig, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
clientRes = HLT_ProcessTlsInit(remoteProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
ASSERT_EQ(HLT_RpcTlsConnect(remoteProcess, clientRes->sslId), HITLS_SUCCESS);
ASSERT_TRUE(HLT_GetTlsAcceptResult(serverRes) == 0);
ASSERT_TRUE(HLT_ProcessTlsWrite(localProcess, serverRes, (uint8_t *)"Hello World", strlen("Hello World")) == 0);
uint8_t readBuf[READ_BUF_SIZE] = {0};
uint32_t readLen;
ASSERT_TRUE(HLT_ProcessTlsRead(remoteProcess, clientRes, readBuf, sizeof(readBuf), &readLen) == 0);
ASSERT_TRUE(readLen == strlen("Hello World"));
ASSERT_TRUE(memcmp("Hello World", readBuf, readLen) == 0);
EXIT:
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
}
/* END_CASE */
/** @
* @test HITLS_TLS1_2_Config_SDV_23_0_5_047
* @title Enable dual-end verification. The server verifies the client certificate only once.
* @precon nan
* @brief
* 1. The server invokes the HITLS_CFG_SetClientVerifySupport and sets the parameter to true.
* 2. Set the value of HITLS_CFG_SetClientOnceVerifySupport to false when the server invokes the
* HITLS_CFG_SetClientOnceVerifySupport.
* 3. The server invokes the HITLS_CFG_SetClientOnceVerifySupport interface to obtain the configuration result.
* 4. The server invokes the HITLS_SetClientOnceVerifySupport interface and sets it to true.
* 5. The server invokes the HITLS_SetClientOnceVerifySupport interface to obtain the configuration result.
* 6. Establish a connection. After the connection is established, perform renegotiation. Stop the status on the server to
* TRY_SEND_CERTIFICATIONATE_REQUEST. The expected result is obtained.
* @expect
* 1. If the status fails to be stopped, the certificate will not be verified during the renegotiation.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_SET_GET_CLIENTVERIFYUPPORT_FUNC_TC001(int clientverify)
{
FRAME_Init();
HITLS_Config *config_c = NULL;
HITLS_Config *config_s = NULL;
FRAME_LinkObj *client = NULL;
FRAME_LinkObj *server = NULL;
config_c = HITLS_CFG_NewTLS12Config();
config_s = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(config_c != NULL);
ASSERT_TRUE(config_s != NULL);
uint8_t c_flag;
if (clientverify) {
HITLS_CFG_SetClientVerifySupport(config_s, true);
} else {
HITLS_CFG_SetClientVerifySupport(config_s, false);
}
HITLS_CFG_SetClientOnceVerifySupport(config_s, false);
HITLS_CFG_GetClientOnceVerifySupport(config_s, &c_flag);
ASSERT_TRUE(c_flag == 0);
client = FRAME_CreateLink(config_c, BSL_UIO_TCP);
ASSERT_TRUE(client != NULL);
server = FRAME_CreateLink(config_s, BSL_UIO_TCP);
ASSERT_TRUE(server != NULL);
HITLS_SetClientOnceVerifySupport(server->ssl, true);
HITLS_GetClientOnceVerifySupport(server->ssl, &c_flag);
ASSERT_TRUE(c_flag == 1);
HITLS_SetRenegotiationSupport(server->ssl, true);
HITLS_SetRenegotiationSupport(client->ssl, true);
HITLS_GetRenegotiationSupport(server->ssl, &c_flag);
ASSERT_TRUE(c_flag == 1);
ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
uint8_t verifyDataNew[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataNewSize = 0;
uint8_t verifyDataOld[MAX_DIGEST_SIZE] = {0};
uint32_t verifyDataOldSize = 0;
ASSERT_TRUE(HITLS_GetFinishVerifyData(server->ssl, verifyDataOld, sizeof(verifyDataOld),
&verifyDataOldSize) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(client->ssl) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_Renegotiate(server->ssl) == HITLS_SUCCESS);
ASSERT_EQ(FRAME_CreateRenegotiationState(client, server, false, TRY_SEND_CERTIFICATE_REQUEST), HITLS_INTERNAL_EXCEPTION);
ASSERT_TRUE(HITLS_GetFinishVerifyData(server->ssl, verifyDataNew, sizeof(verifyDataNew),
&verifyDataNewSize) == HITLS_SUCCESS);
ASSERT_TRUE(memcmp(verifyDataNew, verifyDataOld, verifyDataOldSize) != 0);
EXIT:
HITLS_CFG_FreeConfig(config_c);
HITLS_CFG_FreeConfig(config_s);
FRAME_FreeLink(client);
FRAME_FreeLink(server);
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_ADD_CAINDICATION_FUNC_TC001
* @title: Add different CA flag indication types.
* @precon nan
* @brief
* 1. Invoke the HITLS_CFG_AddCAIndication interface and set the transferred caType to HITLS_TRUSTED_CA_PRE_AGREED and
* HITLS_TRUSTED_CA_PRE_AGREED respectively. HITLS_TRUSTED_CA_KEY_SHA1, HITLS_TRUSTED_CA_X509_NAME,
* HITLS_TRUSTED_CA_CERT_SHA1, When the HITLS_TRUSTED_CA_UNKNOWN macro is used, expected result 1 is obtained.
* 2. Check the return value of the interface. Expected result 2 is obtained.
* @expect
* 1. The invoking is successful.
* 2. The interface returns HITLS_SUCCESS.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_ADD_CAINDICATION_FUNC_TC001(int tlsVersion)
{
FRAME_Init();
HITLS_Config *config = NULL;
uint8_t data[] = {0};
uint32_t len = sizeof(data);
config = GetHitlsConfigViaVersion(tlsVersion);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_PRE_AGREED, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_KEY_SHA1, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_X509_NAME, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_CERT_SHA1, data, len) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_AddCAIndication(config, HITLS_TRUSTED_CA_UNKNOWN, data, len) == HITLS_SUCCESS);
EXIT:
HITLS_CFG_FreeConfig(config);
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_CIPHERBYID_FUNC_TC001
* @title Obtain the CipherId based on the known cipher suite.
* @precon nan
* @brief
* 1. Invoke the HITLS_CFG_GetCipherByID and set the transferred id to the HITLS_AES_128_GCM_SHA256 macro to obtain the
* HITLS_Cipher structure. (Expected result 1)
* 2. Invoke the HITLS_CFG_GetCipherId interface and transfer the obtained structure. (Expected result 2)
* @expect
* 1. The interface returns the corresponding HITLS_Cipher structure.
* 2. HITLS_CIPHER_AES_128_GCM is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_CIPHERBYID_FUNC_TC001()
{
FRAME_Init();
HITLS_CipherAlgo cipherAlgo;
const HITLS_Cipher* cipher = HITLS_CFG_GetCipherByID(HITLS_AES_128_GCM_SHA256);
HITLS_CFG_GetCipherId(cipher, &cipherAlgo);
ASSERT_EQ(cipherAlgo, HITLS_CIPHER_AES_128_GCM);
EXIT:
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_AUTHID_FUNC_TC001
* @title Self-registration cipher. Invoke the interface to obtain the AuthId.
* @precon nan
* @brief
* 1. Register a HITLS_Cipher structure, set cipherid to HITLS_AUTH_NULL, and call HITLS_CFG_GetAuthId. Expected result 1
* is obtained.
* @expect
* 1. HITLS_AUTH_NULL is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_AUTHID_FUNC_TC001()
{
FRAME_Init();
HITLS_AuthAlgo cipherSuite;
HITLS_Cipher *cipher = (HITLS_Cipher *)malloc(sizeof(HITLS_Cipher));
cipher->authAlg = HITLS_AUTH_NULL;
HITLS_CFG_GetAuthId(cipher, &cipherSuite);
ASSERT_EQ(cipherSuite, HITLS_AUTH_NULL);
EXIT:
free(cipher);
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_CIPHERSUITENAME_FUNC_TC001
* @title Query the name of the algorithm suite.
* @precon nan
* @brief
* 1. Set cipher to HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 and invoke the HITLS_CFG_GetCipherSuiteName interface.
* Expected result 1 is obtained.
* @expect
* 1. Return value of the char* conversion interface, which is the same as that of the
* HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_CIPHERSUITENAME_FUNC_TC001()
{
FRAME_Init();
const HITLS_Cipher* cipher = HITLS_CFG_GetCipherByID(HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256);
const uint8_t* name = HITLS_CFG_GetCipherSuiteName(cipher);
ASSERT_TRUE(strcmp((char *)name, "HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0);
EXIT:
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_CIPHERVERSION_FUNC_TC001
* @title Query the cipher suite version and obtain the algorithm based on the ID.
* @precon nan
* @brief
* 1. Set cipher to HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 () and invoke the HITLS_CFG_GetCipherVersion interface.
* (Expected result 1)
* 2. Set cipher to HITLS_AES_128_GCM_SHA256 () and invoke the HITLS_CFG_GetCipherVersion interface. (Expected result 2)
* 3. Set cipher to HITLS_RSA_WITH_AES_128_CBC_SHA () and invoke the HITLS_CFG_GetCipherVersion interface. (Expected
* result 3)
* @expect
* 1. Interface return value: HITLS_VERSION_TLS12
* 2. Interface return value: HITLS_VERSION_TLS13, HITLS_VERSION_TLS13
* 3. Interface return value: HITLS_VERSION_SSL30
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_CIPHERVERSION_FUNC_TC001()
{
FRAME_Init();
int32_t version;
const HITLS_Cipher *cipher = HITLS_CFG_GetCipherByID(HITLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256);
ASSERT_EQ(HITLS_CFG_GetCipherVersion(cipher, &version), HITLS_SUCCESS);
ASSERT_EQ(HITLS_VERSION_TLS12, version);
cipher = HITLS_CFG_GetCipherByID(HITLS_AES_128_GCM_SHA256);
ASSERT_EQ(HITLS_CFG_GetCipherVersion(cipher, &version), HITLS_SUCCESS);
ASSERT_EQ(HITLS_VERSION_TLS13, version);
cipher = HITLS_CFG_GetCipherByID(HITLS_RSA_WITH_AES_128_CBC_SHA);
ASSERT_EQ(HITLS_CFG_GetCipherVersion(cipher, &version), HITLS_SUCCESS);
ASSERT_EQ(HITLS_VERSION_SSL30, version);
EXIT:
version = 1;
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_CIPHERSUITE_FUNC_TC001
* @title Obtain the cipher suite based on the supported cipher suite ID.
* @precon nan
* @brief
* 1. Invoke the HITLS_CFG_GetCipherByID and set the input ID to the HITLS_AES_128_GCM_SHA256 macro to obtain the
* cipherinfo structure. Expected result 1 is obtained.
* 2. Invoke the HITLS_CFG_GetCipherSuite interface and transfer the obtained structure. (Expected result 2)
* @expect
* 1. The interface returns the corresponding HITLS_Cipher structure.
* 2. HITLS_AES_128_GCM_SHA256 is returned.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_CIPHERSUITE_FUNC_TC001()
{
FRAME_Init();
uint16_t cipherSuite;
const HITLS_Cipher* cipher = HITLS_CFG_GetCipherByID(HITLS_AES_128_GCM_SHA256);
HITLS_CFG_GetCipherSuite(cipher, &cipherSuite);
ASSERT_EQ(cipherSuite, HITLS_AES_128_GCM_SHA256);
EXIT:
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_FLIGHTTRANSMITSWITH_FUNC_TC001
* @titleThe client sends messages by flight.
* @precon nan
* @brief 1. The server invokes the HITLS_CFG_SetFlightTransmitSwitch interface and sets the parameter to false. Expected
result 1 is obtained.
2. The server invokes the HITLS_CFG_GetFlightTransmitSwitch interface to obtain the configuration result.
(Expected result 2)
3. The client invokes the HITLS_SetFlightTransmitSwitch interface to set the parameter to true. Expected result
3 is obtained.
4. The client invokes the HITLS_GetFlightTransmitSwitch interface to obtain the configuration result. Expected
result 4 is obtained.
5. Establish a link and count the number of messages sent by the client in the second flight. Expected result 5
is obtained.
* @expect
1. The setting is successful.
2. The obtained result is false.
3. The setting is successful.
4. The obtained result is true.
5. The connection is set up successfully, and the number of the second flight messages sent by the client is 3.
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_FLIGHTTRANSMITSWITH_FUNC_TC001(int version)
{
FRAME_Init();
HITLS_Config *Config = NULL;
HITLS_Ctx *ctx = NULL;
uint8_t support;
Config = GetHitlsConfigViaVersion(version);
ASSERT_TRUE(Config != NULL);
ctx = HITLS_New(Config);
ASSERT_TRUE(ctx != NULL);
HITLS_CFG_SetFlightTransmitSwitch(Config, false);
HITLS_CFG_GetFlightTransmitSwitch(Config, &support);
ASSERT_TRUE(support == false);
HITLS_SetFlightTransmitSwitch(ctx, true);
HITLS_GetFlightTransmitSwitch(ctx, &support);
ASSERT_TRUE(support == true);
HLT_Tls_Res *serverRes = NULL;
HLT_Tls_Res *clientRes = NULL;
HLT_Process *localProcess = NULL;
HLT_Process *remoteProcess = NULL;
localProcess = HLT_InitLocalProcess(HITLS);
ASSERT_TRUE(localProcess != NULL);
remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, Port, true);
ASSERT_TRUE(remoteProcess != NULL);
HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
ASSERT_TRUE(serverCtxConfig != NULL);
HLT_SetFlightTransmitSwitch(serverCtxConfig, false);
serverRes = HLT_ProcessTlsAccept(remoteProcess, version, serverCtxConfig, NULL);
ASSERT_TRUE(serverRes != NULL);
HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
ASSERT_TRUE(clientCtxConfig != NULL);
HLT_SetFlightTransmitSwitch(clientCtxConfig, true);
clientRes = HLT_ProcessTlsInit(localProcess, version, clientCtxConfig, NULL);
ASSERT_TRUE(clientRes != NULL);
HLT_FrameHandle frameHandle = {
.ctx = clientRes->ssl,
.frameCallBack = TEST_MsgHandle,
.userData = NULL,
.expectHsType = CLIENT_KEY_EXCHANGE,
.expectReType = REC_TYPE_HANDSHAKE,
.ioState = EXP_NONE,
.pointType = POINT_SEND,
.method.uioWrite = STUB_Write,
};
ASSERT_TRUE(HLT_SetFrameHandle(&frameHandle) == HITLS_SUCCESS);
g_useFlight = 2;
ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) == HITLS_SUCCESS);
if (version == TLS1_2) {
ASSERT_EQ(g_flag, 3);
} else {
ASSERT_EQ(g_flag, 2);
}
HLT_CleanFrameHandle();
EXIT:
g_flag = 0;
g_flight = 0;
HLT_CleanFrameHandle();
HLT_FreeAllProcess();
HITLS_CFG_FreeConfig(Config);
HITLS_Free(ctx);
return;
}
/* END_CASE */
/** @
* @test SDV_TLS_CFG_GET_MAXCERTLIST_API_TC001
* @title HTLS_CFG_SetMaxCertList, HITLS_CFG_GetMaxCertList, HITLS_SetMaxCertList, and HITLS_GetMaxCertList APIs
* @precon nan
* @brief
* 1. Apply for and initialize config and ctx.
* 2. Set the certificate chain length config to null and invoke the HITLS_CFG_SetMaxCertList interface.
* 3. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value.
* 4. Set the maximum length of the certificate chain by calling the HITLS_CFG_SetMaxCertList interface.
* 5. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value.
* 6. Set the minimum certificate chain length by calling the HITLS_CFG_SetMaxCertList interface.
* 7. Invoke the HITLS_CFG_GetMaxCertList interface and check the output parameter value.
* 8. Use the HITLS_SetMaxCertList and HITLS_GetMaxCertList interfaces to repeat the preceding test.
* @expect
* 1. Initialization succeeds.
* 2. HITLS_NULL_INPUT is returned.
* 3. HITLS_NULL_INPUT is returned.
* 4. The interface returns HITLS_SUCCESS.
* 5. The value of MaxCertList returned by the interface is 2 ^ 32 - 1.
* 6. The interface returns the HITLS_SUCCESS.
* 7. The value of MaxCertList returned by the interface is 0.
* 8. Same as above
@ */
/* BEGIN_CASE */
void SDV_TLS_CFG_GET_MAXCERTLIST_API_TC001()
{
FRAME_Init();
HITLS_Config *tlsConfig;
HITLS_Ctx *ctx = NULL;
tlsConfig = HITLS_CFG_NewTLS12Config();
ASSERT_TRUE(tlsConfig != NULL);
ctx = HITLS_New(tlsConfig);
ASSERT_TRUE(ctx != NULL);
uint32_t maxSize;
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(NULL, MAX_CERT_LIST) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(NULL, &maxSize) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(tlsConfig, MAX_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(tlsConfig, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MAX_CERT_LIST);
ASSERT_TRUE(HITLS_CFG_SetMaxCertList(tlsConfig, MIN_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_CFG_GetMaxCertList(tlsConfig, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MIN_CERT_LIST);
ASSERT_TRUE(HITLS_SetMaxCertList(NULL, MAX_CERT_LIST) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_GetMaxCertList(NULL, &maxSize) == HITLS_NULL_INPUT);
ASSERT_TRUE(HITLS_SetMaxCertList(ctx, MAX_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetMaxCertList(ctx, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MAX_CERT_LIST);
ASSERT_TRUE(HITLS_SetMaxCertList(ctx, MIN_CERT_LIST) == HITLS_SUCCESS);
ASSERT_TRUE(HITLS_GetMaxCertList(ctx, &maxSize) == HITLS_SUCCESS);
ASSERT_TRUE(maxSize == MIN_CERT_LIST);
EXIT:
HITLS_CFG_FreeConfig(tlsConfig);
HITLS_Free(ctx);
}
/* END_CASE */
| 2302_82127028/openHiTLS-examples_1508 | testcode/sdv/testcase/tls/interface_tlcp/test_suite_sdv_hlt_config_interface.c | C | unknown | 24,545 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef ALTER_H
#define ALTER_H
#include <stdbool.h>
#include <stdint.h>
#include "hitls_build.h"
#include "tls.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
ALERT_FLAG_NO = 0, /* no alert message */
ALERT_FLAG_RECV, /* received the alert message */
ALERT_FLAG_SEND, /* the alert message needs to be sent */
} ALERT_FLAG;
/** obtain the messages about receiving and sending by Alert */
typedef struct {
uint8_t flag; /* send and receive flags, see ALERT_FLAG */
uint8_t level; /* Alert level. For details, see ALERT_Level. */
uint8_t description; /* Alert description. For details, see ALERT_Description. */
uint8_t reverse; /* reserve, 4-byte aligned */
} ALERT_Info;
/**
* @ingroup alert
* @brief Alert initialization function
*
* @param ctx [IN] tls Context
*
* @retval HITLS_SUCCESS succeeded.
* @retval HITLS_INTERNAL_EXCEPTION An unexpected internal error occurs.
* @retval HITLS_MEMALLOC_FAIL Failed to apply for memory.
*/
int32_t ALERT_Init(TLS_Ctx *ctx);
/**
* @ingroup alert
* @brief Alert deinitialization function
*
* @param ctx [IN] tls Context
*
*/
void ALERT_Deinit(TLS_Ctx *ctx);
/**
* @ingroup alert
* @brief Check whether there are received or sent alert messages to be processed.
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
*
* @retval true: The processing is required.
* @retval false: No processing is required.
*/
bool ALERT_GetFlag(const TLS_Ctx *ctx);
/**
* @ingroup alert
* @brief Obtain the alert information.
*
* @attention ctx and info cannot be empty. Ensure that the value is used when Alert_GetFlag is true.
* @param ctx [IN] tls Context
* @param info [IN] Alert information record
*/
void ALERT_GetInfo(const TLS_Ctx *ctx, ALERT_Info *info);
/**
* @brief Clear the alert information.
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
*/
void ALERT_CleanInfo(const TLS_Ctx *ctx);
/**
* @brief Send an alert message and cache it in the alert module.
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
* @param level [IN] Alert level
* @param description [IN] alert Description
*
*/
void ALERT_Send(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description);
/**
* @brief Send the alert message cached by the alert module to the network layer.
*
* @attention ctx cannot be empty. Alert_Send must be invoked before flushing.
* @param ctx [IN] tls Context
*
* @retval HITLS_SUCCESS succeeded.
* @retval See REC_Write
*/
int32_t ALERT_Flush(TLS_Ctx *ctx);
/**
* @brief Process alert message after decryption
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
* @param data [IN] alert data
* @param dataLen [IN] alert data length
* @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
int32_t ProcessDecryptedAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen);
/**
* @brief Process plaintext alert message in TLS13
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
* @param data [IN] alert data
* @param dataLen [IN] alert data length
* @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
int32_t ProcessPlainAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen);
/**
* @ingroup alert
* @brief Clear the number of consecutive received warnings
*
* @param ctx [IN] tls Context
*/
void ALERT_ClearWarnCount(TLS_Ctx *ctx);
/**
* @ingroup alert
* @brief Increase the number of alert and check whether it has exceeded the threshold or not
*
* @param ctx [IN] tls Context
* @param threshold [IN] alert number threshold
* @retval the number of alert has exceeded the threshold or not
*/
bool ALERT_HaveExceeded(TLS_Ctx *ctx, uint8_t threshold);
#ifdef HITLS_BSL_LOG
int32_t ReturnAlertProcess(TLS_Ctx *ctx, int32_t err, uint32_t logId, const void *logStr,
ALERT_Description description);
#define RETURN_ALERT_PROCESS(ctx, err, logId, logStr, description) \
ReturnAlertProcess(ctx, err, logId, LOG_STR(logStr), description)
#else
#define RETURN_ALERT_PROCESS(ctx, err, logId, logStr, description) \
(ctx)->method.sendAlert(ctx, ALERT_LEVEL_FATAL, description), (err)
#endif /* HITLS_BSL_LOG */
#ifdef __cplusplus
}
#endif
#endif /* ALTER_H */ | 2302_82127028/openHiTLS-examples_1508 | tls/alert/include/alert.h | C | unknown | 4,799 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#include "securec.h"
#include "bsl_sal.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "hitls_error.h"
#include "tls.h"
#include "rec.h"
#ifdef HITLS_TLS_FEATURE_FLIGHT
#include "bsl_uio.h"
#include "hitls.h"
#endif
#include "record.h"
#include "alert.h"
#define ALERT_DATA_LEN 2u /* alert data length */
/** Alert context, which records the sending and receiving information */
struct AlertCtx {
uint8_t flag; /* send and receive flags, for details, see ALERT_FLAG */
bool isFlush; /* whether the message is sent successfully */
uint8_t warnCount; /* count the number of consecutive received warnings */
uint8_t level; /* Alert level. For details, see ALERT_Level */
uint8_t description; /* Alert description: For details, see ALERT_Description */
uint8_t reverse; /* reserve, 4-byte aligned */
};
bool ALERT_GetFlag(const TLS_Ctx *ctx)
{
return (ctx->alertCtx->flag != ALERT_FLAG_NO);
}
void ALERT_GetInfo(const TLS_Ctx *ctx, ALERT_Info *info)
{
struct AlertCtx *alertCtx = ctx->alertCtx;
info->flag = alertCtx->flag;
info->level = alertCtx->level;
info->description = alertCtx->description;
return;
}
void ALERT_CleanInfo(const TLS_Ctx *ctx)
{
uint8_t alertCount = ctx->alertCtx->warnCount;
(void)memset_s(ctx->alertCtx, sizeof(struct AlertCtx), 0, sizeof(struct AlertCtx));
ctx->alertCtx->warnCount = alertCount;
return;
}
/* check whether the operation is abnormal */
bool AlertIsAbnormalInput(const struct AlertCtx *alertCtx, ALERT_Level level)
{
if (level != ALERT_LEVEL_FATAL && level != ALERT_LEVEL_WARNING) {
return true;
}
if (alertCtx->flag != ALERT_FLAG_NO) {
// a critical alert exists and cannot be overwritten
if (alertCtx->level == ALERT_LEVEL_FATAL) {
return true;
}
// common alarms are not allowed to overwrite CLOSE NOTIFY
if (level == ALERT_LEVEL_WARNING &&
alertCtx->level == ALERT_LEVEL_WARNING &&
alertCtx->description == ALERT_CLOSE_NOTIFY) {
return true;
}
}
return false;
}
void ALERT_Send(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
{
struct AlertCtx *alertCtx = ctx->alertCtx;
// prevent abnormal operations
if (AlertIsAbnormalInput(alertCtx, level)) {
return;
}
alertCtx->level = (uint8_t)level;
alertCtx->description = (uint8_t)description;
alertCtx->flag = ALERT_FLAG_SEND;
alertCtx->isFlush = false;
return;
}
int32_t ALERT_Flush(TLS_Ctx *ctx)
{
struct AlertCtx *alertCtx = ctx->alertCtx;
int32_t ret;
if (alertCtx->flag != ALERT_FLAG_SEND) {
BSL_ERR_PUSH_ERROR(HITLS_ALERT_NO_WANT_SEND);
return HITLS_ALERT_NO_WANT_SEND;
}
#ifdef HITLS_TLS_PROTO_TLS
if (REC_GetOutBufPendingSize(ctx) != 0) {
ret = REC_OutBufFlush(ctx);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
#endif
if (alertCtx->isFlush == false) {
if (ctx->recCtx != NULL && ctx->recCtx->pendingData != NULL && alertCtx->description == ALERT_CLOSE_NOTIFY) {
return HITLS_REC_NORMAL_IO_BUSY;
}
uint8_t data[ALERT_DATA_LEN];
/** obtain the alert level */
data[0] = alertCtx->level;
data[1] = alertCtx->description;
if (ctx->negotiatedInfo.version == HITLS_VERSION_SSL30 && alertCtx->description == ALERT_PROTOCOL_VERSION) {
data[1] = ALERT_HANDSHAKE_FAILURE;
}
/** write the record */
ret = REC_Write(ctx, REC_TYPE_ALERT, data, ALERT_DATA_LEN);
if (!IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask)) {
alertCtx->isFlush = true;
}
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16267, "Write fail");
}
alertCtx->isFlush = true;
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15768, BSL_LOG_LEVEL_WARN, BSL_LOG_BINLOG_TYPE_RUN,
"Sent an Alert msg:level[%u] description[%u]", data[0], data[1], 0, 0);
}
#ifdef HITLS_TLS_FEATURE_FLIGHT
/* if isFlightTransmitEnable is enabled, the stored handshake information needs to be sent */
uint8_t isFlightTransmitEnable = 0;
(void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
if (isFlightTransmitEnable == 1) {
ret = REC_FlightTransmit(ctx);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
#endif /* HITLS_TLS_FEATURE_FLIGHT */
return HITLS_SUCCESS;
}
#ifdef HITLS_TLS_PROTO_TLS13
static uint32_t ALERT_GetVersion(const TLS_Ctx *ctx)
{
if (ctx->negotiatedInfo.version > 0) {
/* the version has been negotiated */
return ctx->negotiatedInfo.version;
} else {
/* if the version is not negotiated, the latest version supported by the local end is returned */
return ctx->config.tlsConfig.maxVersion;
}
}
#endif /* HITLS_TLS_PROTO_TLS13 */
int32_t ALERT_Init(TLS_Ctx *ctx)
{
if (ctx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15772, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ctx null.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
// prevent multi init of ctx->alertCtx
if (ctx->alertCtx != NULL) {
return HITLS_SUCCESS;
}
ctx->alertCtx = (struct AlertCtx *)BSL_SAL_Malloc(sizeof(struct AlertCtx));
if (ctx->alertCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15773, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"malloc alert ctx fail.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return HITLS_MEMALLOC_FAIL;
}
(void)memset_s(ctx->alertCtx, sizeof(struct AlertCtx), 0, sizeof(struct AlertCtx));
return HITLS_SUCCESS;
}
void ALERT_Deinit(TLS_Ctx *ctx)
{
if (ctx == NULL) {
return;
}
BSL_SAL_FREE(ctx->alertCtx);
return;
}
int32_t ProcessDecryptedAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen)
{
struct AlertCtx *alertCtx = ctx->alertCtx;
/** if the message lengths are not equal, an error code is returned */
if (dataLen != ALERT_DATA_LEN) {
BSL_ERR_PUSH_ERROR(HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15769, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"get a alert msg with illegal len %u", dataLen, 0, 0, 0);
ALERT_Send(ctx, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE);
return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
}
/** record the alert message */
if (data[0] == ALERT_LEVEL_FATAL || data[0] == ALERT_LEVEL_WARNING) {
// prevent abnormal operations
if (AlertIsAbnormalInput(alertCtx, data[0]) == true) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID16268, "input abnormal");
}
alertCtx->flag = ALERT_FLAG_RECV;
alertCtx->level = data[0];
alertCtx->description = data[1];
#ifdef HITLS_TLS_PROTO_TLS13
if (ALERT_GetVersion(ctx) == HITLS_VERSION_TLS13 && alertCtx->description != ALERT_CLOSE_NOTIFY) {
alertCtx->level = ALERT_LEVEL_FATAL;
}
#endif
if (alertCtx->level == ALERT_LEVEL_FATAL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16269, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"alert fatal", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
}
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15770, BSL_LOG_LEVEL_WARN, BSL_LOG_BINLOG_TYPE_RUN,
"got a alert msg:level[%u] description[%u]", data[0], data[1], 0, 0);
return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
}
BSL_ERR_PUSH_ERROR(HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15771, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"get a alert msg with illegal type", 0, 0, 0, 0);
/** Decoding error. Send an alert. */
ALERT_Send(ctx, ALERT_LEVEL_FATAL, ALERT_ILLEGAL_PARAMETER);
return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
}
#ifdef HITLS_TLS_PROTO_TLS13
int32_t ProcessPlainAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen)
{
if (ctx->isClient == true && REC_HaveReadSuiteInfo(ctx)) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID16270,
"receive plain alert", ALERT_UNEXPECTED_MESSAGE);
}
if (ctx->isClient == false && ctx->plainAlertForbid == true) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID16271,
"tls1.3 forbid to receive plain alert", ALERT_UNEXPECTED_MESSAGE);
}
return ProcessDecryptedAlert(ctx, data, dataLen);
}
#endif /* HITLS_TLS_PROTO_TLS13 */
void ALERT_ClearWarnCount(TLS_Ctx *ctx)
{
ctx->alertCtx->warnCount = 0;
return;
}
bool ALERT_HaveExceeded(TLS_Ctx *ctx, uint8_t threshold)
{
ctx->alertCtx->warnCount += 1;
return ctx->alertCtx->warnCount >= threshold;
}
#ifdef HITLS_BSL_LOG
int32_t ReturnAlertProcess(TLS_Ctx *ctx, int32_t err, uint32_t logId, const void *logStr,
ALERT_Description description)
{
if (logStr != NULL) {
BSL_LOG_BINLOG_FIXLEN(logId, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, logStr, 0, 0, 0, 0);
}
if (description != ALERT_UNKNOWN) {
ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, description);
}
return err;
}
int32_t ReturnErrorNumberProcess(int32_t err, uint32_t logId, const void *logStr)
{
(void)logStr;
BSL_LOG_BINLOG_FIXLEN(logId, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, logStr, 0, 0, 0, 0);
return err;
}
#endif /* HITLS_BSL_LOG */ | 2302_82127028/openHiTLS-examples_1508 | tls/alert/src/alert.c | C | unknown | 10,275 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef APP_H
#define APP_H
#include <stdint.h>
#include "hitls_build.h"
#include "tls.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @ingroup app
* @brief TLS can read data of any length, not in the unit of record. DTLS can read data in the unit of record.
* Reads num bytes from the CTX to the buffer. Users can transfer any num bytes (num must be greater than 0).
*
* @attention Reads only the application data decrypted by one record at a time.
* HITLS copies the application data to the input cache.
* If the cache size is less than 16K, the maximum size of the application message decrypted from a single record is 16K
* This will result in a partial copy of the application data.
* You can call APP_GetReadPendingBytes to obtain the size of the remaining readable application data in current record.
* This is useful in DTLS scenarios.
*
* @param ctx [IN] TLS context
* @param buf [OUT] Place the data which read from the TLS context into the buffer.
* @param num [IN] Attempting to read num bytes
* @param readLen [OUT] Read length
*
* @retval HITLS_SUCCESS Read successful.
* @retval Other return value refers to REC_Read.
*/
int32_t APP_Read(TLS_Ctx *ctx, uint8_t *buf, uint32_t num, uint32_t *readLen);
/**
* @ingroup app
* @brief Obtain the maximum writable plaintext length of a single record.
*
* @param ctx [IN] TLS_Ctx context
* @param len [OUT] Maximum length of the plaintext
*
* @retval HITLS_SUCCESS Obtain successful.
* @retval Other return value refers to REC_GetMaxWriteSize.
*/
int32_t APP_GetMaxWriteSize(const TLS_Ctx *ctx, uint32_t *len);
/**
* @ingroup app
* @brief Send app message in the unit of record.
*
* @param ctx [IN] TLS context
* @param data [IN] Data to be written
* @param dataLen [IN] Data length
* @param writeLen [OUT] Length of Successful Writes
*
* @retval HITLS_SUCCESS Write successful.
* @retval HITLS_APP_ERR_TOO_LONG_TO_WRITE The data to be written is too long.
* @retval Other reuturn value referst to REC_Write.
*/
int32_t APP_Write(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t *writeLen);
#ifdef __cplusplus
}
#endif
#endif
| 2302_82127028/openHiTLS-examples_1508 | tls/app/include/app.h | C | unknown | 2,688 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#include "securec.h"
#include "bsl_sal.h"
#include "bsl_list.h"
#include "tls_binlog_id.h"
#include "bsl_uio.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "hitls_error.h"
#include "rec.h"
#include "app_ctx.h"
#include "rec.h"
#include "record.h"
#include "app.h"
static int32_t ReadAppData(TLS_Ctx *ctx, uint8_t *buf, uint32_t num, uint32_t *readLen)
{
return REC_Read(ctx, REC_TYPE_APP, buf, readLen, num);
}
int32_t APP_Read(TLS_Ctx *ctx, uint8_t *buf, uint32_t num, uint32_t *readLen)
{
int32_t ret;
uint32_t readbytes;
if (ctx == NULL || buf == NULL || num == 0) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15659, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"APP: input null pointer or read bufLen is 0.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_APP_ERR_ZERO_READ_BUF_LEN);
return HITLS_APP_ERR_ZERO_READ_BUF_LEN;
}
// read data to the buffer in non-blocking mode
do {
ret = ReadAppData(ctx, buf, num, &readbytes);
if (ret != HITLS_SUCCESS) {
return ret;
}
} while (readbytes == 0); // do not exit the loop until data is read
*readLen = readbytes;
return HITLS_SUCCESS;
}
int32_t APP_GetMaxWriteSize(const TLS_Ctx *ctx, uint32_t *len)
{
return REC_GetMaxWriteSize(ctx, len);
}
static int32_t SavePendingData(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen)
{
#ifdef HITLS_TLS_PROTO_DTLS
if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask)) {
return HITLS_SUCCESS;
}
#endif
RecCtx *recCtx = (RecCtx *)ctx->recCtx;
// Stores the plaintext data to be sent.
recCtx->pendingData = data;
recCtx->pendingDataSize = dataLen;
return HITLS_SUCCESS;
}
static int32_t CheckDataLen(TLS_Ctx *ctx, const uint8_t *data, uint32_t *sendLen)
{
RecCtx *recCtx = (RecCtx *)ctx->recCtx;
if (recCtx->pendingData != NULL) {
if ((
#ifdef HITLS_TLS_FEATURE_MODE_ACCEPT_MOVING_WRITE_BUFFER
(ctx->config.tlsConfig.modeSupport & HITLS_MODE_ACCEPT_MOVING_WRITE_BUFFER) == 0 &&
#endif
recCtx->pendingData != data) || recCtx->pendingDataSize > *sendLen) {
ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16241, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"The two buffer addresses are inconsistent.", 0, 0, 0, 0);
return HITLS_APP_ERR_WRITE_BAD_RETRY;
}
*sendLen = recCtx->pendingDataSize;
return HITLS_SUCCESS;
}
uint32_t maxWriteLen = 0u;
int32_t ret = REC_GetMaxWriteSize(ctx, &maxWriteLen);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15660, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"APP: Get record max write size fail.", 0, 0, 0, 0);
return ret;
}
if (*sendLen > maxWriteLen) {
*sendLen = maxWriteLen;
}
return SavePendingData(ctx, data, *sendLen);
}
int32_t APP_Write(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t *writeLen)
{
int32_t ret = HITLS_SUCCESS;
#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
ret = REC_QueryMtu(ctx);
if (ret != HITLS_SUCCESS) {
return ret;
}
#endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */
ret = REC_RecOutBufReSet(ctx);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint32_t sendLen = dataLen;
ret = CheckDataLen(ctx, data, &sendLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
*writeLen = 0;
ret = REC_Write(ctx, REC_TYPE_APP, data, sendLen);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16274, "Write fail");
}
#ifdef HITLS_TLS_FEATURE_FLIGHT
if (ctx->config.tlsConfig.isFlightTransmitEnable) {
ret = REC_FlightTransmit(ctx);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
#endif
*writeLen = sendLen;
ctx->recCtx->pendingData = NULL;
ctx->recCtx->pendingDataSize = 0;
return HITLS_SUCCESS;
} | 2302_82127028/openHiTLS-examples_1508 | tls/app/src/app.c | C | unknown | 4,663 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef APP_CTX_H
#define APP_CTX_H
#include <stdint.h>
#include "hitls_build.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HITLS_TLS_FEATURE_RENEGOTIATION
/**
* @ingroup hitls_cert_type
* @brief Describe the APP cache linked list.
*/
typedef struct BslList AppList;
#endif
typedef struct {
uint8_t *buf; /* buffer */
uint32_t bufSize; /* size of the buffer */
uint32_t start; /* start position */
uint32_t end; /* end position */
} AppBuf;
#ifdef __cplusplus
}
#endif
#endif | 2302_82127028/openHiTLS-examples_1508 | tls/app/src/app_ctx.h | C | unknown | 1,066 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef CHANGE_CIPHER_SPEC_H
#define CHANGE_CIPHER_SPEC_H
#include <stdint.h>
#include "hitls_build.h"
#include "tls.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @ingroup change cipher spec
* @brief CCS initialization function
*
* @param ctx [IN] SSL context
*
* @retval HITLS_SUCCESS Initializition successful.
* @retval HITLS_INTERNAL_EXCEPTION An unexpected internal error occurs.
* @retval HITLS_MEMALLOC_FAIL Failed to apply for memory.
*/
int32_t CCS_Init(TLS_Ctx *ctx);
/**
* @ingroup change cipher spec
* @brief CCS deinitialization function
*
* @param ctx [IN] ssl context
*
*/
void CCS_DeInit(TLS_Ctx *ctx);
/**
* @ingroup change cipher spec
* @brief Check whether the Change cipher spec message is received.
*
* @param ctx [IN] TLS context
*
* @retval True if the Change cipher spec message is received else false.
*/
bool CCS_IsRecv(const TLS_Ctx *ctx);
/**
* @ingroup change cipher spec
* @brief Send a packet for changing the cipher suite.
*
* @param ctx [IN] TLS context
*
* @retval HITLS_SUCCESS Send successful.
* @retval HITLS_INTERNAL_EXCEPTION An unexpected internal error occurs.
* @retval For other error codes, see REC_Write.
*/
int32_t CCS_Send(TLS_Ctx *ctx);
/**
* @ingroup change cipher spec
* @brief Control function
*
* @param ctx [IN] TLS context
* @param cmd [IN] Control command
*
* @retval HITLS_SUCCESS succeeded.
* @retval HITLS_INTERNAL_EXCEPTION An unexpected internal error
* @retval HITLS_CCS_INVALID_CMD Invalid instruction
*/
int32_t CCS_Ctrl(TLS_Ctx *ctx, CCS_Cmd cmd);
/**
* @brief Process CCS message after decryption
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
* @param data [IN] ccs data
* @param dataLen [IN] ccs data length
* @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
int32_t ProcessDecryptedCCS(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen);
/**
* @brief Process plaintext CCS message in TLS13
*
* @attention ctx cannot be empty.
* @param ctx [IN] tls Context
* @param data [IN] ccs data
* @param dataLen [IN] ccs data length
* @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG
*/
int32_t ProcessPlainCCS(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen);
#ifdef __cplusplus
}
#endif
#endif
| 2302_82127028/openHiTLS-examples_1508 | tls/ccs/include/change_cipher_spec.h | C | unknown | 2,864 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#include "securec.h"
#include "bsl_sal.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "hitls_error.h"
#include "bsl_uio.h"
#include "uio_base.h"
#include "rec.h"
#ifdef HITLS_TLS_FEATURE_INDICATOR
#include "indicator.h"
#endif
#include "hs.h"
#include "alert.h"
#include "change_cipher_spec.h"
struct CcsCtx {
bool isReady; /* Whether to allow receiving CCS */
bool ccsRecvflag; /* Indicates whether the CCS is received. */
bool isAllowActiveCipher; /* Flag for allow activating the receiving key suite */
bool activeCipherFlag; /* Flag for activating the receiving key suite */
};
bool CCS_IsRecv(const TLS_Ctx *ctx)
{
return ctx->ccsCtx->ccsRecvflag;
}
int32_t CCS_Send(TLS_Ctx *ctx)
{
int32_t ret;
const uint8_t buf[1] = {1u};
const uint32_t len = 1u;
if (ctx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15616, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ctx null.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_SCTP) && defined(HITLS_TLS_FEATURE_RENEGOTIATION)
/* rfc6083 4.7. Handshake
Before sending a ChangeCipherSpec message, all outstanding SCTP user
messages MUST have been acknowledged by the SCTP peer and MUST NOT be
revoked by the SCTP peer. */
if (BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_SCTP) && ctx->negotiatedInfo.isRenegotiation) {
bool isBuffEmpty = false;
ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_SCTP_SND_BUFF_IS_EMPTY, (int32_t)sizeof(isBuffEmpty), &isBuffEmpty);
if (ret != BSL_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16275, BSL_LOG_LEVEL_FATAL, BSL_LOG_BINLOG_TYPE_RUN,
"UIO_Ctrl fail, ret %d", ret, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_UIO_FAIL);
return HITLS_UIO_FAIL;
}
/* When the SCTP sending buffer is not empty, the CCS cannot be sent. */
if (isBuffEmpty != true) {
BSL_ERR_PUSH_ERROR(HITLS_REC_NORMAL_IO_BUSY);
return HITLS_REC_NORMAL_IO_BUSY;
}
}
#endif
/** Write record */
ret = REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, buf, len);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16276, "Write fail");
}
#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask) &&
BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
ret = REC_RetransmitListAppend(ctx->recCtx, REC_TYPE_CHANGE_CIPHER_SPEC, buf, len);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
#endif
#ifdef HITLS_TLS_FEATURE_INDICATOR
INDICATOR_MessageIndicate(1, HS_GetVersion(ctx), REC_TYPE_CHANGE_CIPHER_SPEC, buf, 1,
ctx, ctx->config.tlsConfig.msgArg);
#endif
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15617, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"written a change cipher spec message.", 0, 0, 0, 0);
return HITLS_SUCCESS;
}
int32_t CCS_Ctrl(TLS_Ctx *ctx, CCS_Cmd cmd)
{
if (ctx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15618, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ctx null.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
switch (cmd) {
case CCS_CMD_RECV_READY:
ctx->ccsCtx->isReady = true;
break;
case CCS_CMD_RECV_EXIT_READY:
ctx->ccsCtx->isReady = false;
ctx->ccsCtx->ccsRecvflag = false;
ctx->ccsCtx->isAllowActiveCipher = false;
ctx->ccsCtx->activeCipherFlag = false;
break;
case CCS_CMD_RECV_ACTIVE_CIPHER_SPEC:
ctx->ccsCtx->isAllowActiveCipher = true;
if (ctx->ccsCtx->ccsRecvflag == true && ctx->ccsCtx->activeCipherFlag == false) {
/** Enable key specification */
int32_t ret = REC_ActivePendingState(ctx, false);
if (ret != HITLS_SUCCESS) {
ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
return ret;
}
ctx->ccsCtx->activeCipherFlag = true;
}
break;
default:
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15619, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"ChangeCipherSpec error ctrl cmd", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_CCS_INVALID_CMD);
return HITLS_CCS_INVALID_CMD;
}
return HITLS_SUCCESS;
}
int32_t CCS_Init(TLS_Ctx *ctx)
{
if (ctx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15620, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ctx null.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
// Prevent the ctx->ccsCtx from being initialized multiple times.
if (ctx->ccsCtx != NULL) {
return HITLS_SUCCESS;
}
ctx->ccsCtx = (struct CcsCtx *)BSL_SAL_Malloc(sizeof(struct CcsCtx));
if (ctx->ccsCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15621, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"ccs ctx malloc failed.", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return HITLS_MEMALLOC_FAIL;
}
(void)memset_s(ctx->ccsCtx, sizeof(struct CcsCtx), 0, sizeof(struct CcsCtx));
return HITLS_SUCCESS;
}
void CCS_DeInit(TLS_Ctx *ctx)
{
if (ctx == NULL) {
return;
}
BSL_SAL_FREE(ctx->ccsCtx);
return;
}
int32_t ProcessPlainCCS(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen)
{
if (ctx->ccsCtx->isReady == false) {
#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask) &&
BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
ctx->rwstate = HITLS_READING;
return HITLS_REC_NORMAL_RECV_BUF_EMPTY;
}
#endif
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID15612,
"recv unexpected ccs msg", ALERT_UNEXPECTED_MESSAGE);
}
/** The read length is abnormal. */
if (dataLen != 1u) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID15613,
"ccs msg length err", ALERT_UNEXPECTED_MESSAGE);
}
/** Message exception. */
if (data[0] != 1u) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID15614,
"ccs msg err", ALERT_UNEXPECTED_MESSAGE);
}
/** Multiple generate ccs messages are received: If UDP transmission is used, ignore the ccs. */
if (ctx->ccsCtx->ccsRecvflag == true && !BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP) &&
HS_GetVersion(ctx) != HITLS_VERSION_TLS13) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID16277,
"Multiple generate ccs msg are received", ALERT_UNEXPECTED_MESSAGE);
}
if (ctx->ccsCtx->isAllowActiveCipher == true && ctx->ccsCtx->activeCipherFlag == false) {
/** Enable key specification */
if (REC_ActivePendingState(ctx, false) != HITLS_SUCCESS) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID16278,
"ActivePendingState err", ALERT_INTERNAL_ERROR);
}
ctx->ccsCtx->activeCipherFlag = true;
}
ctx->ccsCtx->ccsRecvflag = true;
#ifdef HITLS_TLS_FEATURE_INDICATOR
INDICATOR_MessageIndicate(0, HS_GetVersion(ctx), REC_TYPE_CHANGE_CIPHER_SPEC, data, 1,
ctx, ctx->config.tlsConfig.msgArg);
#endif
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15615, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"got a change cipher spec message.", 0, 0, 0, 0);
#ifdef HITLS_TLS_SUITE_CIPHER_CBC
ctx->negotiatedInfo.isEncryptThenMacRead = ctx->negotiatedInfo.isEncryptThenMac;
#endif
return HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
}
int32_t ProcessDecryptedCCS(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen)
{
#ifdef HITLS_TLS_PROTO_TLS13
if (HS_GetVersion(ctx) == HITLS_VERSION_TLS13) {
return RETURN_ALERT_PROCESS(ctx, HITLS_REC_NORMAL_RECV_UNEXPECT_MSG, BINLOG_ID15612,
"recv encrypted ccs msg", ALERT_UNEXPECTED_MESSAGE);
}
#endif
return ProcessPlainCCS(ctx, data, dataLen);
} | 2302_82127028/openHiTLS-examples_1508 | tls/ccs/src/change_cipher_spec.c | C | unknown | 9,066 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "securec.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "bsl_sal.h"
#include "bsl_bytes.h"
#include "bsl_list.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_security.h"
#include "tls.h"
#ifdef HITLS_TLS_FEATURE_SECURITY
#include "security.h"
#endif
#include "cert_mgr_ctx.h"
#include "cert_method.h"
#include "cert_mgr.h"
#include "cert.h"
#include "config_type.h"
#include "pack.h"
#include "custom_extensions.h"
#ifdef HITLS_TLS_FEATURE_SECURITY
static int32_t CheckKeySecbits(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, HITLS_CERT_Key *key)
{
int32_t ret;
int32_t secBits = 0;
HITLS_Config *config = &ctx->config.tlsConfig;
ret = SAL_CERT_KeyCtrl(config, key, CERT_KEY_CTRL_GET_SECBITS, NULL, (void *)&secBits);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16303, "GET_SECBITS fail");
}
ret = SECURITY_SslCheck((HITLS_Ctx *)ctx, HITLS_SECURITY_SECOP_EE_KEY, secBits, 0, cert);
if (ret != SECURITY_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16304, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"SslCheck fail, ret %d", ret, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_EE_KEY_WITH_INSECURE_SECBITS);
ctx->method.sendAlert((TLS_Ctx *)ctx, ALERT_LEVEL_FATAL, ALERT_INSUFFICIENT_SECURITY);
return HITLS_CERT_ERR_EE_KEY_WITH_INSECURE_SECBITS;
}
return HITLS_SUCCESS;
}
#endif
CERT_Type CertKeyType2CertType(HITLS_CERT_KeyType keyType)
{
switch (keyType) {
case TLS_CERT_KEY_TYPE_RSA:
case TLS_CERT_KEY_TYPE_RSA_PSS:
return CERT_TYPE_RSA_SIGN;
case TLS_CERT_KEY_TYPE_DSA:
return CERT_TYPE_DSS_SIGN;
case TLS_CERT_KEY_TYPE_SM2:
case TLS_CERT_KEY_TYPE_ECDSA:
case TLS_CERT_KEY_TYPE_ED25519:
return CERT_TYPE_ECDSA_SIGN;
default:
break;
}
return CERT_TYPE_UNKNOWN;
}
HITLS_CERT_KeyType SAL_CERT_SignScheme2CertKeyType(const HITLS_Ctx *ctx, HITLS_SignHashAlgo signScheme)
{
const TLS_SigSchemeInfo *info = ConfigGetSignatureSchemeInfo(&ctx->config.tlsConfig, signScheme);
if (info == NULL) {
return TLS_CERT_KEY_TYPE_UNKNOWN;
}
return info->keyType;
}
HITLS_SignHashAlgo SAL_CERT_GetDefaultSignHashAlgo(HITLS_CERT_KeyType keyType)
{
switch (keyType) {
case TLS_CERT_KEY_TYPE_RSA:
return CERT_SIG_SCHEME_RSA_PKCS1_SHA1;
case TLS_CERT_KEY_TYPE_RSA_PSS:
return CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256;
case TLS_CERT_KEY_TYPE_DSA:
return CERT_SIG_SCHEME_DSA_SHA1;
case TLS_CERT_KEY_TYPE_ECDSA:
return CERT_SIG_SCHEME_ECDSA_SHA1;
case TLS_CERT_KEY_TYPE_ED25519:
return CERT_SIG_SCHEME_ED25519;
#ifdef HITLS_TLS_PROTO_TLCP11
case TLS_CERT_KEY_TYPE_SM2:
return CERT_SIG_SCHEME_SM2_SM3;
#endif
default:
break;
}
return CERT_SIG_SCHEME_UNKNOWN;
}
int32_t CheckCertType(CERT_Type expectCertType, HITLS_CERT_KeyType checkedKeyType)
{
if (expectCertType == CERT_TYPE_UNKNOWN) {
/* The certificate type is not specified. This check is not required. */
return HITLS_SUCCESS;
}
/* Convert the key type to the certificate type. */
CERT_Type checkedCertType = CertKeyType2CertType(checkedKeyType);
if (expectCertType != checkedCertType) {
BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_UNSUPPORT_CERT);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15034, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"unexpect cert: expect cert type = %u, checked key type = %u.", expectCertType, checkedKeyType, 0, 0);
return HITLS_MSG_HANDLE_UNSUPPORT_CERT;
}
return HITLS_SUCCESS;
}
static bool IsSignSchemeExist(const uint16_t *signSchemeList, uint32_t signSchemeNum, HITLS_SignHashAlgo signScheme)
{
for (uint32_t i = 0; i < signSchemeNum; i++) {
if (signSchemeList[i] == signScheme) {
return true;
}
}
return false;
}
typedef struct {
uint32_t baseSignAlgorithmsSize;
const uint16_t *baseSignAlgorithms;
uint32_t selectSignAlgorithmsSize;
const uint16_t *selectSignAlgorithms;
} SelectSignAlgorithms;
static int32_t CheckSelectSignAlgorithms(TLS_Ctx *ctx, const SelectSignAlgorithms *select,
HITLS_CERT_KeyType checkedKeyType, HITLS_CERT_Key *pubkey, bool isNegotiateSignAlgo)
{
uint32_t baseSignAlgorithmsSize = select->baseSignAlgorithmsSize;
const uint16_t *baseSignAlgorithms = select->baseSignAlgorithms;
uint32_t selectSignAlgorithmsSize = select->selectSignAlgorithmsSize;
const uint16_t *selectSignAlgorithms = select->selectSignAlgorithms;
const TLS_SigSchemeInfo *info = NULL;
(void)pubkey;
#ifdef HITLS_TLS_PROTO_TLS13
int32_t paraId = 0;
(void)SAL_CERT_KeyCtrl(&ctx->config.tlsConfig, pubkey, CERT_KEY_CTRL_GET_PARAM_ID, NULL, (void *)¶Id);
#endif
for (uint32_t i = 0; i < baseSignAlgorithmsSize; i++) {
info = ConfigGetSignatureSchemeInfo(&ctx->config.tlsConfig, baseSignAlgorithms[i]);
if (info == NULL || info->keyType != (int32_t)checkedKeyType) {
continue;
}
#ifdef HITLS_TLS_PROTO_TLS13
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLS13 && info->paraId != 0 && info->paraId != paraId) {
continue;
}
#endif
if (!IsSignSchemeExist(selectSignAlgorithms, selectSignAlgorithmsSize, baseSignAlgorithms[i])) {
/* The signature algorithm must be the same as the algorithm configured on the peer end. */
continue;
}
#ifdef HITLS_TLS_FEATURE_SECURITY
if (SECURITY_SslCheck(ctx, HITLS_SECURITY_SECOP_SIGALG_CHECK, 0, baseSignAlgorithms[i],
NULL) != SECURITY_SUCCESS) {
continue;
}
#endif
if (!isNegotiateSignAlgo) {
/* Only the signature algorithm in the certificate is checked.
The signature algorithm in the handshake message is not negotiated. */
return HITLS_SUCCESS;
}
#ifdef HITLS_TLS_PROTO_TLS13
const uint32_t rsaPkcsv15Mask = 0x01;
const uint32_t dsaMask = 0x02;
const uint32_t sha1Mask = 0x0200;
const uint32_t sha224Mask = 0x0300;
/* rfc8446 4.2.3. Signature Algorithms */
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLS13) {
if (((baseSignAlgorithms[i] & 0xff) == rsaPkcsv15Mask) ||
((baseSignAlgorithms[i] & 0xff) == dsaMask) ||
((baseSignAlgorithms[i] & 0xff00) == sha1Mask) ||
((baseSignAlgorithms[i] & 0xff00) == sha224Mask)) {
/* not defined for use in signed TLS handshake messages in TLS1.3 */
continue;
}
}
#endif
ctx->negotiatedInfo.signScheme = baseSignAlgorithms[i];
return HITLS_SUCCESS;
}
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15981, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"unexpect cert: no available signature scheme, key type = %u.", checkedKeyType, 0, 0, 0);
return HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH;
}
static int32_t CheckSignScheme(TLS_Ctx *ctx, const uint16_t *signSchemeList, uint32_t signSchemeNum,
HITLS_CERT_KeyType checkedKeyType, HITLS_CERT_Key *pubkey, bool isNegotiateSignAlgo)
{
if (signSchemeList == NULL) {
if (!isNegotiateSignAlgo) {
/* Do not save the signature algorithm used for sending handshake messages. */
return HITLS_SUCCESS;
}
/* No signature algorithm is specified.
The default signature algorithm is used when handshake messages are sent. */
HITLS_SignHashAlgo signScheme = SAL_CERT_GetDefaultSignHashAlgo(checkedKeyType);
if (signScheme == CERT_SIG_SCHEME_UNKNOWN
#ifdef HITLS_TLS_FEATURE_SECURITY
|| SECURITY_SslCheck(ctx, HITLS_SECURITY_SECOP_SIGALG_CHECK, 0, signScheme, NULL) != SECURITY_SUCCESS
#endif
) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16074, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"unexpect key type: no available signature scheme, key type = %u.", checkedKeyType, 0, 0, 0);
return HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH;
}
ctx->negotiatedInfo.signScheme = signScheme;
return HITLS_SUCCESS;
}
SelectSignAlgorithms select = { 0 };
bool supportServer = ctx->config.tlsConfig.isSupportServerPreference;
select.baseSignAlgorithmsSize = supportServer ? ctx->config.tlsConfig.signAlgorithmsSize : signSchemeNum;
select.baseSignAlgorithms = supportServer ? ctx->config.tlsConfig.signAlgorithms : signSchemeList;
select.selectSignAlgorithmsSize = supportServer ? signSchemeNum : ctx->config.tlsConfig.signAlgorithmsSize;
select.selectSignAlgorithms = supportServer ? signSchemeList : ctx->config.tlsConfig.signAlgorithms;
return CheckSelectSignAlgorithms(ctx, &select, checkedKeyType, pubkey, isNegotiateSignAlgo);
}
int32_t CheckCurveName(HITLS_Config *config, const uint16_t *curveList, uint32_t curveNum, HITLS_CERT_Key *pubkey)
{
uint32_t curveName = HITLS_NAMED_GROUP_BUTT;
int32_t ret = SAL_CERT_KeyCtrl(config, pubkey, CERT_KEY_CTRL_GET_CURVE_NAME, NULL, (void *)&curveName);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15036, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"internal error: unable to get curve name.", 0, 0, 0, 0);
return ret;
}
for (uint32_t i = 0; i < curveNum; i++) {
if (curveName == curveList[i]) {
return HITLS_SUCCESS;
}
}
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_NO_CURVE_MATCH);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15037, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"unexpect cert: no curve match, which used %u.", curveName, 0, 0, 0);
return HITLS_CERT_ERR_NO_CURVE_MATCH;
}
int32_t CheckPointFormat(HITLS_Config *config, const uint8_t *ecPointFormatList, uint32_t listSize,
HITLS_CERT_Key *pubkey)
{
uint32_t ecPointFormat = HITLS_POINT_FORMAT_BUTT;
int32_t ret = SAL_CERT_KeyCtrl(config, pubkey, CERT_KEY_CTRL_GET_POINT_FORMAT, NULL, (void *)&ecPointFormat);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15038, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"internal error: unable to get point format.", 0, 0, 0, 0);
return ret;
}
for (uint32_t i = 0; i < listSize; i++) {
if (ecPointFormat == ecPointFormatList[i]) {
return HITLS_SUCCESS;
}
}
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_NO_POINT_FORMAT_MATCH);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15039, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"unexpect cert: no point format match, which used %u.", ecPointFormat, 0, 0, 0);
return HITLS_CERT_ERR_NO_POINT_FORMAT_MATCH;
}
int32_t IsEcParamCompatible(HITLS_Config *config, const CERT_ExpectInfo *info, HITLS_CERT_Key *pubkey)
{
int32_t ret;
/* If the client has used a Supported Elliptic Curves Extension, the public key in the server's certificate MUST
respect the client's choice of elliptic curves */
if (info->ellipticCurveNum != 0) {
ret = CheckCurveName(config, info->ellipticCurveList, info->ellipticCurveNum, pubkey);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
if (info->ecPointFormatNum != 0) {
ret = CheckPointFormat(config, info->ecPointFormatList, info->ecPointFormatNum, pubkey);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
return HITLS_SUCCESS;
}
static int32_t CheckCertTypeAndSignScheme(HITLS_Ctx *ctx, const CERT_ExpectInfo *expectCertInfo, HITLS_CERT_Key *pubkey,
bool isNegotiateSignAlgo, bool signCheck)
{
HITLS_Config *config = &ctx->config.tlsConfig;
uint32_t keyType = TLS_CERT_KEY_TYPE_UNKNOWN;
int32_t ret = SAL_CERT_KeyCtrl(config, pubkey, CERT_KEY_CTRL_GET_TYPE, NULL, (void *)&keyType);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15041, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"check certificate error: pubkey type unknown.", 0, 0, 0, 0);
return ret;
}
ret = CheckCertType(expectCertInfo->certType, keyType);
if (ret != HITLS_SUCCESS) {
return ret;
}
if (signCheck == true) {
ret = CheckSignScheme(ctx, expectCertInfo->signSchemeList, expectCertInfo->signSchemeNum,
keyType, pubkey, isNegotiateSignAlgo);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
/* ECDSA certificate. The curve ID and point format must be checked.
TLS_CERT_KEY_TYPE_SM2 does not check the curve ID and point format. TLCP curves is sm2 and is not compressed. */
if (keyType == TLS_CERT_KEY_TYPE_ECDSA && ctx->negotiatedInfo.version != HITLS_VERSION_TLS13) {
ret = IsEcParamCompatible(config, expectCertInfo, pubkey);
}
return ret;
}
int32_t SAL_CERT_CheckCertInfo(HITLS_Ctx *ctx, const CERT_ExpectInfo *expectCertInfo, HITLS_CERT_X509 *cert,
bool isNegotiateSignAlgo, bool signCheck)
{
HITLS_Config *config = &ctx->config.tlsConfig;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
HITLS_CERT_Key *pubkey = NULL;
int32_t ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_PUB_KEY, NULL, (void *)&pubkey);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID15040, "get pubkey fail");
}
do {
#ifdef HITLS_TLS_FEATURE_SECURITY
ret = CheckKeySecbits(ctx, cert, pubkey);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16307, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"CheckKeySecbits fail", 0, 0, 0, 0);
break;
}
#endif
ret = CheckCertTypeAndSignScheme(ctx, expectCertInfo, pubkey, isNegotiateSignAlgo, signCheck);
if (ret != HITLS_SUCCESS) {
break;
}
} while (false);
SAL_CERT_KeyFree(mgrCtx, pubkey);
return ret;
}
/*
* Server: Currently, two certificates are required for either of the two cipher suites supported.
* If the ECDHE cipher suite is used, the client needs to obtain the encrypted certificate to generate the premaster key
* and the signature certificate authenticates the identity.
* If the ECC cipher suite is used, the server public key is required to encrypt the premaster key
* and the signature certificate authentication is required.
* Client: Only the ECDHE cipher suite requires the client encryption certificate.
* In this case, the value of isNeedClientCert is true and may not be two-way authentication. (The specific value
* depends on the server configuration.)
* Therefore, the client does not verify any certificate and only sets the index.
* */
#ifdef HITLS_TLS_PROTO_TLCP11
static int32_t TlcpSelectCertByInfo(HITLS_Ctx *ctx, CERT_ExpectInfo *info)
{
int32_t encCertKeyType = TLS_CERT_KEY_TYPE_SM2;
CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)encCertKeyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_SELECT_CERTIFICATE, BINLOG_ID17336,
"The certificate required by TLCP is not loaded");
}
HITLS_CERT_X509 *cert = certPair->cert;
HITLS_CERT_X509 *encCert = certPair->encCert;
if (ctx->isClient == false || ctx->negotiatedInfo.cipherSuiteInfo.kxAlg == HITLS_KEY_EXCH_ECDHE) {
if (cert == NULL || encCert == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_SELECT_CERTIFICATE, BINLOG_ID15042,
"The certificate required by TLCP is not loaded");
}
ret = SAL_CERT_CheckCertInfo(ctx, info, cert, true, true);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16308, "CheckCertInfo fail");
}
ret = SAL_CERT_CheckCertInfo(ctx, info, encCert, true, false);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16309, "CheckCertInfo fail");
}
} else {
/* Check whether the certificate is missing when the client sends the certificate
or sends it to the server for processing. Check whether the authentication-related signature certificate
or derived encryption certificate exists when the client uses the certificate. */
if (cert != NULL) {
ret = SAL_CERT_CheckCertInfo(ctx, info, cert, true, true);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16310, "CheckCertInfo fail");
}
}
if (encCert != NULL) {
ret = SAL_CERT_CheckCertInfo(ctx, info, encCert, true, false);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16311, "CheckCertInfo fail");
}
}
}
mgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_SM2;
return HITLS_SUCCESS;
}
#endif
static int32_t SelectCertByInfo(HITLS_Ctx *ctx, CERT_ExpectInfo *info)
{
int32_t ret;
CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_UNREGISTERED_CALLBACK);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16312, "unregistered callback");
}
BSL_HASH_Hash *certPairs = mgrCtx->certPairs;
BSL_HASH_Iterator it = BSL_HASH_IterBegin(certPairs);
while (it != BSL_HASH_IterEnd(certPairs)) {
uint32_t keyType = (uint32_t)BSL_HASH_HashIterKey(certPairs, it);
CERT_Pair *certPair = (CERT_Pair *)BSL_HASH_IterValue(certPairs, it);
if (certPair == NULL || certPair->cert == NULL || certPair->privateKey == NULL) {
it = BSL_HASH_IterNext(certPairs, it);
continue;
}
ret = SAL_CERT_CheckCertInfo(ctx, info, certPair->cert, true, true);
if (ret != HITLS_SUCCESS) {
it = BSL_HASH_IterNext(certPairs, it);
continue;
}
/* Find a proper certificate and record the corresponding subscript. */
mgrCtx->currentCertKeyType = keyType;
return HITLS_SUCCESS;
}
return HITLS_CERT_ERR_SELECT_CERTIFICATE;
}
int32_t SAL_CERT_SelectCertByInfo(HITLS_Ctx *ctx, CERT_ExpectInfo *info)
{
int32_t ret = HITLS_SUCCESS;
CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
if (mgrCtx == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16313, "unregistered callback");
}
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLCP_DTLCP11) {
#ifdef HITLS_TLS_PROTO_TLCP11
ret = TlcpSelectCertByInfo(ctx, info);
#endif
} else {
ret = SelectCertByInfo(ctx, info);
}
if (ret == HITLS_SUCCESS) {
return ret;
}
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_SELECT_CERTIFICATE);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16151, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
"select certificate fail. ret %d", ret, 0, 0, 0);
mgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
return HITLS_CERT_ERR_SELECT_CERTIFICATE;
}
int32_t EncodeCertificate(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, PackPacket *pkt, uint32_t certIndex)
{
if (ctx == NULL || pkt == NULL || cert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16314, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
(void)certIndex;
int32_t ret;
HITLS_Config *config = &ctx->config.tlsConfig;
uint32_t certLen = 0;
ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_ENCODE_LEN, NULL, (void *)&certLen);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15043, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"encode certificate error: unable to get encode length.", 0, 0, 0, 0);
return ret;
}
if (certLen == 0) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_ENCODE_CERT);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15044, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"cert encode len is 0", 0, 0, 0, 0);
return HITLS_CERT_ERR_ENCODE_CERT;
}
/* Write the length of the certificate data (3 bytes). */
ret = PackAppendUint24ToBuf(pkt, certLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
/* Reserve space for certificate data and encode directly */
uint8_t *certBuf = NULL;
ret = PackReserveBytes(pkt, certLen, &certBuf);
if (ret != HITLS_SUCCESS) {
return ret;
}
uint32_t usedLen = 0;
/* Write the certificate data using the low-level encoding function */
ret = SAL_CERT_X509Encode(ctx, cert, certBuf, certLen, &usedLen);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16315, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "X509Encode err", 0, 0, 0, 0);
return ret;
}
ret = PackSkipBytes(pkt, usedLen);
if (ret != HITLS_SUCCESS) {
return ret;
}
#ifdef HITLS_TLS_PROTO_TLS13
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLS13) {
/* If an extension applies to the entire chain, it SHOULD be included in the first CertificateEntry. */
/* Start length field for extensions */
uint32_t exLenPos = 0;
ret = PackStartLengthField(pkt, sizeof(uint16_t), &exLenPos);
if (ret != HITLS_SUCCESS) {
return ret;
}
#ifdef HITLS_TLS_FEATURE_CUSTOM_EXTENSION
if (IsPackNeedCustomExtensions(CUSTOM_EXT_FROM_CTX(ctx), HITLS_EX_TYPE_TLS1_3_CERTIFICATE)) {
ret = PackCustomExtensions(ctx, pkt, HITLS_EX_TYPE_TLS1_3_CERTIFICATE, cert, certIndex);
if (ret != HITLS_SUCCESS) {
return ret;
}
}
#endif /* HITLS_TLS_FEATURE_CUSTOM_EXTENSION */
/* Close extension length field */
PackCloseUint16Field(pkt, exLenPos);
}
#endif
return HITLS_SUCCESS;
}
void FreeCertList(HITLS_CERT_X509 **certList, uint32_t certNum)
{
if (certList == NULL) {
return;
}
for (uint32_t i = 0; i < certNum; i++) {
SAL_CERT_X509Free(certList[i]);
}
}
static int32_t EncodeEECert(HITLS_Ctx *ctx, PackPacket *pkt, HITLS_CERT_X509 **cert)
{
CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
CERT_Pair *currentCertPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)mgrCtx->currentCertKeyType, (uintptr_t *)¤tCertPair);
if (ret != HITLS_SUCCESS || currentCertPair == NULL || currentCertPair->cert == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_EXP_CERT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_EXP_CERT, BINLOG_ID16152, "first cert is null");
}
HITLS_CERT_X509 *tmpCert = currentCertPair->cert;
#ifdef HITLS_TLS_FEATURE_SECURITY
HITLS_CERT_Key *key = currentCertPair->privateKey;
ret = CheckKeySecbits(ctx, tmpCert, key);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16317, "check key fail");
}
#endif
/* Write the first device certificate. */
ret = EncodeCertificate(ctx, tmpCert, pkt, 0);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16153, "encode fail");
}
#ifdef HITLS_TLS_PROTO_TLCP11
/* If the TLCP algorithm is used and the encryption certificate is required, write the
second encryption certificate. */
HITLS_CERT_X509 *certEnc = currentCertPair->encCert;
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLCP_DTLCP11 && certEnc != NULL) {
#ifdef HITLS_TLS_FEATURE_SECURITY
HITLS_CERT_Key *keyEnc = currentCertPair->encPrivateKey;
ret = CheckKeySecbits(ctx, certEnc, keyEnc);
if (ret != HITLS_SUCCESS) {
return ret;
}
#endif
ret = EncodeCertificate(ctx, certEnc, pkt, 1);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16154, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"TLCP encode device certificate error.", 0, 0, 0, 0);
return ret;
}
}
#endif
*cert = tmpCert;
return HITLS_SUCCESS;
}
#ifdef HITLS_TLS_FEATURE_SECURITY
static int32_t CheckCertChainFromStore(HITLS_Config *config, HITLS_CERT_X509 *cert)
{
HITLS_CERT_Key *pubkey = NULL;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
int32_t ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_PUB_KEY, NULL, (void *)&pubkey);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CFG_ERR_LOAD_CERT_FILE, BINLOG_ID16318, "GET_PUB_KEY fail");
}
int32_t secBits = 0;
ret = SAL_CERT_KeyCtrl(config, pubkey, CERT_KEY_CTRL_GET_SECBITS, NULL, (void *)&secBits);
SAL_CERT_KeyFree(mgrCtx, pubkey);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16319, "GET_SECBITS fail");
}
ret = SECURITY_CfgCheck(config, HITLS_SECURITY_SECOP_CA_KEY, secBits, 0, cert); // cert key
if (ret != SECURITY_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16320, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"CfgCheck fail, ret %d", ret, 0, 0, 0);
return HITLS_CERT_ERR_CA_KEY_WITH_INSECURE_SECBITS;
}
int32_t signAlg = 0;
ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_SIGN_ALGO, NULL, (void *)&signAlg);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16321, "GET_SIGN_ALGO fail");
}
ret = SECURITY_CfgCheck(config, HITLS_SECURITY_SECOP_SIGALG_CHECK, 0, signAlg, NULL);
if (ret != SECURITY_SUCCESS) {
return HITLS_CERT_ERR_INSECURE_SIG_ALG ;
}
return HITLS_SUCCESS;
}
#endif
static int32_t EncodeCertificateChain(HITLS_Ctx *ctx, PackPacket *pkt)
{
HITLS_CERT_X509 *tempCert = NULL;
HITLS_Config *config = &ctx->config.tlsConfig;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
CERT_Pair *currentCertPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)mgrCtx->currentCertKeyType, (uintptr_t *)¤tCertPair);
if (ret != HITLS_SUCCESS || currentCertPair == NULL) {
return HITLS_SUCCESS;
}
HITLS_CERT_Chain *chain = NULL;
if (BSL_LIST_COUNT(currentCertPair->chain) > 0) {
chain = currentCertPair->chain;
} else {
chain = mgrCtx->extraChain;
}
tempCert = (HITLS_CERT_X509 *)BSL_LIST_GET_FIRST(chain);
uint32_t certIndex = 1;
while (tempCert != NULL) {
ret = EncodeCertificate(ctx, tempCert, pkt, certIndex);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID15048, "encode cert chain err");
}
certIndex++;
tempCert = BSL_LIST_GET_NEXT(chain);
}
return HITLS_SUCCESS;
}
static int32_t EncodeCertStore(HITLS_Ctx *ctx, PackPacket *pkt, HITLS_CERT_X509 *cert)
{
HITLS_Config *config = &ctx->config.tlsConfig;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
HITLS_CERT_Store *store = (mgrCtx->chainStore != NULL) ? mgrCtx->chainStore : mgrCtx->certStore;
HITLS_CERT_X509 *certList[TLS_DEFAULT_VERIFY_DEPTH] = {0};
uint32_t certNum = TLS_DEFAULT_VERIFY_DEPTH;
if (store != NULL) {
int32_t ret = SAL_CERT_BuildChain(config, store, cert, certList, &certNum);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16322, "BuildChain fail");
}
/* The first device certificate has been written. The certificate starts from the second one. */
for (uint32_t i = 1; i < certNum; i++) {
#ifdef HITLS_TLS_FEATURE_SECURITY
ret = CheckCertChainFromStore(config, certList[i]);
if (ret != HITLS_SUCCESS) {
FreeCertList(certList, certNum);
return ret;
}
#endif
ret = EncodeCertificate(ctx, certList[i], pkt, i);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16155, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"encode cert chain error in No.%u.", i, 0, 0, 0);
FreeCertList(certList, certNum);
return ret;
}
}
}
FreeCertList(certList, certNum);
return HITLS_SUCCESS;
}
/*
* The constructed certificate chain is incomplete (excluding the root certificate).
* Therefore, in the buildCertChain callback, the return value is ignored, even if the error returned by this call.
* In fact, certificates are not verified but chains are constructed as many as possible.
* So do not need to invoke buildCertChain if the certificate is encrypted using the TLCP.
* If the TLCP is used, the server has checked that the two certificates are not empty.
* The client does not check, the message is sent based on the configuration.
* If the message will be sent, the signature certificate must exist.
* */
int32_t SAL_CERT_EncodeCertChain(HITLS_Ctx *ctx, PackPacket *pkt)
{
if (ctx == NULL || pkt == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16323, "input null");
}
HITLS_CERT_X509 *cert = NULL;
HITLS_Config *config = &ctx->config.tlsConfig;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
if (mgrCtx == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16324, "unregistered callback");
}
CERT_Pair *currentCertPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)mgrCtx->currentCertKeyType, (uintptr_t *)¤tCertPair);
if (ret != HITLS_SUCCESS || currentCertPair == NULL) {
/* No certificate needs to be sent at the local end. */
return HITLS_SUCCESS;
}
ret = EncodeEECert(ctx, pkt, &cert);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID15046, "encode device cert err");
}
// Check the size. If a certificate exists in the chain, directly put the data in the chain into the buf and return.
if (BSL_LIST_COUNT(currentCertPair->chain) > 0 || BSL_LIST_COUNT(mgrCtx->extraChain) > 0) {
return EncodeCertificateChain(ctx, pkt);
}
return EncodeCertStore(ctx, pkt, cert);
}
#ifdef HITLS_TLS_PROTO_TLS13
// rfc8446 4.4.2.4. Receiving a Certificate Message
// Any endpoint receiving any certificate which it would need to validate using any signature algorithm using an MD5
// hash MUST abort the handshake with a "bad_certificate" alert.
// Currently, the MD5 signature algorithm is not available, but it is still an unknown one.
int32_t CheckCertSignature(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert)
{
HITLS_Config *config = &ctx->config.tlsConfig;
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLS13) {
HITLS_SignHashAlgo signAlg = CERT_SIG_SCHEME_UNKNOWN;
const uint32_t md5Mask = 0x0100;
(void)SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_SIGN_ALGO, NULL, (void *)&signAlg);
if ((signAlg == CERT_SIG_SCHEME_UNKNOWN) || (((uint32_t)signAlg & 0xff00) == md5Mask)) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_CTRL_ERR_GET_SIGN_ALGO, BINLOG_ID16325, "signAlg unknow");
}
}
return HITLS_SUCCESS;
}
#endif
static void DestoryParseChain(HITLS_CERT_X509 *encCert, HITLS_CERT_X509 *cert, HITLS_CERT_Chain *newChain)
{
SAL_CERT_X509Free(encCert);
SAL_CERT_X509Free(cert);
SAL_CERT_ChainFree(newChain);
}
#ifdef HITLS_TLS_PROTO_TLCP11
static bool TlcpCheckSignCertKeyUsage(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert)
{
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLCP_DTLCP11) {
return SAL_CERT_CheckCertKeyUsage(ctx, cert, CERT_KEY_CTRL_IS_DIGITAL_SIGN_USAGE) ||
SAL_CERT_CheckCertKeyUsage(ctx, cert, CERT_KEY_CTRL_IS_NON_REPUDIATION_USAGE);
}
return true;
}
static bool TlcpCheckEncCertKeyUsage(HITLS_Ctx *ctx, HITLS_CERT_X509 *encCert)
{
if (ctx->negotiatedInfo.version == HITLS_VERSION_TLCP_DTLCP11) {
return SAL_CERT_CheckCertKeyUsage(ctx, encCert, CERT_KEY_CTRL_IS_KEYENC_USAGE) ||
SAL_CERT_CheckCertKeyUsage(ctx, encCert, CERT_KEY_CTRL_IS_DATA_ENC_USAGE) ||
SAL_CERT_CheckCertKeyUsage(ctx, encCert, CERT_KEY_CTRL_IS_KEY_AGREEMENT_USAGE);
}
return false;
}
#endif
int32_t ParseChain(HITLS_Ctx *ctx, CERT_Item *item, HITLS_CERT_Chain **chain, HITLS_CERT_X509 **encCert)
{
if (ctx == NULL || chain == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16326, "input null");
}
HITLS_CERT_X509 *encCertLocal = NULL;
HITLS_Config *config = &ctx->config.tlsConfig;
HITLS_CERT_Chain *newChain = SAL_CERT_ChainNew();
if (newChain == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID15049, "ChainNew fail");
}
CERT_Item *listNode = item;
while (listNode != NULL) {
HITLS_CERT_X509 *cert = SAL_CERT_X509Parse(LIBCTX_FROM_CONFIG(config),
ATTRIBUTE_FROM_CONFIG(config), config, listNode->data, listNode->dataSize,
TLS_PARSE_TYPE_BUFF, TLS_PARSE_FORMAT_ASN1);
if (cert == NULL) {
DestoryParseChain(encCertLocal, NULL, newChain);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_PARSE_MSG, BINLOG_ID15050, "parse cert chain err");
}
#ifdef HITLS_TLS_PROTO_TLS13
if (CheckCertSignature(ctx, cert) != HITLS_SUCCESS) {
DestoryParseChain(encCertLocal, cert, newChain);
return HITLS_CERT_CTRL_ERR_GET_SIGN_ALGO;
}
#endif
#ifdef HITLS_TLS_PROTO_TLCP11
if ((encCert != NULL) && (TlcpCheckEncCertKeyUsage(ctx, cert) == true)) {
SAL_CERT_X509Free(encCertLocal);
encCertLocal = cert;
listNode = listNode->next;
continue;
}
#endif
/* Add a certificate to the certificate chain. */
if (SAL_CERT_ChainAppend(newChain, cert) != HITLS_SUCCESS) {
DestoryParseChain(encCertLocal, cert, newChain);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID15051, "ChainAppend fail");
}
listNode = listNode->next;
}
if (encCert != NULL) {
*encCert = encCertLocal;
}
*chain = newChain;
return HITLS_SUCCESS;
}
int32_t SAL_CERT_ParseCertChain(HITLS_Ctx *ctx, CERT_Item *item, CERT_Pair **certPair)
{
if (ctx == NULL || item == NULL || certPair == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16327, "input null");
}
HITLS_CERT_X509 *encCert = NULL;
HITLS_Config *config = &ctx->config.tlsConfig;
if (config->certMgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_UNREGISTERED_CALLBACK);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16328, "unregistered callback");
}
/* Parse the first device certificate. */
HITLS_CERT_X509 *cert = SAL_CERT_X509Parse(LIBCTX_FROM_CONFIG(config),
ATTRIBUTE_FROM_CONFIG(config), config, item->data, item->dataSize,
TLS_PARSE_TYPE_BUFF, TLS_PARSE_FORMAT_ASN1);
if (cert == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_PARSE_MSG, BINLOG_ID15052, "X509Parse fail");
}
#ifdef HITLS_TLS_PROTO_TLS13
if (CheckCertSignature(ctx, cert) != HITLS_SUCCESS) {
SAL_CERT_X509Free(cert);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_CTRL_ERR_GET_SIGN_ALGO, BINLOG_ID16329, "check signature fail");
}
#endif
#ifdef HITLS_TLS_PROTO_TLCP11
if (!TlcpCheckSignCertKeyUsage(ctx, cert)) {
SAL_CERT_X509Free(cert);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_KEYUSAGE, BINLOG_ID15341, "check sign cert keyusage fail");
}
#endif
/* Parse other certificates in the certificate chain. */
HITLS_CERT_Chain *chain = NULL;
HITLS_CERT_X509 **inParseEnc = ctx->negotiatedInfo.version == HITLS_VERSION_TLCP_DTLCP11 ? &encCert : NULL;
int32_t ret = ParseChain(ctx, item->next, &chain, inParseEnc);
if (ret != HITLS_SUCCESS) {
SAL_CERT_X509Free(cert);
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16330, "ParseChain fail");
}
CERT_Pair *newCertPair = BSL_SAL_Calloc(1u, sizeof(CERT_Pair));
if (newCertPair == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
SAL_CERT_X509Free(cert);
SAL_CERT_X509Free(encCert);
SAL_CERT_ChainFree(chain);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID15053, "Calloc fail");
}
newCertPair->cert = cert;
#ifdef HITLS_TLS_PROTO_TLCP11
newCertPair->encCert = encCert;
#endif
newCertPair->chain = chain;
*certPair = newCertPair;
return HITLS_SUCCESS;
}
int32_t SAL_CERT_VerifyCertChain(HITLS_Ctx *ctx, CERT_Pair *certPair, bool isTlcpEncCert)
{
(void)isTlcpEncCert;
if (ctx == NULL || certPair == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16331, "input null");
}
int32_t ret;
uint32_t i = 0;
HITLS_Config *config = &ctx->config.tlsConfig;
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_UNREGISTERED_CALLBACK);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16332, "mgrCtx null");
}
HITLS_CERT_Chain *chain = certPair->chain;
/* Obtain the number of certificates. The first device certificate must also be included. */
uint32_t certNum = (uint32_t)(BSL_LIST_COUNT(chain) + 1);
HITLS_CERT_X509 **certList = (HITLS_CERT_X509 **)BSL_SAL_Calloc(1u, sizeof(HITLS_CERT_X509 *) * certNum);
if (certList == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID15054, "Calloc fail");
}
certList[i++] =
#ifdef HITLS_TLS_PROTO_TLCP11
isTlcpEncCert ? certPair->encCert :
#endif
certPair->cert;
HITLS_CERT_X509 *currCert = NULL;
for (uint32_t index = 0u; index < (certNum - 1); ++index) {
currCert = (HITLS_CERT_X509 *)BSL_LIST_GetIndexNode(index, chain);
certList[i++] = currCert;
}
/* Verify the certificate chain. */
HITLS_CERT_Store *store = (mgrCtx->verifyStore != NULL) ? mgrCtx->verifyStore : mgrCtx->certStore;
if (store == NULL) {
BSL_SAL_FREE(certList);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_VERIFY_CERT_CHAIN, BINLOG_ID16333, "Calloc fail");
}
ret = SAL_CERT_VerifyChain(ctx, store, certList, i);
BSL_SAL_FREE(certList);
return ret;
}
uint32_t SAL_CERT_GetSignMaxLen(HITLS_Config *config, HITLS_CERT_Key *key)
{
uint32_t len = 0;
int32_t ret = SAL_CERT_KeyCtrl(config, key, CERT_KEY_CTRL_GET_SIGN_LEN, NULL, &len);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15056, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"get signature length error: callback ret = 0x%x.", ret, 0, 0, 0);
return 0;
}
return len;
}
#ifdef HITLS_TLS_CONFIG_CERT_CALLBACK
int32_t HITLS_CFG_SetCheckPriKeyCb(HITLS_Config *config, CERT_CheckPrivateKeyCallBack checkPrivateKey)
{
if (config == NULL || config->certMgrCtx == NULL || checkPrivateKey == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
#ifndef HITLS_TLS_FEATURE_PROVIDER
config->certMgrCtx->method.checkPrivateKey = checkPrivateKey;
#endif
return HITLS_SUCCESS;
}
CERT_CheckPrivateKeyCallBack HITLS_CFG_GetCheckPriKeyCb(HITLS_Config *config)
{
if (config == NULL || config->certMgrCtx == NULL) {
return NULL;
}
#ifndef HITLS_TLS_FEATURE_PROVIDER
return config->certMgrCtx->method.checkPrivateKey;
#else
return NULL;
#endif
}
#endif /* HITLS_TLS_CONFIG_CERT_CALLBACK */
#ifdef HITLS_TLS_PROTO_TLCP11
static uint8_t *EncodeEncCert(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, uint32_t *useLen)
{
if (ctx == NULL || cert == NULL || useLen == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16336, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return NULL;
}
uint32_t certLen;
HITLS_Config *config = &ctx->config.tlsConfig;
int32_t ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_ENCODE_LEN, NULL, (void *)&certLen);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16157, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"encode gm enc certificate error: unable to get encode length.", 0, 0, 0, 0);
return NULL;
}
uint8_t *data = BSL_SAL_Calloc(1u, certLen);
if (data == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16158, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"signature data memory alloc fail.", 0, 0, 0, 0);
return NULL;
}
ret = SAL_CERT_X509Encode(ctx, cert, data, certLen, useLen);
if (ret != HITLS_SUCCESS) {
BSL_SAL_FREE(data);
BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16232, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"encode cert error: callback ret = 0x%x.", (uint32_t)ret, 0, 0, 0);
return NULL;
}
return data;
}
uint8_t *SAL_CERT_SrvrGmEncodeEncCert(HITLS_Ctx *ctx, uint32_t *useLen)
{
if (ctx == NULL || useLen == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16337, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return NULL;
}
int keyType = TLS_CERT_KEY_TYPE_SM2;
CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
CERT_Pair *currentCertPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)¤tCertPair);
if (ret != HITLS_SUCCESS || currentCertPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17337, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "encCert null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return NULL;
}
HITLS_CERT_X509 *cert = currentCertPair->encCert;
return EncodeEncCert(ctx, cert, useLen);
}
uint8_t *SAL_CERT_ClntGmEncodeEncCert(HITLS_Ctx *ctx, CERT_Pair *peerCert, uint32_t *useLen)
{
return EncodeEncCert(ctx, peerCert->encCert, useLen);
}
#endif
#if defined(HITLS_TLS_PROTO_TLCP11) || defined(HITLS_TLS_CONFIG_KEY_USAGE)
bool SAL_CERT_CheckCertKeyUsage(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, HITLS_CERT_CtrlCmd keyusage)
{
if (ctx == NULL || cert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16338, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
uint8_t isUsage = false;
if (keyusage != CERT_KEY_CTRL_IS_KEYENC_USAGE && keyusage != CERT_KEY_CTRL_IS_DIGITAL_SIGN_USAGE &&
keyusage != CERT_KEY_CTRL_IS_KEY_CERT_SIGN_USAGE && keyusage != CERT_KEY_CTRL_IS_KEY_AGREEMENT_USAGE &&
keyusage != CERT_KEY_CTRL_IS_DATA_ENC_USAGE && keyusage != CERT_KEY_CTRL_IS_NON_REPUDIATION_USAGE) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16339, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "keyusage err", 0, 0, 0, 0);
return (bool)isUsage;
}
HITLS_Config *config = &ctx->config.tlsConfig;
if (SAL_CERT_X509Ctrl(config, cert, keyusage, NULL, (void *)&isUsage) != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16340, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "%d fail", keyusage, 0, 0, 0);
return false;
}
return (bool)isUsage;
}
#endif | 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert.c | C | unknown | 44,429 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdint.h>
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "bsl_sal.h"
#include "bsl_list.h"
#include "hitls_error.h"
#include "cert_method.h"
#include "cert_mgr_ctx.h"
HITLS_CERT_Chain *SAL_CERT_ChainNew(void)
{
BslList *newChain = BSL_LIST_New(sizeof(HITLS_CERT_X509 *));
if (newChain == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15010, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"new cert chain error: out of memory.", 0, 0, 0, 0);
}
return newChain;
}
int32_t SAL_CERT_ChainAppend(HITLS_CERT_Chain *chain, HITLS_CERT_X509 *cert)
{
/* add the tail to the end of the certificate chain, corresponding to the top of the stack */
int32_t ret = BSL_LIST_AddElement(chain, cert, BSL_LIST_POS_END);
if (ret != BSL_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15011, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"append cert to chain error: out of memory.", 0, 0, 0, 0);
return HITLS_MEMALLOC_FAIL;
}
return HITLS_SUCCESS;
}
static void CertChainInnerDestroyCb(void *cert)
{
SAL_CERT_X509Free((HITLS_CERT_X509 *)cert);
}
/* release the linked list without retaining the head node */
void SAL_CERT_ChainFree(HITLS_CERT_Chain *chain)
{
/* only certificates on the chain are destroyed, chain itself will be not destroyed */
BSL_LIST_DeleteAll(chain, CertChainInnerDestroyCb);
BSL_SAL_FREE(chain);
return;
}
/* copy the certificate chain */
HITLS_CERT_Chain *SAL_CERT_ChainDup(CERT_MgrCtx *mgrCtx, HITLS_CERT_Chain *chain)
{
int32_t ret;
uint32_t listSize = (uint32_t)BSL_LIST_COUNT(chain);
HITLS_CERT_X509 *dupCert = NULL;
HITLS_CERT_X509 *currCert = NULL;
if (BSL_LIST_COUNT(chain) < 0) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16070, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert chain error: list size tainted.", 0, 0, 0, 0);
return NULL;
}
BslList *newChain = SAL_CERT_ChainNew();
if (newChain == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15012, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert chain error: out of memory.", 0, 0, 0, 0);
return NULL;
}
for (uint32_t index = 0u; index < listSize; ++index) {
currCert = (HITLS_CERT_X509 *)BSL_LIST_GetIndexNode(index, chain);
if (currCert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15002, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert error: currCert NULL.", 0, 0, 0, 0);
goto EXIT;
}
dupCert = SAL_CERT_X509Dup(mgrCtx, currCert);
if (dupCert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15013, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert chain error: x509 dup error.", 0, 0, 0, 0);
goto EXIT;
}
ret = SAL_CERT_ChainAppend(newChain, dupCert);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15014, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert chain error: append new cert node error.", 0, 0, 0, 0);
SAL_CERT_X509Free(dupCert);
goto EXIT;
}
}
return newChain;
EXIT:
/* free the certificate chain */
SAL_CERT_ChainFree(newChain);
return NULL;
} | 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_chain.c | C | unknown | 3,956 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stddef.h>
#include "hitls_build.h"
#include "securec.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "hitls_x509_adapt.h"
#ifdef HITLS_TLS_FEATURE_PROVIDER
#include "hitls_pki_x509.h"
#endif /* HITLS_TLS_FEATURE_PROVIDER */
#include "tls_config.h"
#include "tls.h"
#include "cert_mgr_ctx.h"
#include "cert_method.h"
#ifndef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_MgrMethod g_certMgrMethod = {0};
static bool IsMethodValid(const HITLS_CERT_MgrMethod *method)
{
bool valid = method == NULL ||
method->certStoreNew == NULL ||
method->certStoreDup == NULL ||
method->certStoreFree == NULL ||
method->certStoreCtrl == NULL ||
method->buildCertChain == NULL ||
method->verifyCertChain == NULL ||
method->certEncode == NULL ||
method->certParse == NULL ||
method->certDup == NULL ||
method->certFree == NULL ||
method->certCtrl == NULL ||
method->keyParse == NULL ||
method->keyDup == NULL ||
method->keyFree == NULL ||
method->keyCtrl == NULL ||
method->createSign == NULL ||
method->verifySign == NULL ||
method->checkPrivateKey == NULL;
if (valid) {
return false;
}
return true;
}
int32_t HITLS_CERT_RegisterMgrMethod(HITLS_CERT_MgrMethod *method)
{
/* check the callbacks that must be set */
if (IsMethodValid(method) == false) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16108, "input NULL");
}
if (memcpy_s(&g_certMgrMethod, sizeof(HITLS_CERT_MgrMethod), method, sizeof(HITLS_CERT_MgrMethod)) != EOK) {
return HITLS_MEMCPY_FAIL;
}
return HITLS_SUCCESS;
}
void HITLS_CERT_DeinitMgrMethod(void)
{
HITLS_CERT_MgrMethod mgr = {0};
(void)memcpy_s(&g_certMgrMethod, sizeof(HITLS_CERT_MgrMethod), &mgr, sizeof(HITLS_CERT_MgrMethod));
}
HITLS_CERT_MgrMethod *SAL_CERT_GetMgrMethod(void)
{
return &g_certMgrMethod;
}
HITLS_CERT_MgrMethod *HITLS_CERT_GetMgrMethod(void)
{
return SAL_CERT_GetMgrMethod();
}
#endif /* HITLS_TLS_FEATURE_PROVIDER */
int32_t CheckCertCallBackRetVal(const char *logStr, int32_t callBackRet, uint32_t bingLogId, uint32_t hitlsRet)
{
if (callBackRet != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(bingLogId, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"%s error: callback ret = 0x%x.", logStr, callBackRet, 0, 0);
BSL_ERR_PUSH_ERROR((int32_t)hitlsRet);
return (int32_t)hitlsRet;
}
return HITLS_SUCCESS;
}
HITLS_CERT_Store *SAL_CERT_StoreNew(const CERT_MgrCtx *mgrCtx)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
return HITLS_X509_ProviderStoreCtxNew(LIBCTX_FROM_CERT_MGR_CTX(mgrCtx), ATTRIBUTE_FROM_CERT_MGR_CTX(mgrCtx));
#else
return mgrCtx->method.certStoreNew();
#endif
}
HITLS_CERT_Store *SAL_CERT_StoreDup(const CERT_MgrCtx *mgrCtx, HITLS_CERT_Store *store)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
return HITLS_X509_Adapt_StoreDup(store);
#else
return mgrCtx->method.certStoreDup(store);
#endif
}
void SAL_CERT_StoreFree(const CERT_MgrCtx *mgrCtx, HITLS_CERT_Store *store)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
return HITLS_X509_StoreCtxFree(store);
#else
mgrCtx->method.certStoreFree(store);
#endif
}
int32_t SAL_CERT_BuildChain(HITLS_Config *config, HITLS_CERT_Store *store, HITLS_CERT_X509 *cert,
HITLS_CERT_X509 **certList, uint32_t *num)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_BuildCertChain(config, store, cert, certList, num);
#else
ret = config->certMgrCtx->method.buildCertChain(config, store, cert, certList, num);
#endif
return CheckCertCallBackRetVal("cert store build chain by cert", ret, BINLOG_ID16083, HITLS_CERT_ERR_BUILD_CHAIN);
}
int32_t SAL_CERT_VerifyChain(HITLS_Ctx *ctx, HITLS_CERT_Store *store, HITLS_CERT_X509 **certList, uint32_t num)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_VerifyCertChain(ctx, store, certList, num);
#else
ret = ctx->config.tlsConfig.certMgrCtx->method.verifyCertChain(ctx, store, certList, num);
#endif
return CheckCertCallBackRetVal("cert store verify chain", ret, BINLOG_ID16084, HITLS_CERT_ERR_VERIFY_CERT_CHAIN);
}
int32_t SAL_CERT_X509Encode(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, uint8_t *buf, uint32_t len, uint32_t *usedLen)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_CertEncode(ctx, cert, buf, len, usedLen);
#else
ret = ctx->config.tlsConfig.certMgrCtx->method.certEncode(ctx, cert, buf, len, usedLen);
#endif
return CheckCertCallBackRetVal("encode cert", ret, BINLOG_ID16086, HITLS_CERT_ERR_ENCODE_CERT);
}
HITLS_CERT_Chain *SAL_CERT_X509ParseBundleFile(HITLS_Lib_Ctx *libCtx, const char *attrName,
HITLS_Config *config, const uint8_t *buf, uint32_t len,
HITLS_ParseType type, HITLS_ParseFormat format)
{
(void)config;
return HITLS_X509_Adapt_BundleCertParse(libCtx, attrName, buf, len, type, SAL_CERT_GetParseFormatStr(format));
}
HITLS_CERT_X509 *SAL_CERT_X509Parse(HITLS_Lib_Ctx *libCtx, const char *attrName,
HITLS_Config *config, const uint8_t *buf, uint32_t len,
HITLS_ParseType type, HITLS_ParseFormat format)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)config;
return HITLS_CERT_ProviderCertParse(libCtx, attrName, buf, len, type, SAL_CERT_GetParseFormatStr(format));
#else
(void)libCtx;
(void)attrName;
return config->certMgrCtx->method.certParse(config, buf, len, type, format);
#endif
}
HITLS_CERT_X509 *SAL_CERT_X509Dup(const CERT_MgrCtx *mgrCtx, HITLS_CERT_X509 *cert)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
return (HITLS_CERT_X509 *)HITLS_X509_CertDup(cert);
#else
return mgrCtx->method.certDup(cert);
#endif
}
void SAL_CERT_X509Free(HITLS_CERT_X509 *cert)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
HITLS_X509_CertFree(cert);
#else
if (cert == NULL) {
return;
}
g_certMgrMethod.certFree(cert);
#endif
}
HITLS_CERT_X509 *SAL_CERT_X509Ref(const CERT_MgrCtx *mgrCtx, HITLS_CERT_X509 *cert)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
return HITLS_X509_Adapt_CertRef(cert);
#else
if (mgrCtx->method.certRef == NULL) {
return NULL;
}
return mgrCtx->method.certRef(cert);
#endif
}
typedef struct {
const char *name;
HITLS_ParseFormat format;
} ParseFormatMap;
static const ParseFormatMap g_parseFormatMap[] = {
{"PEM", TLS_PARSE_FORMAT_PEM},
{"ASN1", TLS_PARSE_FORMAT_ASN1},
{"PFX_COM", TLS_PARSE_FORMAT_PFX_COM},
{"PKCS12", TLS_PARSE_FORMAT_PKCS12}
};
const char *SAL_CERT_GetParseFormatStr(HITLS_ParseFormat format)
{
for (size_t i = 0; i < sizeof(g_parseFormatMap) / sizeof(g_parseFormatMap[0]); i++) {
if (g_parseFormatMap[i].format == format) {
return g_parseFormatMap[i].name;
}
}
return NULL;
}
#ifndef HITLS_TLS_FEATURE_PROVIDER
static HITLS_ParseFormat GetTlsParseFormat(const char *format)
{
if (format == NULL) {
return TLS_PARSE_FORMAT_BUTT;
}
for (size_t i = 0; i < sizeof(g_parseFormatMap) / sizeof(g_parseFormatMap[0]); i++) {
if (BSL_SAL_StrcaseCmp(format, g_parseFormatMap[i].name) == 0) {
return g_parseFormatMap[i].format;
}
}
return TLS_PARSE_FORMAT_BUTT;
}
#endif
HITLS_CERT_Key *SAL_CERT_KeyParse(HITLS_Config *config, const uint8_t *buf, uint32_t len,
HITLS_ParseType type, const char *format, const char *encodeType)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
return HITLS_X509_Adapt_ProviderKeyParse(config, buf, len, type, format, encodeType);
#else
(void)encodeType;
return config->certMgrCtx->method.keyParse(config, buf, len, type, GetTlsParseFormat(format));
#endif
}
HITLS_CERT_Key *SAL_CERT_KeyDup(const CERT_MgrCtx *mgrCtx, HITLS_CERT_Key *key)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
return (HITLS_CERT_Key *)CRYPT_EAL_PkeyDupCtx(key);
#else
return mgrCtx->method.keyDup(key);
#endif
}
void SAL_CERT_KeyFree(const CERT_MgrCtx *mgrCtx, HITLS_CERT_Key *key)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
(void)mgrCtx;
CRYPT_EAL_PkeyFreeCtx(key);
#else
if (key == NULL) {
return;
}
mgrCtx->method.keyFree(key);
#endif
}
/* change the error code when modifying the ctrl command */
static const struct {
HITLS_CERT_CtrlCmd cmd;
uint32_t err;
} g_tlsCertCtrlErrorCode[] = {
{ CERT_STORE_CTRL_SET_VERIFY_DEPTH, HITLS_CERT_STORE_CTRL_ERR_SET_VERIFY_DEPTH },
{ CERT_STORE_CTRL_ADD_CERT_LIST, HITLS_CERT_STORE_CTRL_ERR_ADD_CERT_LIST },
{ CERT_STORE_CTRL_ADD_CRL_LIST, HITLS_CERT_STORE_CTRL_ERR_ADD_CRL_LIST },
{ CERT_STORE_CTRL_CLEAR_CRL_LIST, HITLS_CERT_STORE_CTRL_ERR_CLEAR_CRL_LIST },
{ CERT_STORE_CTRL_GET_VERIFY_DEPTH, HITLS_CERT_STORE_CTRL_ERR_GET_VERIFY_DEPTH },
{ CERT_CTRL_GET_ENCODE_LEN, HITLS_CERT_CTRL_ERR_GET_ENCODE_LEN },
{ CERT_CTRL_GET_PUB_KEY, HITLS_CERT_CTRL_ERR_GET_PUB_KEY },
{ CERT_CTRL_GET_SIGN_ALGO, HITLS_CERT_CTRL_ERR_GET_SIGN_ALGO },
{ CERT_CTRL_GET_ENCODE_SUBJECT_DN, HITLS_CERT_CTRL_ERR_GET_SUBJECT_DN },
{ CERT_CTRL_IS_SELF_SIGNED, HITLS_CERT_CTRL_ERR_IS_SELF_SIGNED },
{ CERT_KEY_CTRL_GET_SIGN_LEN, HITLS_CERT_KEY_CTRL_ERR_GET_SIGN_LEN },
{ CERT_KEY_CTRL_GET_TYPE, HITLS_CERT_KEY_CTRL_ERR_GET_TYPE },
{ CERT_KEY_CTRL_GET_CURVE_NAME, HITLS_CERT_KEY_CTRL_ERR_GET_CURVE_NAME },
{ CERT_KEY_CTRL_GET_POINT_FORMAT, HITLS_CERT_KEY_CTRL_ERR_GET_POINT_FORMAT },
{ CERT_KEY_CTRL_GET_SECBITS, HITLS_CERT_KEY_CTRL_ERR_GET_SECBITS },
{ CERT_KEY_CTRL_IS_KEYENC_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_ENC_USAGE },
{ CERT_KEY_CTRL_IS_DIGITAL_SIGN_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_DIGITAL_SIGN_USAGE },
{ CERT_KEY_CTRL_IS_KEY_CERT_SIGN_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_KEY_CERT_SIGN_USAGE },
{ CERT_KEY_CTRL_IS_KEY_AGREEMENT_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_KEY_AGREEMENT_USAGE },
{ CERT_KEY_CTRL_GET_PARAM_ID, HITLS_CERT_KEY_CTRL_ERR_GET_PARAM_ID },
{ CERT_KEY_CTRL_IS_DATA_ENC_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_DATA_ENC_USAGE },
{ CERT_KEY_CTRL_IS_NON_REPUDIATION_USAGE, HITLS_CERT_KEY_CTRL_ERR_IS_NON_REPUDIATION_USAGE },
{ CERT_STORE_CTRL_GET_VERIFY_FLAGS, HITLS_CERT_STORE_CTRL_ERR_GET_VERIFY_FLAGS },
{ CERT_STORE_CTRL_SET_VERIFY_FLAGS, HITLS_CERT_STORE_CTRL_ERR_SET_VERIFY_FLAGS },
};
static uint32_t GetTlsCertCtrlErrorCode(HITLS_CERT_CtrlCmd cmd)
{
for (size_t i = 0; i < sizeof(g_tlsCertCtrlErrorCode) / sizeof(g_tlsCertCtrlErrorCode[0]); i++) {
if (g_tlsCertCtrlErrorCode[i].cmd == cmd) {
return g_tlsCertCtrlErrorCode[i].err;
}
}
return HITLS_CERT_CTRL_ERR_INVALID_CMD;
}
int32_t SAL_CERT_StoreCtrl(HITLS_Config *config, HITLS_CERT_Store *store, HITLS_CERT_CtrlCmd cmd, void *in, void *out)
{
int32_t ret;
if (cmd > CERT_CTRL_BUTT - 1) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_CTRL_ERR_INVALID_CMD);
return HITLS_CERT_CTRL_ERR_INVALID_CMD;
}
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_StoreCtrl(config, store, cmd, in, out);
#else
ret = config->certMgrCtx->method.certStoreCtrl(config, store, cmd, in, out);
#endif
return CheckCertCallBackRetVal("cert store ctrl", ret, BINLOG_ID16094, GetTlsCertCtrlErrorCode(cmd));
}
int32_t SAL_CERT_X509Ctrl(HITLS_Config *config, HITLS_CERT_X509 *cert, HITLS_CERT_CtrlCmd cmd, void *in, void *out)
{
if (cert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16279, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
if (cmd > CERT_CTRL_BUTT - 1) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_CTRL_ERR_INVALID_CMD);
return HITLS_CERT_CTRL_ERR_INVALID_CMD;
}
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_CertCtrl(config, cert, cmd, in, out);
#else
ret = config->certMgrCtx->method.certCtrl(config, cert, cmd, in, out);
#endif
return CheckCertCallBackRetVal("cert ctrl", ret, BINLOG_ID16096, GetTlsCertCtrlErrorCode(cmd));
}
int32_t SAL_CERT_KeyCtrl(HITLS_Config *config, HITLS_CERT_Key *key, HITLS_CERT_CtrlCmd cmd, void *in, void *out)
{
if (key == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16280, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
if (cmd > CERT_CTRL_BUTT - 1) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_CTRL_ERR_INVALID_CMD);
return HITLS_CERT_CTRL_ERR_INVALID_CMD;
}
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_KeyCtrl(config, key, cmd, in, out);
#else
ret = config->certMgrCtx->method.keyCtrl(config, key, cmd, in, out);
#endif
return CheckCertCallBackRetVal("key ctrl", ret, BINLOG_ID16098, GetTlsCertCtrlErrorCode(cmd));
}
int32_t SAL_CERT_CreateSign(HITLS_Ctx *ctx, HITLS_CERT_Key *key, CERT_SignParam *signParam)
{
if (key == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16281, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_CreateSign(ctx, key, signParam->signAlgo, signParam->hashAlgo, signParam->data,
signParam->dataLen, signParam->sign, &signParam->signLen);
#else
ret = ctx->config.tlsConfig.certMgrCtx->method.createSign(ctx, key, signParam->signAlgo,
signParam->hashAlgo, signParam->data, signParam->dataLen, signParam->sign, &signParam->signLen);
#endif
return CheckCertCallBackRetVal("create signature", ret, BINLOG_ID16103, HITLS_CERT_ERR_CREATE_SIGN);
}
int32_t SAL_CERT_VerifySign(HITLS_Ctx *ctx, HITLS_CERT_Key *key, CERT_SignParam *signParam)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_VerifySign(ctx, key, signParam->signAlgo,
signParam->hashAlgo, signParam->data, signParam->dataLen, signParam->sign, signParam->signLen);
#else
ret = ctx->config.tlsConfig.certMgrCtx->method.verifySign(ctx, key, signParam->signAlgo,
signParam->hashAlgo, signParam->data, signParam->dataLen, signParam->sign, signParam->signLen);
#endif
return CheckCertCallBackRetVal("verify signature", ret, BINLOG_ID16101, HITLS_CERT_ERR_VERIFY_SIGN);
}
#if defined(HITLS_TLS_SUITE_KX_RSA) || defined(HITLS_TLS_PROTO_TLCP11)
int32_t SAL_CERT_KeyEncrypt(HITLS_Ctx *ctx, HITLS_CERT_Key *key, const uint8_t *in, uint32_t inLen,
uint8_t *out, uint32_t *outLen)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_Encrypt(ctx, key, in, inLen, out, outLen);
#else
if (ctx->config.tlsConfig.certMgrCtx->method.encrypt == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID15333, "unregistered encrypt");
}
ret = ctx->config.tlsConfig.certMgrCtx->method.encrypt(ctx, key, in, inLen, out, outLen);
#endif
return CheckCertCallBackRetVal("pubkey encrypt", ret, BINLOG_ID15059, HITLS_CERT_ERR_ENCRYPT);
}
int32_t SAL_CERT_KeyDecrypt(HITLS_Ctx *ctx, HITLS_CERT_Key *key, const uint8_t *in, uint32_t inLen,
uint8_t *out, uint32_t *outLen)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
return HITLS_X509_Adapt_Decrypt(ctx, key, in, inLen, out, outLen);
#else
if (ctx->config.tlsConfig.certMgrCtx->method.decrypt == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID15334, "unregistered decrypt");
}
return ctx->config.tlsConfig.certMgrCtx->method.decrypt(ctx, key, in, inLen, out, outLen);
#endif
}
#endif /* HITLS_TLS_SUITE_KX_RSA || HITLS_TLS_PROTO_TLCP11 */
int32_t SAL_CERT_CheckPrivateKey(HITLS_Config *config, HITLS_CERT_X509 *cert, HITLS_CERT_Key *key)
{
int32_t ret;
#ifdef HITLS_TLS_FEATURE_PROVIDER
ret = HITLS_X509_Adapt_CheckPrivateKey(config, cert, key);
#else
ret = config->certMgrCtx->method.checkPrivateKey(config, cert, key);
#endif
return CheckCertCallBackRetVal(
"check cert and private key", ret, BINLOG_ID15538, HITLS_CERT_ERR_CHECK_CERT_AND_KEY);
}
HITLS_CERT_CRLList *SAL_CERT_CrlParse(HITLS_Config *config, const uint8_t *buf, uint32_t len,
HITLS_ParseType type, HITLS_ParseFormat format)
{
return HITLS_X509_Adapt_CrlParse(config, buf, len, type, format);
}
void SAL_CERT_CrlFree(HITLS_CERT_CRLList *crlList)
{
HITLS_X509_Adapt_CrlFree(crlList);
}
| 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_method.c | C | unknown | 17,133 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdint.h>
#include "securec.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "bsl_sal.h"
#include "hitls_error.h"
#include "hitls_cert_reg.h"
#include "tls_config.h"
#include "cert_method.h"
#include "cert_mgr_ctx.h"
bool SAL_CERT_MgrIsEnable(void)
{
#ifdef HITLS_TLS_FEATURE_PROVIDER
return true;
#else
HITLS_CERT_MgrMethod *method = SAL_CERT_GetMgrMethod();
return (method->certStoreNew != NULL);
#endif
}
CERT_MgrCtx *SAL_CERT_MgrCtxNew(void)
{
return SAL_CERT_MgrCtxProviderNew(NULL, NULL);
}
CERT_MgrCtx *SAL_CERT_MgrCtxProviderNew(HITLS_Lib_Ctx *libCtx, const char *attrName)
{
CERT_MgrCtx *newCtx = BSL_SAL_Calloc(1, sizeof(CERT_MgrCtx));
if (newCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16085, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"new cert manager context error: out of memory.", 0, 0, 0, 0);
return NULL;
}
newCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
newCtx->certPairs = BSL_HASH_Create(CERT_DEFAULT_HASH_BKT_SIZE, NULL, NULL, NULL, NULL);
if (newCtx->certPairs == NULL) {
BSL_SAL_FREE(newCtx);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17338, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"new cert manager context error: new certPairs failed.", 0, 0, 0, 0);
return NULL;
}
#ifndef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_MgrMethod *method = SAL_CERT_GetMgrMethod();
(void)memcpy_s(&newCtx->method, sizeof(HITLS_CERT_MgrMethod), method, sizeof(HITLS_CERT_MgrMethod));
#endif
newCtx->certStore = SAL_CERT_StoreNew(newCtx);
if (newCtx->certStore == NULL) {
BSL_HASH_Destory(newCtx->certPairs);
BSL_SAL_FREE(newCtx);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15016, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"new cert manager context error: new store failed.", 0, 0, 0, 0);
return NULL;
}
newCtx->libCtx = libCtx;
newCtx->attrName = attrName;
return newCtx;
}
int32_t StoreDup(CERT_MgrCtx *destMgrCtx, CERT_MgrCtx *srcMgrCtx)
{
if (srcMgrCtx->certStore != NULL) {
destMgrCtx->certStore = SAL_CERT_StoreDup(srcMgrCtx, srcMgrCtx->certStore);
if (destMgrCtx->certStore == NULL) {
/* releasing resources at the call point */
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_STORE_DUP, BINLOG_ID16092, "StoreDup fail");
}
}
if (srcMgrCtx->chainStore != NULL) {
destMgrCtx->chainStore = SAL_CERT_StoreDup(srcMgrCtx, srcMgrCtx->chainStore);
if (destMgrCtx->chainStore == NULL) {
/* releasing resources at the call point */
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_STORE_DUP, BINLOG_ID16093, "StoreDup fail");
}
}
if (srcMgrCtx->verifyStore != NULL) {
destMgrCtx->verifyStore = SAL_CERT_StoreDup(srcMgrCtx, srcMgrCtx->verifyStore);
if (destMgrCtx->verifyStore == NULL) {
/* releasing resources at the call point */
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_STORE_DUP, BINLOG_ID16095, "StoreDup fail");
}
}
return HITLS_SUCCESS;
}
CERT_MgrCtx *SAL_CERT_MgrCtxDup(CERT_MgrCtx *mgrCtx)
{
int32_t ret;
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16282, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
CERT_MgrCtx *newCtx = BSL_SAL_Calloc(1, sizeof(CERT_MgrCtx));
if (newCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16097, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"dup cert manager context error: out of memory.", 0, 0, 0, 0);
return NULL;
}
#ifndef HITLS_TLS_FEATURE_PROVIDER
(void)memcpy_s(&newCtx->method, sizeof(HITLS_CERT_MgrMethod), &mgrCtx->method, sizeof(HITLS_CERT_MgrMethod));
#endif
ret = SAL_CERT_HashDup(newCtx, mgrCtx);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16283, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"SAL_CERT_HashDup fail, ret %d", ret, 0, 0, 0);
SAL_CERT_MgrCtxFree(newCtx);
return NULL;
}
if (mgrCtx->extraChain != NULL) {
newCtx->extraChain = SAL_CERT_ChainDup(mgrCtx, mgrCtx->extraChain);
if (newCtx->extraChain == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16284, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"ChainDup fail", 0, 0, 0, 0);
SAL_CERT_MgrCtxFree(newCtx);
return NULL;
}
}
ret = StoreDup(newCtx, mgrCtx);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16285, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"StoreDup fail, ret %d", ret, 0, 0, 0);
SAL_CERT_MgrCtxFree(newCtx);
return NULL;
}
newCtx->currentCertKeyType = mgrCtx->currentCertKeyType;
newCtx->defaultPasswdCb = mgrCtx->defaultPasswdCb;
newCtx->defaultPasswdCbUserData = mgrCtx->defaultPasswdCbUserData;
newCtx->verifyCb = mgrCtx->verifyCb;
newCtx->libCtx = LIBCTX_FROM_CERT_MGR_CTX(mgrCtx);
newCtx->attrName = ATTRIBUTE_FROM_CERT_MGR_CTX(mgrCtx);
#ifdef HITLS_TLS_FEATURE_CERT_CB
newCtx->certCb = mgrCtx->certCb;
newCtx->certCbArg = mgrCtx->certCbArg;
#endif /* HITLS_TLS_FEATURE_CERT_CB */
return newCtx;
}
void SAL_CERT_MgrCtxFree(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return;
}
SAL_CERT_ClearCertAndKey(mgrCtx);
SAL_CERT_ChainFree(mgrCtx->extraChain);
mgrCtx->extraChain = NULL;
SAL_CERT_StoreFree(mgrCtx, mgrCtx->verifyStore);
mgrCtx->verifyStore = NULL;
SAL_CERT_StoreFree(mgrCtx, mgrCtx->chainStore);
mgrCtx->chainStore = NULL;
SAL_CERT_StoreFree(mgrCtx, mgrCtx->certStore);
mgrCtx->certStore = NULL;
BSL_HASH_Destory(mgrCtx->certPairs);
mgrCtx->certPairs = NULL;
BSL_SAL_FREE(mgrCtx);
return;
} | 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_mgr_create.c | C | unknown | 6,550 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include <stdint.h>
#include "securec.h"
#include "tls_binlog_id.h"
#include "bsl_log_internal.h"
#include "bsl_log.h"
#include "bsl_err_internal.h"
#include "bsl_sal.h"
#include "hitls_error.h"
#include "cert_method.h"
#include "cert.h"
#include "cert_mgr_ctx.h"
int32_t SAL_CERT_SetCertStore(CERT_MgrCtx *mgrCtx, HITLS_CERT_Store *store)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
SAL_CERT_StoreFree(mgrCtx, mgrCtx->certStore);
mgrCtx->certStore = store;
return HITLS_SUCCESS;
}
HITLS_CERT_Store *SAL_CERT_GetCertStore(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->certStore;
}
int32_t SAL_CERT_SetChainStore(CERT_MgrCtx *mgrCtx, HITLS_CERT_Store *store)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
SAL_CERT_StoreFree(mgrCtx, mgrCtx->chainStore);
mgrCtx->chainStore = store;
return HITLS_SUCCESS;
}
HITLS_CERT_Store *SAL_CERT_GetChainStore(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->chainStore;
}
int32_t SAL_CERT_SetVerifyStore(CERT_MgrCtx *mgrCtx, HITLS_CERT_Store *store)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
SAL_CERT_StoreFree(mgrCtx, mgrCtx->verifyStore);
mgrCtx->verifyStore = store;
return HITLS_SUCCESS;
}
HITLS_CERT_Store *SAL_CERT_GetVerifyStore(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->verifyStore;
}
static int32_t GetOrInsertCertPair(CERT_MgrCtx *mgrCtx, HITLS_CERT_KeyType keyType, CERT_Pair **certPair)
{
CERT_Pair *newCertPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&newCertPair);
if (ret != HITLS_SUCCESS || newCertPair == NULL) {
newCertPair = BSL_SAL_Calloc(1u, sizeof(CERT_Pair));
if (newCertPair == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID16102, "certPair calloc fail");
}
ret = BSL_HASH_Insert(mgrCtx->certPairs, keyType, 0, (uintptr_t)newCertPair, sizeof(CERT_Pair));
if (ret != HITLS_SUCCESS) {
BSL_SAL_FREE(newCertPair);
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID17339, "insert fail");
}
}
*certPair = newCertPair;
return HITLS_SUCCESS;
}
int32_t SAL_CERT_SetCurrentCert(HITLS_Config *config, HITLS_CERT_X509 *cert, bool isTlcpEncCert)
{
(void)isTlcpEncCert;
if (cert == NULL || config == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_UNREGISTERED_CALLBACK);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_UNREGISTERED_CALLBACK, BINLOG_ID16286, "unregistered callback");
}
HITLS_CERT_Key *pubkey = NULL;
int32_t ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_PUB_KEY, NULL, (void *)&pubkey);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16099, "GET PUB KEY fail");
}
uint32_t keyType = TLS_CERT_KEY_TYPE_UNKNOWN;
ret = SAL_CERT_KeyCtrl(config, pubkey, CERT_KEY_CTRL_GET_TYPE, NULL, (void *)&keyType);
SAL_CERT_KeyFree(mgrCtx, pubkey);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16100, "GET KEY TYPE fail");
}
CERT_Pair *certPair = NULL;
ret = GetOrInsertCertPair(mgrCtx, keyType, &certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
return HITLS_MEMALLOC_FAIL;
}
HITLS_CERT_Key **privateKey = NULL;
HITLS_CERT_X509 **certPairCert = NULL;
#ifdef HITLS_TLS_PROTO_TLCP11
if (isTlcpEncCert) {
privateKey = &certPair->encPrivateKey;
certPairCert = &certPair->encCert;
} else
#endif
{
privateKey = &certPair->privateKey;
certPairCert = &certPair->cert;
}
if (*privateKey != NULL) {
ret = SAL_CERT_CheckPrivateKey(config, cert, *privateKey);
if (ret != HITLS_SUCCESS) {
/* If the certificate does not match the private key, release the private key. */
SAL_CERT_KeyFree(mgrCtx, *privateKey);
*privateKey = NULL;
}
}
SAL_CERT_X509Free(*certPairCert);
*certPairCert = cert;
mgrCtx->currentCertKeyType = keyType;
return HITLS_SUCCESS;
}
HITLS_CERT_X509 *SAL_CERT_GetCurrentCert(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16287, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
uint32_t keyType = mgrCtx->currentCertKeyType;
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16288, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "idx err", 0, 0, 0, 0);
return NULL;
}
return certPair->cert;
}
HITLS_CERT_X509 *SAL_CERT_GetCert(CERT_MgrCtx *mgrCtx, HITLS_CERT_KeyType keyType)
{
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16289, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16290, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "idx err", 0, 0, 0, 0);
return NULL;
}
return certPair->cert;
}
int32_t SAL_CERT_SetCurrentPrivateKey(HITLS_Config *config, HITLS_CERT_Key *key, bool isTlcpEncCertPriKey)
{
(void)isTlcpEncCertPriKey;
if (key == NULL || config == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_UNREGISTERED_CALLBACK);
return HITLS_UNREGISTERED_CALLBACK;
}
uint32_t keyType = TLS_CERT_KEY_TYPE_UNKNOWN;
int32_t ret = SAL_CERT_KeyCtrl(config, key, CERT_KEY_CTRL_GET_TYPE, NULL, (void *)&keyType);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID16104, "get key type fail");
}
CERT_Pair *certPair = NULL;
ret = GetOrInsertCertPair(mgrCtx, keyType, &certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
return HITLS_MEMALLOC_FAIL;
}
HITLS_CERT_Key **certPairPrivateKey = NULL;
HITLS_CERT_X509 **cert = NULL;
#ifdef HITLS_TLS_PROTO_TLCP11
if (isTlcpEncCertPriKey) {
certPairPrivateKey = &certPair->encPrivateKey;
cert = &certPair->encCert;
} else
#endif
{
certPairPrivateKey = &certPair->privateKey;
cert = &certPair->cert;
}
if (*cert != NULL) {
ret = SAL_CERT_CheckPrivateKey(config, *cert, key);
if (ret != HITLS_SUCCESS) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16107, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"set private key error: cert and key mismatch, key type = %u.", keyType, 0, 0, 0);
/* The certificate does not match the private key. */
return ret;
}
}
SAL_CERT_KeyFree(mgrCtx, *certPairPrivateKey);
*certPairPrivateKey = key;
mgrCtx->currentCertKeyType = keyType;
return HITLS_SUCCESS;
}
HITLS_CERT_Key *SAL_CERT_GetCurrentPrivateKey(CERT_MgrCtx *mgrCtx, bool isTlcpEncCert)
{
(void)isTlcpEncCert;
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16291, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
uint32_t keyType = mgrCtx->currentCertKeyType;
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16292, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "certPair null", 0, 0, 0, 0);
return NULL;
}
#ifdef HITLS_TLS_PROTO_TLCP11
if (isTlcpEncCert) {
return certPair->encPrivateKey;
}
#endif
return certPair->privateKey;
}
HITLS_CERT_Key *SAL_CERT_GetPrivateKey(CERT_MgrCtx *mgrCtx, HITLS_CERT_KeyType keyType)
{
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16293, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16294, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "certPair null", 0, 0, 0, 0);
return NULL;
}
return certPair->privateKey;
}
int32_t SAL_CERT_AddChainCert(CERT_MgrCtx *mgrCtx, HITLS_CERT_X509 *cert)
{
if (mgrCtx == NULL || cert == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID16392, "null input");
}
uint32_t keyType = mgrCtx->currentCertKeyType;
if (keyType == TLS_CERT_KEY_TYPE_UNKNOWN) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_ADD_CHAIN_CERT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_ADD_CHAIN_CERT, BINLOG_ID16390, "keyType unknown");
}
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)keyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
/* the certificate has not been loaded yet */
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_ADD_CHAIN_CERT);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_ADD_CHAIN_CERT, BINLOG_ID16391, "certPair null");
}
HITLS_CERT_Chain *newChain = NULL;
HITLS_CERT_Chain *chain = certPair->chain;
if (chain == NULL) {
newChain = SAL_CERT_ChainNew();
if (newChain == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID16295, "ChainNew fail");
}
chain = newChain;
}
ret = SAL_CERT_ChainAppend(chain, cert);
if (ret != HITLS_SUCCESS) {
BSL_SAL_FREE(newChain);
return ret;
}
certPair->chain = chain;
return HITLS_SUCCESS;
}
HITLS_CERT_Chain *SAL_CERT_GetCurrentChainCerts(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16296, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mgrCtx null", 0, 0, 0, 0);
return NULL;
}
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)mgrCtx->currentCertKeyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16297, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "certPair null", 0, 0, 0, 0);
return NULL;
}
return certPair->chain;
}
void SAL_CERT_ClearCurrentChainCerts(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return;
}
CERT_Pair *certPair = NULL;
int32_t ret = BSL_HASH_At(mgrCtx->certPairs, (uintptr_t)mgrCtx->currentCertKeyType, (uintptr_t *)&certPair);
if (ret != HITLS_SUCCESS || certPair == NULL || certPair->chain == NULL) {
return;
}
SAL_CERT_ChainFree(certPair->chain);
certPair->chain = NULL;
return;
}
void SAL_CERT_ClearCertAndKey(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return;
}
BSL_HASH_Hash *certPairs = mgrCtx->certPairs;
for (BSL_HASH_Iterator it = BSL_HASH_IterBegin(certPairs); it != BSL_HASH_IterEnd(certPairs);) {
uint32_t keyType = (uint32_t)BSL_HASH_HashIterKey(certPairs, it);
CERT_Pair *certPair = (CERT_Pair *)BSL_HASH_IterValue(certPairs, it);
SAL_CERT_PairClear(mgrCtx, certPair);
BSL_SAL_FREE(certPair);
it = BSL_HASH_Erase(certPairs, keyType);
}
mgrCtx->currentCertKeyType = TLS_CERT_KEY_TYPE_UNKNOWN;
return;
}
int32_t SAL_CERT_AddExtraChainCert(CERT_MgrCtx *mgrCtx, HITLS_CERT_X509 *cert)
{
if (mgrCtx == NULL || cert == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
HITLS_CERT_Chain *newChain = NULL;
HITLS_CERT_Chain *chain = mgrCtx->extraChain;
if (chain == NULL) {
newChain = SAL_CERT_ChainNew();
if (newChain == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return RETURN_ERROR_NUMBER_PROCESS(HITLS_MEMALLOC_FAIL, BINLOG_ID16298, "ChainNew fail");
}
chain = newChain;
}
int32_t ret = SAL_CERT_ChainAppend(chain, cert);
if (ret != HITLS_SUCCESS) {
BSL_SAL_FREE(newChain);
return ret;
}
mgrCtx->extraChain = chain;
return HITLS_SUCCESS;
}
HITLS_CERT_Chain *SAL_CERT_GetExtraChainCerts(CERT_MgrCtx *mgrCtx, bool isExtraChainCertsOnly)
{
if (mgrCtx == NULL) {
return NULL;
}
if (mgrCtx->extraChain == NULL && !isExtraChainCertsOnly) {
return SAL_CERT_GetCurrentChainCerts(mgrCtx);
}
return mgrCtx->extraChain;
}
void SAL_CERT_ClearExtraChainCerts(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return;
}
HITLS_CERT_Chain *chain = mgrCtx->extraChain;
if (chain == NULL) {
return;
}
SAL_CERT_ChainFree(chain);
mgrCtx->extraChain = NULL;
return;
}
int32_t SAL_CERT_CtrlVerifyParams(HITLS_Config *config, HITLS_CERT_Store *store, uint32_t cmd, void *in, void *out)
{
CERT_MgrCtx *mgrCtx = config->certMgrCtx;
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
HITLS_CERT_Store *tempStore = store;
if (tempStore == NULL) {
tempStore = (mgrCtx->verifyStore != NULL) ? mgrCtx->verifyStore : mgrCtx->certStore;
if (tempStore == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_NULL_INPUT, BINLOG_ID15327, "store is null");
}
}
int32_t ret = SAL_CERT_StoreCtrl(config, tempStore, cmd, in, out);
if (ret != HITLS_SUCCESS) {
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID15326, "SAL_CERT_StoreCtrl fail");
}
return HITLS_SUCCESS;
}
int32_t SAL_CERT_SetDefaultPasswordCb(CERT_MgrCtx *mgrCtx, HITLS_PasswordCb cb)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
mgrCtx->defaultPasswdCb = cb;
return HITLS_SUCCESS;
}
HITLS_PasswordCb SAL_CERT_GetDefaultPasswordCb(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->defaultPasswdCb;
}
int32_t SAL_CERT_SetDefaultPasswordCbUserdata(CERT_MgrCtx *mgrCtx, void *userdata)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
mgrCtx->defaultPasswdCbUserData = userdata;
return HITLS_SUCCESS;
}
void *SAL_CERT_GetDefaultPasswordCbUserdata(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->defaultPasswdCbUserData;
}
int32_t SAL_CERT_SetVerifyCb(CERT_MgrCtx *mgrCtx, HITLS_VerifyCb cb)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
mgrCtx->verifyCb = cb;
return HITLS_SUCCESS;
}
HITLS_VerifyCb SAL_CERT_GetVerifyCb(CERT_MgrCtx *mgrCtx)
{
if (mgrCtx == NULL) {
return NULL;
}
return mgrCtx->verifyCb;
}
#ifdef HITLS_TLS_FEATURE_CERT_CB
int32_t SAL_CERT_SetCertCb(CERT_MgrCtx *mgrCtx, HITLS_CertCb certCb, void *arg)
{
if (mgrCtx == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
return HITLS_NULL_INPUT;
}
mgrCtx->certCb = certCb;
mgrCtx->certCbArg = arg;
return HITLS_SUCCESS;
}
#endif /* HITLS_TLS_FEATURE_CERT_CB */
| 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_mgr_ctrl.c | C | unknown | 16,552 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef CERT_MGR_CTX_H
#define CERT_MGR_CTX_H
#include <stdint.h>
#include "hitls_crypt_type.h"
#include "hitls_cert_reg.h"
#include "cert.h"
#include "bsl_hash.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CERT_DEFAULT_HASH_BKT_SIZE 64u
struct CertPairInner {
HITLS_CERT_X509 *cert; /* device certificate */
#ifdef HITLS_TLS_PROTO_TLCP11
/* encrypted device cert. Currently this field is used only when the peer-end encrypted certificate is stored. */
HITLS_CERT_X509 *encCert;
HITLS_CERT_Key *encPrivateKey;
#endif
HITLS_CERT_Key *privateKey; /* private key corresponding to the certificate */
HITLS_CERT_Chain *chain; /* certificate chain */
};
struct CertMgrCtxInner {
uint32_t currentCertKeyType; /* keyType to the certificate in use. */
/* Indicates the certificate resources on the link. Only one certificate of a type can be loaded. */
BSL_HASH_Hash *certPairs; /* cert hash table. key keyType, value CERT_Pair */
HITLS_CERT_Chain *extraChain;
HITLS_CERT_Store *verifyStore; /* Verifies the store, which is used to verify the certificate chain. */
HITLS_CERT_Store *chainStore; /* Certificate chain store, used to assemble the certificate chain */
HITLS_CERT_Store *certStore; /* Default CA store */
#ifndef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_MgrMethod method; /* callback function */
#endif
HITLS_PasswordCb defaultPasswdCb; /* Default password callback, used in loading certificate. */
void *defaultPasswdCbUserData; /* Set the userData used by the default password callback. */
HITLS_VerifyCb verifyCb; /* Certificate verification callback function */
#ifdef HITLS_TLS_FEATURE_CERT_CB
HITLS_CertCb certCb; /* Certificate callback function */
void *certCbArg; /* Argument for the certificate callback function */
#endif /* HITLS_TLS_FEATURE_CERT_CB */
HITLS_Lib_Ctx *libCtx; /* library context */
const char *attrName; /* attrName */
};
CERT_Type CertKeyType2CertType(HITLS_CERT_KeyType keyType);
int32_t CheckCurveName(HITLS_Config *config, const uint16_t *curveList, uint32_t curveNum, HITLS_CERT_Key *pubkey);
int32_t CheckPointFormat(HITLS_Config *config, const uint8_t *ecPointFormatList, uint32_t listSize,
HITLS_CERT_Key *pubkey);
/* These functions can be stored in a separate header file. */
HITLS_CERT_Chain *SAL_CERT_ChainNew(void);
int32_t SAL_CERT_ChainAppend(HITLS_CERT_Chain *chain, HITLS_CERT_X509 *cert);
HITLS_CERT_Chain *SAL_CERT_ChainDup(CERT_MgrCtx *mgrCtx, HITLS_CERT_Chain *chain);
#define LIBCTX_FROM_CERT_MGR_CTX(mgrCtx) ((mgrCtx == NULL) ? NULL : (mgrCtx)->libCtx)
#define ATTRIBUTE_FROM_CERT_MGR_CTX(mgrCtx) ((mgrCtx == NULL) ? NULL : (mgrCtx)->attrName)
#ifdef __cplusplus
}
#endif
#endif | 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_mgr_ctx.h | C | unknown | 3,486 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#include "securec.h"
#include "bsl_sal.h"
#include "tls_binlog_id.h"
#include "hitls_cert_type.h"
#include "cert_method.h"
#include "cert_mgr.h"
#include "cert_mgr_ctx.h"
HITLS_CERT_X509 *SAL_CERT_PairGetX509(CERT_Pair *certPair)
{
if (certPair == NULL) {
return NULL;
}
return certPair->cert;
}
#ifdef HITLS_TLS_PROTO_TLCP11
HITLS_CERT_X509 *SAL_CERT_GetTlcpEncCert(CERT_Pair *certPair)
{
if (certPair == NULL) {
return NULL;
}
return certPair->encCert;
}
#endif
#if defined(HITLS_TLS_CONNECTION_INFO_NEGOTIATION)
HITLS_CERT_Chain *SAL_CERT_PairGetChain(CERT_Pair *certPair)
{
if (certPair == NULL) {
return NULL;
}
return certPair->chain;
}
#endif /* HITLS_TLS_CONNECTION_INFO_NEGOTIATION */
#ifdef HITLS_TLS_PROTO_TLCP11
static int32_t TlcpCertPairDup(CERT_MgrCtx *mgrCtx, CERT_Pair *srcCertPair, CERT_Pair *destCertPair)
{
if (srcCertPair->encCert != NULL) {
destCertPair->encCert = SAL_CERT_X509Dup(mgrCtx, srcCertPair->encCert);
if (destCertPair->encCert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17341, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"enc X509Dup fail", 0, 0, 0, 0);
return HITLS_CERT_ERR_X509_DUP;
}
}
if (srcCertPair->encPrivateKey != NULL) {
destCertPair->encPrivateKey = SAL_CERT_KeyDup(mgrCtx, srcCertPair->encPrivateKey);
if (destCertPair->encPrivateKey == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17342, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"enc KeyDup fail", 0, 0, 0, 0);
return HITLS_CERT_ERR_X509_DUP;
}
}
return HITLS_SUCCESS;
}
#endif
CERT_Pair *SAL_CERT_PairDup(CERT_MgrCtx *mgrCtx, CERT_Pair *srcCertPair)
{
CERT_Pair *destCertPair = BSL_SAL_Calloc(1, sizeof(CERT_MgrCtx));
if (destCertPair == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16299, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Calloc fail", 0, 0, 0, 0);
return NULL;
}
do {
#ifdef HITLS_TLS_PROTO_TLCP11
if (TlcpCertPairDup(mgrCtx, srcCertPair, destCertPair) != HITLS_SUCCESS) {
break;
}
#endif
if (srcCertPair->cert != NULL) {
destCertPair->cert = SAL_CERT_X509Dup(mgrCtx, srcCertPair->cert);
if (destCertPair->cert == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16300, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"X509Dup fail", 0, 0, 0, 0);
break;
}
}
if (srcCertPair->privateKey != NULL) {
destCertPair->privateKey = SAL_CERT_KeyDup(mgrCtx, srcCertPair->privateKey);
if (destCertPair->privateKey == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16301, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"KeyDup fail", 0, 0, 0, 0);
break;
}
}
if (srcCertPair->chain != NULL) {
destCertPair->chain = SAL_CERT_ChainDup(mgrCtx, srcCertPair->chain);
if (destCertPair->chain == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16302, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"ChainDup fail", 0, 0, 0, 0);
break;
}
}
return destCertPair;
} while (false);
SAL_CERT_PairFree(mgrCtx, destCertPair);
return NULL;
}
void SAL_CERT_PairClear(CERT_MgrCtx *mgrCtx, CERT_Pair *certPair)
{
if (mgrCtx == NULL || certPair == NULL) {
return;
}
if (certPair->cert != NULL) {
SAL_CERT_X509Free(certPair->cert);
}
#ifdef HITLS_TLS_PROTO_TLCP11
if (certPair->encCert != NULL) {
SAL_CERT_X509Free(certPair->encCert);
}
if (certPair->encPrivateKey != NULL) {
SAL_CERT_KeyFree(mgrCtx, certPair->encPrivateKey);
}
#endif
if (certPair->privateKey != NULL) {
SAL_CERT_KeyFree(mgrCtx, certPair->privateKey);
}
if (certPair->chain != NULL) {
SAL_CERT_ChainFree(certPair->chain);
}
(void)memset_s(certPair, sizeof(CERT_Pair), 0, sizeof(CERT_Pair));
return;
}
void SAL_CERT_PairFree(CERT_MgrCtx *mgrCtx, CERT_Pair *certPair)
{
SAL_CERT_PairClear(mgrCtx, certPair);
BSL_SAL_FREE(certPair);
return;
}
int32_t SAL_CERT_HashDup(CERT_MgrCtx *destMgrCtx, CERT_MgrCtx *srcMgrCtx)
{
destMgrCtx->certPairs = BSL_HASH_Create(CERT_DEFAULT_HASH_BKT_SIZE, NULL, NULL, NULL, NULL);
if (destMgrCtx->certPairs == NULL) {
BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17347, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
"BSL_HASH_Create fail", 0, 0, 0, 0);
return HITLS_MEMALLOC_FAIL;
}
BSL_HASH_Hash *certPairs = srcMgrCtx->certPairs;
BSL_HASH_Iterator iter = BSL_HASH_IterBegin(certPairs);
while (iter != BSL_HASH_IterEnd(certPairs)) {
uint32_t keyType = (uint32_t)BSL_HASH_HashIterKey(certPairs, iter);
CERT_Pair *certPair = (CERT_Pair *)BSL_HASH_IterValue(certPairs, iter);
if (certPair != NULL) {
CERT_Pair *newCertPair = SAL_CERT_PairDup(srcMgrCtx, certPair);
if (newCertPair == NULL) {
return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_X509_DUP, BINLOG_ID17348, "x509dup fail");
}
int32_t ret = BSL_HASH_Insert(destMgrCtx->certPairs, keyType, 0, (uintptr_t)newCertPair, sizeof(CERT_Pair));
if (ret != HITLS_SUCCESS) {
SAL_CERT_PairFree(destMgrCtx, newCertPair);
return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID17349, "insert fail");
}
}
iter = BSL_HASH_IterNext(certPairs, iter);
}
return HITLS_SUCCESS;
} | 2302_82127028/openHiTLS-examples_1508 | tls/cert/cert_adapt/cert_pair.c | C | unknown | 6,242 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#if defined(HITLS_TLS_CALLBACK_CERT) || defined(HITLS_TLS_FEATURE_PROVIDER)
#include <stdint.h>
#include <string.h>
#include "bsl_sal.h"
#include "bsl_err_internal.h"
#include "hitls_cert_type.h"
#include "hitls_type.h"
#include "hitls_pki_x509.h"
#include "bsl_list.h"
#include "hitls_error.h"
static int32_t BuildArrayFromList(HITLS_X509_List *list, HITLS_CERT_X509 **listArray, uint32_t *num)
{
HITLS_X509_Cert *elemt = NULL;
int32_t i = 0;
int32_t ret;
for (elemt = BSL_LIST_GET_FIRST(list); elemt != NULL; elemt = BSL_LIST_GET_NEXT(list), i++) {
int ref = 0;
ret = HITLS_X509_CertCtrl(elemt, HITLS_X509_REF_UP, (void *)&ref, (int32_t)sizeof(int));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return ret;
}
listArray[i] = elemt;
}
*num = i;
return HITLS_SUCCESS;
}
static int32_t BuildCertListFromCertArray(HITLS_CERT_X509 **listCert, uint32_t num, HITLS_X509_List **list)
{
int32_t ret = HITLS_SUCCESS;
HITLS_X509_Cert **listArray = (HITLS_X509_Cert **)listCert;
*list = BSL_LIST_New(num);
if (*list == NULL) {
BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
return HITLS_MEMALLOC_FAIL;
}
for (uint32_t i = 0; i < num; i++) {
int ref = 0;
ret = HITLS_X509_CertCtrl(listArray[i], HITLS_X509_REF_UP, (void *)&ref, (int32_t)sizeof(int));
if (ret != HITLS_SUCCESS) {
BSL_LIST_FREE(*list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
return ret;
}
ret = BSL_LIST_AddElement(*list, listArray[i], BSL_LIST_POS_END);
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
BSL_LIST_FREE(*list, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
return ret;
}
}
return HITLS_SUCCESS;
}
int32_t HITLS_X509_Adapt_BuildCertChain(HITLS_Config *config, HITLS_CERT_Store *store, HITLS_CERT_X509 *cert,
HITLS_CERT_X509 **list, uint32_t *num)
{
(void)config;
*num = 0;
HITLS_X509_List *certChain = NULL;
int32_t ret = HITLS_X509_CertChainBuild((HITLS_X509_StoreCtx *)store, false, cert, &certChain);
if (ret != HITLS_SUCCESS) {
return ret;
}
ret = BuildArrayFromList(certChain, list, num);
BSL_LIST_FREE(certChain, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
return ret;
}
int32_t HITLS_X509_Adapt_VerifyCertChain(HITLS_Ctx *ctx, HITLS_CERT_Store *store, HITLS_CERT_X509 **list, uint32_t num)
{
(void)ctx;
/* The default user id as specified in GM/T 0009-2012 */
char sm2DefaultUserid[] = "1234567812345678";
HITLS_X509_List *certList = NULL;
int32_t ret = BuildCertListFromCertArray(list, num, &certList);
if (ret != HITLS_SUCCESS) {
return ret;
}
int64_t sysTime = BSL_SAL_CurrentSysTimeGet();
if (sysTime == 0) {
ret = HITLS_CERT_SELF_ADAPT_INVALID_TIME;
BSL_ERR_PUSH_ERROR(HITLS_CERT_SELF_ADAPT_INVALID_TIME);
goto EXIT;
}
ret = HITLS_X509_StoreCtxCtrl((HITLS_X509_StoreCtx *)store, HITLS_X509_STORECTX_SET_TIME, &sysTime,
sizeof(sysTime));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
goto EXIT;
}
ret = HITLS_X509_StoreCtxCtrl((HITLS_X509_StoreCtx *)store, HITLS_X509_STORECTX_SET_VFY_SM2_USERID,
sm2DefaultUserid, strlen(sm2DefaultUserid));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
goto EXIT;
}
ret = HITLS_X509_CertVerify((HITLS_X509_StoreCtx *)store, certList);
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
}
EXIT:
BSL_LIST_FREE(certList, (BSL_LIST_PFUNC_FREE)HITLS_X509_CertFree);
return ret;
}
#endif /* defined(HITLS_TLS_CALLBACK_CERT) || defined(HITLS_TLS_FEATURE_PROVIDER) */
| 2302_82127028/openHiTLS-examples_1508 | tls/cert/hitls_x509_adapt/hitls_x509_cert_chain.c | C | unknown | 4,346 |
/*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "hitls_build.h"
#if defined(HITLS_TLS_CALLBACK_CERT) || defined(HITLS_TLS_FEATURE_PROVIDER)
#include <stdint.h>
#include "securec.h"
#include "crypt_eal_pkey.h"
#include "hitls_error.h"
#include "hitls_cert_type.h"
#include "hitls_type.h"
#include "hitls_pki_cert.h"
#include "hitls_error.h"
#include "bsl_err_internal.h"
#include "tls_config.h"
#include "cert_mgr_ctx.h"
#include "config_type.h"
int32_t HITLS_X509_Adapt_CertEncode(HITLS_Ctx *ctx, HITLS_CERT_X509 *cert, uint8_t *buf, uint32_t len,
uint32_t *usedLen)
{
(void)ctx;
*usedLen = 0;
uint32_t encodeLen = 0;
int32_t ret = HITLS_X509_CertCtrl((HITLS_X509_Cert *)cert, HITLS_X509_GET_ENCODELEN, &encodeLen,
(int32_t)sizeof(uint32_t));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return ret;
}
if (len < encodeLen) {
BSL_ERR_PUSH_ERROR(HITLS_INVALID_INPUT);
return HITLS_INVALID_INPUT;
}
uint8_t *encodedBuff = NULL;
ret = HITLS_X509_CertCtrl((HITLS_X509_Cert *)cert, HITLS_X509_GET_ENCODE, (void *)&encodedBuff, 0);
if (ret != HITLS_SUCCESS) {
return ret;
}
(void)memcpy_s(buf, len, encodedBuff, encodeLen);
*usedLen = encodeLen;
return ret;
}
#ifdef HITLS_TLS_FEATURE_PROVIDER
HITLS_CERT_X509 *HITLS_CERT_ProviderCertParse(HITLS_Lib_Ctx *libCtx, const char *attrName, const uint8_t *buf,
uint32_t len, HITLS_ParseType type, const char *format)
{
BSL_Buffer encodedCert = { NULL, 0 };
int ret;
HITLS_X509_Cert *cert = NULL;
switch (type) {
case TLS_PARSE_TYPE_FILE:
ret = HITLS_X509_ProviderCertParseFile(libCtx, attrName, format, (const char *)buf, &cert);
break;
case TLS_PARSE_TYPE_BUFF:
encodedCert.data = (uint8_t *)(uintptr_t)buf;
encodedCert.dataLen = len;
ret = HITLS_X509_ProviderCertParseBuff(libCtx, attrName, format, &encodedCert, &cert);
break;
default:
BSL_ERR_PUSH_ERROR(HITLS_CERT_SELF_ADAPT_UNSUPPORT_FORMAT);
ret = HITLS_CERT_SELF_ADAPT_UNSUPPORT_FORMAT;
break;
}
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return NULL;
}
return cert;
}
#else
HITLS_CERT_X509 *HITLS_X509_Adapt_CertParse(HITLS_Config *config, const uint8_t *buf, uint32_t len,
HITLS_ParseType type, HITLS_ParseFormat format)
{
(void)config;
BSL_Buffer encodedCert = { NULL, 0 };
int ret;
HITLS_X509_Cert *cert = NULL;
switch (type) {
case TLS_PARSE_TYPE_FILE:
ret = HITLS_X509_CertParseFile(format, (const char *)buf, &cert);
break;
case TLS_PARSE_TYPE_BUFF:
encodedCert.data = (uint8_t *)(uintptr_t)buf;
encodedCert.dataLen = len;
ret = HITLS_X509_CertParseBuff(format, &encodedCert, &cert);
break;
default:
BSL_ERR_PUSH_ERROR(HITLS_CERT_SELF_ADAPT_UNSUPPORT_FORMAT);
ret = HITLS_CERT_SELF_ADAPT_UNSUPPORT_FORMAT;
break;
}
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return NULL;
}
return cert;
}
#endif
HITLS_CERT_Chain *HITLS_X509_Adapt_BundleCertParse(HITLS_Lib_Ctx *libCtx, const char *attrName, const uint8_t *buf,
uint32_t len, HITLS_ParseType type, const char *format)
{
BSL_Buffer encodedCert = { NULL, 0 };
int ret;
HITLS_X509_List *certlist = NULL;
switch (type) {
case TLS_PARSE_TYPE_FILE:
ret = HITLS_X509_ProviderCertParseBundleFile(libCtx, attrName, format, (const char *)buf, &certlist);
break;
case TLS_PARSE_TYPE_BUFF:
encodedCert.data = (uint8_t *)(uintptr_t)buf;
encodedCert.dataLen = len;
ret = HITLS_X509_ProviderCertParseBundleBuff(libCtx, attrName, format, &encodedCert, &certlist);
break;
default:
BSL_ERR_PUSH_ERROR(HITLS_CERT_SELF_ADAPT_UNSUPPORT_FORMAT);
return NULL;
}
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return NULL;
}
return certlist;
}
void HITLS_X509_Adapt_CertFree(HITLS_CERT_X509 *cert)
{
HITLS_X509_CertFree(cert);
}
HITLS_CERT_X509 *HITLS_X509_Adapt_CertRef(HITLS_CERT_X509 *cert)
{
int ref = 0;
int ret = HITLS_X509_CertCtrl(cert, HITLS_X509_REF_UP, (void *)&ref, (int32_t)sizeof(int));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return NULL;
}
return cert;
}
HITLS_CERT_X509 *HITLS_X509_Adapt_CertDup(HITLS_CERT_X509 *cert)
{
return HITLS_X509_Adapt_CertRef(cert);
}
static HITLS_SignHashAlgo BslCid2SignHashAlgo(HITLS_Config *config, BslCid signAlgId, BslCid hashAlgId)
{
uint32_t size = 0;
const TLS_SigSchemeInfo *sigSchemeInfoList = ConfigGetSignatureSchemeInfoList(config, &size);
for (size_t i = 0; i < size; i++) {
if (sigSchemeInfoList[i].signHashAlgId == (int32_t)signAlgId &&
sigSchemeInfoList[i].hashAlgId == (int32_t)hashAlgId) {
return sigSchemeInfoList[i].signatureScheme;
}
}
return CERT_SIG_SCHEME_UNKNOWN;
}
static int32_t CertCtrlGetSignAlgo(HITLS_Config *config, HITLS_CERT_X509 *cert, HITLS_SignHashAlgo *algSign)
{
BslCid signAlgCid = 0;
BslCid hashCid = 0;
*algSign = CERT_SIG_SCHEME_UNKNOWN;
int32_t ret = HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SIGNALG, &signAlgCid, sizeof(BslCid));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return ret;
}
ret = HITLS_X509_CertCtrl(cert, HITLS_X509_GET_SIGN_MDALG, &hashCid, sizeof(BslCid));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return ret;
}
*algSign = BslCid2SignHashAlgo(config, signAlgCid, hashCid);
return HITLS_SUCCESS;
}
#if defined(HITLS_TLS_PROTO_TLCP11) || defined(HITLS_TLS_CONFIG_KEY_USAGE)
static int32_t CertCheckKeyUsage(HITLS_Config *config, HITLS_CERT_X509 *cert, uint32_t inKeyUsage, bool *res)
{
(void)config;
uint32_t keyUsage = 0;
int32_t ret = HITLS_X509_CertCtrl(cert, HITLS_X509_EXT_GET_KUSAGE, &keyUsage, sizeof(uint32_t));
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
return ret;
}
if (keyUsage == HITLS_X509_EXT_KU_NONE) {
#ifdef HITLS_TLS_PROTO_TLCP11
// Key usage must be present, otherwise the chain is broken.
if (config == NULL) {
return HITLS_INVALID_INPUT;
}
if (config->maxVersion == HITLS_VERSION_TLCP_DTLCP11) {
BSL_ERR_PUSH_ERROR(HITLS_CERT_ERR_NO_KEYUSAGE);
return HITLS_CERT_ERR_NO_KEYUSAGE;
}
#endif
*res = true;
return HITLS_SUCCESS;
}
*res = (keyUsage & inKeyUsage) != 0;
return HITLS_SUCCESS;
}
#endif
int32_t HITLS_X509_Adapt_CertCtrl(HITLS_Config *config, HITLS_CERT_X509 *cert, HITLS_CERT_CtrlCmd cmd,
void *input, void *output)
{
(void)input;
int32_t ret = HITLS_SUCCESS;
switch (cmd) {
case CERT_CTRL_GET_ENCODE_LEN:
ret = HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ENCODELEN, output, (uint32_t)sizeof(int32_t));
break;
case CERT_CTRL_GET_PUB_KEY:
ret = HITLS_X509_CertCtrl(cert, HITLS_X509_GET_PUBKEY, output, (uint32_t)sizeof(CRYPT_EAL_PkeyPub *));
break;
case CERT_CTRL_GET_SIGN_ALGO:
return CertCtrlGetSignAlgo(config, cert, (HITLS_SignHashAlgo *)output);
#if defined(HITLS_TLS_PROTO_TLCP11) || defined(HITLS_TLS_CONFIG_KEY_USAGE)
case CERT_KEY_CTRL_IS_KEYENC_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_KEY_ENCIPHERMENT, (bool *)output);
case CERT_KEY_CTRL_IS_DIGITAL_SIGN_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_DIGITAL_SIGN, (bool *)output);
case CERT_KEY_CTRL_IS_KEY_CERT_SIGN_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_KEY_CERT_SIGN, (bool *)output);
case CERT_KEY_CTRL_IS_KEY_AGREEMENT_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_KEY_AGREEMENT, (bool *)output);
case CERT_KEY_CTRL_IS_DATA_ENC_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_DATA_ENCIPHERMENT, (bool *)output);
case CERT_KEY_CTRL_IS_NON_REPUDIATION_USAGE:
return CertCheckKeyUsage(config, cert, HITLS_X509_EXT_KU_NON_REPUDIATION, (bool *)output);
#endif
case CERT_CTRL_GET_ENCODE_SUBJECT_DN:
ret = HITLS_X509_CertCtrl(cert, HITLS_X509_GET_ENCODE_SUBJECT_DN, output, sizeof(BSL_Buffer *));
break;
case CERT_CTRL_IS_SELF_SIGNED:
return HITLS_X509_CertCtrl(cert, HITLS_X509_IS_SELF_SIGNED, (bool *)output, (uint32_t)sizeof(bool));
default:
BSL_ERR_PUSH_ERROR(HITLS_CERT_SELF_ADAPT_ERR);
return HITLS_CERT_SELF_ADAPT_ERR;
}
if (ret != HITLS_SUCCESS) {
BSL_ERR_PUSH_ERROR(ret);
}
return ret;
}
#endif /* defined(HITLS_TLS_CALLBACK_CERT) || defined(HITLS_TLS_FEATURE_PROVIDER) */
| 2302_82127028/openHiTLS-examples_1508 | tls/cert/hitls_x509_adapt/hitls_x509_cert_magr.c | C | unknown | 9,604 |