• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 /* BEGIN_HEADER */
17 
18 #include "bsl_sal.h"
19 #include "securec.h"
20 #include "bsl_types.h"
21 #include "bsl_log.h"
22 #include "sal_file.h"
23 #include "bsl_init.h"
24 #include "crypt_encode_decode_key.h"
25 #include "crypt_eal_codecs.h"
26 #include "crypt_eal_rand.h"
27 #include "crypt_errno.h"
28 #include "hitls_cms_local.h"
29 #include "hitls_pki_errno.h"
30 
31 /* END_HEADER */
32 
33 /**
34  * For test parse p7-encryptData of wrong conditions.
35 */
36 /* BEGIN_CASE */
SDV_CMS_PARSE_ENCRYPTEDDATA_TC001(Hex * buff)37 void SDV_CMS_PARSE_ENCRYPTEDDATA_TC001(Hex *buff)
38 {
39     BSL_Buffer output = {0};
40     char *pwd = "123456";
41     uint32_t pwdlen = strlen(pwd);
42 
43     int32_t ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, &output);
44     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
45     BSL_SAL_Free(output.data);
46     output.data = NULL;
47 
48     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, NULL, (const uint8_t *)pwd, pwdlen, &output);
49     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
50 
51     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, NULL, pwdlen, &output);
52     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
53 
54     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen, NULL);
55     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
56 
57     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, 8192, &output);
58     ASSERT_EQ(ret, CRYPT_INVALID_ARG);
59 
60     char *pwd1 = "123456@123";
61     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd1, strlen(pwd1),
62         &output);
63     ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
64 
65     char *pwd2 = "";
66     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd2, strlen(pwd2),
67         &output);
68     ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
69 
70     (void)memset_s(buff->x + buff->len - 20, 16, 0, 16); // modify the ciphertext, 16 and 20 are random number.
71     ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
72         &output);
73     ASSERT_EQ(ret, CRYPT_EAL_CIPHER_DATA_ERROR);
74 EXIT:
75     return;
76 }
77 /* END_CASE */
78 
79 /**
80  * For test parse p7-encryptData of right conditions.
81 */
82 /* BEGIN_CASE */
SDV_CMS_PARSE_ENCRYPTEDDATA_TC002(Hex * buff)83 void SDV_CMS_PARSE_ENCRYPTEDDATA_TC002(Hex *buff)
84 {
85     BSL_Buffer output = {0};
86     char *pwd = "123456";
87     uint32_t pwdlen = strlen(pwd);
88     int32_t ret =  CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, (BSL_Buffer *)buff, (const uint8_t *)pwd, pwdlen,
89         &output);
90     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
91 EXIT:
92     BSL_SAL_Free(output.data);
93     return;
94 }
95 /* END_CASE */
96 
97 /**
98  * For test parse p7-DigestInfo of wrong conditions.
99 */
100 /* BEGIN_CASE */
SDV_CMS_PARSE_DIGESTINFO_TC001(Hex * buff,int alg,Hex * digest)101 void SDV_CMS_PARSE_DIGESTINFO_TC001(Hex *buff, int alg, Hex *digest)
102 {
103     BSL_Buffer output = {0};
104     BslCid cid = BSL_CID_UNKNOWN;
105     int32_t ret = HITLS_CMS_ParseDigestInfo(NULL, &cid, &output);
106     ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
107 
108     ret = HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, NULL);
109     ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
110 
111     ret = HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, &output);
112     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
113 
114     ASSERT_EQ(alg, cid);
115     ASSERT_EQ(memcmp(output.data, digest->x, digest->len), 0);
116 EXIT:
117     BSL_SAL_Free(output.data);
118     return;
119 }
120 /* END_CASE */
121 
122 /**
123  * For test parse p7-DigestInfo of right conditions.
124 */
125 /* BEGIN_CASE */
SDV_CMS_PARSE_DIGESTINFO_TC002(Hex * buff,int alg,Hex * digest)126 void SDV_CMS_PARSE_DIGESTINFO_TC002(Hex *buff, int alg, Hex *digest)
127 {
128     BSL_Buffer output = {0};
129     BslCid cid = BSL_CID_UNKNOWN;
130     int32_t ret =  HITLS_CMS_ParseDigestInfo((BSL_Buffer *)buff, &cid, &output);
131     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
132     ASSERT_EQ(alg, cid);
133     ASSERT_EQ(memcmp(output.data, digest->x, digest->len), 0);
134 EXIT:
135     BSL_SAL_Free(output.data);
136     return;
137 }
138 /* END_CASE */
139 
140 /**
141  * For test encode p7-encryptData.
142 */
143 /* BEGIN_CASE */
SDV_CMS_ENCODE_ENCRYPTEDDATA_TC001(Hex * buff)144 void SDV_CMS_ENCODE_ENCRYPTEDDATA_TC001(Hex *buff)
145 {
146     BSL_Buffer data = {buff->x, buff->len};
147     BSL_Buffer output = {0};
148     BSL_Buffer verify = {0};
149     char *pwd = "123456";
150     CRYPT_Pbkdf2Param param = {0};
151     param.pbesId = BSL_CID_PBES2;
152     param.pbkdfId = BSL_CID_PBKDF2;
153     param.hmacId = CRYPT_MAC_HMAC_SHA256;
154     param.symId = CRYPT_CIPHER_AES256_CBC;
155     param.pwd = (uint8_t *)pwd;
156     param.pwdLen = strlen(pwd);
157     param.saltLen = 16;
158     param.itCnt = 2048;
159     CRYPT_EncodeParam paramEx = {CRYPT_DERIVE_PBKDF2, &param};
160 
161     ASSERT_EQ(TestRandInit(), HITLS_PKI_SUCCESS);
162     int32_t ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, NULL, NULL, NULL);
163     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
164     ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, NULL, NULL);
165     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
166     ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, &paramEx, NULL);
167     ASSERT_EQ(ret, CRYPT_NULL_INPUT);
168 
169     param.hmacId = CRYPT_MAC_MAX;
170     ret =  CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, &paramEx, &output);
171     ASSERT_EQ(ret, CRYPT_ERR_ALGID);
172     param.hmacId = CRYPT_MAC_HMAC_SHA256;
173     ret = CRYPT_EAL_EncodePKCS7EncryptDataBuff(NULL, NULL, &data, &paramEx, &output);
174     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
175     ret = CRYPT_EAL_ParseAsn1PKCS7EncryptedData(NULL, NULL, &output, (const uint8_t *)pwd, strlen(pwd), &verify);
176     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
177     ASSERT_COMPARE("encode p7-encryptData", data.data, data.dataLen, verify.data, verify.dataLen);
178 EXIT:
179     TestRandDeInit();
180     BSL_SAL_FREE(verify.data);
181     BSL_SAL_FREE(output.data);
182     return;
183 }
184 /* END_CASE */
185 
186 /**
187  * For test encode p7-DigestInfo.
188 */
189 /* BEGIN_CASE */
SDV_CMS_ENCODE_DIGESTINFO_TC001()190 void SDV_CMS_ENCODE_DIGESTINFO_TC001()
191 {
192     BSL_Buffer input = {0};
193     BSL_Buffer output = {0};
194     BslCid cid = 0;
195     BSL_Buffer digest = {0};
196     int32_t ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, NULL, NULL);
197     ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
198     ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, NULL);
199     ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
200     input.dataLen = 1;
201     ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
202     ASSERT_EQ(ret, HITLS_CMS_ERR_NULL_POINTER);
203     input.dataLen = 0;
204     ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
205     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
206     ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
207     ASSERT_EQ(ret, HITLS_CMS_ERR_INVALID_DATA);
208     BSL_SAL_FREE(output.data);
209     input.data = (uint8_t *)"123456";
210     input.dataLen = 6;
211     ret = HITLS_CMS_EncodeDigestInfoBuff(BSL_CID_MD5, &input, &output);
212     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
213     ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
214     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
215     ASSERT_EQ(cid, BSL_CID_MD5);
216 EXIT:
217     BSL_SAL_FREE(digest.data);
218     BSL_SAL_FREE(output.data);
219     return;
220 }
221 /* END_CASE */
222 
223 /**
224  * For test encode p7-DigestInfo vector.
225 */
226 /* BEGIN_CASE */
SDV_CMS_ENCODE_DIGESTINFO_TC002(int algid,Hex * in)227 void SDV_CMS_ENCODE_DIGESTINFO_TC002(int algid, Hex *in)
228 {
229     BSL_Buffer input = {in->x, in->len};
230     BSL_Buffer output = {0};
231     BslCid cid = 0;
232     BSL_Buffer digest = {0};
233     int32_t ret = HITLS_CMS_EncodeDigestInfoBuff(algid, &input, &output);
234     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
235     ret = HITLS_CMS_ParseDigestInfo(&output, &cid, &digest);
236     ASSERT_EQ(ret, HITLS_PKI_SUCCESS);
237     ASSERT_EQ(cid, algid);
238 EXIT:
239     BSL_SAL_FREE(digest.data);
240     BSL_SAL_FREE(output.data);
241     return;
242 }
243 /* END_CASE */
244