sl@0: /* sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "tpbe.h" sl@0: #include "tactionelement.h" sl@0: #include "t_input.h" sl@0: sl@0: _LIT8(KElementStart, ""); sl@0: _LIT8(KElementEnd, ""); sl@0: sl@0: CTestAction* CActionElement::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionElement::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionElement::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionElement* self = new(ELeave) CActionElement(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionElement::~CActionElement() sl@0: { sl@0: delete iBody; sl@0: } sl@0: sl@0: CActionElement::CActionElement(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: sl@0: : CTestAction(aConsole, aOut), iFs(aFs) sl@0: { sl@0: } sl@0: sl@0: void CActionElement::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction::ConstructL(aTestActionSpec); sl@0: iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); sl@0: iBody->Des().Copy(aTestActionSpec.iActionBody); sl@0: sl@0: } sl@0: sl@0: void CActionElement::DoPerformPrerequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: TInt err = KErrNone; sl@0: TInt pos = 0; sl@0: TPtrC8 encryptElement = Input::ParseElement(*iBody, KElementStart, sl@0: KElementEnd, pos, err); sl@0: sl@0: TPtrC8 kdf = Input::ParseElement(*iBody, KKdfStart, KKdfEnd, pos=0, err); sl@0: if (err == KErrNone) sl@0: iKdf = kdf.AllocL(); sl@0: sl@0: TPtrC8 saltLenBytes = Input::ParseElement(*iBody, KSaltLenBytesStart, KSaltLenBytesEnd, pos=0, err); sl@0: if (err == KErrNone) sl@0: iSaltLenBytes = saltLenBytes.AllocL(); sl@0: sl@0: TPtrC8 iterCount = Input::ParseElement(*iBody, KIterCountStart, KIterCountEnd, pos=0, err); sl@0: if (err == KErrNone) sl@0: iIterCount = iterCount.AllocL(); sl@0: sl@0: TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, sl@0: KPasswdEnd, pos=0, err); sl@0: iPasswd = HBufC::NewL(passwdTemp.Length()); sl@0: TPtr16 passwdTemp3( iPasswd->Des()); sl@0: passwdTemp3.Copy(passwdTemp); sl@0: sl@0: TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, sl@0: KInputEnd, pos=0, err); sl@0: iInput = HBufC8::NewL(inputTemp.Length()); sl@0: *iInput = inputTemp; sl@0: sl@0: TPtrC8 cipher = Input::ParseElement(*iBody, KCipherStart, KCipherEnd); sl@0: if (cipher.Compare(KECipherAES_CBC_128) == 0) sl@0: { sl@0: iCipher = ECipherAES_CBC_128; sl@0: } sl@0: else if (cipher.Compare(KECipherAES_CBC_192) == 0) sl@0: { sl@0: iCipher = ECipherAES_CBC_192; sl@0: } sl@0: else if (cipher.Compare(KECipherAES_CBC_256) == 0) sl@0: { sl@0: iCipher = ECipherAES_CBC_256; sl@0: } sl@0: else if (cipher.Compare(KECipherDES_CBC) == 0) sl@0: { sl@0: iCipher = ECipherDES_CBC; sl@0: } sl@0: else if (cipher.Compare(KECipher3DES_CBC) == 0) sl@0: { sl@0: iCipher = ECipher3DES_CBC; sl@0: } sl@0: else if (cipher.Compare(KECipherRC2_CBC_40) == 0) sl@0: { sl@0: iCipher = ECipherRC2_CBC_40; sl@0: } sl@0: else if (cipher.Compare(KECipherRC2_CBC_128) == 0) sl@0: { sl@0: iCipher = ECipherRC2_CBC_128; sl@0: } sl@0: else if (cipher.Compare(KECipherRC2_CBC_40_16) == 0) sl@0: { sl@0: iCipher = ECipherRC2_CBC_40_16; sl@0: } sl@0: else if (cipher.Compare(KECipherRC2_CBC_128_16) == 0) sl@0: { sl@0: iCipher = ECipherRC2_CBC_128_16; sl@0: } sl@0: else if(cipher.Compare(KECipherARC4_128) == 0) sl@0: { sl@0: iCipher = ECipherARC4_128; sl@0: } sl@0: else if(cipher.Compare(KECipherARC4_40) == 0) sl@0: { sl@0: iCipher = ECipherARC4_40; sl@0: } sl@0: else if(cipher.Compare(KECipher2Key3DES_CBC) == 0) sl@0: { sl@0: iCipher = ECipher2Key3DES_CBC; sl@0: } sl@0: else if(cipher.Compare(KECipherRC2_CBC_40_5) == 0) sl@0: { sl@0: iCipher = ECipherRC2_CBC_40_5; sl@0: } sl@0: else sl@0: { sl@0: iCipher = ECipherAES_CBC_128; sl@0: } sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EAction; sl@0: } sl@0: sl@0: void CActionElement::DoPerformPostrequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: delete iPasswd; sl@0: delete iInput; sl@0: delete iKdf; sl@0: iKdf = 0; sl@0: delete iSaltLenBytes; sl@0: iSaltLenBytes = 0; sl@0: delete iIterCount; sl@0: iIterCount = 0; sl@0: sl@0: iFinished = ETrue; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CActionElement::DoReportAction(void) sl@0: { sl@0: } sl@0: sl@0: void CActionElement::DoCheckResult(TInt) sl@0: { sl@0: sl@0: } sl@0: sl@0: void CActionElement::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: __UHEAP_MARK; sl@0: TRequestStatus* status = &aStatus; sl@0: iResult = EFalse; sl@0: HBufC8* pkcs12Pwd = 0; sl@0: sl@0: // default value is NULL to avoid RVCT warning sl@0: // C2874W: encryption may be used before being set sl@0: CPBEncryptElement* encryption = 0; sl@0: if (iKdf == 0) sl@0: { sl@0: CleanupStack::PushL(pkcs12Pwd); sl@0: encryption = CPBEncryptElement::NewL(*iPasswd, iCipher); sl@0: CleanupStack::PushL(encryption); sl@0: } sl@0: else sl@0: { sl@0: // if supply KDF, must also supply salt len and iteration count sl@0: ASSERT(iSaltLenBytes != 0 && iIterCount != 0); sl@0: sl@0: CPBEncryptParms* ep = CPBEncryptParms::NewL(); sl@0: CleanupStack::PushL(ep); sl@0: sl@0: ep->SetCipherL(iCipher); sl@0: sl@0: TInt saltLenBytes; sl@0: TInt r = TLex8(*iSaltLenBytes).Val(saltLenBytes); sl@0: ASSERT(r == KErrNone); sl@0: ep->ResizeSaltL(saltLenBytes); sl@0: sl@0: TInt iterCount; sl@0: r = TLex8(*iIterCount).Val(iterCount); sl@0: ASSERT(r == KErrNone); sl@0: ep->SetIterations(iterCount); sl@0: sl@0: CleanupStack::PushL((CBase*)0); sl@0: CleanupStack::Pop((CBase*)0); sl@0: sl@0: if (*iKdf == _L8("PKCS#5")) sl@0: { sl@0: ep->SetKdf(CPBEncryptParms::EKdfPkcs5); sl@0: encryption = CPBEncryptElement::NewL(*iPasswd, *ep); sl@0: } sl@0: else if (*iKdf == _L8("PKCS#12")) sl@0: { sl@0: pkcs12Pwd = PKCS12KDF::GeneratePasswordLC(*iPasswd); sl@0: ep->SetKdf(CPBEncryptParms::EKdfPkcs12); sl@0: encryption = CPBEncryptElement::NewL(*pkcs12Pwd, *ep); sl@0: CleanupStack::Pop(pkcs12Pwd); sl@0: } sl@0: else sl@0: User::Panic(_L("Unrec KDF"), 0); sl@0: sl@0: CleanupStack::PopAndDestroy(ep); sl@0: // encryption could leak here, but for reservation above sl@0: CleanupStack::PushL(pkcs12Pwd); sl@0: CleanupStack::PushL(encryption); sl@0: } sl@0: sl@0: TInt cipherTextLength = encryption->MaxCiphertextLength(iInput->Length()); sl@0: TInt plainTextLength = encryption->MaxPlaintextLength(cipherTextLength); sl@0: if (plainTextLength != (cipherTextLength-1)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: CPBEncryptor* encryptor = encryption->NewEncryptL(); sl@0: CleanupStack::PushL(encryptor); sl@0: sl@0: HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); sl@0: TPtr8 ciphertext = ciphertextTemp->Des(); sl@0: encryptor->ProcessFinalL(*iInput, ciphertext); sl@0: sl@0: //create a mem buffer store sl@0: CBufStore* store = CBufStore::NewLC(100); sl@0: RStoreWriteStream write; sl@0: sl@0: //write the encryption data to a stream sl@0: TStreamId dataStreamId = write.CreateLC(*store); sl@0: encryption->EncryptionData().ExternalizeL(write); sl@0: write.CommitL(); sl@0: CleanupStack::PopAndDestroy(); //CreateLC() sl@0: sl@0: //prepare to read the stream back in sl@0: RStoreReadStream read; sl@0: read.OpenLC(*store, dataStreamId); sl@0: sl@0: //read in Encryption Data sl@0: CPBEncryptionData* data = CPBEncryptionData::NewL(read); sl@0: CleanupStack::PopAndDestroy(); //OpenLC() sl@0: CleanupStack::PushL(data); sl@0: sl@0: CPBEncryptElement* encryption1 = sl@0: (pkcs12Pwd == 0) sl@0: ? CPBEncryptElement::NewLC(*data, *iPasswd) sl@0: : CPBEncryptElement::NewLC(*data, *pkcs12Pwd); sl@0: sl@0: CPBDecryptor* decryptor = encryption1->NewDecryptL(); sl@0: CleanupStack::PushL(decryptor); sl@0: HBufC8* plaintextTemp = HBufC8::NewLC(decryptor->MaxOutputLength(ciphertext.Size())); sl@0: TPtr8 plaintext = plaintextTemp->Des(); sl@0: decryptor->Process(ciphertext, plaintext); sl@0: sl@0: //this Mid call is due to get rid of the decrypted padding at the end sl@0: if(plaintext.Mid(0,iInput->Length()) == *iInput) sl@0: { sl@0: iResult = ETrue; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(5); //plaintextTemp,decryptor,encryption1,data,store sl@0: sl@0: CPBEncryptionData* data1 = CPBEncryptionData::NewLC(encryption->EncryptionData()); sl@0: sl@0: CPBEncryptElement* encryption2 = sl@0: (pkcs12Pwd == 0) sl@0: ? CPBEncryptElement::NewLC(*data1, *iPasswd) sl@0: : CPBEncryptElement::NewLC(*data1, *pkcs12Pwd); sl@0: sl@0: CPBDecryptor* decryptor1 = encryption2->NewDecryptLC(); sl@0: HBufC8* plaintextTemp2 = HBufC8::NewLC(decryptor1->MaxOutputLength(ciphertext.Size())); sl@0: TPtr8 plaintext2 = plaintextTemp2->Des(); sl@0: decryptor1->Process(ciphertext, plaintext2); sl@0: sl@0: //this Mid call is due to get rid of the decrypted padding at the end sl@0: if(!(plaintext2.Mid(0,iInput->Length()) == *iInput)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(plaintextTemp2); sl@0: CleanupStack::PopAndDestroy(decryptor1); sl@0: CleanupStack::PopAndDestroy(encryption2); sl@0: CleanupStack::PopAndDestroy(data1); sl@0: CleanupStack::PopAndDestroy(ciphertextTemp); sl@0: CleanupStack::PopAndDestroy(encryptor); sl@0: CleanupStack::PopAndDestroy(encryption); sl@0: CleanupStack::PopAndDestroy(pkcs12Pwd); sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EPostrequisite; sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: void CActionElement::Hex(HBufC8& aString) sl@0: { sl@0: TPtr8 ptr=aString.Des(); sl@0: if (aString.Length()%2) sl@0: { sl@0: ptr.SetLength(0); sl@0: return; sl@0: } sl@0: TInt i; sl@0: for (i=0;i'9'?('A'-10):'0')); sl@0: tmp*=16; sl@0: tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); sl@0: ptr[i/2]=tmp; sl@0: } sl@0: ptr.SetLength(aString.Length()/2); sl@0: }