1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tpkcs10/tpkcs10minortests.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,312 @@
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 reuse test cases
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "tpkcs10minortests.h"
1.24 +
1.25 +_LIT(KKeyLabel, "new pkcs10 test key");
1.26 +
1.27 +// Panics
1.28 +_LIT(KPanicReuseTests, "PKCS10ReuseTests");
1.29 +TInt EPanicInvalidTestData = 1;
1.30 +
1.31 +void CPKCS10ReuseAttributeStep::doTestL()
1.32 + {
1.33 + // Set it to pass initially
1.34 + SetTestStepResult(EPass);
1.35 +
1.36 + //////////////////////////////////////////////////////////////////////////////
1.37 + // Test for reuse case of CPKCS10Attribute class
1.38 + TInt numAttr = iArrayGenAttrOID.Count();
1.39 + __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicReuseTests, EPanicInvalidTestData));
1.40 +
1.41 + HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length());
1.42 + string->Des().Copy(iArrayGenAttrValue[0]);
1.43 +
1.44 + CASN1EncOctetString* attrString1 = CASN1EncOctetString::NewLC(*string);
1.45 +
1.46 + CPKCS10Attribute *genericAttr = CPKCS10Attribute::NewLC(iArrayGenAttrOID[0], attrString1);
1.47 +
1.48 + // Get the encoding
1.49 + CASN1EncBase* attrEncoding1 = genericAttr->GetEncodingLC();
1.50 +
1.51 + // Reset and reuse the class
1.52 + CASN1EncOctetString* attrString2 = CASN1EncOctetString::NewLC(*string);
1.53 + genericAttr->ResetL(iArrayGenAttrOID[0], attrString2);
1.54 + CleanupStack::Pop(attrString2);
1.55 + CASN1EncBase* attrEncoding2 = genericAttr->GetEncodingLC();
1.56 +
1.57 + // Compare the encodings
1.58 + TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
1.59 +
1.60 + CleanupStack::PopAndDestroy(3, genericAttr); // attrEncoding1, attrEncoding2
1.61 + CleanupStack::Pop(attrString1);
1.62 + CleanupStack::PopAndDestroy(string);
1.63 +
1.64 + if (result == EFalse)
1.65 + {
1.66 + SetTestStepResult(EFail);
1.67 + }
1.68 + }
1.69 +
1.70 +void CPKCS10ReuseChallengePasswordAttrStep::doTestL()
1.71 + {
1.72 + // Set it to pass initially
1.73 + SetTestStepResult(EPass);
1.74 +
1.75 + //////////////////////////////////////////////////////////////////////////////
1.76 + // Test for reuse case of CPKCS9ChallengePasswordAttr class
1.77 + HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length());
1.78 + passwordString->Des().Copy(iChallengePassword);
1.79 +
1.80 + CPKCS9ChallengePasswordAttr *chPasswordAttr = CPKCS9ChallengePasswordAttr::NewLC(*passwordString);
1.81 +
1.82 + // Get the encoding
1.83 + CASN1EncBase* attrEncoding1 = chPasswordAttr->GetEncodingLC();
1.84 +
1.85 + // Reset and reuse the class
1.86 + chPasswordAttr->ResetL(*passwordString);
1.87 + CASN1EncBase* attrEncoding2 = chPasswordAttr->GetEncodingLC();
1.88 +
1.89 + // Compare the encodings
1.90 + TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
1.91 +
1.92 + CleanupStack::PopAndDestroy(4, passwordString); // chPasswordAttr, attrEncoding1, attrEncoding2
1.93 +
1.94 + if (result == EFalse)
1.95 + {
1.96 + SetTestStepResult(EFail);
1.97 + }
1.98 + }
1.99 +
1.100 +void CPKCS10ReuseExtensionAttrStep::doTestL()
1.101 + {
1.102 + // Set it to pass initially
1.103 + SetTestStepResult(EPass);
1.104 +
1.105 + //////////////////////////////////////////////////////////////////////////////
1.106 + // Test for reuse case of CPKCS10Attribute class
1.107 + HBufC8 *extValue1 = HBufC8::NewMaxLC(iArrayV3AttrValue[0].Length());
1.108 + extValue1->Des().Copy(iArrayV3AttrValue[0]);
1.109 + CX509CertExtension *v3ExtAttr1 = CX509CertExtension::NewLC(iArrayV3AttrOID[0],
1.110 + iArrayV3AttrCritical[0],
1.111 + *extValue1);
1.112 +
1.113 + HBufC8 *extValue2 = HBufC8::NewMaxLC(iArrayV3AttrValue[1].Length());
1.114 + extValue2->Des().Copy(iArrayV3AttrValue[1]);
1.115 + CX509CertExtension *v3ExtAttr2 = CX509CertExtension::NewLC(iArrayV3AttrOID[1],
1.116 + iArrayV3AttrCritical[1],
1.117 + *extValue2);
1.118 +
1.119 + CPKCS9ExtensionRequestAttr *extReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*v3ExtAttr1);
1.120 + extReqAttr->AddExtensionL(*v3ExtAttr2);
1.121 +
1.122 + // Get the encoding
1.123 + CASN1EncBase* attrEncoding1 = extReqAttr->GetEncodingLC();
1.124 +
1.125 + // Reset and reuse the class
1.126 + extReqAttr->ResetL(*v3ExtAttr1);
1.127 + extReqAttr->AddExtensionL(*v3ExtAttr2);
1.128 + CASN1EncBase* attrEncoding2 = extReqAttr->GetEncodingLC();
1.129 +
1.130 + // Compare the encodings
1.131 + TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
1.132 +
1.133 + CleanupStack::PopAndDestroy(7, extValue1); // v3ExtAttr1, extValue2, v3ExtAttr2, extReqAttr, attrEncoding1, attrEncoding2
1.134 +
1.135 + if (result == EFalse)
1.136 + {
1.137 + SetTestStepResult(EFail);
1.138 + }
1.139 + }
1.140 +
1.141 +CPKCS10TesterActiveCancelRequestA::CPKCS10TesterActiveCancelRequestA( CTestExecuteLogger& aLogger ) :
1.142 + CPKCS10TesterActive( aLogger )
1.143 + {
1.144 + }
1.145 +
1.146 +void CPKCS10CancelRequestAStep::doTestL()
1.147 +{
1.148 +
1.149 + iSched=new(ELeave) CActiveScheduler;
1.150 + CleanupStack::PushL(iSched);
1.151 + CActiveScheduler::Install(iSched);
1.152 +
1.153 + iActiveObjTest = new (ELeave) CPKCS10TesterActiveCancelRequestA(Logger());
1.154 + CleanupStack::PushL(iActiveObjTest);
1.155 +
1.156 + if (iActiveObjTest->doActiveCertRequestL(this) != EPass)
1.157 + {
1.158 + SetTestStepResult(EFail);
1.159 + }
1.160 +
1.161 + CleanupStack::PopAndDestroy(iActiveObjTest);
1.162 + iActiveObjTest = NULL;
1.163 + CleanupStack::PopAndDestroy(iSched);
1.164 + iSched=NULL;
1.165 +
1.166 + }
1.167 +
1.168 +
1.169 +TVerdict CPKCS10TesterActiveCancelRequestA::doActiveCertRequestL(CCertificateRequestStep* aStep)
1.170 + {
1.171 +
1.172 + iTestSuccess= EFail;
1.173 + INFO_PRINTF1(_L("Active tester for Cert Request started. "));
1.174 + iStepPointer = aStep;
1.175 +
1.176 + DeleteAllKeysL();
1.177 +
1.178 + INFO_PRINTF1(_L("initialising keystore"));
1.179 +
1.180 + // Initialise Key store.
1.181 + iKeyStore = CUnifiedKeyStore::NewL(iFs);
1.182 + CleanupStack::PushL(iKeyStore);
1.183 + iKeyStore->Initialize(iStatus);
1.184 + iState = EInitKeyStore;
1.185 + SetActive();
1.186 + CActiveScheduler::Start();
1.187 +
1.188 + // After encoding was produced it checks correctness
1.189 + if(iRunError!=KErrCancel)
1.190 + {
1.191 + iTestSuccess= EFail;
1.192 + }
1.193 + else
1.194 + {
1.195 + iTestSuccess= EPass;
1.196 + }
1.197 +
1.198 + CleanupStack::Pop(iKeyStore);
1.199 + return iTestSuccess;
1.200 +}
1.201 +
1.202 +void CPKCS10TesterActiveCancelRequestA::RunL()
1.203 +{
1.204 +
1.205 + iRunError =KErrNone;
1.206 +
1.207 + User::LeaveIfError(iStatus.Int());
1.208 +
1.209 + switch(iState)
1.210 + {
1.211 +
1.212 + case EDeleteAllInit:
1.213 + INFO_PRINTF1(_L(" listing existing keys\n"));
1.214 + iKeyStore->List(iKeys, iKeyFilter, iStatus);
1.215 + iState = EDeleteAllDelete;
1.216 + SetActive();
1.217 + break;
1.218 +
1.219 + case EDeleteAllDelete:
1.220 + if (iKeys.Count() == 0)
1.221 + {
1.222 + // key log is empty
1.223 + iKeys.Close();
1.224 + CActiveScheduler::Stop();
1.225 + break;
1.226 + }
1.227 +
1.228 + INFO_PRINTF1(_L(" deleting key\n"));
1.229 + iKeyStore->DeleteKey(*iKeys[0], iStatus);
1.230 + iState = EDeleteAllDelete;
1.231 + SetActive();
1.232 + iKeys[0]->Release();
1.233 + iKeys.Remove(0);
1.234 + break;
1.235 +
1.236 + case EInitKeyStore:
1.237 + {
1.238 + INFO_PRINTF1(_L("Importing keys"));
1.239 + TFileName filename;
1.240 + filename = iStepPointer->iPrivateKey;
1.241 + RFile file;
1.242 + User::LeaveIfError(file.Open(iFs,filename,EFileRead));
1.243 + CleanupClosePushL(file);
1.244 + TInt size;
1.245 + User::LeaveIfError(file.Size(size));
1.246 + iKeyData = HBufC8::NewMaxL(size);
1.247 + TPtr8 keyPtr = iKeyData->Des();
1.248 + User::LeaveIfError(file.Read(keyPtr));
1.249 + CleanupStack::PopAndDestroy(); // file
1.250 +
1.251 + TTime start(0.0);
1.252 + TTime end(0.0);
1.253 +
1.254 + // Assumes only one keystore
1.255 + // Check parameters!
1.256 + ASSERT(iKeyInfo == NULL);
1.257 + iKeyStore->ImportKey(0, *(iKeyData), EPKCS15UsageSign, KKeyLabel,0, start, end,iKeyInfo, iStatus);
1.258 + iState = EImportKey;
1.259 + SetActive();
1.260 + break;
1.261 + }
1.262 + case EImportKey:
1.263 + {
1.264 + INFO_PRINTF1(_L("Setting security policy for new stored key"));
1.265 + TSecureId secureId(0x101f7784); // Application secure ID
1.266 + TSecurityPolicy securePolicy(secureId,ECapabilityReadUserData);
1.267 + iKeyStore->SetUsePolicy(iKeyInfo->Handle(),securePolicy,iStatus);
1.268 + iState = EKeyPolicy;
1.269 + SetActive();
1.270 + break;
1.271 + }
1.272 + case EKeyPolicy:
1.273 + {
1.274 + iAttrCollection=CPKCS10Attributes::NewL();
1.275 + CleanupStack::PushL(iAttrCollection);
1.276 + INFO_PRINTF1(_L("Adding generic attributes"));
1.277 + AddGenericAttributesL();
1.278 + INFO_PRINTF1(_L("Adding Challenge password"));
1.279 + AddChallengePasswordL();
1.280 + INFO_PRINTF1(_L("Adding V3 extensions"));
1.281 + AddV3ExtensionsL();
1.282 + INFO_PRINTF1(_L("Generating distinguished name"));
1.283 + iDN=MakeDistinguishedNameL();
1.284 + CleanupStack::PushL(iDN);
1.285 + INFO_PRINTF1(_L("Generating cert request"));
1.286 + iCertRequest=CPKCS10Request::NewL(*iDN,*iKeyInfo,iAttrCollection);
1.287 + CleanupStack::PushL(iCertRequest);
1.288 + INFO_PRINTF1(_L("Setting digest algorithm"));
1.289 + TAlgorithmId digestAlgo=iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg);
1.290 + iCertRequest->SetDigestAlgL(digestAlgo);
1.291 + INFO_PRINTF1(_L("Requesting cert request encoding"));
1.292 +
1.293 + // Clean up
1.294 + CleanupStack::Pop(iCertRequest);
1.295 + CleanupStack::Pop(iDN);
1.296 + CleanupStack::Pop(iAttrCollection);
1.297 + iAttrCollection=NULL;
1.298 + iOutputASN1Encoding=NULL;
1.299 + iCertRequest->CreateEncoding(iOutputASN1Encoding,iStatus);
1.300 + iState=EGenerateCertRequest;
1.301 + SetActive();
1.302 +
1.303 + iCertRequest->Cancel();
1.304 +
1.305 + break;
1.306 + }
1.307 + default:
1.308 + {
1.309 + INFO_PRINTF1(_L("Cert Request Active tester: State corrupted."));
1.310 + User::Leave(KErrCorrupt);
1.311 + }
1.312 + }
1.313 +
1.314 + return;
1.315 +}