|
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 TPtrC8 strengthTemp = Input::ParseElement(encryptElement, KStrengthStart, |
|
94 KStrengthEnd, pos=0, err); |
|
95 |
|
96 TDriveUnit sysDrive (RFs::GetSystemDrive()); |
|
97 if (strengthTemp.CompareF(KStrong)) |
|
98 { |
|
99 iFileName = sysDrive.Name(); |
|
100 iFileName.Append(KStrongFileName); |
|
101 } |
|
102 |
|
103 else |
|
104 { |
|
105 iFileName = sysDrive.Name(); |
|
106 iFileName.Append(KWeakFileName); |
|
107 } |
|
108 |
|
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); |
|
114 |
|
115 TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, |
|
116 KInputEnd, pos=0, err); |
|
117 iInput = HBufC8::NewL(inputTemp.Length()); |
|
118 *iInput = inputTemp; |
|
119 |
|
120 User::RequestComplete(status, KErrNone); |
|
121 iActionState = CTestAction::EAction; |
|
122 } |
|
123 |
|
124 void CActionSetReadPFS::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
125 { |
|
126 TRequestStatus* status = &aStatus; |
|
127 delete iPasswd; |
|
128 delete iInput; |
|
129 |
|
130 iFinished = ETrue; |
|
131 User::RequestComplete(status, KErrNone); |
|
132 } |
|
133 |
|
134 void CActionSetReadPFS::DoReportAction(void) |
|
135 { |
|
136 } |
|
137 |
|
138 void CActionSetReadPFS::DoCheckResult(TInt) |
|
139 { |
|
140 |
|
141 } |
|
142 |
|
143 void CActionSetReadPFS::PerformAction(TRequestStatus& aStatus) |
|
144 { |
|
145 __UHEAP_MARK; |
|
146 TRequestStatus* status = &aStatus; |
|
147 iResult = EFalse; |
|
148 |
|
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'); |
|
154 |
|
155 //prepare to read the streams back in, creating a new TPBEncryptionData |
|
156 RStoreReadStream read; |
|
157 // open the next PFS |
|
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); |
|
161 CleanupStack::Pop(); |
|
162 //read in Encryption data |
|
163 CPBEncryptionData* data = CPBEncryptionData::NewL(read); |
|
164 read.Close(); |
|
165 CleanupStack::PushL(data); |
|
166 |
|
167 //read in encrypted master key |
|
168 TStreamId keyStreamId(1); // we know it was the first stream written |
|
169 read.OpenLC(*store, keyStreamId); |
|
170 CleanupStack::Pop(); |
|
171 HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number |
|
172 read.Close(); |
|
173 //create a new set encryption class |
|
174 CPBEncryptSet* set = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword); |
|
175 |
|
176 //read in ciphertext key |
|
177 TStreamId cipherId(3); // we know it was the third stream written |
|
178 read.OpenLC(*store, cipherId); |
|
179 CleanupStack::Pop(); |
|
180 HBufC8* ciphertextTemp = HBufC8::NewLC(read, 10000); //some large number |
|
181 read.Close(); |
|
182 TPtr8 ciphertext = ciphertextTemp->Des(); |
|
183 |
|
184 HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length()); |
|
185 TPtr8 plaintext = plaintextTemp->Des(); |
|
186 |
|
187 // weak crypto should fail if trying to decrypt strong |
|
188 TRAPD(err, |
|
189 { |
|
190 CPBDecryptor* decryptor = set->NewDecryptLC(); |
|
191 decryptor->Process(ciphertext, plaintext); |
|
192 |
|
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))) |
|
196 { |
|
197 iResult = ETrue; |
|
198 } |
|
199 CleanupStack::PopAndDestroy(decryptor); |
|
200 }); |
|
201 |
|
202 if ((err == KErrKeyNotWeakEnough) && |
|
203 (TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName)) |
|
204 { |
|
205 iResult = ETrue; |
|
206 } |
|
207 |
|
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); |
|
215 |
|
216 User::RequestComplete(status, KErrNone); |
|
217 iActionState = CTestAction::EPostrequisite; |
|
218 __UHEAP_MARKEND; |
|
219 } |
|
220 |
|
221 void CActionSetReadPFS::Hex(HBufC8& aString) |
|
222 { |
|
223 TPtr8 ptr=aString.Des(); |
|
224 if (aString.Length()%2) |
|
225 { |
|
226 ptr.SetLength(0); |
|
227 return; |
|
228 } |
|
229 TInt i; |
|
230 for (i=0;i<aString.Length();i+=2) |
|
231 { |
|
232 TUint8 tmp; |
|
233 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0')); |
|
234 tmp*=16; |
|
235 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); |
|
236 ptr[i/2]=tmp; |
|
237 } |
|
238 ptr.SetLength(aString.Length()/2); |
|
239 } |