os/security/crypto/weakcryptospi/test/tpbe/tactionsetreadpfs.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 "tactionsetreadpfs.h"
sl@0
    20
#include "t_input.h"
sl@0
    21
#include <cryptostrength.h>
sl@0
    22
#include <securityerr.h>
sl@0
    23
#include <pbedata.h>
sl@0
    24
#include <f32file.h>
sl@0
    25
#include <s32file.h>
sl@0
    26
#include <stdlib.h>
sl@0
    27
#include <s32mem.h>
sl@0
    28
#include <s32std.h>
sl@0
    29
sl@0
    30
_LIT8(KReadPFSStart, "<readpfs>");
sl@0
    31
_LIT8(KReadPFSEnd, "</readpfs>");
sl@0
    32
_LIT8(KStrengthStart, "<strength>");
sl@0
    33
_LIT8(KStrengthEnd, "</strength>");
sl@0
    34
_LIT8(KInputStart, "<input>");
sl@0
    35
_LIT8(KInputEnd, "</input>");
sl@0
    36
_LIT8(KPasswdStart, "<passwd>");
sl@0
    37
_LIT8(KPasswdEnd, "</passwd>");
sl@0
    38
_LIT8(KStrong, "strong");
sl@0
    39
_LIT16(KWeakFileName, "\\tpbe\\weak.dat");
sl@0
    40
_LIT16(KStrongFileName, "\\tpbe\\strong.dat");
sl@0
    41
sl@0
    42
CTestAction* CActionSetReadPFS::NewL(RFs& aFs,
sl@0
    43
									   CConsoleBase& aConsole,
sl@0
    44
									   Output& aOut, 
sl@0
    45
									   const TTestActionSpec& aTestActionSpec)
sl@0
    46
	{
sl@0
    47
	CTestAction* self = CActionSetReadPFS::NewLC(aFs, aConsole,
sl@0
    48
		aOut, aTestActionSpec);
sl@0
    49
	CleanupStack::Pop();
sl@0
    50
	return self;
sl@0
    51
	}
sl@0
    52
sl@0
    53
CTestAction* CActionSetReadPFS::NewLC(RFs& aFs,
sl@0
    54
										CConsoleBase& aConsole,
sl@0
    55
										Output& aOut, 
sl@0
    56
										const TTestActionSpec& aTestActionSpec)
sl@0
    57
	{
sl@0
    58
	CActionSetReadPFS* self = new(ELeave) CActionSetReadPFS(aFs, aConsole, aOut);
sl@0
    59
	CleanupStack::PushL(self);
sl@0
    60
	self->ConstructL(aTestActionSpec);
sl@0
    61
	return self;
sl@0
    62
	}
sl@0
    63
sl@0
    64
CActionSetReadPFS::~CActionSetReadPFS()
sl@0
    65
	{
sl@0
    66
	delete iBody;
sl@0
    67
	}
sl@0
    68
sl@0
    69
CActionSetReadPFS::CActionSetReadPFS(RFs& aFs, 
sl@0
    70
								 CConsoleBase& aConsole,
sl@0
    71
								 Output& aOut)
sl@0
    72
								 
sl@0
    73
: CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    74
	{
sl@0
    75
	}
sl@0
    76
sl@0
    77
void CActionSetReadPFS::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    78
	{
sl@0
    79
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    80
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
sl@0
    81
	iBody->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    82
	
sl@0
    83
	}
sl@0
    84
sl@0
    85
void CActionSetReadPFS::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    86
	{
sl@0
    87
	TRequestStatus* status = &aStatus;
sl@0
    88
	TInt err = KErrNone;
sl@0
    89
	TInt pos = 0;
sl@0
    90
	TPtrC8 encryptElement = Input::ParseElement(*iBody, KReadPFSStart,
sl@0
    91
		KReadPFSEnd, pos, err);
sl@0
    92
sl@0
    93
	TPtrC8 strengthTemp = Input::ParseElement(encryptElement, KStrengthStart, 
sl@0
    94
		KStrengthEnd, pos=0, err);
sl@0
    95
		
sl@0
    96
	TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0
    97
	if (strengthTemp.CompareF(KStrong))
sl@0
    98
		{
sl@0
    99
		iFileName = sysDrive.Name();
sl@0
   100
		iFileName.Append(KStrongFileName);
sl@0
   101
		}
sl@0
   102
	
sl@0
   103
	else
sl@0
   104
		{
sl@0
   105
		iFileName = sysDrive.Name();
sl@0
   106
		iFileName.Append(KWeakFileName);
sl@0
   107
		}
sl@0
   108
		
sl@0
   109
	TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, 
sl@0
   110
		KPasswdEnd, pos=0, err);
sl@0
   111
	iPasswd = HBufC::NewL(passwdTemp.Length());
sl@0
   112
	TPtr16 passwdTemp3( iPasswd->Des());
sl@0
   113
	passwdTemp3.Copy(passwdTemp);
sl@0
   114
sl@0
   115
	TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, 
sl@0
   116
		KInputEnd, pos=0, err);
sl@0
   117
	iInput = HBufC8::NewL(inputTemp.Length());
sl@0
   118
	*iInput = inputTemp;
sl@0
   119
sl@0
   120
	User::RequestComplete(status, KErrNone);
sl@0
   121
	iActionState = CTestAction::EAction;
sl@0
   122
	}
sl@0
   123
sl@0
   124
void CActionSetReadPFS::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
   125
	{
sl@0
   126
	TRequestStatus* status = &aStatus;
sl@0
   127
	delete iPasswd;
sl@0
   128
	delete iInput;
sl@0
   129
sl@0
   130
	iFinished = ETrue;
sl@0
   131
	User::RequestComplete(status, KErrNone);
sl@0
   132
	}
