sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2004-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 "t_retrieve.h"
|
sl@0
|
20 |
#include "t_certstoreout.h"
|
sl@0
|
21 |
#include "t_input.h"
|
sl@0
|
22 |
#include "t_certstoredefs.h"
|
sl@0
|
23 |
#include "t_concurrentcertstore.h"
|
sl@0
|
24 |
#include <ccertattributefilter.h>
|
sl@0
|
25 |
|
sl@0
|
26 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
27 |
//CRetrieveCertificate
|
sl@0
|
28 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
29 |
|
sl@0
|
30 |
CTestAction* CRetrieveCertificate::NewL(RFs& aFs,
|
sl@0
|
31 |
CConsoleBase& aConsole,
|
sl@0
|
32 |
Output& aOut,
|
sl@0
|
33 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
CRetrieveCertificate* self = new (ELeave) CRetrieveCertificate(aFs, aConsole, aOut);
|
sl@0
|
36 |
CleanupStack::PushL(self);
|
sl@0
|
37 |
self->ConstructL(aTestActionSpec);
|
sl@0
|
38 |
CleanupStack::Pop(self);
|
sl@0
|
39 |
return self;
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
CRetrieveCertificate::CRetrieveCertificate(RFs& aFs,
|
sl@0
|
43 |
CConsoleBase& aConsole,
|
sl@0
|
44 |
Output& aOut) :
|
sl@0
|
45 |
CCertStoreTestAction(aFs, aConsole, aOut), iDataPtr(NULL, 0),
|
sl@0
|
46 |
iFormat(EUnknownCertificate), iDeletable(EFalse), iTestForDeletable(EFalse)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
void CRetrieveCertificate::ConstructL(const TTestActionSpec& aTestActionSpec)
|
sl@0
|
51 |
{
|
sl@0
|
52 |
CCertStoreTestAction::ConstructL(aTestActionSpec);
|
sl@0
|
53 |
|
sl@0
|
54 |
iFilter = CCertAttributeFilter::NewL();
|
sl@0
|
55 |
|
sl@0
|
56 |
HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
|
sl@0
|
57 |
TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
|
sl@0
|
58 |
Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
|
sl@0
|
59 |
CleanupStack::PopAndDestroy(result);
|
sl@0
|
60 |
|
sl@0
|
61 |
iLabel.Copy(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
|
sl@0
|
62 |
if (iLabel.Length() == 0)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
User::Leave(KErrArgument);
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
// check for a possible deletable flag value for the certificate
|
sl@0
|
68 |
TInt err = KErrNone;
|
sl@0
|
69 |
TInt pos = 0;
|
sl@0
|
70 |
const TDesC8& deletableStr = Input::ParseElement(aTestActionSpec.iActionResult,
|
sl@0
|
71 |
KDeletableStart,
|
sl@0
|
72 |
KDeletableEnd,
|
sl@0
|
73 |
pos,
|
sl@0
|
74 |
err);
|
sl@0
|
75 |
|
sl@0
|
76 |
// set the deletable attribute if a value was found for the certificate
|
sl@0
|
77 |
if (err == KErrNone)
|
sl@0
|
78 |
{
|
sl@0
|
79 |
SetDeletable(deletableStr);
|
sl@0
|
80 |
|
sl@0
|
81 |
// if deletable is found then format value must also be present
|
sl@0
|
82 |
SetFormatL(Input::ParseElement(aTestActionSpec.iActionResult, KCertFormatStart));
|
sl@0
|
83 |
}
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
CRetrieveCertificate::~CRetrieveCertificate()
|
sl@0
|
87 |
{
|
sl@0
|
88 |
Reset();
|
sl@0
|
89 |
delete iFilter;
|
sl@0
|
90 |
}
|
sl@0
|
91 |
|
sl@0
|
92 |
void CRetrieveCertificate::SetDeletable(const TDesC8& aDeletableString)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
iTestForDeletable = ETrue;
|
sl@0
|
95 |
if (aDeletableString.Compare(KTrue)==0)
|
sl@0
|
96 |
{
|
sl@0
|
97 |
iDeletable = ETrue;
|
sl@0
|
98 |
}
|
sl@0
|
99 |
else
|
sl@0
|
100 |
{
|
sl@0
|
101 |
iDeletable = EFalse;
|
sl@0
|
102 |
}
|
sl@0
|
103 |
}
|
sl@0
|
104 |
|
sl@0
|
105 |
void CRetrieveCertificate::SetFormatL(const TDesC8& aFormatString)
|
sl@0
|
106 |
{
|
sl@0
|
107 |
if (aFormatString == KNullDesC8)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
User::Leave(KErrArgument);
|
sl@0
|
110 |
}
|
sl@0
|
111 |
if (aFormatString == KWTLS)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
iFormat = EWTLSCertificate;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
else if (aFormatString == KX509)
|
sl@0
|
116 |
{
|
sl@0
|
117 |
iFormat = EX509Certificate;
|
sl@0
|
118 |
}
|
sl@0
|
119 |
else if (aFormatString == KWTLSURL)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
iFormat = EWTLSCertificateUrl;
|
sl@0
|
122 |
}
|
sl@0
|
123 |
else if (aFormatString == KX509URL)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
iFormat = EX509CertificateUrl;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
else
|
sl@0
|
128 |
{
|
sl@0
|
129 |
iOut.write(_L("Unknown cert format: "));
|
sl@0
|
130 |
iOut.writeString(aFormatString);
|
sl@0
|
131 |
iOut.writeNewLine();
|
sl@0
|
132 |
User::Leave(KErrNotSupported);
|
sl@0
|
133 |
}
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
void CRetrieveCertificate::PerformAction(TRequestStatus& aStatus)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
if (aStatus != KErrNone && iState != ECheckRetrieve)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
iState = EFinished;
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
switch (iState)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
case EList:
|
sl@0
|
146 |
CertStore().List(iCertInfos, *iFilter, aStatus);
|
sl@0
|
147 |
iState = ERetrieveData;
|
sl@0
|
148 |
break;
|
sl@0
|
149 |
|
sl@0
|
150 |
case ERetrieveData:
|
sl@0
|
151 |
{
|
sl@0
|
152 |
for (TInt i = 0 ; i < iCertInfos.Count() ; ++i)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
CCTCertInfo* certInfo = iCertInfos[i];
|
sl@0
|
155 |
if (certInfo->Label() == iLabel)
|
sl@0
|
156 |
{
|
sl@0
|
157 |
iCertInfo = certInfo;
|
sl@0
|
158 |
break;
|
sl@0
|
159 |
}
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
if (!iCertInfo)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
iState = EFinished;
|
sl@0
|
165 |
TRequestStatus* status = &aStatus;
|
sl@0
|
166 |
User::RequestComplete(status, KErrNotFound);
|
sl@0
|
167 |
}
|
sl@0
|
168 |
else
|
sl@0
|
169 |
{
|
sl@0
|
170 |
TRAPD(err, iData = HBufC8::NewL(iCertInfo->Size()));
|
sl@0
|
171 |
if (err != KErrNone)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
iState = EFinished;
|
sl@0
|
174 |
TRequestStatus* status = &aStatus;
|
sl@0
|
175 |
User::RequestComplete(status, err);
|
sl@0
|
176 |
}
|
sl@0
|
177 |
else
|
sl@0
|
178 |
{
|
sl@0
|
179 |
iState = EParseData;
|
sl@0
|
180 |
// iDataPtr has to be be a member as it's passed to asyc retrieve operation
|
sl@0
|
181 |
iDataPtr.Set(iData->Des());
|
sl@0
|
182 |
CertStore().Retrieve(*iCertInfo, iDataPtr, aStatus);
|
sl@0
|
183 |
}
|
sl@0
|
184 |
}
|
sl@0
|
185 |
}
|
sl@0
|
186 |
break;
|
sl@0
|
187 |
|
sl@0
|
188 |
case EParseData:
|
sl@0
|
189 |
{
|
sl@0
|
190 |
switch (iCertInfo->CertificateFormat())
|
sl@0
|
191 |
{
|
sl@0
|
192 |
case EX509Certificate:
|
sl@0
|
193 |
{
|
sl@0
|
194 |
CX509Certificate* cert = CX509Certificate::NewLC(*iData);
|
sl@0
|
195 |
X509CertWriter writer(iOut);
|
sl@0
|
196 |
writer.WriteCert(*cert);
|
sl@0
|
197 |
CleanupStack::PopAndDestroy();
|
sl@0
|
198 |
iOut.writeNewLine();
|
sl@0
|
199 |
}
|
sl@0
|
200 |
break;
|
sl@0
|
201 |
|
sl@0
|
202 |
case EWTLSCertificate:
|
sl@0
|
203 |
{
|
sl@0
|
204 |
CWTLSCertificate* cert = CWTLSCertificate::NewLC(*iData);
|
sl@0
|
205 |
WTLSCertWriter writer(iOut);
|
sl@0
|
206 |
writer.WriteCert(*cert);
|
sl@0
|
207 |
CleanupStack::PopAndDestroy(cert);
|
sl@0
|
208 |
iOut.writeNewLine();
|
sl@0
|
209 |
}
|
sl@0
|
210 |
break;
|
sl@0
|
211 |
|
sl@0
|
212 |
default:
|
sl@0
|
213 |
break;
|
sl@0
|
214 |
}
|
sl@0
|
215 |
|
sl@0
|
216 |
iState = ERetrieveCert;
|
sl@0
|
217 |
TRequestStatus* status = &aStatus;
|
sl@0
|
218 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
break;
|
sl@0
|
221 |
|
sl@0
|
222 |
case ERetrieveCert:
|
sl@0
|
223 |
if (CertStoreType() != EUnifiedCertStore)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
iState = EFinished;
|
sl@0
|
226 |
TRequestStatus* status = &aStatus;
|
sl@0
|
227 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
228 |
}
|
sl@0
|
229 |
else
|
sl@0
|
230 |
{
|
sl@0
|
231 |
iState = ECheckRetrieve;
|
sl@0
|
232 |
UnifiedCertStore().Retrieve(*iCertInfo, iCert, aStatus);
|
sl@0
|
233 |
}
|
sl@0
|
234 |
break;
|
sl@0
|
235 |
|
sl@0
|
236 |
case ECheckRetrieve:
|
sl@0
|
237 |
{
|
sl@0
|
238 |
TCertificateFormat format = iCertInfo->CertificateFormat();
|
sl@0
|
239 |
TInt expectedErr = KErrNotSupported;
|
sl@0
|
240 |
if (format == EX509Certificate || format == EWTLSCertificate)
|
sl@0
|
241 |
{
|
sl@0
|
242 |
expectedErr = KErrNone;
|
sl@0
|
243 |
}
|
sl@0
|
244 |
TInt err = (aStatus.Int() == expectedErr) ? KErrNone : KErrGeneral;
|
sl@0
|
245 |
iState = EFinished;
|
sl@0
|
246 |
TRequestStatus* status = &aStatus;
|
sl@0
|
247 |
User::RequestComplete(status, err);
|
sl@0
|
248 |
}
|
sl@0
|
249 |
break;
|
sl@0
|
250 |
|
sl@0
|
251 |
case EFinished:
|
sl@0
|
252 |
{
|
sl@0
|
253 |
iActionState = EPostrequisite;
|
sl@0
|
254 |
iFinished = ETrue;
|
sl@0
|
255 |
TRequestStatus* status = &aStatus;
|
sl@0
|
256 |
User::RequestComplete(status, aStatus.Int());
|
sl@0
|
257 |
}
|
sl@0
|
258 |
break;
|
sl@0
|
259 |
|
sl@0
|
260 |
default:
|
sl@0
|
261 |
User::Invariant();
|
sl@0
|
262 |
}
|
sl@0
|
263 |
}
|
sl@0
|
264 |
|
sl@0
|
265 |
void CRetrieveCertificate::PerformCancel()
|
sl@0
|
266 |
{
|
sl@0
|
267 |
switch (iState)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
case ERetrieveData:
|
sl@0
|
270 |
CertStore().CancelList();
|
sl@0
|
271 |
break;
|
sl@0
|
272 |
|
sl@0
|
273 |
case EParseData:
|
sl@0
|
274 |
case ECheckRetrieve:
|
sl@0
|
275 |
CertStore().CancelRetrieve();
|
sl@0
|
276 |
break;
|
sl@0
|
277 |
|
sl@0
|
278 |
default:
|
sl@0
|
279 |
break;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
}
|
sl@0
|
282 |
|
sl@0
|
283 |
void CRetrieveCertificate::Reset()
|
sl@0
|
284 |
{
|
sl@0
|
285 |
iState = EList;
|
sl@0
|
286 |
iCertInfos.Close();
|
sl@0
|
287 |
delete iCert;
|
sl@0
|
288 |
iCert = NULL;
|
sl@0
|
289 |
delete iData;
|
sl@0
|
290 |
iData = NULL;
|
sl@0
|
291 |
}
|
sl@0
|
292 |
|
sl@0
|
293 |
void CRetrieveCertificate::DoReportAction()
|
sl@0
|
294 |
{
|
sl@0
|
295 |
iOut.write(_L("Retrieving certificate...\n"));
|
sl@0
|
296 |
iOut.write(_L("\tLabel: %S\n"), &iLabel);
|
sl@0
|
297 |
if (iTestForDeletable)
|
sl@0
|
298 |
{
|
sl@0
|
299 |
iOut.writeString(_L("\tDeletable = "));
|
sl@0
|
300 |
iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
|
sl@0
|
301 |
iOut.writeNewLine();
|
sl@0
|
302 |
}
|
sl@0
|
303 |
}
|
sl@0
|
304 |
|
sl@0
|
305 |
void CRetrieveCertificate::DoPerformPostrequisite(TRequestStatus& aStatus)
|
sl@0
|
306 |
{
|
sl@0
|
307 |
iFinished = ETrue;
|
sl@0
|
308 |
TRequestStatus* status = &aStatus;
|
sl@0
|
309 |
User::RequestComplete(status, aStatus.Int());
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
TBool CRetrieveCertificate::ValidCertInfo()
|
sl@0
|
313 |
{
|
sl@0
|
314 |
// check iFormat and deletable flag of the cert info object
|
sl@0
|
315 |
return ((iCertInfo->IsDeletable() == iDeletable) &&
|
sl@0
|
316 |
(iFormat == iCertInfo->CertificateFormat()));
|
sl@0
|
317 |
}
|
sl@0
|
318 |
|
sl@0
|
319 |
void CRetrieveCertificate::DoCheckResult(TInt aError)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
if (iFinished)
|
sl@0
|
322 |
{
|
sl@0
|
323 |
iResult = (aError == iExpectedResult);
|
sl@0
|
324 |
|
sl@0
|
325 |
if (iResult)
|
sl@0
|
326 |
{
|
sl@0
|
327 |
if (iTestForDeletable)
|
sl@0
|
328 |
{
|
sl@0
|
329 |
iResult = iResult && ValidCertInfo();
|
sl@0
|
330 |
|
sl@0
|
331 |
if (iResult)
|
sl@0
|
332 |
{
|
sl@0
|
333 |
iOut.writeString(_L("\tRetrieved certificate info successfully - \n\n"));
|
sl@0
|
334 |
|
sl@0
|
335 |
}
|
sl@0
|
336 |
}
|
sl@0
|
337 |
else
|
sl@0
|
338 |
{
|
sl@0
|
339 |
iOut.writeString(_L("\tRetrieved certificate successfully\n\n"));
|
sl@0
|
340 |
}
|
sl@0
|
341 |
}
|
sl@0
|
342 |
else
|
sl@0
|
343 |
{
|
sl@0
|
344 |
iOut.writeString(_L("\tRetrieve certificate failed\n\n"));
|
sl@0
|
345 |
// If running tests with multiple threads, failure may be expected
|
sl@0
|
346 |
// need to add this back in when we get concurrent tests working
|
sl@0
|
347 |
//CConcurrentTester::SanitizeTestResult(iOut, iResult);
|
sl@0
|
348 |
}
|
sl@0
|
349 |
}
|
sl@0
|
350 |
}
|