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