|
1 /* |
|
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "tactionsetreadpfs.h" |
|
20 #include "t_input.h" |
|
21 #include <cryptostrength.h> |
|
22 #include <securityerr.h> |
|
23 #include <pbedata.h> |
|
24 #include <f32file.h> |
|
25 #include <s32file.h> |
|
26 #include <stdlib.h> |
|
27 #include <s32mem.h> |
|
28 #include <s32std.h> |
|
29 |
|
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"); |
|
41 |
|
42 CTestAction* CActionSetReadPFS::NewL(RFs& aFs, |
|
43 CConsoleBase& aConsole, |
|
44 Output& aOut, |
|
45 const TTestActionSpec& aTestActionSpec) |
|
46 { |
|
47 CTestAction* self = CActionSetReadPFS::NewLC(aFs, aConsole, |
|
48 aOut, aTestActionSpec); |
|
49 CleanupStack::Pop(); |
|
50 return self; |
|
51 } |
|
52 |
|
53 CTestAction* CActionSetReadPFS::NewLC(RFs& aFs, |
|
54 CConsoleBase& aConsole, |
|
55 Output& aOut, |
|
56 const TTestActionSpec& aTestActionSpec) |
|
57 { |
|
58 CActionSetReadPFS* self = new(ELeave) CActionSetReadPFS(aFs, aConsole, aOut); |
|
59 CleanupStack::PushL(self); |
|
60 self->ConstructL(aTestActionSpec); |
|
61 return self; |
|
62 } |
|
63 |
|
64 CActionSetReadPFS::~CActionSetReadPFS() |
|
65 { |
|
66 delete iBody; |
|
67 } |
|
68 |
|
69 CActionSetReadPFS::CActionSetReadPFS(RFs& aFs, |
|
70 CConsoleBase& aConsole, |
|
71 Output& aOut) |
|
72 |
|
73 : CTestAction(aConsole, aOut), iFs(aFs) |
|
74 { |
|
75 } |
|
76 |
|
77 void CActionSetReadPFS::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
78 { |
|
79 CTestAction::ConstructL(aTestActionSpec); |
|
80 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
81 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
82 |
|
83 } |
|
84 |
|
85 void CActionSetReadPFS::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
86 { |
|
87 TRequestStatus* status = &aStatus; |
|
88 TInt err = KErrNone; |
|
89 TInt pos = 0; |
|
90 TPtrC8 encryptElement = Input::ParseElement(*iBody, KReadPFSStart, |
|
91 KReadPFSEnd, pos, err); |
|
92 |
|
93 pos = 0; |
|
94 TPtrC8 strengthTemp = Input::ParseElement(encryptElement, KStrengthStart, |
|
95 KStrengthEnd, pos, err); |
|
96 |
|
97 TDriveUnit sysDrive (RFs::GetSystemDrive()); |
|
98 if (strengthTemp.CompareF(KStrong)) |
|
99 { |
|
100 iFileName = sysDrive.Name(); |
|
101 iFileName.Append(KStrongFileName); |
|
102 } |
|
103 |
|
104 else |
|
105 { |
|
106 iFileName = sysDrive.Name(); |
|
107 iFileName.Append(KWeakFileName); |
|
108 } |
|
109 |
|
110 pos = 0; |
|
111 TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, |
|
112 KPasswdEnd, pos, err); |
|
113 iPasswd = HBufC::NewL(passwdTemp.Length()); |
|
114 TPtr16 passwdTemp3( iPasswd->Des()); |
|
115 passwdTemp3.Copy(passwdTemp); |
|
116 |
|
117 pos = 0; |
|
118 TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, |
|
119 KInputEnd, pos, err); |
|
120 iInput = HBufC8::NewL(inputTemp.Length()); |
|
121 *iInput = inputTemp; |
|
122 |
|
123 User::RequestComplete(status, KErrNone); |
|
124 iActionState = CTestAction::EAction; |
|
125 } |
|
126 |
|
127 void CActionSetReadPFS::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
128 { |
|
129 TRequestStatus* status = &aStatus; |
|
130 delete iPasswd; |
|
131 delete iInput; |
|
132 |
|
133 iFinished = ETrue; |
|
134 User::RequestComplete(status, KErrNone); |
|
135 } |
|
136 |
|
137 void CActionSetReadPFS::DoReportAction(void) |
|
138 { |
|
139 } |
|
140 |
|
141 void CActionSetReadPFS::DoCheckResult(TInt) |
|
142 { |
|
143 |
|
144 } |
|
145 |
|
146 void CActionSetReadPFS::PerformAction(TRequestStatus& aStatus) |
|
147 { |
|
148 __UHEAP_MARK; |
|
149 TRequestStatus* status = &aStatus; |
|
150 iResult = EFalse; |
|
151 |
|
152 //Change the password by appending the letter 'a' to it |
|
153 HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1); |
|
154 TPtr newPassword = newPasswordTemp->Des(); |
|
155 newPassword.Copy(*iPasswd); |
|
156 newPassword.Append('a'); |
|
157 |
|
158 //prepare to read the streams back in, creating a new TPBEncryptionData |
|
159 RStoreReadStream read; |
|
160 // open the next PFS |
|
161 CFileStore *store = CPermanentFileStore::OpenLC(iFs, iFileName, EFileRead | EFileWrite); |
|
162 TStreamId dataStreamId(2); // we know it was the second stream written |
|
163 read.OpenLC(*store, dataStreamId); |
|
164 CleanupStack::Pop(); |
|
165 //read in Encryption data |
|
166 CPBEncryptionData* data = CPBEncryptionData::NewL(read); |
|
167 read.Close(); |
|
168 CleanupStack::PushL(data); |
|
169 |
|
170 //read in encrypted master key |
|
171 TStreamId keyStreamId(1); // we know it was the first stream written |
|
172 read.OpenLC(*store, keyStreamId); |
|
173 CleanupStack::Pop(); |
|
174 HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number |
|
175 read.Close(); |
|
176 //create a new set encryption class |
|
177 CPBEncryptSet* set = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword); |
|
178 |
|
179 //read in ciphertext key |
|
180 TStreamId cipherId(3); // we know it was the third stream written |
|
181 read.OpenLC(*store, cipherId); |
|
182 CleanupStack::Pop(); |
|
183 HBufC8* ciphertextTemp = HBufC8::NewLC(read, 10000); //some large number |
|
184 read.Close(); |
|
185 TPtr8 ciphertext = ciphertextTemp->Des(); |
|
186 |
|
187 HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length()); |
|
188 TPtr8 plaintext = plaintextTemp->Des(); |
|
189 |
|
190 // weak crypto should fail if trying to decrypt strong |
|
191 TRAPD(err, |
|
192 { |
|
193 CPBDecryptor* decryptor = set->NewDecryptLC(); |
|
194 decryptor->Process(ciphertext, plaintext); |
|
195 |
|
196 //this Mid call is due to get rid of the decrypted padding at the end |
|
197 if ((plaintext.Mid(0,iInput->Length()) == *iInput) && |
|
198 !((TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName))) |
|
199 { |
|
200 iResult = ETrue; |
|
201 } |
|
202 CleanupStack::PopAndDestroy(decryptor); |
|
203 }); |
|
204 |
|
205 if ((err == KErrKeyNotWeakEnough) && |
|
206 (TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName)) |
|
207 { |
|
208 iResult = ETrue; |
|
209 } |
|
210 |
|
211 CleanupStack::PopAndDestroy(plaintextTemp); |
|
212 CleanupStack::PopAndDestroy(ciphertextTemp); |
|
213 CleanupStack::PopAndDestroy(set); |
|
214 CleanupStack::PopAndDestroy(encryptedMasterKey); |
|
215 CleanupStack::PopAndDestroy(data); |
|
216 CleanupStack::PopAndDestroy(store); |
|
217 CleanupStack::PopAndDestroy(newPasswordTemp); |
|
218 |
|
219 User::RequestComplete(status, KErrNone); |
|
220 iActionState = CTestAction::EPostrequisite; |
|
221 __UHEAP_MARKEND; |
|
222 } |
|
223 |
|
224 void CActionSetReadPFS::Hex(HBufC8& aString) |
|
225 { |
|
226 TPtr8 ptr=aString.Des(); |
|
227 if (aString.Length()%2) |
|
228 { |
|
229 ptr.SetLength(0); |
|
230 return; |
|
231 } |
|
232 TInt i; |
|
233 for (i=0;i<aString.Length();i+=2) |
|
234 { |
|
235 TUint8 tmp; |
|
236 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0')); |
|
237 tmp*=16; |
|
238 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); |
|
239 ptr[i/2]=tmp; |
|
240 } |
|
241 ptr.SetLength(aString.Length()/2); |
|
242 } |