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