os/security/cryptoservices/certificateandkeymgmt/twtlscert/CorruptionTest.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-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 "CorruptionTest.h"
sl@0
    20
#include "wtlscert.h"
sl@0
    21
#include "tcertutils.h"
sl@0
    22
#include "t_output.h"
sl@0
    23
#include "t_input.h"
sl@0
    24
#include <random.h>
sl@0
    25
sl@0
    26
#include <asymmetric.h>
sl@0
    27
sl@0
    28
#include <bigint.h>
sl@0
    29
#include <hash.h>
sl@0
    30
#include <securityerr.h>
sl@0
    31
sl@0
    32
TInt CCorruptionTest::nInstances = 0;
sl@0
    33
_LIT(KCorruptLogFile, "WTLSCERTCorruptLog.txt");
sl@0
    34
_LIT(KPathStart, "<path>");
sl@0
    35
_LIT(KIterationsStart, "<iterations>");
sl@0
    36
sl@0
    37
//////////////////////////////////////////////////////////////////////
sl@0
    38
// Construction/Destruction
sl@0
    39
//////////////////////////////////////////////////////////////////////
sl@0
    40
sl@0
    41
CTestAction* CCorruptionTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    42
		Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    43
	{
sl@0
    44
	CTestAction* self = CCorruptionTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
    45
	CleanupStack::Pop(self);
sl@0
    46
	return self;
sl@0
    47
	}
sl@0
    48
sl@0
    49
CTestAction* CCorruptionTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    50
		Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    51
	{
sl@0
    52
	CCorruptionTest* self = new(ELeave) CCorruptionTest(aFs, aConsole, aOut);
sl@0
    53
	CleanupStack::PushL(self);
sl@0
    54
	self->ConstructL(aTestActionSpec);
sl@0
    55
	return self;
sl@0
    56
	}
sl@0
    57
sl@0
    58
CCorruptionTest::CCorruptionTest(RFs& aFs, 
sl@0
    59
								 CConsoleBase& aConsole,
sl@0
    60
								 Output& aOut)
sl@0
    61
: CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    62
	{
sl@0
    63
	nFileNumber = 0;
sl@0
    64
	}
sl@0
    65
sl@0
    66
CCorruptionTest::~CCorruptionTest(void)
sl@0
    67
	{
sl@0
    68
	delete iDirList;
sl@0
    69
	delete iWriter;
sl@0
    70
	iLogFile.Close();
sl@0
    71
	delete iNullOut;
sl@0
    72
	delete iCorruptOut;
sl@0
    73
	};
sl@0
    74
sl@0
    75
void CCorruptionTest::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    76
	{
sl@0
    77
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    78
	if(nInstances==0)
sl@0
    79
		{
sl@0
    80
		nInstances++;
sl@0
    81
		TInt pos=0;
sl@0
    82
		HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
sl@0
    83
		iNullOut = new(ELeave) NullOutput;
sl@0
    84
		body->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    85
		TPtrC chainBuf = Input::ParseElement(*body, KPathStart, pos);
sl@0
    86
		iPath.Copy(chainBuf);
sl@0
    87
		TPtrC itertaionBuf = Input::ParseElement(*body, KIterationsStart, pos);
sl@0
    88
		TLex itLex(itertaionBuf);
sl@0
    89
sl@0
    90
		itLex.Val(iIterations);
sl@0
    91
		CleanupStack::PopAndDestroy();
sl@0
    92
		User::LeaveIfError(iLogFile.Replace(iFs,KCorruptLogFile,EFileWrite));
sl@0
    93
		iCorruptOut = new(ELeave) FileOutput(iLogFile);
sl@0
    94
		iWriter = new(ELeave) WTLSCertWriter(iCorruptOut);
sl@0
    95
		}
sl@0
    96
	else
sl@0
    97
		{
sl@0
    98
		SetScriptError(ESyntax, _L("Only one corruption test can be run in each script"));
sl@0
    99
		iFinished = ETrue;
sl@0
   100
		}
sl@0
   101
	}
sl@0
   102
sl@0
   103
sl@0
   104
