sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Implementation of reuse test cases sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "tpkcs10minortests.h" sl@0: sl@0: _LIT(KKeyLabel, "new pkcs10 test key"); sl@0: sl@0: // Panics sl@0: _LIT(KPanicReuseTests, "PKCS10ReuseTests"); sl@0: TInt EPanicInvalidTestData = 1; sl@0: sl@0: void CPKCS10ReuseAttributeStep::doTestL() sl@0: { sl@0: // Set it to pass initially sl@0: SetTestStepResult(EPass); sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: // Test for reuse case of CPKCS10Attribute class sl@0: TInt numAttr = iArrayGenAttrOID.Count(); sl@0: __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicReuseTests, EPanicInvalidTestData)); sl@0: sl@0: HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length()); sl@0: string->Des().Copy(iArrayGenAttrValue[0]); sl@0: sl@0: CASN1EncOctetString* attrString1 = CASN1EncOctetString::NewLC(*string); sl@0: sl@0: CPKCS10Attribute *genericAttr = CPKCS10Attribute::NewLC(iArrayGenAttrOID[0], attrString1); sl@0: sl@0: // Get the encoding sl@0: CASN1EncBase* attrEncoding1 = genericAttr->GetEncodingLC(); sl@0: sl@0: // Reset and reuse the class sl@0: CASN1EncOctetString* attrString2 = CASN1EncOctetString::NewLC(*string); sl@0: genericAttr->ResetL(iArrayGenAttrOID[0], attrString2); sl@0: CleanupStack::Pop(attrString2); sl@0: CASN1EncBase* attrEncoding2 = genericAttr->GetEncodingLC(); sl@0: sl@0: // Compare the encodings sl@0: TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2); sl@0: sl@0: CleanupStack::PopAndDestroy(3, genericAttr); // attrEncoding1, attrEncoding2 sl@0: CleanupStack::Pop(attrString1); sl@0: CleanupStack::PopAndDestroy(string); sl@0: sl@0: if (result == EFalse) sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: sl@0: void CPKCS10ReuseChallengePasswordAttrStep::doTestL() sl@0: { sl@0: // Set it to pass initially sl@0: SetTestStepResult(EPass); sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: // Test for reuse case of CPKCS9ChallengePasswordAttr class sl@0: HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length()); sl@0: passwordString->Des().Copy(iChallengePassword); sl@0: sl@0: CPKCS9ChallengePasswordAttr *chPasswordAttr = CPKCS9ChallengePasswordAttr::NewLC(*passwordString); sl@0: sl@0: // Get the encoding sl@0: CASN1EncBase* attrEncoding1 = chPasswordAttr->GetEncodingLC(); sl@0: sl@0: // Reset and reuse the class sl@0: chPasswordAttr->ResetL(*passwordString); sl@0: CASN1EncBase* attrEncoding2 = chPasswordAttr->GetEncodingLC(); sl@0: sl@0: // Compare the encodings sl@0: TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2); sl@0: sl@0: CleanupStack::PopAndDestroy(4, passwordString); // chPasswordAttr, attrEncoding1, attrEncoding2 sl@0: sl@0: if (result == EFalse) sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: sl@0: void CPKCS10ReuseExtensionAttrStep::doTestL() sl@0: { sl@0: // Set it to pass initially sl@0: SetTestStepResult(EPass); sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: // Test for reuse case of CPKCS10Attribute class sl@0: HBufC8 *extValue1 = HBufC8::NewMaxLC(iArrayV3AttrValue[0].Length()); sl@0: extValue1->Des().Copy(iArrayV3AttrValue[0]); sl@0: CX509CertExtension *v3ExtAttr1 = CX509CertExtension::NewLC(iArrayV3AttrOID[0], sl@0: iArrayV3AttrCritical[0], sl@0: *extValue1); sl@0: sl@0: HBufC8 *extValue2 = HBufC8::NewMaxLC(iArrayV3AttrValue[1].Length()); sl@0: extValue2->Des().Copy(iArrayV3AttrValue[1]); sl@0: CX509CertExtension *v3ExtAttr2 = CX509CertExtension::NewLC(iArrayV3AttrOID[1], sl@0: iArrayV3AttrCritical[1], sl@0: *extValue2); sl@0: sl@0: CPKCS9ExtensionRequestAttr *extReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*v3ExtAttr1); sl@0: extReqAttr->AddExtensionL(*v3ExtAttr2); sl@0: sl@0: // Get the encoding sl@0: CASN1EncBase* attrEncoding1 = extReqAttr->GetEncodingLC(); sl@0: sl@0: // Reset and reuse the class sl@0: extReqAttr->ResetL(*v3ExtAttr1); sl@0: extReqAttr->AddExtensionL(*v3ExtAttr2); sl@0: CASN1EncBase* attrEncoding2 = extReqAttr->GetEncodingLC(); sl@0: sl@0: // Compare the encodings sl@0: TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2); sl@0: sl@0: CleanupStack::PopAndDestroy(7, extValue1); // v3ExtAttr1, extValue2, v3ExtAttr2, extReqAttr, attrEncoding1, attrEncoding2 sl@0: sl@0: if (result == EFalse) sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: sl@0: CPKCS10TesterActiveCancelRequestA::CPKCS10TesterActiveCancelRequestA( CTestExecuteLogger& aLogger ) : sl@0: CPKCS10TesterActive( aLogger ) sl@0: { sl@0: } sl@0: sl@0: void CPKCS10CancelRequestAStep::doTestL() sl@0: { sl@0: sl@0: iSched=new(ELeave) CActiveScheduler; sl@0: CleanupStack::PushL(iSched); sl@0: CActiveScheduler::Install(iSched); sl@0: sl@0: iActiveObjTest = new (ELeave) CPKCS10TesterActiveCancelRequestA(Logger()); sl@0: CleanupStack::PushL(iActiveObjTest); sl@0: sl@0: if (iActiveObjTest->doActiveCertRequestL(this) != EPass) sl@0: { sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(iActiveObjTest); sl@0: iActiveObjTest = NULL; sl@0: CleanupStack::PopAndDestroy(iSched); sl@0: iSched=NULL; sl@0: sl@0: } sl@0: sl@0: sl@0: TVerdict CPKCS10TesterActiveCancelRequestA::doActiveCertRequestL(CCertificateRequestStep* aStep) sl@0: { sl@0: sl@0: iTestSuccess= EFail; sl@0: INFO_PRINTF1(_L("Active tester for Cert Request started. ")); sl@0: iStepPointer = aStep; sl@0: sl@0: DeleteAllKeysL(); sl@0: sl@0: INFO_PRINTF1(_L("initialising keystore")); sl@0: sl@0: // Initialise Key store. sl@0: iKeyStore = CUnifiedKeyStore::NewL(iFs); sl@0: CleanupStack::PushL(iKeyStore); sl@0: iKeyStore->Initialize(iStatus); sl@0: iState = EInitKeyStore; sl@0: SetActive(); sl@0: CActiveScheduler::Start(); sl@0: sl@0: // After encoding was produced it checks correctness sl@0: if(iRunError!=KErrCancel) sl@0: { sl@0: iTestSuccess= EFail; sl@0: } sl@0: else sl@0: { sl@0: iTestSuccess= EPass; sl@0: } sl@0: sl@0: CleanupStack::Pop(iKeyStore); sl@0: return iTestSuccess; sl@0: } sl@0: sl@0: void CPKCS10TesterActiveCancelRequestA::RunL() sl@0: { sl@0: sl@0: iRunError =KErrNone; sl@0: sl@0: User::LeaveIfError(iStatus.Int()); sl@0: sl@0: switch(iState) sl@0: { sl@0: sl@0: case EDeleteAllInit: sl@0: INFO_PRINTF1(_L(" listing existing keys\n")); sl@0: iKeyStore->List(iKeys, iKeyFilter, iStatus); sl@0: iState = EDeleteAllDelete; sl@0: SetActive(); sl@0: break; sl@0: sl@0: case EDeleteAllDelete: sl@0: if (iKeys.Count() == 0) sl@0: { sl@0: // key log is empty sl@0: iKeys.Close(); sl@0: CActiveScheduler::Stop(); sl@0: break; sl@0: } sl@0: sl@0: INFO_PRINTF1(_L(" deleting key\n")); sl@0: iKeyStore->DeleteKey(*iKeys[0], iStatus); sl@0: iState = EDeleteAllDelete; sl@0: SetActive(); sl@0: iKeys[0]->Release(); sl@0: iKeys.Remove(0); sl@0: break; sl@0: sl@0: case EInitKeyStore: sl@0: { sl@0: INFO_PRINTF1(_L("Importing keys")); sl@0: TFileName filename; sl@0: filename = iStepPointer->iPrivateKey; sl@0: RFile file; sl@0: User::LeaveIfError(file.Open(iFs,filename,EFileRead)); sl@0: CleanupClosePushL(file); sl@0: TInt size; sl@0: User::LeaveIfError(file.Size(size)); sl@0: iKeyData = HBufC8::NewMaxL(size); sl@0: TPtr8 keyPtr = iKeyData->Des(); sl@0: User::LeaveIfError(file.Read(keyPtr)); sl@0: CleanupStack::PopAndDestroy(); // file sl@0: sl@0: TTime start(0.0); sl@0: TTime end(0.0); sl@0: sl@0: // Assumes only one keystore sl@0: // Check parameters! sl@0: ASSERT(iKeyInfo == NULL); sl@0: iKeyStore->ImportKey(0, *(iKeyData), EPKCS15UsageSign, KKeyLabel,0, start, end,iKeyInfo, iStatus); sl@0: iState = EImportKey; sl@0: SetActive(); sl@0: break; sl@0: } sl@0: case EImportKey: sl@0: { sl@0: INFO_PRINTF1(_L("Setting security policy for new stored key")); sl@0: TSecureId secureId(0x101f7784); // Application secure ID sl@0: TSecurityPolicy securePolicy(secureId,ECapabilityReadUserData); sl@0: iKeyStore->SetUsePolicy(iKeyInfo->Handle(),securePolicy,iStatus); sl@0: iState = EKeyPolicy; sl@0: SetActive(); sl@0: break; sl@0: } sl@0: case EKeyPolicy: sl@0: { sl@0: iAttrCollection=CPKCS10Attributes::NewL(); sl@0: CleanupStack::PushL(iAttrCollection); sl@0: INFO_PRINTF1(_L("Adding generic attributes")); sl@0: AddGenericAttributesL(); sl@0: INFO_PRINTF1(_L("Adding Challenge password")); sl@0: AddChallengePasswordL(); sl@0: INFO_PRINTF1(_L("Adding V3 extensions")); sl@0: AddV3ExtensionsL(); sl@0: INFO_PRINTF1(_L("Generating distinguished name")); sl@0: iDN=MakeDistinguishedNameL(); sl@0: CleanupStack::PushL(iDN); sl@0: INFO_PRINTF1(_L("Generating cert request")); sl@0: iCertRequest=CPKCS10Request::NewL(*iDN,*iKeyInfo,iAttrCollection); sl@0: CleanupStack::PushL(iCertRequest); sl@0: INFO_PRINTF1(_L("Setting digest algorithm")); sl@0: TAlgorithmId digestAlgo=iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg); sl@0: iCertRequest->SetDigestAlgL(digestAlgo); sl@0: INFO_PRINTF1(_L("Requesting cert request encoding")); sl@0: sl@0: // Clean up sl@0: CleanupStack::Pop(iCertRequest); sl@0: CleanupStack::Pop(iDN); sl@0: CleanupStack::Pop(iAttrCollection); sl@0: iAttrCollection=NULL; sl@0: iOutputASN1Encoding=NULL; sl@0: iCertRequest->CreateEncoding(iOutputASN1Encoding,iStatus); sl@0: iState=EGenerateCertRequest; sl@0: SetActive(); sl@0: sl@0: iCertRequest->Cancel(); sl@0: sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: INFO_PRINTF1(_L("Cert Request Active tester: State corrupted.")); sl@0: User::Leave(KErrCorrupt); sl@0: } sl@0: } sl@0: sl@0: return; sl@0: }