sl@0: /* sl@0: * Copyright (c) 2004-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: * sl@0: */ sl@0: sl@0: sl@0: #include "tactionsetreadpfs.h" sl@0: #include "t_input.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: _LIT8(KReadPFSStart, ""); sl@0: _LIT8(KReadPFSEnd, ""); sl@0: _LIT8(KStrengthStart, ""); sl@0: _LIT8(KStrengthEnd, ""); sl@0: _LIT8(KInputStart, ""); sl@0: _LIT8(KInputEnd, ""); sl@0: _LIT8(KPasswdStart, ""); sl@0: _LIT8(KPasswdEnd, ""); sl@0: _LIT8(KStrong, "strong"); sl@0: _LIT16(KWeakFileName, "\\tpbe\\weak.dat"); sl@0: _LIT16(KStrongFileName, "\\tpbe\\strong.dat"); sl@0: sl@0: CTestAction* CActionSetReadPFS::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionSetReadPFS::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionSetReadPFS::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionSetReadPFS* self = new(ELeave) CActionSetReadPFS(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionSetReadPFS::~CActionSetReadPFS() sl@0: { sl@0: delete iBody; sl@0: } sl@0: sl@0: CActionSetReadPFS::CActionSetReadPFS(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: sl@0: : CTestAction(aConsole, aOut), iFs(aFs) sl@0: { sl@0: } sl@0: sl@0: void CActionSetReadPFS::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction::ConstructL(aTestActionSpec); sl@0: iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); sl@0: iBody->Des().Copy(aTestActionSpec.iActionBody); sl@0: sl@0: } sl@0: sl@0: void CActionSetReadPFS::DoPerformPrerequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: TPtrC8 encryptElement = Input::ParseElement(*iBody, KReadPFSStart, sl@0: KReadPFSEnd, pos, err); sl@0: sl@0: TPtrC8 strengthTemp = Input::ParseElement(encryptElement, KStrengthStart, sl@0: KStrengthEnd, pos=0, err); sl@0: sl@0: TDriveUnit sysDrive (RFs::GetSystemDrive()); sl@0: if (strengthTemp.CompareF(KStrong)) sl@0: { sl@0: iFileName = sysDrive.Name(); sl@0: iFileName.Append(KStrongFileName); sl@0: } sl@0: sl@0: else sl@0: { sl@0: iFileName = sysDrive.Name(); sl@0: iFileName.Append(KWeakFileName); sl@0: } sl@0: sl@0: TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, sl@0: KPasswdEnd, pos=0, err); sl@0: iPasswd = HBufC::NewL(passwdTemp.Length()); sl@0: TPtr16 passwdTemp3( iPasswd->Des()); sl@0: passwdTemp3.Copy(passwdTemp); sl@0: sl@0: TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, sl@0: KInputEnd, pos=0, err); sl@0: iInput = HBufC8::NewL(inputTemp.Length()); sl@0: *iInput = inputTemp; sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EAction; sl@0: } sl@0: sl@0: void CActionSetReadPFS::DoPerformPostrequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: delete iPasswd; sl@0: delete iInput; sl@0: sl@0: iFinished = ETrue; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CActionSetReadPFS::DoReportAction(void) sl@0: { sl@0: } sl@0: sl@0: void CActionSetReadPFS::DoCheckResult(TInt) sl@0: { sl@0: sl@0: } sl@0: sl@0: void CActionSetReadPFS::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: __UHEAP_MARK; sl@0: TRequestStatus* status = &aStatus; sl@0: iResult = EFalse; sl@0: sl@0: //Change the password by appending the letter 'a' to it sl@0: HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1); sl@0: TPtr newPassword = newPasswordTemp->Des(); sl@0: newPassword.Copy(*iPasswd); sl@0: newPassword.Append('a'); sl@0: sl@0: //prepare to read the streams back in, creating a new TPBEncryptionData sl@0: RStoreReadStream read; sl@0: // open the next PFS sl@0: CFileStore *store = CPermanentFileStore::OpenLC(iFs, iFileName, EFileRead | EFileWrite); sl@0: TStreamId dataStreamId(2); // we know it was the second stream written sl@0: read.OpenLC(*store, dataStreamId); sl@0: CleanupStack::Pop(); sl@0: //read in Encryption data sl@0: CPBEncryptionData* data = CPBEncryptionData::NewL(read); sl@0: read.Close(); sl@0: CleanupStack::PushL(data); sl@0: sl@0: //read in encrypted master key sl@0: TStreamId keyStreamId(1); // we know it was the first stream written sl@0: read.OpenLC(*store, keyStreamId); sl@0: CleanupStack::Pop(); sl@0: HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number sl@0: read.Close(); sl@0: //create a new set encryption class sl@0: CPBEncryptSet* set = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword); sl@0: sl@0: //read in ciphertext key sl@0: TStreamId cipherId(3); // we know it was the third stream written sl@0: read.OpenLC(*store, cipherId); sl@0: CleanupStack::Pop(); sl@0: HBufC8* ciphertextTemp = HBufC8::NewLC(read, 10000); //some large number sl@0: read.Close(); sl@0: TPtr8 ciphertext = ciphertextTemp->Des(); sl@0: sl@0: HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length()); sl@0: TPtr8 plaintext = plaintextTemp->Des(); sl@0: sl@0: // weak crypto should fail if trying to decrypt strong sl@0: TRAPD(err, sl@0: { sl@0: CPBDecryptor* decryptor = set->NewDecryptLC(); sl@0: decryptor->Process(ciphertext, plaintext); sl@0: sl@0: //this Mid call is due to get rid of the decrypted padding at the end sl@0: if ((plaintext.Mid(0,iInput->Length()) == *iInput) && sl@0: !((TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName))) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: CleanupStack::PopAndDestroy(decryptor); sl@0: }); sl@0: sl@0: if ((err == KErrKeyNotWeakEnough) && sl@0: (TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName)) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(plaintextTemp); sl@0: CleanupStack::PopAndDestroy(ciphertextTemp); sl@0: CleanupStack::PopAndDestroy(set); sl@0: CleanupStack::PopAndDestroy(encryptedMasterKey); sl@0: CleanupStack::PopAndDestroy(data); sl@0: CleanupStack::PopAndDestroy(store); sl@0: CleanupStack::PopAndDestroy(newPasswordTemp); sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EPostrequisite; sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: void CActionSetReadPFS::Hex(HBufC8& aString) sl@0: { sl@0: TPtr8 ptr=aString.Des(); sl@0: if (aString.Length()%2) sl@0: { sl@0: ptr.SetLength(0); sl@0: return; sl@0: } sl@0: TInt i; sl@0: for (i=0;i'9'?('A'-10):'0')); sl@0: tmp*=16; sl@0: tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); sl@0: ptr[i/2]=tmp; sl@0: } sl@0: ptr.SetLength(aString.Length()/2); sl@0: }