void CCorruptionTest::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
   105
	{
sl@0
   106
	HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
sl@0
   107
	TPtr searchPathPtr(searchPath->Des());
sl@0
   108
	searchPathPtr.Copy(iPath);
sl@0
   109
	searchPathPtr.Append(_L("*"));
sl@0
   110
    TInt err = iFs.GetDir(searchPathPtr, KEntryAttMaskSupported, ESortByName, iDirList);
sl@0
   111
	if (err != KErrNone)
sl@0
   112
		{
sl@0
   113
		iConsole.Printf(_L("Error getting directory "));
sl@0
   114
		iConsole.Printf(searchPathPtr);
sl@0
   115
		iConsole.Printf(_L("\n"));
sl@0
   116
		iOut.writeString(_L("Error getting directory "));
sl@0
   117
		iOut.writeString(searchPathPtr);
sl@0
   118
		iOut.writeNewLine();
sl@0
   119
		iFinished = ETrue;
sl@0
   120
		TRequestStatus* status = &aStatus;
sl@0
   121
		User::RequestComplete(status, KErrNone);
sl@0
   122
		SetScriptError(EFileNotFound, searchPathPtr);
sl@0
   123
		}
sl@0
   124
	else
sl@0
   125
		{
sl@0
   126
		iConsole.Printf(_L("Please view "));
sl@0
   127
		iConsole.Printf(KCorruptLogFile);
sl@0
   128
		iConsole.Printf(_L(" for results\n"));
sl@0
   129
		iOut.writeString(_L("Please view "));
sl@0
   130
		iOut.writeString(KCorruptLogFile);
sl@0
   131
		iOut.writeString(_L(" for results"));
sl@0
   132
		iOut.writeNewLine();
sl@0
   133
		iActionState = EAction;
sl@0
   134
		TRequestStatus* status = &aStatus;
sl@0
   135
		User::RequestComplete(status, KErrNone);
sl@0
   136
		iResult = ETrue;
sl@0
   137
		}
sl@0
   138
	CleanupStack::PopAndDestroy();
sl@0
   139
	}
sl@0
   140
sl@0
   141
void CCorruptionTest::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
   142
	{
sl@0
   143
	TRequestStatus* status = &aStatus;
sl@0
   144
	iFinished = ETrue;
sl@0
   145
	User::RequestComplete(status, KErrNone);
sl@0
   146
	}
sl@0
   147
sl@0
   148
sl@0
   149
sl@0
   150
