sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include <pbedata.h>
|
sl@0
|
20 |
#include <stdlib.h>
|
sl@0
|
21 |
#include <s32mem.h>
|
sl@0
|
22 |
#include <s32std.h>
|
sl@0
|
23 |
#include "tpbe.h"
|
sl@0
|
24 |
#include "tactionset.h"
|
sl@0
|
25 |
#include "t_input.h"
|
sl@0
|
26 |
|
sl@0
|
27 |
_LIT8(KSetStart, "<set>");
|
sl@0
|
28 |
_LIT8(KSetEnd, "</set>");
|
sl@0
|
29 |
|
sl@0
|
30 |
CTestAction* CActionSet::NewL(RFs& aFs,
|
sl@0
|
31 |
CConsoleBase& aConsole,
|
sl@0
|
32 |
Output& aOut,
|
sl@0
|
33 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
CTestAction* self = CActionSet::NewLC(aFs, aConsole,
|
sl@0
|
36 |
aOut, aTestActionSpec);
|
sl@0
|
37 |
CleanupStack::Pop();
|
sl@0
|
38 |
return self;
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
CTestAction* CActionSet::NewLC(RFs& aFs,
|
sl@0
|
42 |
CConsoleBase& aConsole,
|
sl@0
|
43 |
Output& aOut,
|
sl@0
|
44 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
45 |
{
|
sl@0
|
46 |
CActionSet* self = new(ELeave) CActionSet(aFs, aConsole, aOut);
|
sl@0
|
47 |
CleanupStack::PushL(self);
|
sl@0
|
48 |
self->ConstructL(aTestActionSpec);
|
sl@0
|
49 |
return self;
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
CActionSet::~CActionSet()
|
sl@0
|
53 |
{
|
sl@0
|
54 |
delete iBody;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
CActionSet::CActionSet(RFs& aFs,
|
sl@0
|
58 |
CConsoleBase& aConsole,
|
sl@0
|
59 |
Output& aOut)
|
sl@0
|
60 |
|
sl@0
|
61 |
: CTestAction(aConsole, aOut), iFs(aFs)
|
sl@0
|
62 |
{
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
void CActionSet::ConstructL(const TTestActionSpec& aTestActionSpec)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
CTestAction::ConstructL(aTestActionSpec);
|
sl@0
|
68 |
iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
|
sl@0
|
69 |
iBody->Des().Copy(aTestActionSpec.iActionBody);
|
sl@0
|
70 |
|
sl@0
|
71 |
}
|
sl@0
|
72 |
|
sl@0
|
73 |
void CActionSet::DoPerformPrerequisite(TRequestStatus& aStatus)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
TRequestStatus* status = &aStatus;
|
sl@0
|
76 |
TInt err = KErrNone;
|
sl@0
|
77 |
TInt pos = 0;
|
sl@0
|
78 |
TPtrC8 encryptElement = Input::ParseElement(*iBody, KSetStart,
|
sl@0
|
79 |
KSetEnd, pos, err);
|
sl@0
|
80 |
TPtrC8 kdf = Input::ParseElement(*iBody, KKdfStart, KKdfEnd, pos=0, err);
|
sl@0
|
81 |
if (err == KErrNone)
|
sl@0
|
82 |
iKdf = kdf.AllocL();
|
sl@0
|
83 |
|
sl@0
|
84 |
TPtrC8 saltLenBytes = Input::ParseElement(*iBody, KSaltLenBytesStart, KSaltLenBytesEnd, pos=0, err);
|
sl@0
|
85 |
if (err == KErrNone)
|
sl@0
|
86 |
iSaltLenBytes = saltLenBytes.AllocL();
|
sl@0
|
87 |
|
sl@0
|
88 |
TPtrC8 iterCount = Input::ParseElement(*iBody, KIterCountStart, KIterCountEnd, pos=0, err);
|
sl@0
|
89 |
if (err == KErrNone)
|
sl@0
|
90 |
iIterCount = iterCount.AllocL();
|
sl@0
|
91 |
|
sl@0
|
92 |
TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart,
|
sl@0
|
93 |
KPasswdEnd, pos=0, err);
|
sl@0
|
94 |
iPasswd = HBufC::NewL(passwdTemp.Length());
|
sl@0
|
95 |
TPtr16 passwdTemp3( iPasswd->Des());
|
sl@0
|
96 |
passwdTemp3.Copy(passwdTemp);
|
sl@0
|
97 |
|
sl@0
|
98 |
TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart,
|
sl@0
|
99 |
KInputEnd, pos=0, err);
|
sl@0
|
100 |
iInput = HBufC8::NewL(inputTemp.Length());
|
sl@0
|
101 |
*iInput = inputTemp;
|
sl@0
|
102 |
|
sl@0
|
103 |
TPtrC8 cipher = Input::ParseElement(*iBody, KCipherStart, KCipherEnd);
|
sl@0
|
104 |
if (cipher.Compare(KECipherAES_CBC_128) == 0)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
iCipher = ECipherAES_CBC_128;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
else if (cipher.Compare(KECipherAES_CBC_192) == 0)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
iCipher = ECipherAES_CBC_192;
|
sl@0
|
111 |
}
|
sl@0
|
112 |
else if (cipher.Compare(KECipherAES_CBC_256) == 0)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
iCipher = ECipherAES_CBC_256;
|
sl@0
|
115 |
}
|
sl@0
|
116 |
else if (cipher.Compare(KECipherDES_CBC) == 0)
|
sl@0
|
117 |
{
|
sl@0
|
118 |
iCipher = ECipherDES_CBC;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
else if (cipher.Compare(KECipher3DES_CBC) == 0)
|
sl@0
|
121 |
{
|
sl@0
|
122 |
iCipher = ECipher3DES_CBC;
|
sl@0
|
123 |
}
|
sl@0
|
124 |
else if (cipher.Compare(KECipherRC2_CBC_40) == 0)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
iCipher = ECipherRC2_CBC_40;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
else if (cipher.Compare(KECipherRC2_CBC_128) == 0)
|
sl@0
|
129 |
{
|
sl@0
|
130 |
iCipher = ECipherRC2_CBC_128;
|
sl@0
|
131 |
}
|
sl@0
|
132 |
else if (cipher.Compare(KECipherRC2_CBC_40_16) == 0)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
iCipher = ECipherRC2_CBC_40_16;
|
sl@0
|
135 |
}
|
sl@0
|
136 |
else if (cipher.Compare(KECipherRC2_CBC_128_16) == 0)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
iCipher = ECipherRC2_CBC_128_16;
|
sl@0
|
139 |
}
|
sl@0
|
140 |
else if(cipher.Compare(KECipher2Key3DES_CBC) == 0)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
iCipher = ECipher2Key3DES_CBC;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
else if(cipher.Compare(KECipherRC2_CBC_40_5) == 0)
|
sl@0
|
145 |
{
|
sl@0
|
146 |
iCipher = ECipherRC2_CBC_40_5;
|
sl@0
|
147 |
}
|
sl@0
|
148 |
else
|
sl@0
|
149 |
{
|
sl@0
|
150 |
iCipher = ECipherAES_CBC_128; // Default value if the <cipher> tag is missing
|
sl@0
|
151 |
}
|
sl@0
|
152 |
|
sl@0
|
153 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
154 |
iActionState = CTestAction::EAction;
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
void CActionSet::DoPerformPostrequisite(TRequestStatus& aStatus)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
TRequestStatus* status = &aStatus;
|
sl@0
|
160 |
delete iPasswd;
|
sl@0
|
161 |
delete iInput;
|
sl@0
|
162 |
delete iKdf;
|
sl@0
|
163 |
iKdf = 0;
|
sl@0
|
164 |
delete iSaltLenBytes;
|
sl@0
|
165 |
iSaltLenBytes = 0;
|
sl@0
|
166 |
delete iIterCount;
|
sl@0
|
167 |
iIterCount = 0;
|
sl@0
|
168 |
|
sl@0
|
169 |
iFinished = ETrue;
|
sl@0
|
170 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
void CActionSet::DoReportAction(void)
|
sl@0
|
174 |
{
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
void CActionSet::DoCheckResult(TInt)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
void CActionSet::PerformAction(TRequestStatus& aStatus)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
__UHEAP_MARK;
|
sl@0
|
185 |
TRequestStatus* status = &aStatus;
|
sl@0
|
186 |
iResult = EFalse;
|
sl@0
|
187 |
HBufC8* pkcs12Pwd = 0;
|
sl@0
|
188 |
|
sl@0
|
189 |
// default value is NULL to avoid RVCT warning
|
sl@0
|
190 |
// C2874W: set may be used before being set
|
sl@0
|
191 |
CPBEncryptSet* set = 0;
|
sl@0
|
192 |
if (iKdf == 0)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
CleanupStack::PushL(pkcs12Pwd);
|
sl@0
|
195 |
set = CPBEncryptSet::NewLC(*iPasswd, iCipher);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
else
|
sl@0
|
198 |
{
|
sl@0
|
199 |
// if supply KDF, must also supply salt len and iteration count
|
sl@0
|
200 |
ASSERT(iSaltLenBytes != 0 && iIterCount != 0);
|
sl@0
|
201 |
|
sl@0
|
202 |
CPBEncryptParms* ep = CPBEncryptParms::NewLC();
|
sl@0
|
203 |
|
sl@0
|
204 |
ep->SetCipherL(iCipher);
|
sl@0
|
205 |
|
sl@0
|
206 |
TInt saltLenBytes;
|
sl@0
|
207 |
TInt r = TLex8(*iSaltLenBytes).Val(saltLenBytes);
|
sl@0
|
208 |
ASSERT(r == KErrNone);
|
sl@0
|
209 |
ep->ResizeSaltL(saltLenBytes);
|
sl@0
|
210 |
|
sl@0
|
211 |
TInt iterCount;
|
sl@0
|
212 |
r = TLex8(*iIterCount).Val(iterCount);
|
sl@0
|
213 |
ASSERT(r == KErrNone);
|
sl@0
|
214 |
ep->SetIterations(iterCount);
|
sl@0
|
215 |
|
sl@0
|
216 |
CleanupStack::PushL((CBase*)0);
|
sl@0
|
217 |
CleanupStack::Pop((CBase*)0);
|
sl@0
|
218 |
|
sl@0
|
219 |
if (*iKdf == _L8("PKCS#5"))
|
sl@0
|
220 |
{
|
sl@0
|
221 |
ep->SetKdf(CPBEncryptParms::EKdfPkcs5);
|
sl@0
|
222 |
set = CPBEncryptSet::NewL(*iPasswd, *ep);
|
sl@0
|
223 |
}
|
sl@0
|
224 |
else if (*iKdf == _L8("PKCS#12"))
|
sl@0
|
225 |
{
|
sl@0
|
226 |
pkcs12Pwd = PKCS12KDF::GeneratePasswordLC(*iPasswd);
|
sl@0
|
227 |
ep->SetKdf(CPBEncryptParms::EKdfPkcs12);
|
sl@0
|
228 |
set = CPBEncryptSet::NewL(*pkcs12Pwd, *ep);
|
sl@0
|
229 |
CleanupStack::Pop(pkcs12Pwd);
|
sl@0
|
230 |
}
|
sl@0
|
231 |
else
|
sl@0
|
232 |
User::Panic(_L("Unrec KDF"), 0);
|
sl@0
|
233 |
|
sl@0
|
234 |
CleanupStack::PopAndDestroy(ep);
|
sl@0
|
235 |
// encryption could leak here, but for reservation above
|
sl@0
|
236 |
CleanupStack::PushL(pkcs12Pwd);
|
sl@0
|
237 |
CleanupStack::PushL(set);
|
sl@0
|
238 |
}
|
sl@0
|
239 |
CPBEncryptor* encryptor = set->NewEncryptLC();
|
sl@0
|
240 |
HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length()));
|
sl@0
|
241 |
|
sl@0
|
242 |
TPtr8 ciphertext = ciphertextTemp->Des();
|
sl@0
|
243 |
encryptor->ProcessFinalL(*iInput, ciphertext);
|
sl@0
|
244 |
TBuf<128> newPwdTemp(*iPasswd);
|
sl@0
|
245 |
newPwdTemp.Append('a');
|
sl@0
|
246 |
|
sl@0
|
247 |
TBuf8<128> newPwdTemp8;
|
sl@0
|
248 |
|
sl@0
|
249 |
TPBPassword newPassword(KNullDesC);
|
sl@0
|
250 |
if (pkcs12Pwd == 0)
|
sl@0
|
251 |
new(&newPassword) TPBPassword(newPwdTemp);
|
sl@0
|
252 |
else
|
sl@0
|
253 |
{
|
sl@0
|
254 |
HBufC8* newPwd = PKCS12KDF::GeneratePasswordLC(newPwdTemp);
|
sl@0
|
255 |
newPwdTemp8.Copy(*newPwd);
|
sl@0
|
256 |
new(&newPassword) TPBPassword(newPwdTemp8);
|
sl@0
|
257 |
CleanupStack::PopAndDestroy(newPwd);
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
set->ChangePasswordL(newPassword);
|
sl@0
|
261 |
|
sl@0
|
262 |
//create a mem buffer store
|
sl@0
|
263 |
CBufStore* store = CBufStore::NewLC(100);
|
sl@0
|
264 |
RStoreWriteStream write;
|
sl@0
|
265 |
|
sl@0
|
266 |
//write the encrypted master key to a stream
|
sl@0
|
267 |
TStreamId keyStreamId = write.CreateLC(*store);
|
sl@0
|
268 |
write << set->EncryptedMasterKey();
|
sl@0
|
269 |
write.CommitL();
|
sl@0
|
270 |
CleanupStack::PopAndDestroy(); //CreateLC()
|
sl@0
|
271 |
|
sl@0
|
272 |
//write the encryption data to another stream
|
sl@0
|
273 |
TStreamId dataStreamId = write.CreateLC(*store);
|
sl@0
|
274 |
set->EncryptionData().ExternalizeL(write);
|
sl@0
|
275 |
write.CommitL();
|
sl@0
|
276 |
CleanupStack::PopAndDestroy(); //CreateLC()
|
sl@0
|
277 |
|
sl@0
|
278 |
//prepare to read the streams back in, creating a new TPBEncryptionData
|
sl@0
|
279 |
RStoreReadStream read;
|
sl@0
|
280 |
read.OpenLC(*store, dataStreamId);
|
sl@0
|
281 |
|
sl@0
|
282 |
//read in Encryption data
|
sl@0
|
283 |
CPBEncryptionData* data = CPBEncryptionData::NewL(read);
|
sl@0
|
284 |
CleanupStack::PopAndDestroy(); //OpenLC()
|
sl@0
|
285 |
CleanupStack::PushL(data);
|
sl@0
|
286 |
|
sl@0
|
287 |
//read in encrypted master key
|
sl@0
|
288 |
read.OpenLC(*store, keyStreamId);
|
sl@0
|
289 |
HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number
|
sl@0
|
290 |
|
sl@0
|
291 |
//create a new set encryption class
|
sl@0
|
292 |
CPBEncryptSet* set2 = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword);
|
sl@0
|
293 |
|
sl@0
|
294 |
HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length());
|
sl@0
|
295 |
TPtr8 plaintext = plaintextTemp->Des();
|
sl@0
|
296 |
|
sl@0
|
297 |
CPBDecryptor* decryptor = set2->NewDecryptLC();
|
sl@0
|
298 |
decryptor->Process(ciphertext, plaintext);
|
sl@0
|
299 |
|
sl@0
|
300 |
//this Mid call is due to get rid of the decrypted padding at the end
|
sl@0
|
301 |
if(plaintext.Mid(0,iInput->Length()) == *iInput)
|
sl@0
|
302 |
{
|
sl@0
|
303 |
iResult = ETrue;
|
sl@0
|
304 |
}
|
sl@0
|
305 |
|
sl@0
|
306 |
CleanupStack::PopAndDestroy(decryptor);
|
sl@0
|
307 |
CleanupStack::PopAndDestroy(plaintextTemp);
|
sl@0
|
308 |
CleanupStack::PopAndDestroy(set2);
|
sl@0
|
309 |
CleanupStack::PopAndDestroy(encryptedMasterKey);
|
sl@0
|
310 |
CleanupStack::PopAndDestroy(1); //OpenLC
|
sl@0
|
311 |
CleanupStack::PopAndDestroy(data);
|
sl@0
|
312 |
CleanupStack::PopAndDestroy(store);
|
sl@0
|
313 |
CleanupStack::PopAndDestroy(ciphertextTemp);
|
sl@0
|
314 |
CleanupStack::PopAndDestroy(encryptor);
|
sl@0
|
315 |
CleanupStack::PopAndDestroy(set);
|
sl@0
|
316 |
CleanupStack::PopAndDestroy(pkcs12Pwd);
|
sl@0
|
317 |
|
sl@0
|
318 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
319 |
iActionState = CTestAction::EPostrequisite;
|
sl@0
|
320 |
__UHEAP_MARKEND;
|
sl@0
|
321 |
}
|
sl@0
|
322 |
|
sl@0
|
323 |
void CActionSet::Hex(HBufC8& aString)
|
sl@0
|
324 |
{
|
sl@0
|
325 |
TPtr8 ptr=aString.Des();
|
sl@0
|
326 |
if (aString.Length()%2)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
ptr.SetLength(0);
|
sl@0
|
329 |
return;
|
sl@0
|
330 |
}
|
sl@0
|
331 |
TInt i;
|
sl@0
|
332 |
for (i=0;i<aString.Length();i+=2)
|
sl@0
|
333 |
{
|
sl@0
|
334 |
TUint8 tmp;
|
sl@0
|
335 |
tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
|
sl@0
|
336 |
tmp*=16;
|
sl@0
|
337 |
tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
|
sl@0
|
338 |
ptr[i/2]=tmp;
|
sl@0
|
339 |
}
|
sl@0
|
340 |
ptr.SetLength(aString.Length()/2);
|
sl@0
|
341 |
}
|