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 "tactionset.h" sl@0: #include "t_input.h" sl@0: sl@0: _LIT8(KSetStart, ""); sl@0: _LIT8(KSetEnd, ""); sl@0: sl@0: CTestAction* CActionSet::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CActionSet::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CActionSet::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CActionSet* self = new(ELeave) CActionSet(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CActionSet::~CActionSet() sl@0: { sl@0: delete iBody; sl@0: } sl@0: sl@0: CActionSet::CActionSet(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 CActionSet::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 CActionSet::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, KSetStart, sl@0: KSetEnd, pos, err); 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(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; // Default value if the tag is missing sl@0: } sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EAction; sl@0: } sl@0: sl@0: void CActionSet::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 CActionSet::DoReportAction(void) sl@0: { sl@0: } sl@0: sl@0: void CActionSet::DoCheckResult(TInt) sl@0: { sl@0: sl@0: } sl@0: sl@0: void CActionSet::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: set may be used before being set sl@0: CPBEncryptSet* set = 0; sl@0: if (iKdf == 0) sl@0: { sl@0: CleanupStack::PushL(pkcs12Pwd); sl@0: set = CPBEncryptSet::NewLC(*iPasswd, iCipher); 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::NewLC(); 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: set = CPBEncryptSet::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: set = CPBEncryptSet::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(set); sl@0: } sl@0: CPBEncryptor* encryptor = set->NewEncryptLC(); sl@0: HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); sl@0: sl@0: TPtr8 ciphertext = ciphertextTemp->Des(); sl@0: encryptor->ProcessFinalL(*iInput, ciphertext); sl@0: TBuf<128> newPwdTemp(*iPasswd); sl@0: newPwdTemp.Append('a'); sl@0: sl@0: TBuf8<128> newPwdTemp8; sl@0: sl@0: TPBPassword newPassword(KNullDesC); sl@0: if (pkcs12Pwd == 0) sl@0: new(&newPassword) TPBPassword(newPwdTemp); sl@0: else sl@0: { sl@0: HBufC8* newPwd = PKCS12KDF::GeneratePasswordLC(newPwdTemp); sl@0: newPwdTemp8.Copy(*newPwd); sl@0: new(&newPassword) TPBPassword(newPwdTemp8); sl@0: CleanupStack::PopAndDestroy(newPwd); sl@0: } sl@0: sl@0: set->ChangePasswordL(newPassword); 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 encrypted master key to a stream sl@0: TStreamId keyStreamId = write.CreateLC(*store); sl@0: write << set->EncryptedMasterKey(); sl@0: write.CommitL(); sl@0: CleanupStack::PopAndDestroy(); //CreateLC() sl@0: sl@0: //write the encryption data to another stream sl@0: TStreamId dataStreamId = write.CreateLC(*store); sl@0: set->EncryptionData().ExternalizeL(write); sl@0: write.CommitL(); sl@0: CleanupStack::PopAndDestroy(); //CreateLC() sl@0: sl@0: //prepare to read the streams back in, creating a new TPBEncryptionData 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: //read in encrypted master key sl@0: read.OpenLC(*store, keyStreamId); sl@0: HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number sl@0: sl@0: //create a new set encryption class sl@0: CPBEncryptSet* set2 = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword); sl@0: sl@0: HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length()); sl@0: TPtr8 plaintext = plaintextTemp->Des(); sl@0: sl@0: CPBDecryptor* decryptor = set2->NewDecryptLC(); 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(decryptor); sl@0: CleanupStack::PopAndDestroy(plaintextTemp); sl@0: CleanupStack::PopAndDestroy(set2); sl@0: CleanupStack::PopAndDestroy(encryptedMasterKey); sl@0: CleanupStack::PopAndDestroy(1); //OpenLC sl@0: CleanupStack::PopAndDestroy(data); sl@0: CleanupStack::PopAndDestroy(store); sl@0: CleanupStack::PopAndDestroy(ciphertextTemp); sl@0: CleanupStack::PopAndDestroy(encryptor); sl@0: CleanupStack::PopAndDestroy(set); 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 CActionSet::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: }