Update contrib.
2 * Copyright (c) 2004-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.
19 #include "tactionsetreadpfs.h"
21 #include <cryptostrength.h>
22 #include <securityerr.h>
30 _LIT8(KReadPFSStart, "<readpfs>");
31 _LIT8(KReadPFSEnd, "</readpfs>");
32 _LIT8(KStrengthStart, "<strength>");
33 _LIT8(KStrengthEnd, "</strength>");
34 _LIT8(KInputStart, "<input>");
35 _LIT8(KInputEnd, "</input>");
36 _LIT8(KPasswdStart, "<passwd>");
37 _LIT8(KPasswdEnd, "</passwd>");
38 _LIT8(KStrong, "strong");
39 _LIT16(KWeakFileName, "\\tpbe\\weak.dat");
40 _LIT16(KStrongFileName, "\\tpbe\\strong.dat");
42 CTestAction* CActionSetReadPFS::NewL(RFs& aFs,
43 CConsoleBase& aConsole,
45 const TTestActionSpec& aTestActionSpec)
47 CTestAction* self = CActionSetReadPFS::NewLC(aFs, aConsole,
48 aOut, aTestActionSpec);
53 CTestAction* CActionSetReadPFS::NewLC(RFs& aFs,
54 CConsoleBase& aConsole,
56 const TTestActionSpec& aTestActionSpec)
58 CActionSetReadPFS* self = new(ELeave) CActionSetReadPFS(aFs, aConsole, aOut);
59 CleanupStack::PushL(self);
60 self->ConstructL(aTestActionSpec);
64 CActionSetReadPFS::~CActionSetReadPFS()
69 CActionSetReadPFS::CActionSetReadPFS(RFs& aFs,
70 CConsoleBase& aConsole,
73 : CTestAction(aConsole, aOut), iFs(aFs)
77 void CActionSetReadPFS::ConstructL(const TTestActionSpec& aTestActionSpec)
79 CTestAction::ConstructL(aTestActionSpec);
80 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
81 iBody->Des().Copy(aTestActionSpec.iActionBody);
85 void CActionSetReadPFS::DoPerformPrerequisite(TRequestStatus& aStatus)
87 TRequestStatus* status = &aStatus;
90 TPtrC8 encryptElement = Input::ParseElement(*iBody, KReadPFSStart,
91 KReadPFSEnd, pos, err);
93 TPtrC8 strengthTemp = Input::ParseElement(encryptElement, KStrengthStart,
94 KStrengthEnd, pos=0, err);
96 TDriveUnit sysDrive (RFs::GetSystemDrive());
97 if (strengthTemp.CompareF(KStrong))
99 iFileName = sysDrive.Name();
100 iFileName.Append(KStrongFileName);
105 iFileName = sysDrive.Name();
106 iFileName.Append(KWeakFileName);
109 TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart,
110 KPasswdEnd, pos=0, err);
111 iPasswd = HBufC::NewL(passwdTemp.Length());
112 TPtr16 passwdTemp3( iPasswd->Des());
113 passwdTemp3.Copy(passwdTemp);
115 TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart,
116 KInputEnd, pos=0, err);
117 iInput = HBufC8::NewL(inputTemp.Length());
120 User::RequestComplete(status, KErrNone);
121 iActionState = CTestAction::EAction;
124 void CActionSetReadPFS::DoPerformPostrequisite(TRequestStatus& aStatus)
126 TRequestStatus* status = &aStatus;
131 User::RequestComplete(status, KErrNone);
134 void CActionSetReadPFS::DoReportAction(void)
138 void CActionSetReadPFS::DoCheckResult(TInt)
143 void CActionSetReadPFS::PerformAction(TRequestStatus& aStatus)
146 TRequestStatus* status = &aStatus;
149 //Change the password by appending the letter 'a' to it
150 HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
151 TPtr newPassword = newPasswordTemp->Des();
152 newPassword.Copy(*iPasswd);
153 newPassword.Append('a');
155 //prepare to read the streams back in, creating a new TPBEncryptionData
156 RStoreReadStream read;
158 CFileStore *store = CPermanentFileStore::OpenLC(iFs, iFileName, EFileRead | EFileWrite);
159 TStreamId dataStreamId(2); // we know it was the second stream written
160 read.OpenLC(*store, dataStreamId);
162 //read in Encryption data
163 CPBEncryptionData* data = CPBEncryptionData::NewL(read);
165 CleanupStack::PushL(data);
167 //read in encrypted master key
168 TStreamId keyStreamId(1); // we know it was the first stream written
169 read.OpenLC(*store, keyStreamId);
171 HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number
173 //create a new set encryption class
174 CPBEncryptSet* set = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword);
176 //read in ciphertext key
177 TStreamId cipherId(3); // we know it was the third stream written
178 read.OpenLC(*store, cipherId);
180 HBufC8* ciphertextTemp = HBufC8::NewLC(read, 10000); //some large number
182 TPtr8 ciphertext = ciphertextTemp->Des();
184 HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length());
185 TPtr8 plaintext = plaintextTemp->Des();
187 // weak crypto should fail if trying to decrypt strong
190 CPBDecryptor* decryptor = set->NewDecryptLC();
191 decryptor->Process(ciphertext, plaintext);
193 //this Mid call is due to get rid of the decrypted padding at the end
194 if ((plaintext.Mid(0,iInput->Length()) == *iInput) &&
195 !((TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName)))
199 CleanupStack::PopAndDestroy(decryptor);
202 if ((err == KErrKeyNotWeakEnough) &&
203 (TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName))
208 CleanupStack::PopAndDestroy(plaintextTemp);
209 CleanupStack::PopAndDestroy(ciphertextTemp);
210 CleanupStack::PopAndDestroy(set);
211 CleanupStack::PopAndDestroy(encryptedMasterKey);
212 CleanupStack::PopAndDestroy(data);
213 CleanupStack::PopAndDestroy(store);
214 CleanupStack::PopAndDestroy(newPasswordTemp);
216 User::RequestComplete(status, KErrNone);
217 iActionState = CTestAction::EPostrequisite;
221 void CActionSetReadPFS::Hex(HBufC8& aString)
223 TPtr8 ptr=aString.Des();
224 if (aString.Length()%2)
230 for (i=0;i<aString.Length();i+=2)
233 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
235 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
238 ptr.SetLength(aString.Length()/2);