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"
20 #include "SyntaxTest.h"
25 #include <asymmetric.h>
28 TInt CCorruptionTest::nInstances = 0;
29 _LIT(KCorruptLogFile, "X509CorruptLog.txt");
30 _LIT(KPathStart, "<path>");
31 _LIT(KIterationsStart, "<iterations>");
33 //////////////////////////////////////////////////////////////////////
34 // Construction/Destruction
35 //////////////////////////////////////////////////////////////////////
37 CTestAction* CCorruptionTest::NewL(RFs& aFs, CConsoleBase& aConsole,
38 Output& aOut, const TTestActionSpec& aTestActionSpec)
40 CTestAction* self = CCorruptionTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
41 CleanupStack::Pop(self);
45 CTestAction* CCorruptionTest::NewLC(RFs& aFs, CConsoleBase& aConsole,
46 Output& aOut, const TTestActionSpec& aTestActionSpec)
48 CCorruptionTest* self = new(ELeave) CCorruptionTest(aFs, aConsole, aOut);
49 CleanupStack::PushL(self);
50 self->ConstructL(aTestActionSpec);
54 CCorruptionTest::CCorruptionTest(RFs& aFs,
55 CConsoleBase& aConsole,
57 : CTestAction(aConsole, aOut), iFs(aFs)
62 CCorruptionTest::~CCorruptionTest(void)
71 void CCorruptionTest::ConstructL(const TTestActionSpec& aTestActionSpec)
73 CTestAction::ConstructL(aTestActionSpec);
77 HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
81 iNullOut = new(ELeave) NullOutput;
82 body->Des().Copy(aTestActionSpec.iActionBody);
83 TPtrC chainBuf = Input::ParseElement(*body, KPathStart, pos);
85 TPtrC itertaionBuf = Input::ParseElement(*body, KIterationsStart, pos);
86 TLex itLex(itertaionBuf);
88 itLex.Val(iIterations);
89 CleanupStack::PopAndDestroy(); // body
90 User::LeaveIfError(iLogFile.Replace(iFs,KCorruptLogFile,EFileWrite));
91 iCorruptOut = new(ELeave) FileOutput(iLogFile);
92 iWriter = new(ELeave) CertWriter(iCorruptOut);
96 SetScriptError(ESyntax, _L("Only one corruption test can be run in each script"));
102 void CCorruptionTest::DoPerformPrerequisite(TRequestStatus& aStatus)
104 HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
105 TPtr searchPathPtr(searchPath->Des());
106 searchPathPtr.Copy(iPath);
107 searchPathPtr.Append(_L("*"));
108 TInt err = iFs.GetDir(searchPathPtr, KEntryAttMaskSupported, ESortByName, iDirList);
111 iConsole.Printf(_L("Error getting directory "));
112 iConsole.Printf(searchPathPtr);
113 iConsole.Printf(_L("\n"));
114 iOut.writeString(_L("Error getting directory "));
115 iOut.writeString(searchPathPtr);
118 TRequestStatus* status = &aStatus;
119 User::RequestComplete(status, KErrNone);
120 SetScriptError(EFileNotFound, searchPathPtr);
124 iConsole.Printf(_L("Please view "));
125 iConsole.Printf(KCorruptLogFile);
126 iConsole.Printf(_L(" for results\n"));
127 iOut.writeString(_L("Please view "));
128 iOut.writeString(KCorruptLogFile);
129 iOut.writeString(_L(" for results"));
131 iActionState = EAction;
132 TRequestStatus* status = &aStatus;
133 User::RequestComplete(status, KErrNone);
136 CleanupStack::PopAndDestroy();
139 void CCorruptionTest::DoPerformPostrequisite(TRequestStatus& aStatus)
141 TRequestStatus* status = &aStatus;
143 User::RequestComplete(status, KErrNone);
146 void CCorruptionTest::RunCorruptionTestL(const TDesC &aFilename)
149 for (TInt i = 0; i < iIterations; i++)
151 HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
152 TPtr8 pBuf = buf->Des();
153 TInt len = pBuf.Size();
155 //list of positions altered
156 HBufC* posOctsBuf = HBufC::NewLC(70);
157 TPtr pPosOctsBuf = posOctsBuf->Des();
158 pPosOctsBuf.SetLength(0);
159 pPosOctsBuf.Append(_L("p:"));
160 //list of octets altered
161 HBufC8* octsBuf = HBufC8::NewLC(40);
162 TPtr8 pOctsBuf = octsBuf->Des();
163 pOctsBuf.SetLength(0);
165 for (TInt j = 0; j < 10; j++)
168 HBufC8* rand = HBufC8::NewLC(5);
169 TPtr8 pRand = rand->Des();
171 TRandom::RandomL(pRand);
173 for (TInt k = 0; k < 4 ; k++)
179 TUint8 newOctet = pRand[4];
180 //update output lines
181 pPosOctsBuf.AppendNum(pos);
182 pPosOctsBuf.Append(_L(" "));
183 pOctsBuf.Append(pBuf.Mid(pos, 1));
184 TPtrC8 pNewOct = pRand.Right(1);
185 pOctsBuf.Append(pNewOct);
186 //switch the selected octet
187 pBuf[pos] = newOctet;
189 iCorruptOut->writeString(pPosOctsBuf);
190 iCorruptOut->writeNewLine();
191 iCorruptOut->writeString(_L("o:"));
192 iCorruptOut->writeOctetString(pOctsBuf);
193 iCorruptOut->writeNewLine();
194 //try to make corrupt cert
195 CX509Certificate* cert = NULL;
196 TRAPD(err, cert = CX509Certificate::NewL(pBuf));
197 CleanupStack::PushL(cert);
201 // Don't bother attempting to verify if public key modulus is even
202 // since CMontgomery methods will fail...
203 TKeyFactory* theKeyFactory = new (ELeave) TX509KeyFactory();
204 CleanupStack::PushL(theKeyFactory);
205 CRSAPublicKey* key = theKeyFactory->RSAPublicKeyL(cert->PublicKey().KeyData());
206 CleanupStack::Pop(theKeyFactory);
207 delete theKeyFactory;
209 const TInteger& theN = key->N();
212 RInteger input = RInteger::NewL(cert->Signature());
213 CleanupStack::PushL(input);
214 // Check that the signature is not out of bounds for the key
215 // Otherwise the crypto library will panic (descriptor out of bounds)
216 if ( (input < theN) && (input > 0) )
218 TRAP_IGNORE(res = cert->VerifySignatureL(cert->PublicKey().KeyData()));
221 iCorruptOut->writeString(_L("!!!"));
222 iCorruptOut->writeNewLine();
226 CleanupStack::PopAndDestroy(&input);
231 iCorruptOut->writeString(_L("r:"));
232 iCorruptOut->writeNum(err);
233 iCorruptOut->writeNewLine();
234 CleanupStack::PopAndDestroy(2);// rand, cert
236 CleanupStack::PopAndDestroy(3);//
241 // These tests are very slow and do not need to be performed on every
242 // certificate in the x509 test directory. Corrupt every 10th certificate.
243 void CCorruptionTest::PerformAction(TRequestStatus& aStatus)
245 TBuf<256> filename = (*iDirList)[nFileNumber].iName;
248 if( (filename.CompareF(KResultsFile)!=0) && (nFileNumber%10==0) )
250 iCorruptOut->writeString(_L("file:"));
251 iCorruptOut->writeString(filename);
252 iCorruptOut->writeNewLine();
253 iConsole.Printf(_L("file:%S\n"), &filename);
255 TRAP(error, RunCorruptionTestL(filename));
256 if(error == KErrNoMemory)
261 if(++nFileNumber == iDirList->Count())
263 iActionState = EPostrequisite;
265 TRequestStatus* status = &aStatus;
266 User::RequestComplete(status, KErrNone);
269 void CCorruptionTest::DoReportAction()
273 void CCorruptionTest::DoCheckResult(TInt /*aError*/)
277 HBufC8* CCorruptionTest::readCertLC(const TDesC& aFilename)
280 fullname.Append(iPath);
281 fullname.Append(aFilename);
284 User::LeaveIfError( file.Open(iFs, fullname, EFileRead) );
290 HBufC8* res = HBufC8::NewLC(size);
294 RFileReadStream stream;
295 User::LeaveIfError(stream.Open(iFs, fullname, EFileStream));
296 stream.ReadL(p, size);