crypto/weakcryptospi/test/tpbe/tactionsetwritepfs.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 	TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, 
       
    90 		KPasswdEnd, pos=0, err);
       
    91 	iPasswd = HBufC::NewL(passwdTemp.Length());
       
    92 	TPtr16 passwdTemp3( iPasswd->Des());
       
    93 	passwdTemp3.Copy(passwdTemp);
       
    94 
       
    95 	TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, 
       
    96 		KInputEnd, pos=0, err);
       
    97 	iInput = HBufC8::NewL(inputTemp.Length());
       
    98 	*iInput = inputTemp;
       
    99 
       
   100 	User::RequestComplete(status, KErrNone);
       
   101 	iActionState = CTestAction::EAction;
       
   102 	}
       
   103 
       
   104 void CActionSetWritePFS::DoPerformPostrequisite(TRequestStatus& aStatus)
       
   105 	{
       
   106 	TRequestStatus* status = &aStatus;
       
   107 	delete iPasswd;
       
   108 	delete iInput;
       
   109 
       
   110 	iFinished = ETrue;
       
   111 	User::RequestComplete(status, KErrNone);
       
   112 	}
       
   113 
       
   114 void CActionSetWritePFS::DoReportAction(void)
       
   115 	{
       
   116 	}
       
   117 
       
   118 void CActionSetWritePFS::DoCheckResult(TInt)
       
   119 	{
       
   120 
       
   121 	}
       
   122 
       
   123 void CActionSetWritePFS::PerformAction(TRequestStatus& aStatus)
       
   124 	{
       
   125 	__UHEAP_MARK;
       
   126 	TRequestStatus* status = &aStatus;
       
   127 	iResult = EFalse;
       
   128 	
       
   129 	CPBEncryptSet* set = CPBEncryptSet::NewL(*iPasswd);
       
   130 	CleanupStack::PushL(set);
       
   131 	CPBEncryptor* encryptor = set->NewEncryptLC();
       
   132 
       
   133 	HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); 
       
   134 
       
   135 	TPtr8 ciphertext = ciphertextTemp->Des();	
       
   136 	encryptor->ProcessFinalL(*iInput, ciphertext);
       
   137 	
       
   138 	TInt cipherTextLength = set->MaxCiphertextLength(iInput->Length());
       
   139 	TInt plainTextLength = set->MaxPlaintextLength(cipherTextLength);
       
   140 	if (plainTextLength > cipherTextLength)
       
   141 		{
       
   142 		iResult = EFalse;
       
   143 		}
       
   144 
       
   145 	//Change the password by appending the letter 'a' to it
       
   146 	HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
       
   147 	TPtr newPassword = newPasswordTemp->Des();
       
   148 	newPassword.Copy(*iPasswd);
       
   149 	newPassword.Append('a');
       
   150 
       
   151 	set->ChangePasswordL(newPassword);
       
   152 	
       
   153 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   154 	TBuf<128> pfsFileName (sysDrive.Name());
       
   155 
       
   156 	if (TCrypto::Strength() == TCrypto::EStrong)
       
   157 		pfsFileName.Append(KStrongFileName);
       
   158 	else
       
   159 		pfsFileName.Append(KWeakFileName);
       
   160 
       
   161 	RStoreWriteStream write;
       
   162 	
       
   163 	CFileStore *store = CPermanentFileStore::ReplaceLC(iFs, pfsFileName, EFileRead | EFileWrite);
       
   164 	store->SetTypeL(store->Layout());
       
   165 	
       
   166 	//write the encrypted master key to a new stream
       
   167 	write.CreateLC(*store);
       
   168 	write << set->EncryptedMasterKey();
       
   169 	write.CommitL();
       
   170 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   171 
       
   172 	//write the encryption data to a new stream
       
   173 	write.CreateLC(*store);
       
   174 	set->EncryptionData().ExternalizeL(write);
       
   175 	write.CommitL();
       
   176 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   177 
       
   178 	//write the cyphertext to a new stream
       
   179 	write.CreateLC(*store);
       
   180 	write << ciphertext;
       
   181 	write.CommitL();
       
   182 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   183 
       
   184 	store->Commit();
       
   185 
       
   186 	// Finished writing the PFS
       
   187 	iResult = ETrue;
       
   188 	CleanupStack::PopAndDestroy(store);
       
   189 	CleanupStack::PopAndDestroy(newPasswordTemp);
       
   190 	CleanupStack::PopAndDestroy(ciphertextTemp);
       
   191 	CleanupStack::PopAndDestroy(encryptor);
       
   192 	CleanupStack::PopAndDestroy(set);
       
   193 	User::RequestComplete(status, KErrNone);
       
   194 	iActionState = CTestAction::EPostrequisite;
       
   195 	__UHEAP_MARKEND;
       
   196 	}
       
   197 
       
   198 void CActionSetWritePFS::Hex(HBufC8& aString)
       
   199     {
       
   200     TPtr8 ptr=aString.Des();
       
   201     if (aString.Length()%2)
       
   202         {
       
   203         ptr.SetLength(0);
       
   204         return;
       
   205         }
       
   206     TInt i;
       
   207     for (i=0;i<aString.Length();i+=2)
       
   208         {
       
   209         TUint8 tmp;
       
   210         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   211         tmp*=16;
       
   212         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   213         ptr[i/2]=tmp;
       
   214         }
       
   215     ptr.SetLength(aString.Length()/2);
       
   216     }