sl@0
   133
sl@0
   134
void CActionSetReadPFS::DoReportAction(void)
sl@0
   135
	{
sl@0
   136
	}
sl@0
   137
sl@0
   138
void CActionSetReadPFS::DoCheckResult(TInt)
sl@0
   139
	{
sl@0
   140
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CActionSetReadPFS::PerformAction(TRequestStatus& aStatus)
sl@0
   144
	{
sl@0
   145
	__UHEAP_MARK;
sl@0
   146
	TRequestStatus* status = &aStatus;
sl@0
   147
	iResult = EFalse;
sl@0
   148
	
sl@0
   149
	//Change the password by appending the letter 'a' to it
sl@0
   150
	HBufC* newPasswordTemp = HBufC::NewMaxLC(iPasswd->Length()+1);
sl@0
   151
	TPtr newPassword = newPasswordTemp->Des();
sl@0
   152
	newPassword.Copy(*iPasswd);
sl@0
   153
	newPassword.Append('a');
sl@0
   154
sl@0
   155
	//prepare to read the streams back in, creating a new TPBEncryptionData
sl@0
   156
	RStoreReadStream read;
sl@0
   157
	// open the next PFS
sl@0
   158
	CFileStore *store = CPermanentFileStore::OpenLC(iFs, iFileName, EFileRead | EFileWrite);
sl@0
   159
	TStreamId dataStreamId(2); // we know it was the second stream written
sl@0
   160
	read.OpenLC(*store, dataStreamId);
sl@0
   161
	CleanupStack::Pop();
sl@0
   162
	//read in Encryption data
sl@0
   163
	CPBEncryptionData* data = CPBEncryptionData::NewL(read);
sl@0
   164
	read.Close();
sl@0
   165
	CleanupStack::PushL(data);
sl@0
   166
sl@0
   167
	//read in encrypted master key
sl@0
   168
	TStreamId keyStreamId(1); // we know it was the first stream written
sl@0
   169
	read.OpenLC(*store, keyStreamId);
sl@0
   170
	CleanupStack::Pop();
sl@0
   171
	HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number
sl@0
   172
	read.Close();
sl@0
   173
	//create a new set encryption class
sl@0
   174
	CPBEncryptSet* set = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword);
sl@0
   175
sl@0
   176
	//read in ciphertext key
sl@0
   177
	TStreamId cipherId(3); // we know it was the third stream written
sl@0
   178
	read.OpenLC(*store, cipherId);
sl@0
   179
	CleanupStack::Pop();
sl@0
   180
	HBufC8* ciphertextTemp = HBufC8::NewLC(read, 10000); //some large number
sl@0
   181
	read.Close();
sl@0
   182
	TPtr8 ciphertext = ciphertextTemp->Des();
sl@0
   183
	
sl@0
   184
	HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length());
sl@0
   185
	TPtr8 plaintext = plaintextTemp->Des();	
sl@0
   186
sl@0
   187
	// weak crypto should fail if trying to decrypt strong
sl@0
   188
	TRAPD(err, 
sl@0
   189
		{
sl@0
   190
		CPBDecryptor* decryptor = set->NewDecryptLC();
sl@0
   191
		decryptor->Process(ciphertext, plaintext);
sl@0
   192
sl@0
   193
		//this Mid call is due to get rid of the decrypted padding at the end
sl@0
   194
		if ((plaintext.Mid(0,iInput->Length()) == *iInput) &&
sl@0
   195
		!((TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName)))
sl@0
   196
			{
sl@0
   197
			iResult = ETrue;
sl@0
   198
			}
sl@0
   199
		CleanupStack::PopAndDestroy(decryptor);
sl@0
   200
		});
sl@0
   201
sl@0
   202
	if ((err == KErrKeyNotWeakEnough) && 
sl@0
   203
	(TCrypto::Strength() == TCrypto::EWeak) && (iFileName == KStrongFileName))
sl@0
   204
		{
sl@0
   205
		iResult = ETrue;
sl@0
   206
		}
sl@0
   207
sl@0
   208
	CleanupStack::PopAndDestroy(plaintextTemp);
sl@0
   209
	CleanupStack::PopAndDestroy(ciphertextTemp);
sl@0
   210
	CleanupStack::PopAndDestroy(set);
sl@0
   211
	CleanupStack::PopAndDestroy(encryptedMasterKey);
sl@0
   212
	CleanupStack::PopAndDestroy(data);
sl@0
   213
	CleanupStack::PopAndDestroy(store);
sl@0
   214
	CleanupStack::PopAndDestroy(newPasswordTemp);
sl@0
   215
sl@0
   216
	User::RequestComplete(status, KErrNone);
sl@0
   217
	iActionState = CTestAction::EPostrequisite;
sl@0
   218
	__UHEAP_MARKEND;
sl@0
   219
	}
sl@0
   220
sl@0
   221
void CActionSetReadPFS::Hex(HBufC8& aString)
sl@0
   222
    {
sl@0
   223
    TPtr8 ptr=aString.Des();
sl@0
   224
    if (aString.Length()%2)
sl@0
   225
        {
sl@0
   226
        ptr.SetLength(0);
sl@0
   227
        return;
sl@0
   228
        }
sl@0
   229
    TInt i;
sl@0
   230
    for (i=0;i<aString.Length();i+=2)
sl@0
   231
        {
sl@0
   232
        TUint8 tmp;
sl@0
   233
        tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
sl@0
   234
        tmp*=16;
sl@0
   235
        tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
sl@0
   236
        ptr[i/2]=tmp;
sl@0
   237
        }
sl@0
   238
    ptr.SetLength(aString.Length()/2);
sl@0
   239
    }