os/security/crypto/weakcryptospi/test/tpbe/tactionsetwritepfs.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tpbe/tactionsetwritepfs.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,216 @@
     1.4 +/*
     1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "tactionsetwritepfs.h"
    1.23 +#include "t_input.h"
    1.24 +#include <cryptostrength.h>
    1.25 +#include <pbedata.h>
    1.26 +#include <f32file.h>
    1.27 +#include <s32file.h>
    1.28 +#include <stdlib.h>
    1.29 +#include <s32mem.h>
    1.30 +#include <s32std.h>
    1.31 +
    1.32 +_LIT8(KWritePFSStart, "<writepfs>");
    1.33 +_LIT8(KWritePFSEnd, "</writepfs>");
    1.34 +_LIT8(KInputStart, "<input>");
    1.35 +_LIT8(KInputEnd, "</input>");
    1.36 +_LIT8(KPasswdStart, "<passwd>");
    1.37 +_LIT8(KPasswdEnd, "</passwd>");
    1.38 +_LIT16(KWeakFileName, "\\tpbe\\weak.dat");
    1.39 +_LIT16(KStrongFileName, "\\tpbe\\strong.dat");
    1.40 +
    1.41 +CTestAction* CActionSetWritePFS::NewL(RFs& aFs,
    1.42 +									   CConsoleBase& aConsole,
    1.43 +									   Output& aOut, 
    1.44 +									   const TTestActionSpec& aTestActionSpec)
    1.45 +	{
    1.46 +	CTestAction* self = CActionSetWritePFS::NewLC(aFs, aConsole,
    1.47 +		aOut, aTestActionSpec);
    1.48 +	CleanupStack::Pop();
    1.49 +	return self;
    1.50 +	}
    1.51 +
    1.52 +CTestAction* CActionSetWritePFS::NewLC(RFs& aFs,
    1.53 +										CConsoleBase& aConsole,
    1.54 +										Output& aOut, 
    1.55 +										const TTestActionSpec& aTestActionSpec)
    1.56 +	{
    1.57 +	CActionSetWritePFS* self = new(ELeave) CActionSetWritePFS(aFs, aConsole, aOut);
    1.58 +	CleanupStack::PushL(self);
    1.59 +	self->ConstructL(aTestActionSpec);
    1.60 +	return self;
    1.61 +	}
    1.62 +
    1.63 +CActionSetWritePFS::~CActionSetWritePFS()
    1.64 +	{
    1.65 +	delete iBody;
    1.66 +	}
    1.67 +
    1.68 +CActionSetWritePFS::CActionSetWritePFS(RFs& aFs, 
    1.69 +								 CConsoleBase& aConsole,
    1.70 +								 Output& aOut)
    1.71 +								 
    1.72 +: CTestAction(aConsole, aOut), iFs(aFs)
    1.73 +	{
    1.74 +	}
    1.75 +
    1.76 +void CActionSetWritePFS::ConstructL(const TTestActionSpec& aTestActionSpec)
    1.77 +	{
    1.78 +	CTestAction::ConstructL(aTestActionSpec);
    1.79 +	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
    1.80 +	iBody->Des().Copy(aTestActionSpec.iActionBody);
    1.81 +	
    1.82 +	}
    1.83 +
    1.84 +void CActionSetWritePFS::DoPerformPrerequisite(TRequestStatus& aStatus)
    1.85 +	{
    1.86 +	TRequestStatus* status = &aStatus;
    1.87 +	TInt err = KErrNone;
    1.88 +	TInt pos = 0;
    1.89 +	TPtrC8 encryptElement = Input::ParseElement(*iBody, KWritePFSStart,
    1.90 +		KWritePFSEnd, pos, err);
    1.91 +
    1.92 +	TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, 
    1.93 +		KPasswdEnd, pos=0, err);
    1.94 +	iPasswd = HBufC::NewL(passwdTemp.Length());
    1.95 +	TPtr16 passwdTemp3( iPasswd->Des());
    1.96 +	passwdTemp3.Copy(passwdTemp);
    1.97 +
    1.98 +	TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, 
    1.99 +		KInputEnd, pos=0, err);
   1.100 +	iInput = HBufC8::NewL(inputTemp.Length());
   1.101 +	*iInput = inputTemp;
   1.102 +
   1.103 +	User::RequestComplete(status, KErrNone);
   1.104 +	iActionState = CTestAction::EAction;
   1.105 +	}
   1.106 +
   1.107 +void CActionSetWritePFS::DoPerformPostrequisite(TRequestStatus& aStatus)
   1.108 +	{
   1.109 +	TRequestStatus* status = &aStatus;
   1.110 +	delete iPasswd;
   1.111 +	delete iInput;
   1.112 +
   1.113 +	iFinished = ETrue;
   1.114 +	User::RequestComplete(status, KErrNone);
   1.115 +	}
   1.116 +
   1.117 +void CActionSetWritePFS::DoReportAction(void)
   1.118 +	{
   1.119 +	}
   1.120 +
   1.121 +void CActionSetWritePFS::DoCheckResult(TInt)
   1.122 +	{
   1.123 +
   1.124 +	}
   1.125 +
   1.126 +void CActionSetWritePFS::PerformAction(TRequestStatus& aStatus)
   1.127 +	{
   1.128 +	__UHEAP_MARK;
   1.129 +	TRequestStatus* status = &aStatus;
   1.130 +	iResult = EFalse;
   1.131 +	
   1.132 +	CPBEncryptSet* set = CPBEncryptSet::NewL(*iPasswd);
   1.133 +	CleanupStack::PushL(set);
   1.134 +	CPBEncryptor* encryptor = set->NewEncryptLC();
   1.135 +
   1.136 +	HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); 
   1.137 +
   1.138 +	TPtr8 ciphertext = ciphertextTemp->Des();	
   1.139 +	encryptor->ProcessFinalL(*iInput, ciphertext);
   1.140 +	
   1.141 +	TInt cipherTextLength = set->MaxCiphertextLength(iInput->Length());
   1.142 +	TInt plainTextLength = set->MaxPlaintextLength(cipherTextLength);
   1.143 +	if (plainTextLength > cipherTextLength)
   1.144 +		{
   1.145 +		iResult = EFalse;
   1.146 +		}
   1.147 +
   1.148 +	//Change the password by appending the letter 'a' to it
   1.149 +	HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
   1.150 +	TPtr newPassword = newPasswordTemp->Des();
   1.151 +	newPassword.Copy(*iPasswd);
   1.152 +	newPassword.Append('a');
   1.153 +
   1.154 +	set->ChangePasswordL(newPassword);
   1.155 +	
   1.156 +	TDriveUnit sysDrive (RFs::GetSystemDrive());
   1.157 +	TBuf<128> pfsFileName (sysDrive.Name());
   1.158 +
   1.159 +	if (TCrypto::Strength() == TCrypto::EStrong)
   1.160 +		pfsFileName.Append(KStrongFileName);
   1.161 +	else
   1.162 +		pfsFileName.Append(KWeakFileName);
   1.163 +
   1.164 +	RStoreWriteStream write;
   1.165 +	
   1.166 +	CFileStore *store = CPermanentFileStore::ReplaceLC(iFs, pfsFileName, EFileRead | EFileWrite);
   1.167 +	store->SetTypeL(store->Layout());
   1.168 +	
   1.169 +	//write the encrypted master key to a new stream
   1.170 +	write.CreateLC(*store);
   1.171 +	write << set->EncryptedMasterKey();
   1.172 +	write.CommitL();
   1.173 +	CleanupStack::PopAndDestroy(); //CreateLC()
   1.174 +
   1.175 +	//write the encryption data to a new stream
   1.176 +	write.CreateLC(*store);
   1.177 +	set->EncryptionData().ExternalizeL(write);
   1.178 +	write.CommitL();
   1.179 +	CleanupStack::PopAndDestroy(); //CreateLC()
   1.180 +
   1.181 +	//write the cyphertext to a new stream
   1.182 +	write.CreateLC(*store);
   1.183 +	write << ciphertext;
   1.184 +	write.CommitL();
   1.185 +	CleanupStack::PopAndDestroy(); //CreateLC()
   1.186 +
   1.187 +	store->Commit();
   1.188 +
   1.189 +	// Finished writing the PFS
   1.190 +	iResult = ETrue;
   1.191 +	CleanupStack::PopAndDestroy(store);
   1.192 +	CleanupStack::PopAndDestroy(newPasswordTemp);
   1.193 +	CleanupStack::PopAndDestroy(ciphertextTemp);
   1.194 +	CleanupStack::PopAndDestroy(encryptor);
   1.195 +	CleanupStack::PopAndDestroy(set);
   1.196 +	User::RequestComplete(status, KErrNone);
   1.197 +	iActionState = CTestAction::EPostrequisite;
   1.198 +	__UHEAP_MARKEND;
   1.199 +	}
   1.200 +
   1.201 +void CActionSetWritePFS::Hex(HBufC8& aString)
   1.202 +    {
   1.203 +    TPtr8 ptr=aString.Des();
   1.204 +    if (aString.Length()%2)
   1.205 +        {
   1.206 +        ptr.SetLength(0);
   1.207 +        return;
   1.208 +        }
   1.209 +    TInt i;
   1.210 +    for (i=0;i<aString.Length();i+=2)
   1.211 +        {
   1.212 +        TUint8 tmp;
   1.213 +        tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
   1.214 +        tmp*=16;
   1.215 +        tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
   1.216 +        ptr[i/2]=tmp;
   1.217 +        }
   1.218 +    ptr.SetLength(aString.Length()/2);
   1.219 +    }