1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <string>
18 #include "securec.h"
19 #include "cf_blob.h"
20 #include "cf_object_base.h"
21 #include "x509_csr.h"
22 #include "x509_certificate.h"
23 #include "x509_distinguished_name.h"
24 #include "crypto_x509_test_common.h"
25
26 using namespace std;
27 using namespace testing::ext;
28
29 namespace {
30 class X509CertificateGenCsrTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
38 static HcfX509Certificate *g_x509CertObj = nullptr;
39 static HcfX509DistinguishedName *g_x509Name = nullptr;
40
41 static string g_rsaPrikey = "-----BEGIN RSA PRIVATE KEY-----\r\n"
42 "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\r\n"
43 "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\r\n"
44 "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\r\n"
45 "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\r\n"
46 "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\r\n"
47 "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\r\n"
48 "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\r\n"
49 "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\r\n"
50 "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\r\n"
51 "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\r\n"
52 "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\r\n"
53 "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\r\n"
54 "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\r\n"
55 "-----END RSA PRIVATE KEY-----\r\n";
56
57 static string g_rsaPrikeyWithPass = "-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n"
58 "MIIBvTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIuyVgAc9+xrMCAggA\r\n"
59 "MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAECBBCF/4JkSXBKtMqZvVgWb1o1BIIB\r\n"
60 "YH7RtuVy5b+NjVW/8zfoWSeUxs5OB2HcAboiepwYZOyTyZyHCafUpQhMVuz6GaN/\r\n"
61 "JIVIrMFGRP2MdmILeASna7+IFoFPxcMU7rhQ5xpyo3oCOhFWBXZ2yanMDV8WiDCf\r\n"
62 "kPPtMXHrDoiwxHoKkrTlDMrfPwh5/K4xpyFOVqFtfsUS8rfsUQyHHt5NzsqgwKRh\r\n"
63 "y1DmMUSlz/ncLJdDhVFFKIUoZuaPDuw3g1YZpM2LWnc5AFqM3wYDx0AgNqG8Wckg\r\n"
64 "dZTweKY7rxYWfDkTt+KoUz16FFxjhunoBmC29Hv/vcxPPbYg8c5qd683AghTdND0\r\n"
65 "OKWK0yX19IJjcJy0TDpp0y3XAxXWKm5T/vLybv620iMdtxmGYVK5Wk/mZnbrb2d4\r\n"
66 "2eIcfx+TgOnIYNn4uSD/MuM5jmSKb7McsTAW9TTT7xBZWKxKcFZFw2/wpleP1jf3\r\n"
67 "Evuf/snUUCFMXLqtzFHTug8=\r\n"
68 "-----END ENCRYPTED PRIVATE KEY-----\r\n";
69 static string g_rsaPrikeyInvalid = "-----BEGIN RSA PRIVATE KEY-----\n"
70 "InvalidKeyContent\n"
71 "-----END RSA PRIVATE KEY-----\n";
72
SetUpTestCase()73 void X509CertificateGenCsrTest::SetUpTestCase()
74 {
75 HcfX509Certificate *x509CertObj = nullptr;
76 CfEncodingBlob inStream = { 0 };
77 inStream.data = reinterpret_cast<uint8_t *>(const_cast<char *>(g_testSelfSignedCaCert));
78 inStream.encodingFormat = CF_FORMAT_PEM;
79 inStream.len = strlen(g_testSelfSignedCaCert) + 1;
80 (void)HcfX509CertificateCreate(&inStream, &x509CertObj);
81 ASSERT_NE(x509CertObj, nullptr);
82 g_x509CertObj = x509CertObj;
83
84 CfBlob out = { 0 };
85 CfResult ret = g_x509CertObj->getIssuerName(g_x509CertObj, &out);
86 EXPECT_EQ(ret, CF_SUCCESS);
87 EXPECT_NE(out.data, nullptr);
88
89 HcfX509DistinguishedName *x509Name = nullptr;
90 ret = HcfX509DistinguishedNameCreate(&out, true, &x509Name);
91 EXPECT_EQ(ret, CF_SUCCESS);
92 EXPECT_NE(x509Name, nullptr);
93 g_x509Name = x509Name;
94 CfBlobDataClearAndFree(&out);
95 }
TearDownTestCase()96 void X509CertificateGenCsrTest::TearDownTestCase()
97 {
98 CfObjDestroy(g_x509Name);
99 CfObjDestroy(g_x509CertObj);
100 }
SetUp()101 void X509CertificateGenCsrTest::SetUp() {}
TearDown()102 void X509CertificateGenCsrTest::TearDown() {}
103
104
105 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest001, TestSize.Level0)
106 {
107 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
108 char mdname[] = "SHA256";
109 char attributeName[] = "challengePassword";
110 char attributeValue[] = "test123456";
111
112 csrConf->subject = g_x509Name;
113 csrConf->isPem = true;
114 csrConf->mdName = reinterpret_cast<char *>(mdname);
115
116 HcfAttributesArray *attributeArray = (HcfAttributesArray *)CfMalloc(sizeof(HcfAttributesArray), 0);
117 attributeArray->array = (HcfAttributes *)CfMalloc(sizeof(HcfAttributes), 0);
118 attributeArray->attributeSize = 1;
119 attributeArray->array->attributeName = reinterpret_cast<char *>(attributeName);
120 attributeArray->array->attributeValue = reinterpret_cast<char *>(attributeValue);
121 csrConf->attribute.array = attributeArray->array;
122 csrConf->attribute.attributeSize = attributeArray->attributeSize;
123
124 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
125 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
126 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikey.length() + 1));
127 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
128 if (privateKey->privateKey->data != nullptr) {
129 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikey.length() + 1,
130 g_rsaPrikey.c_str(), g_rsaPrikey.length() + 1);
131 privateKey->privateKey->len = g_rsaPrikey.length() + 1;
132 }
133
134 CfBlob csrBlob = { 0 };
135 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_SUCCESS);
136 EXPECT_NE(csrBlob.data, nullptr);
137
138 char* csrString = reinterpret_cast<char*>(csrBlob.data);
139 EXPECT_TRUE(strstr(csrString, "BEGIN CERTIFICATE REQUEST") != nullptr);
140 EXPECT_TRUE(strstr(csrString, "END CERTIFICATE REQUEST") != nullptr);
141
142 CfFree(attributeArray->array);
143 CfFree(attributeArray);
144 CfFree(privateKey->privateKey->data);
145 CfFree(privateKey->privateKey);
146 CfFree(privateKey);
147 CfFree(csrConf);
148 CfBlobDataFree(&csrBlob);
149 }
150
151 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest002, TestSize.Level0)
152 {
153 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
154 csrConf->subject = g_x509Name;
155 csrConf->isPem = true;
156
157 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
158 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
159 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikey.length() + 1));
160 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
161 if (privateKey->privateKey->data != nullptr) {
162 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikey.length() + 1,
163 g_rsaPrikey.c_str(), g_rsaPrikey.length() + 1);
164 privateKey->privateKey->len = g_rsaPrikey.length() + 1;
165 }
166
167 CfBlob csrBlob = { 0 };
168 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_ERR_CRYPTO_OPERATION);
169
170 CfFree(privateKey->privateKey->data);
171 CfFree(privateKey->privateKey);
172 CfFree(privateKey);
173 CfFree(csrConf);
174 CfBlobDataFree(&csrBlob);
175 }
176
177 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest003, TestSize.Level0)
178 {
179 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
180 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
181 CfBlob csrBlob = { 0 };
182 EXPECT_EQ(HcfX509CertificateGenCsr(nullptr, csrConf, &csrBlob), CF_INVALID_PARAMS);
183 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, nullptr, &csrBlob), CF_INVALID_PARAMS);
184 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, nullptr), CF_INVALID_PARAMS);
185
186 CfFree(privateKey);
187 CfFree(csrConf);
188 }
189
190 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest004, TestSize.Level0)
191 {
192 const char* digestAlgorithms[] = {"SHA256", "SHA384", "SHA512"};
193
194 for (const auto& digest : digestAlgorithms) {
195 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
196 csrConf->subject = g_x509Name;
197 csrConf->isPem = true;
198 csrConf->mdName = const_cast<char*>(digest);
199
200 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
201 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
202 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikey.length() + 1));
203 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
204 if (privateKey->privateKey->data != nullptr) {
205 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikey.length() + 1,
206 g_rsaPrikey.c_str(), g_rsaPrikey.length() + 1);
207 privateKey->privateKey->len = g_rsaPrikey.length() + 1;
208 }
209
210 CfBlob csrBlob = { 0 };
211 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_SUCCESS);
212
213 CfFree(privateKey->privateKey->data);
214 CfFree(privateKey->privateKey);
215 CfFree(privateKey);
216 CfFree(csrConf);
217 CfBlobDataFree(&csrBlob);
218 }
219 }
220 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest005, TestSize.Level0)
221 {
222 struct TestAttribute {
223 const char* name;
224 const char* value;
225 };
226
227 const TestAttribute testAttributes[] = {
228 {"challengePassword", "test123456"},
229 {"unstructuredName", "TestUnstructuredName"},
230 {"emailAddress", "test@example.com"},
231 {"subjectAltName", "DNS:test.example.com"}
232 };
233 const size_t attributeCount = sizeof(testAttributes) / sizeof(TestAttribute);
234
235 for (size_t i = 0; i < attributeCount; i++) {
236 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
237 char mdname[] = "SHA256";
238
239 csrConf->subject = g_x509Name;
240 csrConf->isPem = true;
241 csrConf->mdName = reinterpret_cast<char *>(mdname);
242
243
244 HcfAttributesArray *attributeArray = (HcfAttributesArray *)CfMalloc(sizeof(HcfAttributesArray), 0);
245 attributeArray->array = (HcfAttributes *)CfMalloc(sizeof(HcfAttributes), 0);
246 attributeArray->attributeSize = 1;
247 attributeArray->array->attributeName = const_cast<char*>(testAttributes[i].name);
248 attributeArray->array->attributeValue = const_cast<char*>(testAttributes[i].value);
249 csrConf->attribute.array = attributeArray->array;
250 csrConf->attribute.attributeSize = attributeArray->attributeSize;
251
252
253 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
254 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
255 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikey.length() + 1));
256 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
257 if (privateKey->privateKey->data != nullptr) {
258 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikey.length() + 1,
259 g_rsaPrikey.c_str(), g_rsaPrikey.length() + 1);
260 privateKey->privateKey->len = g_rsaPrikey.length() + 1;
261 }
262
263 CfBlob csrBlob = { 0 };
264 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_SUCCESS);
265 EXPECT_NE(csrBlob.data, nullptr);
266
267 char* csrString = reinterpret_cast<char*>(csrBlob.data);
268 EXPECT_TRUE(strstr(csrString, "BEGIN CERTIFICATE REQUEST") != nullptr);
269 EXPECT_TRUE(strstr(csrString, "END CERTIFICATE REQUEST") != nullptr);
270
271
272 CfFree(attributeArray->array);
273 CfFree(attributeArray);
274 CfFree(privateKey->privateKey->data);
275 CfFree(privateKey->privateKey);
276 CfFree(privateKey);
277 CfFree(csrConf);
278 CfBlobDataFree(&csrBlob);
279 }
280 }
281
282
283 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest006, TestSize.Level0)
284 {
285 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
286 char mdname[] = "SHA256";
287
288 csrConf->subject = g_x509Name;
289 csrConf->isPem = true;
290 csrConf->mdName = reinterpret_cast<char *>(mdname);
291
292
293 HcfAttributesArray *attributeArray = (HcfAttributesArray *)CfMalloc(sizeof(HcfAttributesArray), 0);
294 attributeArray->array = new HcfAttributes[3];
295 attributeArray->attributeSize = 3;
296
297
298 attributeArray->array[0].attributeName = const_cast<char*>("challengePassword");
299 attributeArray->array[0].attributeValue = const_cast<char*>("test123456");
300
301
302 attributeArray->array[1].attributeName = const_cast<char*>("unstructuredName");
303 attributeArray->array[1].attributeValue = const_cast<char*>("TestUnstructuredName");
304
305
306 attributeArray->array[2].attributeName = const_cast<char*>("emailAddress");
307 attributeArray->array[2].attributeValue = const_cast<char*>("test@example.com");
308
309 csrConf->attribute.array = attributeArray->array;
310 csrConf->attribute.attributeSize = attributeArray->attributeSize;
311
312
313 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
314 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
315 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikey.length() + 1));
316 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
317 if (privateKey->privateKey->data != nullptr) {
318 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikey.length() + 1,
319 g_rsaPrikey.c_str(), g_rsaPrikey.length() + 1);
320 privateKey->privateKey->len = g_rsaPrikey.length() + 1;
321 }
322
323 CfBlob csrBlob = { 0 };
324 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_SUCCESS);
325 EXPECT_NE(csrBlob.data, nullptr);
326
327
328 char* csrString = reinterpret_cast<char*>(csrBlob.data);
329 EXPECT_TRUE(strstr(csrString, "BEGIN CERTIFICATE REQUEST") != nullptr);
330 EXPECT_TRUE(strstr(csrString, "END CERTIFICATE REQUEST") != nullptr);
331
332
333 delete[] attributeArray->array;
334 CfFree(attributeArray);
335 CfFree(privateKey->privateKey->data);
336 CfFree(privateKey->privateKey);
337 CfFree(privateKey);
338 CfFree(csrConf);
339 CfBlobDataFree(&csrBlob);
340 }
341
342 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest007, TestSize.Level0)
343 {
344 struct TestCase {
345 const string& privateKey;
346 const char* password;
347 CfResult expectedResult;
348 };
349
350 TestCase testCases[] = {
351 {g_rsaPrikey, nullptr, CF_ERR_CRYPTO_OPERATION},
352 {g_rsaPrikeyWithPass, "password123", CF_ERR_CRYPTO_OPERATION},
353 {g_rsaPrikeyInvalid, nullptr, CF_ERR_CRYPTO_OPERATION}
354 };
355
356 for (const auto& testCase : testCases) {
357 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
358 csrConf->subject = g_x509Name;
359 csrConf->isPem = true;
360
361 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
362 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
363 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(testCase.privateKey.length() + 1));
364 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
365 if (privateKey->privateKey->data != nullptr) {
366 (void)memcpy_s(privateKey->privateKey->data, testCase.privateKey.length() + 1,
367 testCase.privateKey.c_str(), testCase.privateKey.length() + 1);
368 privateKey->privateKey->len = testCase.privateKey.length() + 1;
369 }
370
371 if (testCase.password != nullptr) {
372 privateKey->privateKeyPassword = const_cast<char*>(testCase.password);
373 }
374
375 CfBlob csrBlob = { 0 };
376 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), testCase.expectedResult);
377
378 if (testCase.expectedResult == CF_SUCCESS) {
379 EXPECT_NE(csrBlob.data, nullptr);
380 char* csrString = reinterpret_cast<char*>(csrBlob.data);
381 EXPECT_TRUE(strstr(csrString, "BEGIN CERTIFICATE REQUEST") != nullptr);
382 }
383
384 CfFree(privateKey->privateKey->data);
385 CfFree(privateKey->privateKey);
386 CfFree(privateKey);
387 CfFree(csrConf);
388 CfBlobDataFree(&csrBlob);
389 }
390 }
391
392 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest008, TestSize.Level0)
393 {
394 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
395 csrConf->subject = g_x509Name;
396 csrConf->isPem = true;
397
398 struct TestCase {
399 const char* password;
400 CfResult expectedResult;
401 };
402
403 TestCase testCases[] = {
404 {"password123", CF_ERR_CRYPTO_OPERATION},
405 {"wrongpassword", CF_ERR_CRYPTO_OPERATION},
406 {nullptr, CF_ERR_CRYPTO_OPERATION},
407 {"", CF_ERR_CRYPTO_OPERATION}
408 };
409
410 for (const auto& testCase : testCases) {
411 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
412 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
413 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikeyWithPass.length() + 1));
414 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
415 if (privateKey->privateKey->data != nullptr) {
416 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikeyWithPass.length() + 1,
417 g_rsaPrikeyWithPass.c_str(), g_rsaPrikeyWithPass.length() + 1);
418 privateKey->privateKey->len = g_rsaPrikeyWithPass.length() + 1;
419 }
420
421 privateKey->privateKeyPassword = const_cast<char*>(testCase.password);
422
423 CfBlob csrBlob = { 0 };
424 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), testCase.expectedResult);
425
426 CfFree(privateKey->privateKey->data);
427 CfFree(privateKey->privateKey);
428 CfFree(privateKey);
429 CfBlobDataFree(&csrBlob);
430 }
431
432 CfFree(csrConf);
433 }
434
435 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest009, TestSize.Level0)
436 {
437 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
438 csrConf->subject = g_x509Name;
439 csrConf->isPem = true;
440 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
441 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
442 privateKey->privateKey->data = nullptr;
443 privateKey->privateKey->len = 0;
444 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
445
446 CfBlob csrBlob = { 0 };
447 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_ERR_CRYPTO_OPERATION);
448
449 CfFree(privateKey->privateKey->data);
450 CfFree(privateKey->privateKey);
451 CfFree(privateKey);
452 CfBlobDataFree(&csrBlob);
453
454 CfFree(csrConf);
455 }
456
457 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest010, TestSize.Level0)
458 {
459 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
460 csrConf->subject = g_x509Name;
461 csrConf->isPem = true;
462
463 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
464 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
465 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(1));
466 privateKey->privateKey->len = 0;
467 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
468
469 CfBlob csrBlob = { 0 };
470 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_ERR_CRYPTO_OPERATION);
471
472 CfFree(privateKey->privateKey->data);
473 CfFree(privateKey->privateKey);
474 CfFree(privateKey);
475 CfFree(csrConf);
476 CfBlobDataFree(&csrBlob);
477 }
478
479 HWTEST_F(X509CertificateGenCsrTest, X509CsrTest011, TestSize.Level0)
480 {
481 HcfGenCsrConf *csrConf = (HcfGenCsrConf *)CfMalloc(sizeof(HcfGenCsrConf), 0);
482 csrConf->subject = g_x509Name;
483 csrConf->isPem = true;
484
485 PrivateKeyInfo *privateKey = (PrivateKeyInfo *)CfMalloc(sizeof(PrivateKeyInfo), 0);
486 privateKey->privateKey = (CfEncodingBlob *)CfMalloc(sizeof(CfEncodingBlob), 0);
487 privateKey->privateKey->data = static_cast<uint8_t *>(malloc(g_rsaPrikeyWithPass.length() + 1));
488 privateKey->privateKey->encodingFormat = CF_FORMAT_PEM;
489 if (privateKey->privateKey->data != nullptr) {
490 (void)memcpy_s(privateKey->privateKey->data, g_rsaPrikeyWithPass.length() + 1,
491 g_rsaPrikeyWithPass.c_str(), g_rsaPrikeyWithPass.length() + 1);
492 privateKey->privateKey->len = g_rsaPrikeyWithPass.length() + 1;
493 }
494
495 string longPassword(1024, 'a');
496 privateKey->privateKeyPassword = const_cast<char*>(longPassword.c_str());
497
498 CfBlob csrBlob = { 0 };
499 EXPECT_EQ(HcfX509CertificateGenCsr(privateKey, csrConf, &csrBlob), CF_ERR_CRYPTO_OPERATION);
500
501 CfFree(privateKey->privateKey->data);
502 CfFree(privateKey->privateKey);
503 CfFree(privateKey);
504 CfFree(csrConf);
505 CfBlobDataFree(&csrBlob);
506 }
507
508 }