Update contrib.
2 * Copyright (c) 1998-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.
24 #include "tactionelement.h"
27 _LIT8(KElementStart, "<element>");
28 _LIT8(KElementEnd, "</element>");
30 CTestAction* CActionElement::NewL(RFs& aFs,
31 CConsoleBase& aConsole,
33 const TTestActionSpec& aTestActionSpec)
35 CTestAction* self = CActionElement::NewLC(aFs, aConsole,
36 aOut, aTestActionSpec);
41 CTestAction* CActionElement::NewLC(RFs& aFs,
42 CConsoleBase& aConsole,
44 const TTestActionSpec& aTestActionSpec)
46 CActionElement* self = new(ELeave) CActionElement(aFs, aConsole, aOut);
47 CleanupStack::PushL(self);
48 self->ConstructL(aTestActionSpec);
52 CActionElement::~CActionElement()
57 CActionElement::CActionElement(RFs& aFs,
58 CConsoleBase& aConsole,
61 : CTestAction(aConsole, aOut), iFs(aFs)
65 void CActionElement::ConstructL(const TTestActionSpec& aTestActionSpec)
67 CTestAction::ConstructL(aTestActionSpec);
68 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
69 iBody->Des().Copy(aTestActionSpec.iActionBody);
73 void CActionElement::DoPerformPrerequisite(TRequestStatus& aStatus)
75 TRequestStatus* status = &aStatus;
78 TPtrC8 encryptElement = Input::ParseElement(*iBody, KElementStart,
79 KElementEnd, pos, err);
81 TPtrC8 kdf = Input::ParseElement(*iBody, KKdfStart, KKdfEnd, pos=0, err);
85 TPtrC8 saltLenBytes = Input::ParseElement(*iBody, KSaltLenBytesStart, KSaltLenBytesEnd, pos=0, err);
87 iSaltLenBytes = saltLenBytes.AllocL();
89 TPtrC8 iterCount = Input::ParseElement(*iBody, KIterCountStart, KIterCountEnd, pos=0, err);
91 iIterCount = iterCount.AllocL();
93 TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart,
94 KPasswdEnd, pos=0, err);
95 iPasswd = HBufC::NewL(passwdTemp.Length());
96 TPtr16 passwdTemp3( iPasswd->Des());
97 passwdTemp3.Copy(passwdTemp);
99 TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart,
100 KInputEnd, pos=0, err);
101 iInput = HBufC8::NewL(inputTemp.Length());
104 TPtrC8 cipher = Input::ParseElement(*iBody, KCipherStart, KCipherEnd);
105 if (cipher.Compare(KECipherAES_CBC_128) == 0)
107 iCipher = ECipherAES_CBC_128;
109 else if (cipher.Compare(KECipherAES_CBC_192) == 0)
111 iCipher = ECipherAES_CBC_192;
113 else if (cipher.Compare(KECipherAES_CBC_256) == 0)
115 iCipher = ECipherAES_CBC_256;
117 else if (cipher.Compare(KECipherDES_CBC) == 0)
119 iCipher = ECipherDES_CBC;
121 else if (cipher.Compare(KECipher3DES_CBC) == 0)
123 iCipher = ECipher3DES_CBC;
125 else if (cipher.Compare(KECipherRC2_CBC_40) == 0)
127 iCipher = ECipherRC2_CBC_40;
129 else if (cipher.Compare(KECipherRC2_CBC_128) == 0)
131 iCipher = ECipherRC2_CBC_128;
133 else if (cipher.Compare(KECipherRC2_CBC_40_16) == 0)
135 iCipher = ECipherRC2_CBC_40_16;
137 else if (cipher.Compare(KECipherRC2_CBC_128_16) == 0)
139 iCipher = ECipherRC2_CBC_128_16;
141 else if(cipher.Compare(KECipherARC4_128) == 0)
143 iCipher = ECipherARC4_128;
145 else if(cipher.Compare(KECipherARC4_40) == 0)
147 iCipher = ECipherARC4_40;
149 else if(cipher.Compare(KECipher2Key3DES_CBC) == 0)
151 iCipher = ECipher2Key3DES_CBC;
153 else if(cipher.Compare(KECipherRC2_CBC_40_5) == 0)
155 iCipher = ECipherRC2_CBC_40_5;
159 iCipher = ECipherAES_CBC_128;
162 User::RequestComplete(status, KErrNone);
163 iActionState = CTestAction::EAction;
166 void CActionElement::DoPerformPostrequisite(TRequestStatus& aStatus)
168 TRequestStatus* status = &aStatus;
173 delete iSaltLenBytes;
179 User::RequestComplete(status, KErrNone);
182 void CActionElement::DoReportAction(void)
186 void CActionElement::DoCheckResult(TInt)
191 void CActionElement::PerformAction(TRequestStatus& aStatus)
194 TRequestStatus* status = &aStatus;
196 HBufC8* pkcs12Pwd = 0;
198 // default value is NULL to avoid RVCT warning
199 // C2874W: encryption may be used before being set
200 CPBEncryptElement* encryption = 0;
203 CleanupStack::PushL(pkcs12Pwd);
204 encryption = CPBEncryptElement::NewL(*iPasswd, iCipher);
205 CleanupStack::PushL(encryption);
209 // if supply KDF, must also supply salt len and iteration count
210 ASSERT(iSaltLenBytes != 0 && iIterCount != 0);
212 CPBEncryptParms* ep = CPBEncryptParms::NewL();
213 CleanupStack::PushL(ep);
215 ep->SetCipherL(iCipher);
218 TInt r = TLex8(*iSaltLenBytes).Val(saltLenBytes);
219 ASSERT(r == KErrNone);
220 ep->ResizeSaltL(saltLenBytes);
223 r = TLex8(*iIterCount).Val(iterCount);
224 ASSERT(r == KErrNone);
225 ep->SetIterations(iterCount);
227 CleanupStack::PushL((CBase*)0);
228 CleanupStack::Pop((CBase*)0);
230 if (*iKdf == _L8("PKCS#5"))
232 ep->SetKdf(CPBEncryptParms::EKdfPkcs5);
233 encryption = CPBEncryptElement::NewL(*iPasswd, *ep);
235 else if (*iKdf == _L8("PKCS#12"))
237 pkcs12Pwd = PKCS12KDF::GeneratePasswordLC(*iPasswd);
238 ep->SetKdf(CPBEncryptParms::EKdfPkcs12);
239 encryption = CPBEncryptElement::NewL(*pkcs12Pwd, *ep);
240 CleanupStack::Pop(pkcs12Pwd);
243 User::Panic(_L("Unrec KDF"), 0);
245 CleanupStack::PopAndDestroy(ep);
246 // encryption could leak here, but for reservation above
247 CleanupStack::PushL(pkcs12Pwd);
248 CleanupStack::PushL(encryption);
251 TInt cipherTextLength = encryption->MaxCiphertextLength(iInput->Length());
252 TInt plainTextLength = encryption->MaxPlaintextLength(cipherTextLength);
253 if (plainTextLength != (cipherTextLength-1))
258 CPBEncryptor* encryptor = encryption->NewEncryptL();
259 CleanupStack::PushL(encryptor);
261 HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length()));
262 TPtr8 ciphertext = ciphertextTemp->Des();
263 encryptor->ProcessFinalL(*iInput, ciphertext);
265 //create a mem buffer store
266 CBufStore* store = CBufStore::NewLC(100);
267 RStoreWriteStream write;
269 //write the encryption data to a stream
270 TStreamId dataStreamId = write.CreateLC(*store);
271 encryption->EncryptionData().ExternalizeL(write);
273 CleanupStack::PopAndDestroy(); //CreateLC()
275 //prepare to read the stream back in
276 RStoreReadStream read;
277 read.OpenLC(*store, dataStreamId);
279 //read in Encryption Data
280 CPBEncryptionData* data = CPBEncryptionData::NewL(read);
281 CleanupStack::PopAndDestroy(); //OpenLC()
282 CleanupStack::PushL(data);
284 CPBEncryptElement* encryption1 =
286 ? CPBEncryptElement::NewLC(*data, *iPasswd)
287 : CPBEncryptElement::NewLC(*data, *pkcs12Pwd);
289 CPBDecryptor* decryptor = encryption1->NewDecryptL();
290 CleanupStack::PushL(decryptor);
291 HBufC8* plaintextTemp = HBufC8::NewLC(decryptor->MaxOutputLength(ciphertext.Size()));
292 TPtr8 plaintext = plaintextTemp->Des();
293 decryptor->Process(ciphertext, plaintext);
295 //this Mid call is due to get rid of the decrypted padding at the end
296 if(plaintext.Mid(0,iInput->Length()) == *iInput)
301 CleanupStack::PopAndDestroy(5); //plaintextTemp,decryptor,encryption1,data,store
303 CPBEncryptionData* data1 = CPBEncryptionData::NewLC(encryption->EncryptionData());
305 CPBEncryptElement* encryption2 =
307 ? CPBEncryptElement::NewLC(*data1, *iPasswd)
308 : CPBEncryptElement::NewLC(*data1, *pkcs12Pwd);
310 CPBDecryptor* decryptor1 = encryption2->NewDecryptLC();
311 HBufC8* plaintextTemp2 = HBufC8::NewLC(decryptor1->MaxOutputLength(ciphertext.Size()));
312 TPtr8 plaintext2 = plaintextTemp2->Des();
313 decryptor1->Process(ciphertext, plaintext2);
315 //this Mid call is due to get rid of the decrypted padding at the end
316 if(!(plaintext2.Mid(0,iInput->Length()) == *iInput))
321 CleanupStack::PopAndDestroy(plaintextTemp2);
322 CleanupStack::PopAndDestroy(decryptor1);
323 CleanupStack::PopAndDestroy(encryption2);
324 CleanupStack::PopAndDestroy(data1);
325 CleanupStack::PopAndDestroy(ciphertextTemp);
326 CleanupStack::PopAndDestroy(encryptor);
327 CleanupStack::PopAndDestroy(encryption);
328 CleanupStack::PopAndDestroy(pkcs12Pwd);
330 User::RequestComplete(status, KErrNone);
331 iActionState = CTestAction::EPostrequisite;
335 void CActionElement::Hex(HBufC8& aString)
337 TPtr8 ptr=aString.Des();
338 if (aString.Length()%2)
344 for (i=0;i<aString.Length();i+=2)
347 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
349 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
352 ptr.SetLength(aString.Length()/2);