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