crypto/weakcrypto/test/tpbe/tactionsetwritepfs.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     1 /*
       
     2 * Copyright (c) 2004-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 "tactionsetwritepfs.h"
       
    20 #include "t_input.h"
       
    21 #include <cryptostrength.h>
       
    22 #include <pbedata.h>
       
    23 #include <f32file.h>
       
    24 #include <s32file.h>
       
    25 #include <stdlib.h>
       
    26 #include <s32mem.h>
       
    27 #include <s32std.h>
       
    28 
       
    29 _LIT8(KWritePFSStart, "<writepfs>");
       
    30 _LIT8(KWritePFSEnd, "</writepfs>");
       
    31 _LIT8(KInputStart, "<input>");
       
    32 _LIT8(KInputEnd, "</input>");
       
    33 _LIT8(KPasswdStart, "<passwd>");
       
    34 _LIT8(KPasswdEnd, "</passwd>");
       
    35 _LIT16(KWeakFileName, "\\tpbe\\weak.dat");
       
    36 _LIT16(KStrongFileName, "\\tpbe\\strong.dat");
       
    37 
       
    38 CTestAction* CActionSetWritePFS::NewL(RFs& aFs,
       
    39 									   CConsoleBase& aConsole,
       
    40 									   Output& aOut, 
       
    41 									   const TTestActionSpec& aTestActionSpec)
       
    42 	{
       
    43 	CTestAction* self = CActionSetWritePFS::NewLC(aFs, aConsole,
       
    44 		aOut, aTestActionSpec);
       
    45 	CleanupStack::Pop();
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 CTestAction* CActionSetWritePFS::NewLC(RFs& aFs,
       
    50 										CConsoleBase& aConsole,
       
    51 										Output& aOut, 
       
    52 										const TTestActionSpec& aTestActionSpec)
       
    53 	{
       
    54 	CActionSetWritePFS* self = new(ELeave) CActionSetWritePFS(aFs, aConsole, aOut);
       
    55 	CleanupStack::PushL(self);
       
    56 	self->ConstructL(aTestActionSpec);
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 CActionSetWritePFS::~CActionSetWritePFS()
       
    61 	{
       
    62 	delete iBody;
       
    63 	}
       
    64 
       
    65 CActionSetWritePFS::CActionSetWritePFS(RFs& aFs, 
       
    66 								 CConsoleBase& aConsole,
       
    67 								 Output& aOut)
       
    68 								 
       
    69 : CTestAction(aConsole, aOut), iFs(aFs)
       
    70 	{
       
    71 	}
       
    72 
       
    73 void CActionSetWritePFS::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    74 	{
       
    75 	CTestAction::ConstructL(aTestActionSpec);
       
    76 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
       
    77 	iBody->Des().Copy(aTestActionSpec.iActionBody);
       
    78 	
       
    79 	}
       
    80 
       
    81 void CActionSetWritePFS::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    82 	{
       
    83 	TRequestStatus* status = &aStatus;
       
    84 	TInt err = KErrNone;
       
    85 	TInt pos = 0;
       
    86 	TPtrC8 encryptElement = Input::ParseElement(*iBody, KWritePFSStart,
       
    87 		KWritePFSEnd, pos, err);
       
    88 
       
    89 	pos = 0;
       
    90 	TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, 
       
    91 		KPasswdEnd, pos, err);
       
    92 	iPasswd = HBufC::NewL(passwdTemp.Length());
       
    93 	TPtr16 passwdTemp3( iPasswd->Des());
       
    94 	passwdTemp3.Copy(passwdTemp);
       
    95 
       
    96 	pos = 0;
       
    97 	TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, 
       
    98 		KInputEnd, pos, err);
       
    99 	iInput = HBufC8::NewL(inputTemp.Length());
       
   100 	*iInput = inputTemp;
       
   101 
       
   102 	User::RequestComplete(status, KErrNone);
       
   103 	iActionState = CTestAction::EAction;
       
   104 	}
       
   105 
       
   106 void CActionSetWritePFS::DoPerformPostrequisite(TRequestStatus& aStatus)
       
   107 	{
       
   108 	TRequestStatus* status = &aStatus;
       
   109 	delete iPasswd;
       
   110 	delete iInput;
       
   111 
       
   112 	iFinished = ETrue;
       
   113 	User::RequestComplete(status, KErrNone);
       
   114 	}
       
   115 
       
   116 void CActionSetWritePFS::DoReportAction(void)
       
   117 	{
       
   118 	}
       
   119 
       
   120 void CActionSetWritePFS::DoCheckResult(TInt)
       
   121 	{
       
   122 
       
   123 	}
       
   124 
       
   125 void CActionSetWritePFS::PerformAction(TRequestStatus& aStatus)
       
   126 	{
       
   127 	__UHEAP_MARK;
       
   128 	TRequestStatus* status = &aStatus;
       
   129 	iResult = EFalse;
       
   130 	
       
   131 	CPBEncryptSet* set = CPBEncryptSet::NewLC(*iPasswd);
       
   132 	CPBEncryptor* encryptor = set->NewEncryptLC();
       
   133 
       
   134 	HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); 
       
   135 
       
   136 	TPtr8 ciphertext = ciphertextTemp->Des();	
       
   137 	encryptor->ProcessFinalL(*iInput, ciphertext);
       
   138 
       
   139 	//Change the password by appending the letter 'a' to it
       
   140 	HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
       
   141 	TPtr newPassword = newPasswordTemp->Des();
       
   142 	newPassword.Copy(*iPasswd);
       
   143 	newPassword.Append('a');
       
   144 
       
   145 	set->ChangePasswordL(newPassword);
       
   146 	
       
   147 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   148 	TDriveName driveName(sysDrive.Name());
       
   149 	TBuf<128> pfsFileName (driveName);
       
   150 
       
   151 	if (TCrypto::Strength() == TCrypto::EStrong)
       
   152 		pfsFileName.Append(KStrongFileName);
       
   153 	else
       
   154 		pfsFileName.Append(KWeakFileName);
       
   155 
       
   156 	RStoreWriteStream write;
       
   157 	
       
   158 	CFileStore *store = CPermanentFileStore::ReplaceLC(iFs, pfsFileName, EFileRead | EFileWrite);
       
   159 	store->SetTypeL(store->Layout());
       
   160 	
       
   161 	//write the encrypted master key to a new stream
       
   162 	write.CreateLC(*store);
       
   163 	write << set->EncryptedMasterKey();
       
   164 	write.CommitL();
       
   165 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   166 
       
   167 	//write the encryption data to a new stream
       
   168 	write.CreateLC(*store);
       
   169 	set->EncryptionData().ExternalizeL(write);
       
   170 	write.CommitL();
       
   171 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   172 
       
   173 	//write the cyphertext to a new stream
       
   174 	write.CreateLC(*store);
       
   175 	write << ciphertext;
       
   176 	write.CommitL();
       
   177 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   178 
       
   179 	store->Commit();
       
   180 
       
   181 	// Finished writing the PFS
       
   182 	iResult = ETrue;
       
   183 	CleanupStack::PopAndDestroy(store);
       
   184 	CleanupStack::PopAndDestroy(newPasswordTemp);
       
   185 	CleanupStack::PopAndDestroy(ciphertextTemp);
       
   186 	CleanupStack::PopAndDestroy(encryptor);
       
   187 	CleanupStack::PopAndDestroy(set);
       
   188 	User::RequestComplete(status, KErrNone);
       
   189 	iActionState = CTestAction::EPostrequisite;
       
   190 	__UHEAP_MARKEND;
       
   191 	}
       
   192 
       
   193 void CActionSetWritePFS::Hex(HBufC8& aString)
       
   194     {
       
   195     TPtr8 ptr=aString.Des();
       
   196     if (aString.Length()%2)
       
   197         {
       
   198         ptr.SetLength(0);
       
   199         return;
       
   200         }
       
   201     TInt i;
       
   202     for (i=0;i<aString.Length();i+=2)
       
   203         {
       
   204         TUint8 tmp;
       
   205         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   206         tmp*=16;
       
   207         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   208         ptr[i/2]=tmp;
       
   209         }
       
   210     ptr.SetLength(aString.Length()/2);
       
   211     }