First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "CorruptionTest.h"
21 #include "tcertutils.h"
26 #include <asymmetric.h>
30 #include <securityerr.h>
32 TInt CCorruptionTest::nInstances = 0;
33 _LIT(KCorruptLogFile, "WTLSCERTCorruptLog.txt");
34 _LIT(KPathStart, "<path>");
35 _LIT(KIterationsStart, "<iterations>");
37 //////////////////////////////////////////////////////////////////////
38 // Construction/Destruction
39 //////////////////////////////////////////////////////////////////////
41 CTestAction* CCorruptionTest::NewL(RFs& aFs, CConsoleBase& aConsole,
42 Output& aOut, const TTestActionSpec& aTestActionSpec)
44 CTestAction* self = CCorruptionTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
45 CleanupStack::Pop(self);
49 CTestAction* CCorruptionTest::NewLC(RFs& aFs, CConsoleBase& aConsole,
50 Output& aOut, const TTestActionSpec& aTestActionSpec)
52 CCorruptionTest* self = new(ELeave) CCorruptionTest(aFs, aConsole, aOut);
53 CleanupStack::PushL(self);
54 self->ConstructL(aTestActionSpec);
58 CCorruptionTest::CCorruptionTest(RFs& aFs,
59 CConsoleBase& aConsole,
61 : CTestAction(aConsole, aOut), iFs(aFs)
66 CCorruptionTest::~CCorruptionTest(void)
75 void CCorruptionTest::ConstructL(const TTestActionSpec& aTestActionSpec)
77 CTestAction::ConstructL(aTestActionSpec);
82 HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
83 iNullOut = new(ELeave) NullOutput;
84 body->Des().Copy(aTestActionSpec.iActionBody);
85 TPtrC chainBuf = Input::ParseElement(*body, KPathStart, pos);
87 TPtrC itertaionBuf = Input::ParseElement(*body, KIterationsStart, pos);
88 TLex itLex(itertaionBuf);
90 itLex.Val(iIterations);
91 CleanupStack::PopAndDestroy();
92 User::LeaveIfError(iLogFile.Replace(iFs,KCorruptLogFile,EFileWrite));
93 iCorruptOut = new(ELeave) FileOutput(iLogFile);
94 iWriter = new(ELeave) WTLSCertWriter(iCorruptOut);
98 SetScriptError(ESyntax, _L("Only one corruption test can be run in each script"));
104 void CCorruptionTest::DoPerformPrerequisite(TRequestStatus& aStatus)
106 HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
107 TPtr searchPathPtr(searchPath->Des());
108 searchPathPtr.Copy(iPath);
109 searchPathPtr.Append(_L("*"));
110 TInt err = iFs.GetDir(searchPathPtr, KEntryAttMaskSupported, ESortByName, iDirList);
113 iConsole.Printf(_L("Error getting directory "));
114 iConsole.Printf(searchPathPtr);
115 iConsole.Printf(_L("\n"));
116 iOut.writeString(_L("Error getting directory "));
117 iOut.writeString(searchPathPtr);
120 TRequestStatus* status = &aStatus;
121 User::RequestComplete(status, KErrNone);
122 SetScriptError(EFileNotFound, searchPathPtr);
126 iConsole.Printf(_L("Please view "));
127 iConsole.Printf(KCorruptLogFile);
128 iConsole.Printf(_L(" for results\n"));
129 iOut.writeString(_L("Please view "));
130 iOut.writeString(KCorruptLogFile);
131 iOut.writeString(_L(" for results"));
133 iActionState = EAction;
134 TRequestStatus* status = &aStatus;
135 User::RequestComplete(status, KErrNone);
138 CleanupStack::PopAndDestroy();
141 void CCorruptionTest::DoPerformPostrequisite(TRequestStatus& aStatus)
143 TRequestStatus* status = &aStatus;
145 User::RequestComplete(status, KErrNone);
150 void CCorruptionTest::RunCorruptionTestL(const TDesC &aFilename)
152 CSystemRandom *random = CSystemRandom::NewLC();
153 iWriter->SetOut(iNullOut);
154 for (TInt i = 0; i < iIterations; i++)
156 HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
157 TPtr8 pBuf = buf->Des();
158 TInt len = pBuf.Size();
160 //list of positions altered
161 HBufC* posOctsBuf = HBufC::NewLC(70);
162 TPtr pPosOctsBuf = posOctsBuf->Des();
163 pPosOctsBuf.SetLength(0);
164 pPosOctsBuf.Append(_L("p:"));
165 //list of octets altered
166 HBufC8* octsBuf = HBufC8::NewLC(40);
167 TPtr8 pOctsBuf = octsBuf->Des();
168 pOctsBuf.SetLength(0);
170 for (TInt j = 0; j < 10; j++)
173 HBufC8* rand = HBufC8::NewLC(5);
174 TPtr8 pRand = rand->Des();
176 TRAPD(err, random->GenerateBytesL(pRand));
177 if((err != KErrNone) && (err != KErrNotSecure))
180 for (TInt k = 0; k < 4 ; k++)
186 TUint8 newOctet = pRand[4];
187 //update output lines
188 pPosOctsBuf.AppendNum(pos);
189 pPosOctsBuf.Append(_L(" "));
190 pOctsBuf.Append(pBuf.Mid(pos, 1));
191 TPtrC8 pNewOct = pRand.Right(1);
192 pOctsBuf.Append(pNewOct);
193 //switch the selected octet
194 pBuf[pos] = newOctet;
196 iCorruptOut->writeString(pPosOctsBuf);
197 iCorruptOut->writeNewLine();
198 iCorruptOut->writeString(_L("o:"));
199 iCorruptOut->writeOctetString(pOctsBuf);
200 iCorruptOut->writeNewLine();
201 //try to make corrupt cert
202 CWTLSCertificate* cert = NULL;
203 TRAP(err, cert = CWTLSCertificate::NewL(pBuf));
204 CleanupStack::PushL(cert);
207 TRAP_IGNORE(iWriter->WriteCert(*cert));
210 // Don't bother attempting to verify if public key modulus is an even
211 // value since CMontgomery methods will panic
213 CRSAPublicKey* key = CWTLSRSAPublicKey::NewL(cert->PublicKey().KeyData());
214 const TInteger& theN = key->N();
216 {// Check that the signature is an appropriate value for the key
217 // Otherwise the crypto library will panic (descriptor out of bounds)
218 RInteger input = RInteger::NewL(cert->Signature());
219 CleanupStack::PushL(input);
220 if ( (input < theN) && (input > 0) )
222 TRAP_IGNORE(res = cert->VerifySignatureL(cert->PublicKey().KeyData()));
225 iCorruptOut->writeString(_L("!!!"));
226 iCorruptOut->writeNewLine();
230 CleanupStack::PopAndDestroy(&input);
236 iCorruptOut->writeString(_L("r:"));
237 iCorruptOut->writeNum(err);
238 iCorruptOut->writeNewLine();
239 CleanupStack::PopAndDestroy(2);//cert, randomness
241 CleanupStack::PopAndDestroy(3);//
243 CleanupStack::PopAndDestroy(1); //random
244 iWriter->SetOut(iCorruptOut);
247 void CCorruptionTest::PerformAction(TRequestStatus& aStatus)
249 TBuf<256> filename = (*iDirList)[nFileNumber].iName;
251 TRAP_IGNORE(hash = CSHA1::NewL());
252 iCorruptOut->writeString(_L("file:"));
253 iCorruptOut->writeString(filename);
254 iCorruptOut->writeNewLine();
255 iConsole.Printf(_L("file:%S\n"), &filename);
257 TRAP_IGNORE(RunCorruptionTestL(filename));
260 if(nFileNumber == iDirList->Count())
262 iActionState = EPostrequisite;
264 TRequestStatus* status = &aStatus;
265 User::RequestComplete(status, KErrNone);
269 void CCorruptionTest::DoReportAction()
273 void CCorruptionTest::DoCheckResult(TInt /*aError*/)