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