First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implementation of negative step classes for PKCS#10 tests.
20 #include "tpkcs10negatives.h"
22 #include <unifiedkeystore.h>
25 _LIT(KKeyLabel, "new pkcs10 neg test key");
28 _LIT(KPanicNegativeTests, "PKCS10NegativeTests");
29 TInt EPanicNegInvalidTestData = 1;
31 // String of 256 characters
32 _LIT8(KHugeString, "p000000001p000000002p000000003p000000004p000000005p000000006p000000007"
33 "p000000008p000000009p000000010p000000011p000000012p000000013p000000014"
34 "p000000015p000000016p000000017p000000018p000000019p000000020p000000021"
35 "p000000022p000000023p000000024p000000025p23456");
38 CPKCS10NegTesterActive::CPKCS10NegTesterActive(CTestExecuteLogger& aLogger) :
39 CActive(EPriorityStandard),
41 iKeyImportStarted(EFalse)
43 CActiveScheduler::Add(this);
44 User::LeaveIfError(iFs.Connect());
47 CPKCS10NegTesterActive::~CPKCS10NegTesterActive()
59 CCTKeyInfo* CPKCS10NegTesterActive::doImportKeyL(CCertificateRequestStep *aStep)
61 INFO_PRINTF1(_L("Active object tester for Key Import"));
64 // Initialise Key store
65 iKeyStore = CUnifiedKeyStore::NewL(iFs);
66 iKeyStore->Initialize(iStatus);
69 CActiveScheduler::Start();
71 User::LeaveIfError(iStatus.Int());
76 void CPKCS10NegTesterActive::RunL()
78 iError = iStatus.Int();
79 if (iError != KErrNone)
81 CActiveScheduler::Stop();
84 if (iKeyImportStarted == EFalse)
86 INFO_PRINTF1(_L("Importing keys"));
88 filename = iStep->iPrivateKey;
90 User::LeaveIfError(file.Open(iFs, filename, EFileRead));
91 CleanupClosePushL(file);
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
102 // Assumes only one keystore
103 ASSERT(iKeyInfo == NULL);
104 iKeyStore->ImportKey(0, *iKeyData, EPKCS15UsageSign, KKeyLabel,0, start, end, iKeyInfo, iStatus);
105 iKeyImportStarted = ETrue;
111 CActiveScheduler::Stop();
115 TInt CPKCS10NegTesterActive::RunError(TInt aError)
118 CActiveScheduler::Stop();
122 TVerdict CPKCS10NegPKCS10GenericAttr::doTestStepL()
124 // Set it to pass initially
125 SetTestStepResult(EPass);
127 //////////////////////////////////////////////////////////////////////////////
128 // Test for invalid attribute for NewL
129 TInt numAttr = iArrayGenAttrOID.Count();
130 __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
132 HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length());
133 string->Des().Copy(iArrayGenAttrValue[0]);
135 CASN1EncOctetString* attrString = CASN1EncOctetString::NewLC(*string);
137 CPKCS10Attribute *genericAttr = NULL;
138 TRAPD(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[0], attrString));
139 CleanupStack::Pop(attrString);
140 CleanupStack::PopAndDestroy(string);
151 if (err != KErrBadDescriptor)
153 SetTestStepResult(EFail);
156 //////////////////////////////////////////////////////////////////////////////
157 // Test for NULL value for NewL
158 __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
160 string = HBufC8::NewMaxLC(iArrayGenAttrValue[1].Length());
161 string->Des().Copy(iArrayGenAttrValue[1]);
163 if (*string == KNullDesC8)
169 attrString = CASN1EncOctetString::NewLC(*string);
172 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[1], attrString));
173 if (attrString != NULL)
175 CleanupStack::Pop(attrString);
177 CleanupStack::PopAndDestroy(string);
188 if (err != KErrArgument)
190 SetTestStepResult(EFail);
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]);
199 attrString = CASN1EncOctetString::NewLC(*string);
201 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
202 CleanupStack::Pop(attrString);
203 CleanupStack::PopAndDestroy(string);
205 // Should not be an error
208 SetTestStepResult(EFail);
211 // Try to add a NULL value
212 TRAP(err, genericAttr->AddValueL(NULL));
215 // Should return an error
216 if (err != KErrArgument)
218 SetTestStepResult(EFail);
221 //////////////////////////////////////////////////////////////////////////////
222 // Test for invalid attribute for ResetL
223 string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length());
224 string->Des().Copy(iArrayGenAttrValue[2]);
226 attrString = CASN1EncOctetString::NewLC(*string);
228 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
229 CleanupStack::Pop(attrString);
231 // Should not be an error
234 SetTestStepResult(EFail);
237 // Try to reset with an invalid attribute OID
238 attrString = CASN1EncOctetString::NewLC(*string);
239 TRAP(err, genericAttr->ResetL(KNullDesC, attrString));
246 CleanupStack::Pop(attrString);
247 CleanupStack::PopAndDestroy(string);
250 // Should return an error
251 if (err != KErrBadDescriptor)
253 SetTestStepResult(EFail);
256 //////////////////////////////////////////////////////////////////////////////
257 // Test for NULL value for ResetL
258 string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length());
259 string->Des().Copy(iArrayGenAttrValue[2]);
261 attrString = CASN1EncOctetString::NewLC(*string);
263 TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
264 CleanupStack::Pop(attrString);
265 CleanupStack::PopAndDestroy(string);
267 // Should not be an error
270 SetTestStepResult(EFail);
273 // Try to reset with a NULL value
274 TRAP(err, genericAttr->ResetL(iArrayGenAttrOID[2], NULL));
277 // Should return an error
278 if (err != KErrArgument)
280 SetTestStepResult(EFail);
283 return TestStepResult();
286 TVerdict CPKCS10NegPKCS9ChallengePasswordAttr::doTestStepL()
288 // Set it to pass initially
289 SetTestStepResult(EPass);
291 //////////////////////////////////////////////////////////////////////////////
292 // Test for empty password for NewL
293 HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length());
294 passwordString->Des().Copy(iChallengePassword);
296 CPKCS9ChallengePasswordAttr *chPasswordAttr = NULL;
297 TRAPD(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(*passwordString));
298 CleanupStack::PopAndDestroy(passwordString);
300 if (err != KErrArgument)
302 SetTestStepResult(EFail);
305 //////////////////////////////////////////////////////////////////////////////
306 // Test for big password for ResetL
307 TRAP(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(_L8("password")));
312 SetTestStepResult(EFail);
315 TRAP(err, chPasswordAttr->ResetL(KHugeString));
316 delete chPasswordAttr;
318 if (err != KErrArgument)
320 SetTestStepResult(EFail);
323 return TestStepResult();
326 TVerdict CPKCS10NegPKCS9ExtensionRequestAttr::doTestStepL()
328 // Set it to pass initially
329 SetTestStepResult(EPass);
331 //////////////////////////////////////////////////////////////////////////////
332 // Test for empty attribute OID for NewL
333 TInt numAttr = iArrayV3AttrOID.Count();
334 __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
336 CX509CertExtension* x509CertExtn = CX509CertExtension::NewL(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value"));
338 CPKCS9ExtensionRequestAttr *chExtReqAttr = NULL;
339 TRAPD(err, chExtReqAttr = CPKCS9ExtensionRequestAttr::NewL(*x509CertExtn));
343 if (err != KErrBadDescriptor)
345 SetTestStepResult(EFail);
348 //////////////////////////////////////////////////////////////////////////////
349 // Test for empty attribute OID for AddExtensionL
350 __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
352 x509CertExtn = CX509CertExtension::NewLC(iArrayV3AttrOID[1], iArrayV3AttrCritical[1], _L8("value"));
353 chExtReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*x509CertExtn);
355 CX509CertExtension *x509CertExtnNew = CX509CertExtension::NewLC(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value"));
356 TRAP(err, chExtReqAttr->AddExtensionL(*x509CertExtnNew));
358 CleanupStack::PopAndDestroy(3, x509CertExtn); // chExtReqAttr, x509CertExtnNew
360 if (err != KErrBadDescriptor)
362 SetTestStepResult(EFail);
365 return TestStepResult();
368 TVerdict CPKCS10NegPKCS10Attributes::doTestStepL()
370 // Set it to pass initially
371 SetTestStepResult(EPass);
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);
380 if (err != KErrArgument)
382 SetTestStepResult(EFail);
385 return TestStepResult();
388 TVerdict CPKCS10NegPKCS10Request::doTestStepL()
390 // Set it to pass initially
391 SetTestStepResult(EPass);
393 //////////////////////////////////////////////////////////////////////////////
394 // Test for invalid DN for NewL
395 CActiveScheduler* sch = new(ELeave) CActiveScheduler;
396 CActiveScheduler::Install(sch);
399 CPKCS10NegTesterActive* activeObj = new (ELeave) CPKCS10NegTesterActive(Logger());
400 CCTKeyInfo *keyInfo = activeObj->doImportKeyL(this);
402 // Try to generate a cert req
403 CX500DistinguishedName* dn = NULL;
404 CPKCS10Request* req = NULL;
405 TRAPD(err, req = CPKCS10Request::NewL(*dn, *keyInfo));
407 if (err != KErrArgument)
409 SetTestStepResult(EFail);
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);
419 req = CPKCS10Request::NewL(*dn, *keyInfo);
421 TRAP(err, req->SetDigestAlgL(EDH));
424 array->ResetAndDestroy();
425 CleanupStack::PopAndDestroy(2, array); //array, dn
427 if (err != KErrArgument)
429 SetTestStepResult(EFail);
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);
439 req = CPKCS10Request::NewL(*dn, *keyInfo);
441 CX500DistinguishedName* nullDN = NULL;
442 TRAP(err, req->SetDistinguishedNameL(*nullDN));
445 array->ResetAndDestroy();
446 CleanupStack::PopAndDestroy(2, array); //array, dn
448 if (err != KErrArgument)
450 SetTestStepResult(EFail);
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);
460 req = CPKCS10Request::NewL(*dn, *keyInfo);
462 CCTKeyInfo* nullKeyInfo = NULL;
463 TRAP(err, req->SetKeyInfoL(*nullKeyInfo));
466 array->ResetAndDestroy();
467 CleanupStack::PopAndDestroy(2, array); //array, dn
469 if (err != KErrArgument)
471 SetTestStepResult(EFail);
474 delete activeObj; // Will release keyInfo
477 return TestStepResult();