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 "tactionsetwritepfs.h"
21 #include <cryptostrength.h>
29 _LIT8(KWritePFSStart, "<writepfs>");
30 _LIT8(KWritePFSEnd, "</writepfs>");
31 _LIT8(KInputStart, "<input>");
32 _LIT8(KInputEnd, "</input>");
33 _LIT8(KPasswdStart, "<passwd>");
34 _LIT8(KPasswdEnd, "</passwd>");
35 _LIT16(KWeakFileName, "\\tpbe\\weak.dat");
36 _LIT16(KStrongFileName, "\\tpbe\\strong.dat");
38 CTestAction* CActionSetWritePFS::NewL(RFs& aFs,
39 CConsoleBase& aConsole,
41 const TTestActionSpec& aTestActionSpec)
43 CTestAction* self = CActionSetWritePFS::NewLC(aFs, aConsole,
44 aOut, aTestActionSpec);
49 CTestAction* CActionSetWritePFS::NewLC(RFs& aFs,
50 CConsoleBase& aConsole,
52 const TTestActionSpec& aTestActionSpec)
54 CActionSetWritePFS* self = new(ELeave) CActionSetWritePFS(aFs, aConsole, aOut);
55 CleanupStack::PushL(self);
56 self->ConstructL(aTestActionSpec);
60 CActionSetWritePFS::~CActionSetWritePFS()
65 CActionSetWritePFS::CActionSetWritePFS(RFs& aFs,
66 CConsoleBase& aConsole,
69 : CTestAction(aConsole, aOut), iFs(aFs)
73 void CActionSetWritePFS::ConstructL(const TTestActionSpec& aTestActionSpec)
75 CTestAction::ConstructL(aTestActionSpec);
76 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
77 iBody->Des().Copy(aTestActionSpec.iActionBody);
81 void CActionSetWritePFS::DoPerformPrerequisite(TRequestStatus& aStatus)
83 TRequestStatus* status = &aStatus;
86 TPtrC8 encryptElement = Input::ParseElement(*iBody, KWritePFSStart,
87 KWritePFSEnd, pos, err);
89 TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart,
90 KPasswdEnd, pos=0, err);
91 iPasswd = HBufC::NewL(passwdTemp.Length());
92 TPtr16 passwdTemp3( iPasswd->Des());
93 passwdTemp3.Copy(passwdTemp);
95 TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart,
96 KInputEnd, pos=0, err);
97 iInput = HBufC8::NewL(inputTemp.Length());
100 User::RequestComplete(status, KErrNone);
101 iActionState = CTestAction::EAction;
104 void CActionSetWritePFS::DoPerformPostrequisite(TRequestStatus& aStatus)
106 TRequestStatus* status = &aStatus;
111 User::RequestComplete(status, KErrNone);
114 void CActionSetWritePFS::DoReportAction(void)
118 void CActionSetWritePFS::DoCheckResult(TInt)
123 void CActionSetWritePFS::PerformAction(TRequestStatus& aStatus)
126 TRequestStatus* status = &aStatus;
129 CPBEncryptSet* set = CPBEncryptSet::NewL(*iPasswd);
130 CleanupStack::PushL(set);
131 CPBEncryptor* encryptor = set->NewEncryptLC();
133 HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length()));
135 TPtr8 ciphertext = ciphertextTemp->Des();
136 encryptor->ProcessFinalL(*iInput, ciphertext);
138 TInt cipherTextLength = set->MaxCiphertextLength(iInput->Length());
139 TInt plainTextLength = set->MaxPlaintextLength(cipherTextLength);
140 if (plainTextLength > cipherTextLength)
145 //Change the password by appending the letter 'a' to it
146 HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
147 TPtr newPassword = newPasswordTemp->Des();
148 newPassword.Copy(*iPasswd);
149 newPassword.Append('a');
151 set->ChangePasswordL(newPassword);
153 TDriveUnit sysDrive (RFs::GetSystemDrive());
154 TBuf<128> pfsFileName (sysDrive.Name());
156 if (TCrypto::Strength() == TCrypto::EStrong)
157 pfsFileName.Append(KStrongFileName);
159 pfsFileName.Append(KWeakFileName);
161 RStoreWriteStream write;
163 CFileStore *store = CPermanentFileStore::ReplaceLC(iFs, pfsFileName, EFileRead | EFileWrite);
164 store->SetTypeL(store->Layout());
166 //write the encrypted master key to a new stream
167 write.CreateLC(*store);
168 write << set->EncryptedMasterKey();
170 CleanupStack::PopAndDestroy(); //CreateLC()
172 //write the encryption data to a new stream
173 write.CreateLC(*store);
174 set->EncryptionData().ExternalizeL(write);
176 CleanupStack::PopAndDestroy(); //CreateLC()
178 //write the cyphertext to a new stream
179 write.CreateLC(*store);
182 CleanupStack::PopAndDestroy(); //CreateLC()
186 // Finished writing the PFS
188 CleanupStack::PopAndDestroy(store);
189 CleanupStack::PopAndDestroy(newPasswordTemp);
190 CleanupStack::PopAndDestroy(ciphertextTemp);
191 CleanupStack::PopAndDestroy(encryptor);
192 CleanupStack::PopAndDestroy(set);
193 User::RequestComplete(status, KErrNone);
194 iActionState = CTestAction::EPostrequisite;
198 void CActionSetWritePFS::Hex(HBufC8& aString)
200 TPtr8 ptr=aString.Des();
201 if (aString.Length()%2)
207 for (i=0;i<aString.Length();i+=2)
210 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
212 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
215 ptr.SetLength(aString.Length()/2);