1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tpkcs10/tpkcs10negatives.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,479 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Implementation of negative step classes for PKCS#10 tests.
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "tpkcs10negatives.h"
1.24 +#include <x500dn.h>
1.25 +#include <unifiedkeystore.h>
1.26 +
1.27 +// Key label
1.28 +_LIT(KKeyLabel, "new pkcs10 neg test key");
1.29 +
1.30 +// Panics
1.31 +_LIT(KPanicNegativeTests, "PKCS10NegativeTests");
1.32 +TInt EPanicNegInvalidTestData = 1;
1.33 +
1.34 +// String of 256 characters
1.35 +_LIT8(KHugeString, "p000000001p000000002p000000003p000000004p000000005p000000006p000000007"
1.36 + "p000000008p000000009p000000010p000000011p000000012p000000013p000000014"
1.37 + "p000000015p000000016p000000017p000000018p000000019p000000020p000000021"
1.38 + "p000000022p000000023p000000024p000000025p23456");
1.39 +
1.40 +
1.41 +CPKCS10NegTesterActive::CPKCS10NegTesterActive(CTestExecuteLogger& aLogger) :
1.42 + CActive(EPriorityStandard),
1.43 + iLogger(aLogger),
1.44 + iKeyImportStarted(EFalse)
1.45 + {
1.46 + CActiveScheduler::Add(this);
1.47 + User::LeaveIfError(iFs.Connect());
1.48 + }
1.49 +
1.50 +CPKCS10NegTesterActive::~CPKCS10NegTesterActive()
1.51 + {
1.52 + Cancel();
1.53 + delete iKeyData;
1.54 + if (iKeyInfo)
1.55 + {
1.56 + iKeyInfo->Release();
1.57 + }
1.58 + delete iKeyStore;
1.59 + iFs.Close();
1.60 + }
1.61 +
1.62 +CCTKeyInfo* CPKCS10NegTesterActive::doImportKeyL(CCertificateRequestStep *aStep)
1.63 +{
1.64 + INFO_PRINTF1(_L("Active object tester for Key Import"));
1.65 +
1.66 + iStep = aStep;
1.67 + // Initialise Key store
1.68 + iKeyStore = CUnifiedKeyStore::NewL(iFs);
1.69 + iKeyStore->Initialize(iStatus);
1.70 +
1.71 + SetActive();
1.72 + CActiveScheduler::Start();
1.73 +
1.74 + User::LeaveIfError(iStatus.Int());
1.75 +
1.76 + return iKeyInfo;
1.77 +}
1.78 +
1.79 +void CPKCS10NegTesterActive::RunL()
1.80 + {
1.81 + iError = iStatus.Int();
1.82 + if (iError != KErrNone)
1.83 + {
1.84 + CActiveScheduler::Stop();
1.85 + }
1.86 +
1.87 + if (iKeyImportStarted == EFalse)
1.88 + {
1.89 + INFO_PRINTF1(_L("Importing keys"));
1.90 + TFileName filename;
1.91 + filename = iStep->iPrivateKey;
1.92 + RFile file;
1.93 + User::LeaveIfError(file.Open(iFs, filename, EFileRead));
1.94 + CleanupClosePushL(file);
1.95 + TInt size;
1.96 + User::LeaveIfError(file.Size(size));
1.97 + iKeyData = HBufC8::NewMaxL(size);
1.98 + TPtr8 keyPtr = iKeyData->Des();
1.99 + User::LeaveIfError(file.Read(keyPtr));
1.100 + CleanupStack::PopAndDestroy(); // file
1.101 +
1.102 + TTime start(0.0);
1.103 + TTime end(0.0);
1.104 +
1.105 + // Assumes only one keystore
1.106 + ASSERT(iKeyInfo == NULL);
1.107 + iKeyStore->ImportKey(0, *iKeyData, EPKCS15UsageSign, KKeyLabel,0, start, end, iKeyInfo, iStatus);
1.108 + iKeyImportStarted = ETrue;
1.109 + SetActive();
1.110 + }
1.111 + else
1.112 + {
1.113 + // Key import done
1.114 + CActiveScheduler::Stop();
1.115 + }
1.116 + }
1.117 +
1.118 +TInt CPKCS10NegTesterActive::RunError(TInt aError)
1.119 + {
1.120 + iError = aError;
1.121 + CActiveScheduler::Stop();
1.122 + return KErrNone;
1.123 + }
1.124 +
1.125 +TVerdict CPKCS10NegPKCS10GenericAttr::doTestStepL()
1.126 + {
1.127 + // Set it to pass initially
1.128 + SetTestStepResult(EPass);
1.129 +
1.130 + //////////////////////////////////////////////////////////////////////////////
1.131 + // Test for invalid attribute for NewL
1.132 + TInt numAttr = iArrayGenAttrOID.Count();
1.133 + __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
1.134 +
1.135 + HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length());
1.136 + string->Des().Copy(iArrayGenAttrValue[0]);
1.137 +
1.138 + CASN1EncOctetString* attrString = CASN1EncOctetString::NewLC(*string);
1.139 +
1.140 + CPKCS10Attribute *genericAttr = NULL;
1.141 + TRAPD(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[0], attrString));
1.142 + CleanupStack::Pop(attrString);
1.143 + CleanupStack::PopAndDestroy(string);
1.144 +
1.145 + if (err != KErrNone)
1.146 + {
1.147 + delete attrString;
1.148 + }
1.149 + else
1.150 + {
1.151 + delete genericAttr;
1.152 + }
1.153 +
1.154 + if (err != KErrBadDescriptor)
1.155 + {
1.156 + SetTestStepResult(EFail);
1.157 + }
1.158 +
1.159 + //////////////////////////////////////////////////////////////////////////////
1.160 + // Test for NULL value for NewL
1.161 + __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
1.162 +
1.163 + string = HBufC8::NewMaxLC(iArrayGenAttrValue[1].Length());
1.164 + string->Des().Copy(iArrayGenAttrValue[1]);
1.165 +
1.166 + if (*string == KNullDesC8)
1.167 + {
1.168 + attrString = NULL;
1.169 + }
1.170 + else
1.171 + {
1.172 + attrString = CASN1EncOctetString::NewLC(*string);
1.173 + }
1.174 +
1.175 + TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[1], attrString));
1.176 + if (attrString != NULL)
1.177 + {
1.178 + CleanupStack::Pop(attrString);
1.179 + }
1.180 + CleanupStack::PopAndDestroy(string);
1.181 +
1.182 + if (err != KErrNone)
1.183 + {
1.184 + delete attrString;
1.185 + }
1.186 + else
1.187 + {
1.188 + delete genericAttr;
1.189 + }
1.190 +
1.191 + if (err != KErrArgument)
1.192 + {
1.193 + SetTestStepResult(EFail);
1.194 + }
1.195 +
1.196 + //////////////////////////////////////////////////////////////////////////////
1.197 + // Test for NULL value for AddValueL
1.198 + __ASSERT_ALWAYS((numAttr > 2), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
1.199 + string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length());
1.200 + string->Des().Copy(iArrayGenAttrValue[2]);
1.201 +
1.202 + attrString = CASN1EncOctetString::NewLC(*string);
1.203 +
1.204 + TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
1.205 + CleanupStack::Pop(attrString);
1.206 + CleanupStack::PopAndDestroy(string);
1.207 +
1.208 + // Should not be an error
1.209 + if (err != KErrNone)
1.210 + {
1.211 + SetTestStepResult(EFail);
1.212 + }
1.213 +
1.214 + // Try to add a NULL value
1.215 + TRAP(err, genericAttr->AddValueL(NULL));
1.216 + delete genericAttr;
1.217 +
1.218 + // Should return an error
1.219 + if (err != KErrArgument)
1.220 + {
1.221 + SetTestStepResult(EFail);
1.222 + }
1.223 +
1.224 + //////////////////////////////////////////////////////////////////////////////
1.225 + // Test for invalid attribute for ResetL
1.226 + string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length());
1.227 + string->Des().Copy(iArrayGenAttrValue[2]);
1.228 +
1.229 + attrString = CASN1EncOctetString::NewLC(*string);
1.230 +
1.231 + TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
1.232 + CleanupStack::Pop(attrString);
1.233 +
1.234 + // Should not be an error
1.235 + if (err != KErrNone)
1.236 + {
1.237 + SetTestStepResult(EFail);
1.238 + }
1.239 +
1.240 + // Try to reset with an invalid attribute OID
1.241 + attrString = CASN1EncOctetString::NewLC(*string);
1.242 + TRAP(err, genericAttr->ResetL(KNullDesC, attrString));
1.243 +
1.244 + if (err != KErrNone)
1.245 + {
1.246 + delete attrString;
1.247 + }
1.248 +
1.249 + CleanupStack::Pop(attrString);
1.250 + CleanupStack::PopAndDestroy(string);
1.251 + delete genericAttr;
1.252 +
1.253 + // Should return an error
1.254 + if (err != KErrBadDescriptor)
1.255 + {
1.256 + SetTestStepResult(EFail);
1.257 + }
1.258 +
1.259 + //////////////////////////////////////////////////////////////////////////////
1.260 + // Test for NULL value for ResetL
1.261 + string = HBufC8::NewMaxLC(iArrayGenAttrValue[2].Length());
1.262 + string->Des().Copy(iArrayGenAttrValue[2]);
1.263 +
1.264 + attrString = CASN1EncOctetString::NewLC(*string);
1.265 +
1.266 + TRAP(err, genericAttr = CPKCS10Attribute::NewL(iArrayGenAttrOID[2], attrString));
1.267 + CleanupStack::Pop(attrString);
1.268 + CleanupStack::PopAndDestroy(string);
1.269 +
1.270 + // Should not be an error
1.271 + if (err != KErrNone)
1.272 + {
1.273 + SetTestStepResult(EFail);
1.274 + }
1.275 +
1.276 + // Try to reset with a NULL value
1.277 + TRAP(err, genericAttr->ResetL(iArrayGenAttrOID[2], NULL));
1.278 + delete genericAttr;
1.279 +
1.280 + // Should return an error
1.281 + if (err != KErrArgument)
1.282 + {
1.283 + SetTestStepResult(EFail);
1.284 + }
1.285 +
1.286 + return TestStepResult();
1.287 + }
1.288 +
1.289 +TVerdict CPKCS10NegPKCS9ChallengePasswordAttr::doTestStepL()
1.290 + {
1.291 + // Set it to pass initially
1.292 + SetTestStepResult(EPass);
1.293 +
1.294 + //////////////////////////////////////////////////////////////////////////////
1.295 + // Test for empty password for NewL
1.296 + HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length());
1.297 + passwordString->Des().Copy(iChallengePassword);
1.298 +
1.299 + CPKCS9ChallengePasswordAttr *chPasswordAttr = NULL;
1.300 + TRAPD(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(*passwordString));
1.301 + CleanupStack::PopAndDestroy(passwordString);
1.302 +
1.303 + if (err != KErrArgument)
1.304 + {
1.305 + SetTestStepResult(EFail);
1.306 + }
1.307 +
1.308 + //////////////////////////////////////////////////////////////////////////////
1.309 + // Test for big password for ResetL
1.310 + TRAP(err, chPasswordAttr = CPKCS9ChallengePasswordAttr::NewL(_L8("password")));
1.311 +
1.312 + // Should not fail
1.313 + if (err != KErrNone)
1.314 + {
1.315 + SetTestStepResult(EFail);
1.316 + }
1.317 +
1.318 + TRAP(err, chPasswordAttr->ResetL(KHugeString));
1.319 + delete chPasswordAttr;
1.320 +
1.321 + if (err != KErrArgument)
1.322 + {
1.323 + SetTestStepResult(EFail);
1.324 + }
1.325 +
1.326 + return TestStepResult();
1.327 + }
1.328 +
1.329 +TVerdict CPKCS10NegPKCS9ExtensionRequestAttr::doTestStepL()
1.330 + {
1.331 + // Set it to pass initially
1.332 + SetTestStepResult(EPass);
1.333 +
1.334 + //////////////////////////////////////////////////////////////////////////////
1.335 + // Test for empty attribute OID for NewL
1.336 + TInt numAttr = iArrayV3AttrOID.Count();
1.337 + __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
1.338 +
1.339 + CX509CertExtension* x509CertExtn = CX509CertExtension::NewL(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value"));
1.340 +
1.341 + CPKCS9ExtensionRequestAttr *chExtReqAttr = NULL;
1.342 + TRAPD(err, chExtReqAttr = CPKCS9ExtensionRequestAttr::NewL(*x509CertExtn));
1.343 +
1.344 + delete x509CertExtn;
1.345 +
1.346 + if (err != KErrBadDescriptor)
1.347 + {
1.348 + SetTestStepResult(EFail);
1.349 + }
1.350 +
1.351 + //////////////////////////////////////////////////////////////////////////////
1.352 + // Test for empty attribute OID for AddExtensionL
1.353 + __ASSERT_ALWAYS((numAttr > 1), User::Panic(KPanicNegativeTests, EPanicNegInvalidTestData));
1.354 +
1.355 + x509CertExtn = CX509CertExtension::NewLC(iArrayV3AttrOID[1], iArrayV3AttrCritical[1], _L8("value"));
1.356 + chExtReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*x509CertExtn);
1.357 +
1.358 + CX509CertExtension *x509CertExtnNew = CX509CertExtension::NewLC(iArrayV3AttrOID[0], iArrayV3AttrCritical[0], _L8("value"));
1.359 + TRAP(err, chExtReqAttr->AddExtensionL(*x509CertExtnNew));
1.360 +
1.361 + CleanupStack::PopAndDestroy(3, x509CertExtn); // chExtReqAttr, x509CertExtnNew
1.362 +
1.363 + if (err != KErrBadDescriptor)
1.364 + {
1.365 + SetTestStepResult(EFail);
1.366 + }
1.367 +
1.368 + return TestStepResult();
1.369 + }
1.370 +
1.371 +TVerdict CPKCS10NegPKCS10Attributes::doTestStepL()
1.372 + {
1.373 + // Set it to pass initially
1.374 + SetTestStepResult(EPass);
1.375 +
1.376 + //////////////////////////////////////////////////////////////////////////////
1.377 + // Test for NULL attribute for AddPKCSAttributeL
1.378 + CPKCS10Attributes *attrList = NULL;
1.379 + attrList = CPKCS10Attributes::NewLC();
1.380 + TRAPD(err, attrList->AddPKCSAttributeL(NULL));
1.381 + CleanupStack::PopAndDestroy(attrList);
1.382 +
1.383 + if (err != KErrArgument)
1.384 + {
1.385 + SetTestStepResult(EFail);
1.386 + }
1.387 +
1.388 + return TestStepResult();
1.389 + }
1.390 +
1.391 +TVerdict CPKCS10NegPKCS10Request::doTestStepL()
1.392 + {
1.393 + // Set it to pass initially
1.394 + SetTestStepResult(EPass);
1.395 +
1.396 + //////////////////////////////////////////////////////////////////////////////
1.397 + // Test for invalid DN for NewL
1.398 + CActiveScheduler* sch = new(ELeave) CActiveScheduler;
1.399 + CActiveScheduler::Install(sch);
1.400 +
1.401 + // Import the key
1.402 + CPKCS10NegTesterActive* activeObj = new (ELeave) CPKCS10NegTesterActive(Logger());
1.403 + CCTKeyInfo *keyInfo = activeObj->doImportKeyL(this);
1.404 +
1.405 + // Try to generate a cert req
1.406 + CX500DistinguishedName* dn = NULL;
1.407 + CPKCS10Request* req = NULL;
1.408 + TRAPD(err, req = CPKCS10Request::NewL(*dn, *keyInfo));
1.409 +
1.410 + if (err != KErrArgument)
1.411 + {
1.412 + SetTestStepResult(EFail);
1.413 + }
1.414 +
1.415 + //////////////////////////////////////////////////////////////////////////////
1.416 + // Test for EDH for SetDigestAlgL
1.417 + CArrayPtrFlat<CX520AttributeTypeAndValue>* array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7);
1.418 + CleanupStack::PushL(array);
1.419 + dn = CX500DistinguishedName::NewL(*array);
1.420 + CleanupStack::PushL(dn);
1.421 +
1.422 + req = CPKCS10Request::NewL(*dn, *keyInfo);
1.423 +
1.424 + TRAP(err, req->SetDigestAlgL(EDH));
1.425 +
1.426 + delete req;
1.427 + array->ResetAndDestroy();
1.428 + CleanupStack::PopAndDestroy(2, array); //array, dn
1.429 +
1.430 + if (err != KErrArgument)
1.431 + {
1.432 + SetTestStepResult(EFail);
1.433 + }
1.434 +
1.435 + //////////////////////////////////////////////////////////////////////////////
1.436 + // Test for NULL DN for SetDistinguishedNameL
1.437 + array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7);
1.438 + CleanupStack::PushL(array);
1.439 + dn = CX500DistinguishedName::NewL(*array);
1.440 + CleanupStack::PushL(dn);
1.441 +
1.442 + req = CPKCS10Request::NewL(*dn, *keyInfo);
1.443 +
1.444 + CX500DistinguishedName* nullDN = NULL;
1.445 + TRAP(err, req->SetDistinguishedNameL(*nullDN));
1.446 +
1.447 + delete req;
1.448 + array->ResetAndDestroy();
1.449 + CleanupStack::PopAndDestroy(2, array); //array, dn
1.450 +
1.451 + if (err != KErrArgument)
1.452 + {
1.453 + SetTestStepResult(EFail);
1.454 + }
1.455 +
1.456 + //////////////////////////////////////////////////////////////////////////////
1.457 + // Test for NULL for SetKeyInfoL
1.458 + array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7);
1.459 + CleanupStack::PushL(array);
1.460 + dn = CX500DistinguishedName::NewL(*array);
1.461 + CleanupStack::PushL(dn);
1.462 +
1.463 + req = CPKCS10Request::NewL(*dn, *keyInfo);
1.464 +
1.465 + CCTKeyInfo* nullKeyInfo = NULL;
1.466 + TRAP(err, req->SetKeyInfoL(*nullKeyInfo));
1.467 +
1.468 + delete req;
1.469 + array->ResetAndDestroy();
1.470 + CleanupStack::PopAndDestroy(2, array); //array, dn
1.471 +
1.472 + if (err != KErrArgument)
1.473 + {
1.474 + SetTestStepResult(EFail);
1.475 + }
1.476 +
1.477 + delete activeObj; // Will release keyInfo
1.478 + delete sch;
1.479 +
1.480 + return TestStepResult();
1.481 + }
1.482 +