void CCorruptionTest::RunCorruptionTestL(const TDesC &aFilename)
sl@0
   151
	{
sl@0
   152
	CSystemRandom *random = CSystemRandom::NewLC();
sl@0
   153
	iWriter->SetOut(iNullOut);
sl@0
   154
	for (TInt i = 0; i < iIterations; i++)
sl@0
   155
		{
sl@0
   156
		HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
sl@0
   157
		TPtr8 pBuf = buf->Des();
sl@0
   158
		TInt len = pBuf.Size();
sl@0
   159
		TInt pos = 0;
sl@0
   160
				//list of positions altered
sl@0
   161
		HBufC* posOctsBuf = HBufC::NewLC(70);
sl@0
   162
		TPtr pPosOctsBuf = posOctsBuf->Des();
sl@0
   163
		pPosOctsBuf.SetLength(0);
sl@0
   164
		pPosOctsBuf.Append(_L("p:"));
sl@0
   165
				//list of octets altered
sl@0
   166
		HBufC8* octsBuf = HBufC8::NewLC(40);
sl@0
   167
		TPtr8 pOctsBuf = octsBuf->Des();
sl@0
   168
		pOctsBuf.SetLength(0);
sl@0
   169
sl@0
   170
		for (TInt j = 0; j < 10; j++)
sl@0
   171
			{
sl@0
   172
				//randomness
sl@0
   173
			HBufC8* rand = HBufC8::NewLC(5);
sl@0
   174
			TPtr8 pRand = rand->Des();
sl@0
   175
			pRand.SetLength(5);
sl@0
   176
			TRAPD(err, random->GenerateBytesL(pRand));
sl@0
   177
			if((err != KErrNone) && (err != KErrNotSecure))
sl@0
   178
				User::Leave(err);
sl@0
   179
			TUint num = 0;
sl@0
   180
			for (TInt k = 0; k < 4 ; k++)
sl@0
   181
				{
sl@0
   182
				num<<=8;
sl@0
   183
				num+=pRand[k];
sl@0
   184
				}
sl@0
   185
			pos = num % len;
sl@0
   186
			TUint8 newOctet = pRand[4];
sl@0
   187
				//update output lines
sl@0
   188
			pPosOctsBuf.AppendNum(pos);
sl@0
   189
			pPosOctsBuf.Append(_L(" "));
sl@0
   190
			pOctsBuf.Append(pBuf.Mid(pos, 1));
sl@0
   191
			TPtrC8 pNewOct = pRand.Right(1);
sl@0
   192
			pOctsBuf.Append(pNewOct);
sl@0
   193
			//switch the selected octet
sl@0
   194
			pBuf[pos] = newOctet;
sl@0
   195
			//file out
sl@0
   196
			iCorruptOut->writeString(pPosOctsBuf);
sl@0
   197
			iCorruptOut->writeNewLine();
sl@0
   198
			iCorruptOut->writeString(_L("o:"));
sl@0
   199
			iCorruptOut->writeOctetString(pOctsBuf);
sl@0
   200
			iCorruptOut->writeNewLine();
sl@0
   201
			//try to make corrupt cert
sl@0
   202
			CWTLSCertificate* cert = NULL;
sl@0
   203
			TRAP(err, cert = CWTLSCertificate::NewL(pBuf));
sl@0
   204
			CleanupStack::PushL(cert);
sl@0
   205
			if (err == KErrNone)
sl@0
   206
				{
sl@0
   207
				TRAP_IGNORE(iWriter->WriteCert(*cert));
sl@0
   208
				TBool res = EFalse;;
sl@0
   209
				
sl@0
   210
			//	Don't bother attempting to verify if public key modulus is an even
sl@0
   211
			//	value since CMontgomery methods will panic
sl@0
   212
						
sl@0
   213
				CRSAPublicKey* key = CWTLSRSAPublicKey::NewL(cert->PublicKey().KeyData());
sl@0
   214
				const TInteger& theN = key->N();
sl@0
   215
				if (theN.IsOdd())
sl@0
   216
					{//	Check that the signature is an appropriate value for the key
sl@0
   217
					//	Otherwise the crypto library will panic (descriptor out of bounds)
sl@0
   218
					RInteger input = RInteger::NewL(cert->Signature());
sl@0
   219
					CleanupStack::PushL(input);
sl@0
   220
					if ( (input < theN) && (input > 0) )
sl@0
   221
						{
sl@0
   222
						TRAP_IGNORE(res = cert->VerifySignatureL(cert->PublicKey().KeyData()));
sl@0
   223
						if (res)
sl@0
   224
							{
sl@0
   225
							iCorruptOut->writeString(_L("!!!"));
sl@0
   226
							iCorruptOut->writeNewLine();
sl@0
   227
							}
sl@0
   228
						}
sl@0
   229
					
sl@0
   230
					CleanupStack::PopAndDestroy(&input);
sl@0
   231
					}
sl@0
   232
sl@0
   233
					delete key;
sl@0
   234
				}
sl@0
   235
sl@0
   236
			iCorruptOut->writeString(_L("r:"));
sl@0
   237
			iCorruptOut->writeNum(err);
sl@0
   238
			iCorruptOut->writeNewLine();
sl@0
   239
			CleanupStack::PopAndDestroy(2);//cert, randomness
sl@0
   240
			}
sl@0
   241
		CleanupStack::PopAndDestroy(3);//
sl@0
   242
		}
sl@0
   243
	CleanupStack::PopAndDestroy(1); //random
sl@0
   244
	iWriter->SetOut(iCorruptOut);
sl@0
   245
	}
sl@0
   246
sl@0
   247
void CCorruptionTest::PerformAction(TRequestStatus& aStatus)
sl@0
   248
	{
sl@0
   249
	TBuf<256> filename = (*iDirList)[nFileNumber].iName;
sl@0
   250
	CSHA1* hash = NULL;
sl@0
   251
	TRAP_IGNORE(hash = CSHA1::NewL());
sl@0
   252
	iCorruptOut->writeString(_L("file:"));
sl@0
   253
	iCorruptOut->writeString(filename);
sl@0
   254
	iCorruptOut->writeNewLine();
sl@0
   255
	iConsole.Printf(_L("file:%S\n"), &filename);
sl@0
   256
sl@0
   257
	TRAP_IGNORE(RunCorruptionTestL(filename));
sl@0
   258
sl@0
   259
	nFileNumber++;
sl@0
   260
	if(nFileNumber == iDirList->Count())
sl@0
   261
		{
sl@0
   262
		iActionState = EPostrequisite;
sl@0
   263
		};
sl@0
   264
	TRequestStatus* status = &aStatus;
sl@0
   265
	User::RequestComplete(status, KErrNone);
sl@0
   266
	delete hash;
sl@0
   267
	}
sl@0
   268
sl@0
   269
void CCorruptionTest::DoReportAction()
sl@0
   270
	{
sl@0
   271
	}
sl@0
   272
sl@0
   273
void CCorruptionTest::DoCheckResult(TInt /*aError*/)
sl@0
   274
	{
sl@0
   275
	}
sl@0
   276