|
1 /* |
|
2 * Copyright (c) 2002-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 "CFSCertAppsServer.h" |
|
20 #include "CCertAppsSession.h" |
|
21 #include "CCertAppsConduit.h" |
|
22 #include "fstokencliserv.h" |
|
23 #include "fstokenutil.h" |
|
24 #include "certstorepatchdata.h" |
|
25 #include <certificateapps.h> |
|
26 #include <u32hal.h> |
|
27 |
|
28 // Filename where all data is stored |
|
29 _LIT(KCertAppsFilename,"certclients.dat"); |
|
30 _LIT(KCertAppsFile,"certclients*.dat"); |
|
31 |
|
32 CFSCertAppsServer* CFSCertAppsServer::NewL() |
|
33 { |
|
34 CFSCertAppsServer* self = new (ELeave) CFSCertAppsServer(); |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop(self); |
|
38 return self; |
|
39 } |
|
40 |
|
41 CFSCertAppsServer::CFSCertAppsServer() |
|
42 { |
|
43 } |
|
44 |
|
45 void CFSCertAppsServer::ConstructL() |
|
46 { |
|
47 iConduit = CCertAppsConduit::NewL(*this); |
|
48 |
|
49 // Connect to the filesystem |
|
50 User::LeaveIfError(iFs.Connect()); |
|
51 |
|
52 iPatchableConst = KAggregateCertStore; |
|
53 |
|
54 #ifdef __WINS__ |
|
55 // For the emulator allow the constant to be patched via epoc.ini |
|
56 UserSvr::HalFunction(EHalGroupEmulator, EEmulatorHalIntProperty, |
|
57 (TAny*)"KAggregateCertStore", &iPatchableConst); // read emulator property (if present) |
|
58 #endif |
|
59 |
|
60 // Retrieves the store |
|
61 OpenStoreL(); |
|
62 } |
|
63 |
|
64 CFSCertAppsServer::~CFSCertAppsServer() |
|
65 { |
|
66 delete iStore; |
|
67 iFs.Close(); |
|
68 iClients.Close(); |
|
69 delete iConduit; |
|
70 } |
|
71 |
|
72 CCertAppsSession* CFSCertAppsServer::CreateSessionL() |
|
73 { |
|
74 return CCertAppsSession::NewL(*iConduit); |
|
75 } |
|
76 |
|
77 void CFSCertAppsServer::AddL(const TCertificateAppInfo& aClient) |
|
78 { |
|
79 // see if application already exists. If so, then leave |
|
80 |
|
81 if (FindApplication(aClient.Id())) |
|
82 { |
|
83 User::Leave(KErrAlreadyExists); |
|
84 } |
|
85 User::LeaveIfError(iClients.Append(aClient)); |
|
86 TRAPD(err, ReplaceAndCommitL(-1)); |
|
87 if (err != KErrNone) |
|
88 { |
|
89 // We must remove the client from iClients if we didn't manage |
|
90 // to add it to the store |
|
91 iClients.Remove(iClients.Count() - 1); |
|
92 User::Leave(err); |
|
93 } |
|
94 } |
|
95 |
|
96 void CFSCertAppsServer::RemoveL(const TUid& aUid) |
|
97 { |
|
98 // Make sure the application for that uid exists |
|
99 TInt i; |
|
100 if (!FindApplication(aUid, &i)) |
|
101 { |
|
102 User::Leave(KErrNotFound); |
|
103 } |
|
104 |
|
105 ReplaceAndCommitL(i); |
|
106 |
|
107 // We managed to remove it from the store, so we remove it from the |
|
108 // iClients array |
|
109 iClients.Remove(i); |
|
110 } |
|
111 |
|
112 TInt CFSCertAppsServer::ApplicationCountL() const |
|
113 { |
|
114 return iClients.Count(); |
|
115 } |
|
116 |
|
117 void CFSCertAppsServer::ApplicationsL(RArray<TCertificateAppInfo>& aAppArray) const |
|
118 { |
|
119 // Make a copy of the array |
|
120 TInt count = iClients.Count(); |
|
121 |
|
122 for (TInt i = 0; i < count; ++i) |
|
123 { |
|
124 aAppArray.Append(iClients[i]); |
|
125 } |
|
126 } |
|
127 |
|
128 void CFSCertAppsServer::ApplicationL(const TUid& aUid, TCertificateAppInfo& aInfo) const |
|
129 { |
|
130 const TCertificateAppInfo* app = FindApplication(aUid); |
|
131 |
|
132 // leave if not found |
|
133 if (!app) |
|
134 { |
|
135 User::Leave(KErrNotFound); |
|
136 } |
|
137 |
|
138 // make a copy and return to client |
|
139 aInfo = *app; |
|
140 } |
|
141 |
|
142 const TCertificateAppInfo* CFSCertAppsServer::FindApplication(const TUid& aUid, TInt* aIndex) const |
|
143 { |
|
144 // This helper function tries to find an application with the given |
|
145 // Uid. It returns NULL if not found. aIndex returns the index into |
|
146 // the array successful |
|
147 const TCertificateAppInfo* retVal = NULL; |
|
148 TInt end = iClients.Count(); |
|
149 for (TInt i = 0; i < end; ++i) |
|
150 { |
|
151 if (iClients[i].Id() == aUid) |
|
152 { |
|
153 // check if an index is required to be returned |
|
154 if (aIndex) |
|
155 { |
|
156 *aIndex = i; |
|
157 } |
|
158 retVal = &iClients[i]; |
|
159 break; |
|
160 } |
|
161 } |
|
162 |
|
163 return retVal; |
|
164 } |
|
165 |
|
166 void CFSCertAppsServer::AggregateStoreFileL(const TDesC& aFile) |
|
167 { |
|
168 ASSERT(iPatchableConst); |
|
169 |
|
170 // if patchable constant is enabled |
|
171 // 1. open read-only permanent file store on each file. |
|
172 // 2. open certificate client entry list of each store. |
|
173 // 3. aggregate the entries. |
|
174 RFile file; |
|
175 User::LeaveIfError(file.Open(iFs, aFile, EFileRead)); |
|
176 CleanupClosePushL(file); |
|
177 CPermanentFileStore* store = CPermanentFileStore::FromL(file); |
|
178 // now owned by store |
|
179 CleanupStack::Pop(&file); |
|
180 CleanupStack::PushL(store); |
|
181 |
|
182 // Read id of cert list stream |
|
183 TStreamId streamId; |
|
184 RStoreReadStream stream; |
|
185 stream.OpenLC(*store, store->Root()); |
|
186 stream >> streamId; |
|
187 CleanupStack::PopAndDestroy(&stream); |
|
188 |
|
189 // Read the certificate's clients entry list |
|
190 stream.OpenLC(*store, streamId); |
|
191 TInt count = stream.ReadInt32L(); |
|
192 RArray<TCertificateAppInfo> entryList; |
|
193 for (TInt i = 0 ; i < count ; i++) |
|
194 { |
|
195 TCertificateAppInfo clientInfo; |
|
196 stream >> clientInfo; |
|
197 entryList.AppendL(clientInfo); |
|
198 } |
|
199 CleanupStack::PopAndDestroy(&stream); |
|
200 CleanupClosePushL(entryList); |
|
201 |
|
202 MergeCertificateEntryListL(entryList); |
|
203 // cleanup entryList and store instances. |
|
204 CleanupStack::PopAndDestroy(2,store); |
|
205 } |
|
206 |
|
207 TBool CFSCertAppsServer::FindUid(const TUid& aUid) |
|
208 { |
|
209 TInt end = iClients.Count(); |
|
210 for (TInt i = 0; i < end; ++i) |
|
211 { |
|
212 if (iClients[i].Id() == aUid) |
|
213 { |
|
214 return ETrue; |
|
215 } |
|
216 } |
|
217 return EFalse; |
|
218 } |
|
219 |
|
220 void CFSCertAppsServer::MergeCertificateEntryListL(const RArray<TCertificateAppInfo>& aSourceList) |
|
221 { |
|
222 ASSERT(iPatchableConst); |
|
223 |
|
224 // if patchable constant is enabled |
|
225 TInt sourceCount = aSourceList.Count(); |
|
226 for(TInt i = 0; i < sourceCount; i++) |
|
227 { |
|
228 // compare if the uid pre-exists in the composite list. |
|
229 if (!FindUid(aSourceList[i].Id())) |
|
230 { |
|
231 // Aggregation: append this entry to the composite list. |
|
232 iClients.AppendL(aSourceList[i]); |
|
233 } |
|
234 // Eclipsing: Higher order store cert client entries with same UIDs take precedence over lower order |
|
235 // store cert client entries therefore the later are not included in the composite cert client entry list. |
|
236 // Higher order store client entries are ones which are aggregated prior to other client entries. |
|
237 } |
|
238 } |
|
239 |
|
240 void CFSCertAppsServer::OpenCompositeStoreL(const TDesC& aFilename) |
|
241 { |
|
242 ASSERT(iPatchableConst); |
|
243 |
|
244 // 1. create a new empty certstore file under system drive with the name 'certclients.dat'. |
|
245 // 2. this will be the composite store and the instances 'iEntryList' and 'iStore' will be initialized with this. |
|
246 // 3. make private rom drive path where certstore files are located. |
|
247 // 4. collect the certstore file names in a list. |
|
248 // 5. make private rom drive path on each file. |
|
249 // 6. populate the composite store with certificate client entries present in rom drive certstores. |
|
250 |
|
251 // create a new empty certstore file 'certclients.dat' under system drive. |
|
252 CreateStoreL(aFilename); |
|
253 // restore permanent store on it |
|
254 // this will be the composite store after complete aggregation. |
|
255 ReadStoreContentsL(aFilename); |
|
256 |
|
257 RBuf romFilename; |
|
258 romFilename.CreateL(KMaxFilenameLength); |
|
259 CleanupClosePushL(romFilename); |
|
260 FileUtils::MakePrivateROMFilenameL(iFs, KCertAppsFile, romFilename); |
|
261 CDir* filenameList = NULL; |
|
262 User::LeaveIfError(iFs.GetDir(romFilename, KEntryAttNormal, ESortByName|EDescending, filenameList)); |
|
263 CleanupStack::PopAndDestroy(&romFilename); |
|
264 CleanupStack::PushL(filenameList); |
|
265 TInt count = filenameList->Count(); |
|
266 |
|
267 // aggregate ROM stores iteratively |
|
268 for(TInt index = 0; index < count; index++) |
|
269 { |
|
270 RBuf fileName; |
|
271 fileName.CreateL(KMaxFileName); |
|
272 CleanupClosePushL(fileName); |
|
273 FileUtils::MakePrivateROMFilenameL(iFs, ((*filenameList)[index]).iName, fileName); |
|
274 // if there is any corrupt certstore present then we will simply ignore its |
|
275 // aggregation and proceed with aggregating remaining stores. |
|
276 TRAP_IGNORE(AggregateStoreFileL(fileName)); |
|
277 CleanupStack::PopAndDestroy(&fileName); |
|
278 } |
|
279 // write the 'iClients' to the composite store. |
|
280 ReplaceAndCommitL(-1); |
|
281 CleanupStack::PopAndDestroy(filenameList); |
|
282 } |
|
283 |
|
284 // this logic should be handled by a superclass or mixin |
|
285 void CFSCertAppsServer::OpenStoreL() |
|
286 { |
|
287 RBuf filename; |
|
288 filename.CreateL(KMaxFilenameLength); |
|
289 CleanupClosePushL(filename); |
|
290 FileUtils::MakePrivateFilenameL(iFs, KCertAppsFilename, filename); |
|
291 |
|
292 // Attempt to open the store |
|
293 // need to test opening corrupt store |
|
294 TRAPD(err, ReadStoreContentsL(filename)); |
|
295 |
|
296 if (err == KErrNoMemory || err == KErrInUse) |
|
297 { |
|
298 User::Leave(err); |
|
299 } |
|
300 |
|
301 if (err != KErrNone) |
|
302 { |
|
303 // Couldn't open RAM based store, copy from ROM |
|
304 FileUtils::EnsurePathL(iFs, filename); |
|
305 |
|
306 if(iPatchableConst) |
|
307 { |
|
308 OpenCompositeStoreL(filename); |
|
309 } |
|
310 else |
|
311 { |
|
312 RBuf romFilename; |
|
313 romFilename.CreateL(KMaxFilenameLength); |
|
314 CleanupClosePushL(romFilename); |
|
315 FileUtils::MakePrivateROMFilenameL(iFs, KCertAppsFilename, romFilename); |
|
316 |
|
317 if (FileUtils::ExistsL(iFs, romFilename)) |
|
318 { |
|
319 FileUtils::CopyL(iFs, romFilename, filename); |
|
320 } |
|
321 else |
|
322 { |
|
323 CreateStoreL(filename); |
|
324 } |
|
325 CleanupStack::PopAndDestroy(&romFilename); |
|
326 //Retry open, and leave on failure |
|
327 ReadStoreContentsL(filename); |
|
328 } |
|
329 } |
|
330 CleanupStack::PopAndDestroy(&filename); |
|
331 ASSERT(iStore); |
|
332 } |
|
333 |
|
334 void CFSCertAppsServer::ReadStoreContentsL(const TDesC& aFilename) |
|
335 { |
|
336 // Make sure the store is not read-only |
|
337 User::LeaveIfError(iFs.SetAtt(aFilename, KEntryAttNormal, KEntryAttReadOnly)); |
|
338 |
|
339 RFile file; |
|
340 User::LeaveIfError(file.Open(iFs, aFilename, EFileRead | EFileWrite)); |
|
341 CleanupClosePushL(file); |
|
342 CPermanentFileStore* store = CPermanentFileStore::FromL(file); |
|
343 CleanupStack::Pop(&file); |
|
344 CleanupStack::PushL(store); |
|
345 |
|
346 // now read the root stream to get the id of our main stream |
|
347 RStoreReadStream readStream; |
|
348 readStream.OpenLC(*store, store->Root()); |
|
349 readStream >> iId; // This can leave |
|
350 CleanupStack::PopAndDestroy(&readStream); |
|
351 |
|
352 // finally, restore the stream which contains the client arrays. |
|
353 // First long is the number of entries, then each entry |
|
354 readStream.OpenLC(*store, iId); |
|
355 TInt count = readStream.ReadInt32L(); |
|
356 for (TInt i = 0; i < count; ++i) |
|
357 { |
|
358 TCertificateAppInfo clientInfo; |
|
359 readStream >> clientInfo; |
|
360 User::LeaveIfError(iClients.Append(clientInfo)); |
|
361 } |
|
362 CleanupStack::PopAndDestroy(&readStream); |
|
363 |
|
364 ASSERT(!iStore); |
|
365 iStore = store; |
|
366 CleanupStack::Pop(store); |
|
367 } |
|
368 |
|
369 void CFSCertAppsServer::CreateStoreL(const TDesC& aFilename) |
|
370 { |
|
371 // If for some reason we can't complete the creation of the store, we want |
|
372 // to be sure that we don't leave a half-constructed store on the device |
|
373 // as we will then be in trouble when we try to open the store |
|
374 TCleanupItem deleteStoreFile(DeleteStoreFile, this); |
|
375 CleanupStack::PushL(deleteStoreFile); |
|
376 |
|
377 iFs.Delete(aFilename); // ignore errors |
|
378 |
|
379 RFile file; |
|
380 User::LeaveIfError(file.Create(iFs, aFilename, EFileWrite)); |
|
381 CleanupClosePushL(file); |
|
382 |
|
383 CPermanentFileStore* store = CPermanentFileStore::NewL(file); |
|
384 CleanupStack::Pop(&file); // now owned by store |
|
385 CleanupStack::PushL(store); |
|
386 store->SetTypeL(KPermanentFileStoreLayoutUid); |
|
387 |
|
388 RStoreWriteStream clientsStream; |
|
389 TStreamId id = clientsStream.CreateLC(*store); |
|
390 WriteClientArrayL(clientsStream); |
|
391 CleanupStack::PopAndDestroy(&clientsStream); |
|
392 |
|
393 RStoreWriteStream rootStream; |
|
394 TStreamId rootId = rootStream.CreateLC(*store); |
|
395 rootStream << id; |
|
396 rootStream.CommitL(); |
|
397 CleanupStack::PopAndDestroy(&rootStream); |
|
398 |
|
399 store->SetRootL(rootId); |
|
400 store->CommitL(); |
|
401 |
|
402 CleanupStack::PopAndDestroy(store); |
|
403 CleanupStack::Pop(); // deleteStoreFile |
|
404 } |
|
405 |
|
406 void CFSCertAppsServer::RevertStore(TAny* aStore) |
|
407 { |
|
408 // this is a CleanupItem |
|
409 __ASSERT_DEBUG(aStore, PanicServer(EPanicNotInitialised)); |
|
410 |
|
411 CPermanentFileStore* store = reinterpret_cast<CPermanentFileStore *>(aStore); |
|
412 store->Revert(); |
|
413 } |
|
414 |
|
415 void CFSCertAppsServer::DeleteStoreFile(TAny *aThis) |
|
416 { |
|
417 __ASSERT_DEBUG(aThis, PanicServer(EPanicNotInitialised)); |
|
418 |
|
419 // should call non-static member |
|
420 |
|
421 CFSCertAppsServer* self = reinterpret_cast<CFSCertAppsServer*>(aThis); |
|
422 |
|
423 // Something strange has occurred if we can't get the ramStorePath. |
|
424 // Since we can't leave we have to ignore the error |
|
425 TFileName ramStorePath; |
|
426 TRAPD(err, FileUtils::MakePrivateFilenameL(self->iFs, KCertAppsFilename, ramStorePath)); |
|
427 if (!err) |
|
428 { |
|
429 self->iFs.Delete(ramStorePath); |
|
430 } |
|
431 } |
|
432 |
|
433 void CFSCertAppsServer::ReplaceAndCommitL(TInt aExcludedIndex) |
|
434 { |
|
435 TCleanupItem cleanupStore(RevertStore, iStore); |
|
436 CleanupStack::PushL(cleanupStore); |
|
437 |
|
438 // compact the store |
|
439 iStore->ReclaimL(); // do we need to reclaim |
|
440 iStore->CompactL(); |
|
441 |
|
442 RStoreWriteStream outputStream; |
|
443 outputStream.ReplaceLC(*iStore, iId); |
|
444 |
|
445 WriteClientArrayL(outputStream, aExcludedIndex); |
|
446 |
|
447 CleanupStack::PopAndDestroy(&outputStream); |
|
448 iStore->CommitL(); |
|
449 CleanupStack::Pop(); // cleanupStore |
|
450 } |
|
451 |
|
452 void CFSCertAppsServer::WriteClientArrayL(RWriteStream& stream, TInt aExcludedIndex) const |
|
453 { |
|
454 // the count of elements to be written is the arraycount - 1 if we exclude |
|
455 // something, otherwise the arraycount |
|
456 TInt arrayEnd = iClients.Count(); |
|
457 TInt count = (aExcludedIndex < 0) ? (arrayEnd) : (arrayEnd - 1); |
|
458 |
|
459 stream.WriteInt32L(count); |
|
460 |
|
461 for (TInt i = 0; i < arrayEnd; ++i) |
|
462 { |
|
463 if (i != aExcludedIndex) |
|
464 { |
|
465 stream << iClients[i]; // This can leave |
|
466 } |
|
467 } |
|
468 stream.CommitL(); |
|
469 } |