|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Implementation of negative step classes for PKCS#10 tests. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "tpkcs10negatives.h" |
|
21 #include <x500dn.h> |
|
22 #include <unifiedkeystore.h> |
|
23 |
|
24 // Key label |
|
25 _LIT(KKeyLabel, "new pkcs10 neg test key"); |
|
26 |
|
27 // Panics |
|
28 _LIT(KPanicNegativeTests, "PKCS10NegativeTests"); |
|
29 TInt EPanicNegInvalidTestData = 1; |
|
30 |
|
31 // String of 256 characters |
|
32 _LIT8(KHugeString, "p000000001p000000002p000000003p000000004p000000005p000000006p000000007" |
|
33 "p000000008p000000009p000000010p000000011p000000012p000000013p000000014" |
|
34 "p000000015p000000016p000000017p000000018p000000019p000000020p000000021" |
|
35 "p000000022p000000023p000000024p000000025p23456"); |
|
36 |
|
37 |
|
38 CPKCS10NegTesterActive::CPKCS10NegTesterActive(CTestExecuteLogger& aLogger) : |
|
39 CActive(EPriorityStandard), |
|
40 iLogger(aLogger), |
|
41 iKeyImportStarted(EFalse) |
|
42 { |
|
43 CActiveScheduler::Add(this); |
|
44 User::LeaveIfError(iFs.Connect()); |
|
45 } |
|
46 |
|
47 CPKCS10NegTesterActive::~CPKCS10NegTesterActive() |
|
48 { |
|
49 Cancel(); |
|
50 delete iKeyData; |
|
51 if (iKeyInfo) |
|
52 { |
|
53 iKeyInfo->Release(); |
|
54 } |
|
55 delete iKeyStore; |
|
56 iFs.Close(); |
|
57 } |
|
58 |
|
59 CCTKeyInfo* CPKCS10NegTesterActive::doImportKeyL(CCertificateRequestStep *aStep) |
|
60 { |
|
61 INFO_PRINTF1(_L("Active object tester for Key Import")); |
|
62 |
|
63 iStep = aStep; |
|
64 // Initialise Key store |
|
65 iKeyStore = CUnifiedKeyStore::NewL(iFs); |
|
66 iKeyStore->Initialize(iStatus); |
|
67 |
|
68 SetActive(); |
|
69 CActiveScheduler::Start(); |
|
70 |
|
71 User::LeaveIfError(iStatus.Int()); |
|
72 |
|
73 return iKeyInfo; |
|
74 } |
|
75 |
|
76 void CPKCS10NegTesterActive::RunL() |
|
77 { |
|
78 iError = iStatus.Int(); |
|
79 if (iError != KErrNone) |
|
80 { |
|
81 CActiveScheduler::Stop(); |
|
82 } |
|
83 |
|
84 if (iKeyImportStarted == EFalse) |
|
85 { |
|
86 INFO_PRINTF1(_L("Importing keys")); |
|
87 TFileName filename; |
|
88 filename = iStep->iPrivateKey; |
|
89 RFile file; |
|
90 User::LeaveIfError(file.Open(iFs, filename, EFileRead)); |
|
91 CleanupClosePushL(file); |
|
92 TInt size; |
|
93 User::LeaveIfError(file.Size(size)); |
|
94 iKeyData = HBufC8::NewMaxL(size); |
|
95 TPtr8 keyPtr = iKeyData->Des(); |
|
96 User::LeaveIfError(file.Read(keyPtr)); |
|
97 CleanupStack::PopAndDestroy(); // file |
|
98 |
|
99 TTime start(0.0); |
|
100 TTime end(0.0); |
|
101 |
|
102 // Assumes only one keystore |
|
103 ASSERT(iKeyInfo == NULL); |
|
104 iKeyStore->ImportKey(0, *iKeyData, EPKCS15UsageSign, KKeyLabel,0, start, end, iKeyInfo, iStatus); |
|
105 iKeyImportStarted = ETrue; |
|
106 SetActive(); |
|
107 } |
|
108 else |
|
109 { |
|
110 // Key import done |
|
111 CActiveScheduler::Stop(); |
|
112 } |
|
113 } |
|
114 |
|
115 TInt CPKCS10NegTesterActive::RunError(TInt aError) |
|
116 { |
|
117 iError = aError; |
|
118 CActiveScheduler::Stop(); |
|
119 return KErrNone; |
|
120 } |
|
121 |
|
122 TVerdict CPKCS10NegPKCS10GenericAttr::doTestStepL() |
|
123 { |
|
124 // Set it to pass initially |
|
125 SetTestStepResult(EPass); |
|
126 |
|
127 ////////////////////////////////////////////////////////////////////////////// |
|
128 // Test for invalid attribute for NewL |
|
129 TInt numAttr = iArrayGenAttrOID.Count(); |
|
130 __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData)); |
|
131 |
|
132 HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length()); |
|
133 string->Des().Copy(iArrayGenAttrValue[0]); |
|
134 |
|
135 CASN1EncOctetString* attrString = CASN1EncOctetString::NewLC(*string); |
|
136 |
|
137 CPKCS10Attribute *genericAttr = NULL; |
|
138 TRAPD(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[0], attrString)); |
|
139 CleanupStack::Pop(attrString); |
|
140 CleanupStack::PopAndDestroy(string); |
|
141 |
|
142 if (err != KErrNone) |
|
143 { |
|
144 delete attrString; |
|
145 } |
|
146 else |
|
147 { |
|
148 delete genericAttr; |
|
149 } |
|
150 |
|
151 if (err != KErrBadDescriptor) |
|
152 { |
|
153 SetTestStepResult(EFail); |
|
154 } |
|
155 |
|
156 ////////////////////////////////////////////////////////////////////////////// |
|
157 // Test for NULL value for NewL |
|
158 __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData)); |
|
159 |
|
160 string = HBufC8::NewMaxLC(iArrayGenAttrValue[1].Length()); |
|
161 string->Des().Copy(iArrayGenAttrValue[1]); |
|
162 |
|
163 if (*string == KNullDesC8) |
|
164 { |
|
165 attrString = NULL; |
|
166 } |
|
167 else |
|
168 { |
|
169 attrString = CASN1EncOctetString::NewLC(*string); |
|
170 } |
|
171 |
|
172 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[1], attrString)); |
|
173 if (attrString != NULL) |
|
174 { |
|
175 CleanupStack::Pop(attrString); |
|
176 } |
|
177 CleanupStack::PopAndDestroy(string); |
|
178 |
|
179 if (err != KErrNone) |
|
180 { |
|
181 delete attrString; |
|
182 } |
|
183 else |
|
184 { |
|
185 delete genericAttr; |
|
186 } |
|
187 |
|
188 if (err != KErrArgument) |
|
189 { |
|
190 SetTestStepResult(EFail); |
|
191 } |
|
192 |
|
193 ////////////////////////////////////////////////////////////////////////////// |
|
194 // Test for NULL value for AddValueL |
|
195 __ASSERT_ALWAYS((numAttr > 2), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData)); |
|
196 string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length()); |
|
197 string->Des().Copy(iArrayGenAttrValue[2]); |
|
198 |
|
199 attrString = CASN1EncOctetString::NewLC(*string); |
|
200 |
|
201 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString)); |
|
202 CleanupStack::Pop(attrString); |
|
203 CleanupStack::PopAndDestroy(string); |
|
204 |
|
205 // Should not be an error |
|
206 if (err != KErrNone) |
|
207 { |
|
208 SetTestStepResult(EFail); |
|
209 } |
|
210 |
|
211 // Try to add a NULL value |
|
212 TRAP(err, genericAttr->AddValueL(NULL)); |
|
213 delete genericAttr; |
|
214 |
|
215 // Should return an error |
|
216 if (err != KErrArgument) |
|
217 { |
|
218 SetTestStepResult(EFail); |
|
219 } |
|
220 |
|
221 ////////////////////////////////////////////////////////////////////////////// |
|
222 // Test for invalid attribute for ResetL |
|
223 string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length()); |
|
224 string->Des().Copy(iArrayGenAttrValue[2]); |
|
225 |
|
226 attrString = CASN1EncOctetString::NewLC(*string); |
|
227 |
|
228 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString)); |
|
229 CleanupStack::Pop(attrString); |
|
230 |
|
231 // Should not be an error |
|
232 if (err != KErrNone) |
|
233 { |
|
234 SetTestStepResult(EFail); |
|
235 } |
|
236 |
|
237 // Try to reset with an invalid attribute OID |
|
238 attrString = CASN1EncOctetString::NewLC(*string); |
|
239 TRAP(err, genericAttr->ResetL(KNullDesC, attrString)); |
|
240 |
|
241 if (err != KErrNone) |
|
242 { |
|
243 delete attrString; |
|
244 } |
|
245 |
|
246 CleanupStack::Pop(attrString); |
|
247 CleanupStack::PopAndDestroy(string); |
|
248 delete genericAttr; |
|
249 |
|
250 // Should return an error |
|
251 if (err != KErrBadDescriptor) |
|
252 { |
|
253 SetTestStepResult(EFail); |
|
254 } |
|
255 |
|
256 ////////////////////////////////////////////////////////////////////////////// |
|
257 // Test for NULL value for ResetL |
|
258 string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length()); |
|
259 string->Des().Copy(iArrayGenAttrValue[2]); |
|
260 |
|
261 attrString = CASN1EncOctetString::NewLC(*string); |
|
262 |
|
263 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString)); |
|
264 CleanupStack::Pop(attrString); |
|
265 CleanupStack::PopAndDestroy(string); |
|
266 |
|
267 // Should not be an error |
|
268 if (err != KErrNone) |
|
269 { |
|
270 SetTestStepResult(EFail); |
|
271 } |
|
272 |
|
273 // Try to reset with a NULL value |
|
274 TRAP(err, genericAttr->ResetL(iArrayGenAttrOID[2], NULL)); |
|
275 delete genericAttr; |
|
276 |
|
277 // Should return an error |
|
278 if (err != KErrArgument) |
|
279 { |
|
280 SetTestStepResult(EFail); |
|
281 } |
|
282 |
|
283 return TestStepResult(); |
|
284 } |
|
285 |
|
286 TVerdict CPKCS10NegPKCS9ChallengePasswordAttr::doTestStepL() |
|
287 { |
|
288 // Set it to pass initially |
|
289 SetTestStepResult(EPass); |
|
290 |
|
291 ////////////////////////////////////////////////////////////////////////////// |
|
292 // Test for empty password for NewL |
|
293 HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length()); |
|
294 passwordString->Des().Copy(iChallengePassword); |
|
295 |
|
296 CPKCS9ChallengePasswordAttr *chPasswordAttr = NULL; |
|
297 TRAPD(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(*passwordString)); |
|
298 CleanupStack::PopAndDestroy(passwordString); |
|
299 |
|
300 if (err != KErrArgument) |
|
301 { |
|
302 SetTestStepResult(EFail); |
|
303 } |
|
304 |
|
305 ////////////////////////////////////////////////////////////////////////////// |
|
306 // Test for big password for ResetL |
|
307 TRAP(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(_L8("password"))); |
|
308 |
|
309 // Should not fail |
|
310 if (err != KErrNone) |
|
311 { |
|
312 SetTestStepResult(EFail); |
|
313 } |
|
314 |
|
315 TRAP(err, chPasswordAttr->ResetL(KHugeString)); |
|
316 delete chPasswordAttr; |
|
317 |
|
318 if (err != KErrArgument) |
|
319 { |
|
320 SetTestStepResult(EFail); |
|
321 } |
|
322 |
|
323 return TestStepResult(); |
|
324 } |
|
325 |
|
326 TVerdict CPKCS10NegPKCS9ExtensionRequestAttr::doTestStepL() |
|
327 { |
|
328 // Set it to pass initially |
|
329 SetTestStepResult(EPass); |
|
330 |
|
331 ////////////////////////////////////////////////////////////////////////////// |
|
332 // Test for empty attribute OID for NewL |
|
333 TInt numAttr = iArrayV3AttrOID.Count(); |
|
334 __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData)); |
|
335 |
|
336 CX509CertExtension* x509CertExtn = CX509CertExtension::NewL(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value")); |
|
337 |
|
338 CPKCS9ExtensionRequestAttr *chExtReqAttr = NULL; |
|
339 TRAPD(err, chExtReqAttr = CPKCS9ExtensionRequestAttr::NewL(*x509CertExtn)); |
|
340 |
|
341 delete x509CertExtn; |
|
342 |
|
343 if (err != KErrBadDescriptor) |
|
344 { |
|
345 SetTestStepResult(EFail); |
|
346 } |
|
347 |
|
348 ////////////////////////////////////////////////////////////////////////////// |
|
349 // Test for empty attribute OID for AddExtensionL |
|
350 __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData)); |
|
351 |
|
352 x509CertExtn = CX509CertExtension::NewLC(iArrayV3AttrOID[1], iArrayV3AttrCritical[1], _L8("value")); |
|
353 chExtReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*x509CertExtn); |
|
354 |
|
355 CX509CertExtension *x509CertExtnNew = CX509CertExtension::NewLC(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value")); |
|
356 TRAP(err, chExtReqAttr->AddExtensionL(*x509CertExtnNew)); |
|
357 |
|
358 CleanupStack::PopAndDestroy(3, x509CertExtn); // chExtReqAttr, x509CertExtnNew |
|
359 |
|
360 if (err != KErrBadDescriptor) |
|
361 { |
|
362 SetTestStepResult(EFail); |
|
363 } |
|
364 |
|
365 return TestStepResult(); |
|
366 } |
|
367 |
|
368 TVerdict CPKCS10NegPKCS10Attributes::doTestStepL() |
|
369 { |
|
370 // Set it to pass initially |
|
371 SetTestStepResult(EPass); |
|
372 |
|
373 ////////////////////////////////////////////////////////////////////////////// |
|
374 // Test for NULL attribute for AddPKCSAttributeL |
|
375 CPKCS10Attributes *attrList = NULL; |
|
376 attrList = CPKCS10Attributes::NewLC(); |
|
377 TRAPD(err, attrList->AddPKCSAttributeL(NULL)); |
|
378 CleanupStack::PopAndDestroy(attrList); |
|
379 |
|
380 if (err != KErrArgument) |
|
381 { |
|
382 SetTestStepResult(EFail); |
|
383 } |
|
384 |
|
385 return TestStepResult(); |
|
386 } |
|
387 |
|
388 TVerdict CPKCS10NegPKCS10Request::doTestStepL() |
|
389 { |
|
390 // Set it to pass initially |
|
391 SetTestStepResult(EPass); |
|
392 |
|
393 ////////////////////////////////////////////////////////////////////////////// |
|
394 // Test for invalid DN for NewL |
|
395 CActiveScheduler* sch = new(ELeave) CActiveScheduler; |
|
396 CActiveScheduler::Install(sch); |
|
397 |
|
398 // Import the key |
|
399 CPKCS10NegTesterActive* activeObj = new (ELeave) CPKCS10NegTesterActive(Logger()); |
|
400 CCTKeyInfo *keyInfo = activeObj->doImportKeyL(this); |
|
401 |
|
402 // Try to generate a cert req |
|
403 CX500DistinguishedName* dn = NULL; |
|
404 CPKCS10Request* req = NULL; |
|
405 TRAPD(err, req = CPKCS10Request::NewL(*dn, *keyInfo)); |
|
406 |
|
407 if (err != KErrArgument) |
|
408 { |
|
409 SetTestStepResult(EFail); |
|
410 } |
|
411 |
|
412 ////////////////////////////////////////////////////////////////////////////// |
|
413 // Test for EDH for SetDigestAlgL |
|
414 CArrayPtrFlat<CX520AttributeTypeAndValue>* array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7); |
|
415 CleanupStack::PushL(array); |
|
416 dn = CX500DistinguishedName::NewL(*array); |
|
417 CleanupStack::PushL(dn); |
|
418 |
|
419 req = CPKCS10Request::NewL(*dn, *keyInfo); |
|
420 |
|
421 TRAP(err, req->SetDigestAlgL(EDH)); |
|
422 |
|
423 delete req; |
|
424 array->ResetAndDestroy(); |
|
425 CleanupStack::PopAndDestroy(2, array); //array, dn |
|
426 |
|
427 if (err != KErrArgument) |
|
428 { |
|
429 SetTestStepResult(EFail); |
|
430 } |
|
431 |
|
432 ////////////////////////////////////////////////////////////////////////////// |
|
433 // Test for NULL DN for SetDistinguishedNameL |
|
434 array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7); |
|
435 CleanupStack::PushL(array); |
|
436 dn = CX500DistinguishedName::NewL(*array); |
|
437 CleanupStack::PushL(dn); |
|
438 |
|
439 req = CPKCS10Request::NewL(*dn, *keyInfo); |
|
440 |
|
441 CX500DistinguishedName* nullDN = NULL; |
|
442 TRAP(err, req->SetDistinguishedNameL(*nullDN)); |
|
443 |
|
444 delete req; |
|
445 array->ResetAndDestroy(); |
|
446 CleanupStack::PopAndDestroy(2, array); //array, dn |
|
447 |
|
448 if (err != KErrArgument) |
|
449 { |
|
450 SetTestStepResult(EFail); |
|
451 } |
|
452 |
|
453 ////////////////////////////////////////////////////////////////////////////// |
|
454 // Test for NULL for SetKeyInfoL |
|
455 array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7); |
|
456 CleanupStack::PushL(array); |
|
457 dn = CX500DistinguishedName::NewL(*array); |
|
458 CleanupStack::PushL(dn); |
|
459 |
|
460 req = CPKCS10Request::NewL(*dn, *keyInfo); |
|
461 |
|
462 CCTKeyInfo* nullKeyInfo = NULL; |
|
463 TRAP(err, req->SetKeyInfoL(*nullKeyInfo)); |
|
464 |
|
465 delete req; |
|
466 array->ResetAndDestroy(); |
|
467 CleanupStack::PopAndDestroy(2, array); //array, dn |
|
468 |
|
469 if (err != KErrArgument) |
|
470 { |
|
471 SetTestStepResult(EFail); |
|
472 } |
|
473 |
|
474 delete activeObj; // Will release keyInfo |
|
475 delete sch; |
|
476 |
|
477 return TestStepResult(); |
|
478 } |