|
1 /* |
|
2 * Copyright (c) 2006 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 "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: Implementation of TrustedSitesStoreServer |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "TrustedSitesServer.h" |
|
21 #include "TrustedSitesConduit.h" |
|
22 #include "TrustedSitesSession.h" |
|
23 #include "DevTokenCliServ.h" |
|
24 #include "DevTokenUtil.h" |
|
25 #include "DevTokenDataTypes.h" |
|
26 #include "DevTokenServer.h" |
|
27 #include "DevandTruSrvCertStoreServer.h" |
|
28 #include <x509cert.h> |
|
29 #include <ccertattributefilter.h> |
|
30 #include <e32cmn.h> |
|
31 #include <badesca.h> |
|
32 |
|
33 _LIT(KTrustedSitesStoreFilename,"TrustedSitesStore.dat"); |
|
34 // Maximum length of SQL query clause |
|
35 const TInt KMaxSQLLength = 256; |
|
36 |
|
37 // API policing |
|
38 _LIT_SECURITY_POLICY_C1(KReadSecurityPolicy, ECapabilityReadUserData); |
|
39 _LIT_SECURITY_POLICY_C1(KWriteSecurityPolicy, ECapabilityWriteDeviceData); |
|
40 |
|
41 // ======== MEMBER FUNCTIONS ======== |
|
42 |
|
43 //CTrustedSitesServer |
|
44 |
|
45 // --------------------------------------------------------------------------- |
|
46 // CTrustedSitesServer::NewL() |
|
47 // --------------------------------------------------------------------------- |
|
48 // |
|
49 CTrustedSitesServer* CTrustedSitesServer::NewL( CDevTokenServer* aServer ) |
|
50 { |
|
51 CTrustedSitesServer* self = new (ELeave) CTrustedSitesServer( aServer ); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(); |
|
54 CleanupStack::Pop(self); |
|
55 return self; |
|
56 } |
|
57 |
|
58 |
|
59 // --------------------------------------------------------------------------- |
|
60 // CTrustedSitesServer::CTrustedSitesServer() |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 CTrustedSitesServer::CTrustedSitesServer( CDevTokenServer* aServer ) : |
|
64 iServer ( aServer ) |
|
65 { |
|
66 } |
|
67 |
|
68 |
|
69 // --------------------------------------------------------------------------- |
|
70 // CTrustedSitesServer::ConstructL() |
|
71 // --------------------------------------------------------------------------- |
|
72 // |
|
73 void CTrustedSitesServer::ConstructL() |
|
74 { |
|
75 iConduit = CTrustedSitesConduit::NewL(*this); |
|
76 |
|
77 User::LeaveIfError( iFs.Connect() ); |
|
78 |
|
79 TBuf<KMaxFilenameLength> filename; |
|
80 |
|
81 FileUtils::MakePrivateFilenameL(iFs, KTrustedSitesStoreFilename, filename); |
|
82 |
|
83 TInt err = iDatabase.Open( iFs, filename ); |
|
84 |
|
85 if ( err != KErrNone ) |
|
86 { |
|
87 CreateDBL(); // Create DB |
|
88 User::LeaveIfError( iDatabase.Open( iFs, filename ) ); |
|
89 } |
|
90 } |
|
91 |
|
92 |
|
93 // --------------------------------------------------------------------------- |
|
94 // CTrustedSitesServer::CreateDBL() |
|
95 // --------------------------------------------------------------------------- |
|
96 // |
|
97 void CTrustedSitesServer::CreateDBL() |
|
98 { |
|
99 |
|
100 RFs fsSession; |
|
101 User::LeaveIfError( fsSession.Connect() ); // Connect to file server |
|
102 |
|
103 CleanupClosePushL( fsSession ); |
|
104 |
|
105 RDbNamedDatabase database; |
|
106 |
|
107 TBuf<KMaxFilenameLength> filename; |
|
108 |
|
109 FileUtils::MakePrivateFilenameL(fsSession, KTrustedSitesStoreFilename, filename); |
|
110 |
|
111 FileUtils::EnsurePathL(fsSession, filename); |
|
112 CleanupClosePushL( database ); |
|
113 |
|
114 User::LeaveIfError( database.Create( fsSession, filename ) ); |
|
115 |
|
116 // Create tables |
|
117 |
|
118 // Trusted sites table |
|
119 _LIT( KSQLCreateTable1, "CREATE TABLE Certificates (\ |
|
120 CertID COUNTER NOT NULL,\ |
|
121 CertHash CHAR(20) NOT NULL)"); |
|
122 |
|
123 _LIT( KSQLCreateTable2, "CREATE TABLE TrustedSites (\ |
|
124 CertID INTEGER,\ |
|
125 SiteName VARCHAR(100) NOT NULL,\ |
|
126 Forgiven INTEGER NOT NULL,\ |
|
127 OutOfDate INTEGER NOT NULL,\ |
|
128 ClientUID INTEGER NOT NULL )"); |
|
129 |
|
130 User::LeaveIfError( database.Execute( KSQLCreateTable1 ) ); |
|
131 User::LeaveIfError( database.Execute( KSQLCreateTable2 ) ); |
|
132 |
|
133 CleanupStack::PopAndDestroy( 2 ); // database, fsSession |
|
134 } |
|
135 |
|
136 |
|
137 // --------------------------------------------------------------------------- |
|
138 // CTrustedSitesServer::~CTrustedSitesServer() |
|
139 // --------------------------------------------------------------------------- |
|
140 // |
|
141 CTrustedSitesServer::~CTrustedSitesServer() |
|
142 { |
|
143 delete iConduit; |
|
144 iDatabase.Close(); // Close database |
|
145 iFs.Close(); |
|
146 } |
|
147 |
|
148 |
|
149 // --------------------------------------------------------------------------- |
|
150 // CTrustedSitesServer::CreateSessionL() |
|
151 // --------------------------------------------------------------------------- |
|
152 // |
|
153 CTrustedSitesSession* CTrustedSitesServer::CreateSessionL() |
|
154 { |
|
155 return CTrustedSitesSession::NewL(*iConduit); |
|
156 } |
|
157 |
|
158 |
|
159 // --------------------------------------------------------------------------- |
|
160 // CTrustedSitesServer::AddL() |
|
161 // --------------------------------------------------------------------------- |
|
162 // |
|
163 void CTrustedSitesServer::AddL(const TDesC8& aCertHash, const TDesC& aSiteName, const RMessage2& aMessage ) |
|
164 { |
|
165 // API policing |
|
166 if (!KWriteSecurityPolicy.CheckPolicy(aMessage)) |
|
167 { |
|
168 User::Leave(KErrPermissionDenied); |
|
169 } |
|
170 |
|
171 TInt certID; |
|
172 GetCertIDL( aCertHash, certID ); // Get certificate ID |
|
173 |
|
174 if ( certID == KErrNotFound ) // Not found |
|
175 { |
|
176 iDatabase.Begin(); // Begin transaction |
|
177 |
|
178 TRAPD( err, DoAddCertL( aCertHash )); |
|
179 |
|
180 if ( err ) // Some error occurred |
|
181 { |
|
182 iDatabase.Rollback(); // Rollback changes |
|
183 User::Leave ( err ); |
|
184 } |
|
185 else |
|
186 { |
|
187 iDatabase.Commit(); // Commit changes |
|
188 } |
|
189 |
|
190 GetCertIDL( aCertHash, certID ); |
|
191 |
|
192 if( certID == KErrNotFound ) |
|
193 { |
|
194 User::Leave( KErrNotFound ); |
|
195 } |
|
196 |
|
197 iDatabase.Begin(); |
|
198 |
|
199 TRAP( err, DoAddSiteNameL( certID, aSiteName )); |
|
200 |
|
201 if ( err ) // Some error occurred |
|
202 { |
|
203 iDatabase.Rollback(); // Rollback changes |
|
204 User::Leave ( err ); |
|
205 } |
|
206 else |
|
207 { |
|
208 iDatabase.Commit(); // Commit changes |
|
209 } |
|
210 } |
|
211 else |
|
212 { |
|
213 if(IsSiteAlreadyExistL( certID, aSiteName )) |
|
214 { |
|
215 return; |
|
216 } |
|
217 else |
|
218 { |
|
219 iDatabase.Begin(); // Begin transaction |
|
220 |
|
221 TRAPD( err, DoAddSiteNameL( certID, aSiteName )); |
|
222 |
|
223 if ( err ) // Some error occurred |
|
224 { |
|
225 iDatabase.Rollback(); // Rollback changes |
|
226 User::Leave ( err ); |
|
227 } |
|
228 else |
|
229 { |
|
230 iDatabase.Commit(); // Commit changes |
|
231 } |
|
232 return; |
|
233 } |
|
234 } |
|
235 |
|
236 } |
|
237 |
|
238 |
|
239 // --------------------------------------------------------------------------- |
|
240 // CTrustedSitesServer::AddL() |
|
241 // --------------------------------------------------------------------------- |
|
242 // |
|
243 void CTrustedSitesServer::AddL(const TDesC8& aCertHash, const TDesC& aSiteName ) |
|
244 { |
|
245 TInt certID; |
|
246 GetCertIDL( aCertHash, certID ); // Get certificate ID |
|
247 |
|
248 if ( certID == KErrNotFound ) // Not found |
|
249 { |
|
250 iDatabase.Begin(); // Begin transaction |
|
251 |
|
252 TRAPD( err, DoAddCertL( aCertHash )); |
|
253 |
|
254 if ( err ) // Some error occurred |
|
255 { |
|
256 iDatabase.Rollback(); // Rollback changes |
|
257 User::Leave ( err ); |
|
258 } |
|
259 else |
|
260 { |
|
261 iDatabase.Commit(); // Commit changes |
|
262 } |
|
263 |
|
264 GetCertIDL( aCertHash, certID ); |
|
265 |
|
266 if( certID == KErrNotFound ) |
|
267 { |
|
268 User::Leave( KErrNotFound ); |
|
269 } |
|
270 |
|
271 iDatabase.Begin(); |
|
272 |
|
273 TRAP( err, DoAddSiteNameL( certID, aSiteName )); |
|
274 |
|
275 if ( err ) // Some error occurred |
|
276 { |
|
277 iDatabase.Rollback(); // Rollback changes |
|
278 User::Leave ( err ); |
|
279 } |
|
280 else |
|
281 { |
|
282 iDatabase.Commit(); // Commit changes |
|
283 } |
|
284 } |
|
285 else |
|
286 { |
|
287 if(IsSiteAlreadyExistL( certID, aSiteName )) |
|
288 { |
|
289 return; |
|
290 } |
|
291 else |
|
292 { |
|
293 iDatabase.Begin(); // Begin transaction |
|
294 |
|
295 TRAPD( err, DoAddSiteNameL( certID, aSiteName )); |
|
296 |
|
297 if ( err ) // Some error occurred |
|
298 { |
|
299 iDatabase.Rollback(); // Rollback changes |
|
300 User::Leave ( err ); |
|
301 } |
|
302 else |
|
303 { |
|
304 iDatabase.Commit(); // Commit changes |
|
305 } |
|
306 return; |
|
307 } |
|
308 } |
|
309 } |
|
310 |
|
311 |
|
312 // --------------------------------------------------------------------------- |
|
313 // CTrustedSitesServer::GetCertIDL() |
|
314 // --------------------------------------------------------------------------- |
|
315 // |
|
316 void CTrustedSitesServer::GetCertIDL( const TDesC8& aCertHash, TInt& aCertID ) |
|
317 { |
|
318 _LIT( KSQLQuery, "SELECT CertID,\ |
|
319 CertHash\ |
|
320 FROM Certificates" ); |
|
321 |
|
322 RDbView view; |
|
323 |
|
324 User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( KSQLQuery ) ) ); |
|
325 |
|
326 CleanupClosePushL( view ); |
|
327 |
|
328 // Get data |
|
329 TInt i = 0; |
|
330 |
|
331 while ( i == 0 && view.NextL() ) // Should find only one ID |
|
332 { |
|
333 view.GetL(); |
|
334 |
|
335 if ( !view.ColDes8( 2 ).Compare( aCertHash ) ) |
|
336 { |
|
337 aCertID = view.ColUint32( 1 ); |
|
338 i++; |
|
339 } |
|
340 } |
|
341 |
|
342 if ( i == 0 ) // Not found any matching rows |
|
343 { |
|
344 aCertID = KErrNotFound; |
|
345 } |
|
346 |
|
347 CleanupStack::PopAndDestroy( 1 ); // view |
|
348 } |
|
349 |
|
350 |
|
351 // --------------------------------------------------------------------------- |
|
352 // CTrustedSitesServer::DoAddSiteNameL() |
|
353 // --------------------------------------------------------------------------- |
|
354 // |
|
355 void CTrustedSitesServer::DoAddSiteNameL(const TInt& aCertID, const TDesC& aSiteName ) |
|
356 { |
|
357 //insert the normal trusted site record,not for forgiven site |
|
358 TBuf<KMaxSQLLength> SQL; |
|
359 |
|
360 _LIT( KSQLInsert1, |
|
361 "INSERT INTO TrustedSites (CertID,SiteName,Forgiven,OutOfDate,ClientUID) VALUES (" ); |
|
362 |
|
363 SQL.Copy(KSQLInsert1); |
|
364 SQL.AppendNum(aCertID); |
|
365 SQL.Append(_L(",'")); |
|
366 |
|
367 SQL.Append( aSiteName ); |
|
368 SQL.Append(_L("',")); |
|
369 //not forgiven |
|
370 SQL.AppendNum(0); |
|
371 SQL.Append(_L(",")); |
|
372 //not allowed out of date |
|
373 SQL.AppendNum(0); |
|
374 //set the UID to -1 since in the normal case we don't need the information |
|
375 SQL.Append(_L(",")); |
|
376 SQL.AppendNum(-1); |
|
377 SQL.Append(_L(")")); |
|
378 |
|
379 User::LeaveIfError( iDatabase.Execute( SQL ) ); |
|
380 } |
|
381 |
|
382 |
|
383 // --------------------------------------------------------------------------- |
|
384 // CTrustedSitesServer::DoAddCertL() |
|
385 // --------------------------------------------------------------------------- |
|
386 // |
|
387 void CTrustedSitesServer::DoAddCertL( const TDesC8& aCertHash ) |
|
388 { |
|
389 RDbView view; |
|
390 |
|
391 _LIT( KSQLInsertCert, "SELECT CertHash\ |
|
392 FROM Certificates" ); |
|
393 |
|
394 User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( KSQLInsertCert ), |
|
395 TDbWindow::EUnlimited, RDbView::EInsertOnly ) ); |
|
396 |
|
397 CleanupClosePushL( view ); |
|
398 |
|
399 view.InsertL(); |
|
400 view.SetColL( 1, aCertHash ); |
|
401 view.PutL(); |
|
402 |
|
403 CleanupStack::PopAndDestroy( 1 ); |
|
404 } |
|
405 |
|
406 |
|
407 // --------------------------------------------------------------------------- |
|
408 // CTrustedSitesServer::IsSiteAlreadyExistL() |
|
409 // --------------------------------------------------------------------------- |
|
410 // |
|
411 TBool CTrustedSitesServer::IsSiteAlreadyExistL(const TInt& aCertID, const TDesC& aSiteName) |
|
412 { |
|
413 RDbView view; |
|
414 _LIT( KSQLQuery, |
|
415 "SELECT SiteName FROM TrustedSites WHERE certID = " ); |
|
416 |
|
417 TBuf<KMaxSQLLength> SQLStatement; |
|
418 SQLStatement.Copy( KSQLQuery ); |
|
419 |
|
420 SQLStatement.AppendNum( aCertID ); |
|
421 |
|
422 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
423 |
|
424 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ), TDbWindow::EUnlimited ); |
|
425 |
|
426 if ( err != KErrNone ) |
|
427 { |
|
428 User::Leave( KErrArgument ); |
|
429 } |
|
430 |
|
431 err = view.EvaluateAll(); |
|
432 |
|
433 CleanupClosePushL( view ); |
|
434 |
|
435 TBool alreadyExist = EFalse; |
|
436 // Get data |
|
437 while ( view.NextL() ) // Should find only one certificate |
|
438 { |
|
439 TRAPD(error, view.GetL()); |
|
440 error = error; |
|
441 if(!aSiteName.Compare(view.ColDes( 1 ))) |
|
442 { |
|
443 alreadyExist = ETrue; |
|
444 break; |
|
445 } |
|
446 } |
|
447 CleanupStack::PopAndDestroy( 1 ); |
|
448 return alreadyExist; |
|
449 } |
|
450 |
|
451 |
|
452 // --------------------------------------------------------------------------- |
|
453 // CTrustedSitesServer::IsOutOfDateAllowedForTheSiteL() |
|
454 // --------------------------------------------------------------------------- |
|
455 // |
|
456 TBool CTrustedSitesServer::IsOutOfDateAllowedForTheSiteL( const TInt& aCertID, const TDesC& aSiteName ) |
|
457 { |
|
458 RDbView view; |
|
459 _LIT( KSQLQuery, |
|
460 "SELECT SiteName,OutOfDate FROM TrustedSites WHERE certID = " ); |
|
461 |
|
462 TBuf<KMaxSQLLength> SQLStatement; |
|
463 SQLStatement.Copy( KSQLQuery ); |
|
464 |
|
465 SQLStatement.AppendNum( aCertID ); |
|
466 |
|
467 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
468 |
|
469 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ), TDbWindow::EUnlimited ); |
|
470 |
|
471 if ( err != KErrNone ) |
|
472 { |
|
473 User::Leave( KErrArgument ); |
|
474 } |
|
475 |
|
476 err = view.EvaluateAll(); |
|
477 |
|
478 CleanupClosePushL( view ); |
|
479 |
|
480 TBool outofdate = EFalse; |
|
481 // Get data |
|
482 while ( view.NextL() ) // Should find only one certificate |
|
483 { |
|
484 TRAPD(error, view.GetL()); |
|
485 error = error; |
|
486 |
|
487 if ( (!aSiteName.Compare( view.ColDes( 1 ) ) ) && ( view.ColInt( 2 ) == 1 ) ) |
|
488 { |
|
489 outofdate = ETrue; |
|
490 break; |
|
491 } |
|
492 } |
|
493 |
|
494 CleanupStack::PopAndDestroy( 1 ); //view |
|
495 return outofdate; |
|
496 } |
|
497 |
|
498 |
|
499 // --------------------------------------------------------------------------- |
|
500 // CTrustedSitesServer::IsTrustedSiteL() |
|
501 // --------------------------------------------------------------------------- |
|
502 // |
|
503 TBool CTrustedSitesServer::IsTrustedSiteL( const TDesC8& aCertHash, const TDesC& aSiteName, const TDesC8& aCert, const RMessage2& aMessage ) |
|
504 { |
|
505 // API policing |
|
506 if (!KReadSecurityPolicy.CheckPolicy(aMessage)) |
|
507 { |
|
508 User::Leave(KErrPermissionDenied); |
|
509 } |
|
510 |
|
511 //is it a forgiven site |
|
512 if ( IsForgivenSiteAlreadyL( aSiteName ) ) |
|
513 { |
|
514 // Insert the certificate into database |
|
515 DoOverWriteCertL( aSiteName, aCertHash ); |
|
516 |
|
517 //check if the certificate is already existing |
|
518 CCertAttributeFilter* filter = CCertAttributeFilter::NewL(); |
|
519 CleanupStack::PushL(filter); |
|
520 filter->SetOwnerType(EPeerCertificate); |
|
521 RPointerArray<CDevTokenCertInfo> certs; |
|
522 RMessage2 message; |
|
523 iServer->CertStoreServerL().ListL( *filter,certs,message, ETrue ); |
|
524 |
|
525 CleanupStack::PopAndDestroy(); //filter |
|
526 |
|
527 TBool exist = EFalse; |
|
528 if ( certs.Count() ) |
|
529 { |
|
530 for (TInt i= 0; i<certs.Count();i++) |
|
531 { |
|
532 HBufC8* certData = iServer->CertStoreServerL().RetrieveLC( certs[i]->CertificateId(),message,ETrue ); |
|
533 TPtr8 certPtr = certData->Des(); |
|
534 |
|
535 //certificate from trusted server certstore |
|
536 CX509Certificate* cert = CX509Certificate::NewLC( certPtr ); |
|
537 //certificate from client |
|
538 CX509Certificate* cert2 = CX509Certificate::NewLC( aCert ); |
|
539 |
|
540 if ( cert->Fingerprint() == cert2->Fingerprint() ) |
|
541 { |
|
542 exist = ETrue; |
|
543 CleanupStack::PopAndDestroy(3); //cert, cert2, certData |
|
544 break; |
|
545 } |
|
546 |
|
547 CleanupStack::PopAndDestroy(3); //cert, cert2, certData |
|
548 } |
|
549 } |
|
550 |
|
551 if ( exist ) |
|
552 { |
|
553 //server certificate has saved already, no need to save again |
|
554 return ETrue; |
|
555 } |
|
556 |
|
557 TDevTokenAddCertDataStruct data; |
|
558 data.iLabel.Zero(); |
|
559 data.iSubjectKeyId.Zero(); |
|
560 |
|
561 CX509Certificate* cert = CX509Certificate::NewL(aCert); |
|
562 data.iSubjectKeyId.Copy( cert->KeyIdentifierL() ); |
|
563 delete cert; |
|
564 |
|
565 data.iIssuerKeyId.Zero(); |
|
566 data.iLabel.Copy(aSiteName); |
|
567 data.iFormat = EX509Certificate; |
|
568 data.iCertificateOwnerType = EPeerCertificate; |
|
569 |
|
570 iServer->CertStoreServerL().AddL( data, aCert, aMessage, ETrue ) ; |
|
571 |
|
572 return ETrue; |
|
573 } |
|
574 |
|
575 //It isn't a forgiven site, query in the normal way to check whether it is trusted by the user |
|
576 TInt certID; |
|
577 GetCertIDL( aCertHash, certID ); // Get certificate ID |
|
578 |
|
579 if ( certID == KErrNotFound ) // Not found |
|
580 { |
|
581 return EFalse; |
|
582 } |
|
583 else |
|
584 { |
|
585 if(IsSiteAlreadyExistL( certID, aSiteName )) |
|
586 { |
|
587 return ETrue; |
|
588 } |
|
589 else |
|
590 { |
|
591 return EFalse; |
|
592 } |
|
593 } |
|
594 } |
|
595 |
|
596 |
|
597 |
|
598 // --------------------------------------------------------------------------- |
|
599 // CTrustedSitesServer::IsOutOfDateAllowedL() |
|
600 // --------------------------------------------------------------------------- |
|
601 // |
|
602 TBool CTrustedSitesServer::IsOutOfDateAllowedL( const TDesC8& aCertHash, const TDesC& aSiteName, const RMessage2& aMessage ) |
|
603 { |
|
604 // API policing |
|
605 if (!KReadSecurityPolicy.CheckPolicy(aMessage)) |
|
606 { |
|
607 User::Leave(KErrPermissionDenied); |
|
608 } |
|
609 |
|
610 TInt certID; |
|
611 GetCertIDL( aCertHash, certID ); // Get certificate ID |
|
612 |
|
613 if ( certID == KErrNotFound ) // Not found |
|
614 { |
|
615 return EFalse; |
|
616 } |
|
617 else |
|
618 { |
|
619 if(IsOutOfDateAllowedForTheSiteL( certID, aSiteName )) |
|
620 { |
|
621 return ETrue; |
|
622 } |
|
623 else |
|
624 { |
|
625 return EFalse; |
|
626 } |
|
627 } |
|
628 } |
|
629 |
|
630 |
|
631 // --------------------------------------------------------------------------- |
|
632 // CTrustedSitesServer::GetTrustedSitesL() |
|
633 // --------------------------------------------------------------------------- |
|
634 // |
|
635 void CTrustedSitesServer::GetTrustedSitesL( const TDesC8& aCertHash, |
|
636 RPointerArray<HBufC>& aListOfSites, |
|
637 const RMessage2& aMessage) |
|
638 { |
|
639 // API policing |
|
640 if (!KReadSecurityPolicy.CheckPolicy(aMessage)) |
|
641 { |
|
642 User::Leave(KErrPermissionDenied); |
|
643 } |
|
644 |
|
645 TInt certID; |
|
646 GetCertIDL( aCertHash, certID ); |
|
647 |
|
648 if( certID == KErrNotFound ) |
|
649 { |
|
650 User::Leave( KErrNotFound ); |
|
651 } |
|
652 DoGetTrustedSitesL( certID, aListOfSites ); |
|
653 } |
|
654 |
|
655 |
|
656 // --------------------------------------------------------------------------- |
|
657 // CTrustedSitesServer::DoGetTrustedSitesL() |
|
658 // --------------------------------------------------------------------------- |
|
659 // |
|
660 void CTrustedSitesServer::DoGetTrustedSitesL( const TInt& aCertID, |
|
661 RPointerArray<HBufC>& aListOfSites) |
|
662 { |
|
663 RDbView view; |
|
664 _LIT( KSQLQuery, |
|
665 "SELECT SiteName FROM TrustedSites WHERE certID = " ); |
|
666 |
|
667 TBuf<KMaxSQLLength> SQLStatement; |
|
668 SQLStatement.Copy( KSQLQuery ); |
|
669 |
|
670 SQLStatement.AppendNum( aCertID ); |
|
671 |
|
672 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
673 |
|
674 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ), TDbWindow::EUnlimited ); |
|
675 |
|
676 if ( err != KErrNone ) |
|
677 { |
|
678 User::Leave( KErrArgument ); |
|
679 } |
|
680 |
|
681 err = view.EvaluateAll(); |
|
682 |
|
683 CleanupClosePushL( view ); |
|
684 |
|
685 // Get data |
|
686 while ( view.NextL() ) // Should find only one certificate |
|
687 { |
|
688 TRAPD(error, view.GetL()); |
|
689 error = error; |
|
690 HBufC* temp = HBufC::NewL(KMaxSiteName); |
|
691 TPtr ptrTemp = temp->Des(); |
|
692 ptrTemp.Copy(view.ColDes( 1 )); |
|
693 aListOfSites.Append(temp); |
|
694 } |
|
695 CleanupStack::PopAndDestroy( 1 ); |
|
696 } |
|
697 |
|
698 |
|
699 // --------------------------------------------------------------------------- |
|
700 // CTrustedSitesServer::RemoveL() |
|
701 // --------------------------------------------------------------------------- |
|
702 // |
|
703 void CTrustedSitesServer::RemoveL( const TDesC8& aCertHash ) |
|
704 { |
|
705 TInt certID; |
|
706 GetCertIDL( aCertHash, certID ); |
|
707 |
|
708 if( certID == KErrNotFound ) |
|
709 { |
|
710 User::Leave( KErrNotFound ); |
|
711 } |
|
712 CCertAttributeFilter* filter = CCertAttributeFilter::NewL(); |
|
713 CleanupStack::PushL(filter); |
|
714 filter->SetOwnerType(EPeerCertificate); |
|
715 RPointerArray<CDevTokenCertInfo> certs; |
|
716 RMessage2 message; |
|
717 iServer->CertStoreServerL().ListL(*filter,certs,message, ETrue ); |
|
718 |
|
719 CleanupStack::PopAndDestroy(); //filter |
|
720 |
|
721 TBool stillexist = EFalse; |
|
722 if ( certs.Count() ) |
|
723 { |
|
724 for (TInt i= 0; i<certs.Count();i++) |
|
725 { |
|
726 HBufC8* certData = iServer->CertStoreServerL().RetrieveLC( certs[i]->CertificateId(),message,ETrue ); |
|
727 |
|
728 TPtr8 certPtr = certData->Des(); |
|
729 |
|
730 CX509Certificate* cert = CX509Certificate::NewLC( certPtr ); |
|
731 |
|
732 if ( cert->Fingerprint() == aCertHash ) |
|
733 { |
|
734 stillexist = ETrue; |
|
735 CleanupStack::PopAndDestroy( 2 ); //certData, cert |
|
736 break; |
|
737 } |
|
738 |
|
739 CleanupStack::PopAndDestroy( 2 ); //certData, cert |
|
740 } |
|
741 } |
|
742 |
|
743 if ( stillexist ) |
|
744 { |
|
745 //there are still same server certificate saved in cert store |
|
746 //with different label, doesn't delete information then |
|
747 return; |
|
748 } |
|
749 iDatabase.Begin(); // Begin transaction |
|
750 |
|
751 TRAPD( err, DoRemoveL( certID )); |
|
752 |
|
753 if ( err ) // Some error occurred |
|
754 { |
|
755 iDatabase.Rollback(); // Rollback changes |
|
756 User::Leave ( err ); |
|
757 } |
|
758 else |
|
759 { |
|
760 iDatabase.Commit(); // Commit changes |
|
761 } |
|
762 } |
|
763 |
|
764 |
|
765 // --------------------------------------------------------------------------- |
|
766 // CTrustedSitesServer::DoRemoveL() |
|
767 // --------------------------------------------------------------------------- |
|
768 // |
|
769 void CTrustedSitesServer::DoRemoveL(const TInt& aCertID ) |
|
770 { |
|
771 _LIT( KSQLDelete1, "DELETE FROM Certificates WHERE CertID = " ); |
|
772 _LIT( KSQLDelete2, "DELETE FROM TrustedSites WHERE CertID = " ); |
|
773 |
|
774 TBuf<KMaxSQLLength> SQLStatement1; |
|
775 TBuf<KMaxSQLLength> SQLStatement2; |
|
776 |
|
777 SQLStatement1.Copy( KSQLDelete1 ); |
|
778 SQLStatement1.AppendNum( aCertID ); |
|
779 TPtrC sqlStat1( SQLStatement1.PtrZ() ); |
|
780 |
|
781 SQLStatement2.Copy( KSQLDelete2 ); |
|
782 SQLStatement2.AppendNum( aCertID ); |
|
783 TPtrC sqlStat2( SQLStatement2.PtrZ() ); |
|
784 |
|
785 User::LeaveIfError( iDatabase.Execute( sqlStat1) ); |
|
786 User::LeaveIfError( iDatabase.Execute( sqlStat2 ) ); |
|
787 } |
|
788 |
|
789 |
|
790 // --------------------------------------------------------------------------- |
|
791 // CTrustedSitesServer::AddForgivenSiteL() |
|
792 // --------------------------------------------------------------------------- |
|
793 // |
|
794 void CTrustedSitesServer::AddForgivenSiteL( const TDesC& aSite, const TBool& aOutOfDateAllowed, const RMessage2& aMessage ) |
|
795 { |
|
796 // API policing,WriteDeviceData |
|
797 if (!KWriteSecurityPolicy.CheckPolicy(aMessage)) |
|
798 { |
|
799 User::Leave(KErrPermissionDenied); |
|
800 } |
|
801 |
|
802 if ( IsForgivenSiteAlreadyL( aSite ) ) |
|
803 { |
|
804 //the hostname is in table already. |
|
805 DoOverWriteOutOfDateL( aSite, aOutOfDateAllowed ); |
|
806 return; |
|
807 } |
|
808 else |
|
809 { |
|
810 iDatabase.Begin(); // Begin transaction |
|
811 |
|
812 TRAPD( err, DoAddForgivenSiteL( aSite, aOutOfDateAllowed, aMessage.SecureId().iId )); |
|
813 |
|
814 if ( err ) // Some error occurred |
|
815 { |
|
816 iDatabase.Rollback(); // Rollback changes |
|
817 User::Leave ( err ); |
|
818 } |
|
819 else |
|
820 { |
|
821 iDatabase.Commit(); // Commit changes |
|
822 } |
|
823 } |
|
824 } |
|
825 |
|
826 |
|
827 // --------------------------------------------------------------------------- |
|
828 // CTrustedSitesServer::RemoveAllForgivenSitesL() |
|
829 // --------------------------------------------------------------------------- |
|
830 // |
|
831 void CTrustedSitesServer::RemoveAllForgivenSitesL( const RMessage2& aMessage ) |
|
832 { |
|
833 // API policing,WriteDeviceData |
|
834 if (!KWriteSecurityPolicy.CheckPolicy(aMessage)) |
|
835 { |
|
836 User::Leave(KErrPermissionDenied); |
|
837 } |
|
838 |
|
839 // get certid arrays for the ClientUID |
|
840 RDbView view; |
|
841 _LIT( KSQLQuery, |
|
842 "SELECT SiteName,CertID FROM TrustedSites WHERE ClientUID = " ); |
|
843 |
|
844 TBuf<KMaxSQLLength> SQLStatement; |
|
845 SQLStatement.Copy( KSQLQuery ); |
|
846 |
|
847 SQLStatement.AppendNum( aMessage.SecureId().iId ); |
|
848 |
|
849 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
850 |
|
851 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ), TDbWindow::EUnlimited ); |
|
852 |
|
853 if ( err != KErrNone ) |
|
854 { |
|
855 User::Leave( KErrArgument ); |
|
856 } |
|
857 |
|
858 err = view.EvaluateAll(); |
|
859 |
|
860 CleanupClosePushL( view ); |
|
861 |
|
862 RArray<TInt> certIDs; |
|
863 RArray<TInt> certIDs2; |
|
864 if ( view.CountL() ) |
|
865 { |
|
866 // Get data |
|
867 while ( view.NextL() ) // Should find only one certificate |
|
868 { |
|
869 TRAPD(error, view.GetL()); |
|
870 error = error; |
|
871 certIDs.Append( view.ColInt( 2 ) ); |
|
872 } |
|
873 CleanupStack::PopAndDestroy( 1 ); //view |
|
874 } |
|
875 else |
|
876 { |
|
877 CleanupStack::PopAndDestroy( 1 ); //view |
|
878 //no match found, nothing to delete |
|
879 return; |
|
880 } |
|
881 |
|
882 if ( certIDs.Count() == 0 ) |
|
883 { |
|
884 //no cert id, just return; |
|
885 return; |
|
886 } |
|
887 |
|
888 //the certID may contain -1, then no need to delete the certificate but go to delete the site directly |
|
889 //filter the -1 out. |
|
890 for ( TInt i = 0; i < certIDs.Count() ; i++ ) |
|
891 { |
|
892 if ( certIDs[i] != -1 ) |
|
893 { |
|
894 certIDs2.Append( certIDs[i] ); |
|
895 } |
|
896 } |
|
897 |
|
898 // if certid2 is not empty, it means there are certificates associated with the sitename, |
|
899 // we should delete it. |
|
900 if ( certIDs2.Count() > 0 ) |
|
901 { |
|
902 // get the certhash array for certid arrays |
|
903 //the desc8 array to put the certhash |
|
904 TInt num = certIDs2.Count(); |
|
905 CDesC8ArrayFlat* descarray = new (ELeave) CDesC8ArrayFlat( num ); |
|
906 CleanupStack::PushL( descarray ); |
|
907 |
|
908 for ( TInt i = 0; i < certIDs2.Count(); i++ ) |
|
909 { |
|
910 //empty the previous command |
|
911 SQLStatement.Zero(); |
|
912 _LIT( KSQLQuery, "SELECT CertHash FROM Certificates WHERE CertID =" ); |
|
913 SQLStatement.Append( KSQLQuery ); |
|
914 SQLStatement.AppendNum( certIDs2[i] ); |
|
915 |
|
916 TPtrC sqlStat1( SQLStatement.PtrZ() ); |
|
917 |
|
918 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat1 ), TDbWindow::EUnlimited ); |
|
919 |
|
920 if ( err != KErrNone ) |
|
921 { |
|
922 User::Leave( KErrArgument ); |
|
923 } |
|
924 |
|
925 err = view.EvaluateAll(); |
|
926 |
|
927 CleanupClosePushL( view ); |
|
928 |
|
929 //put the hash into array |
|
930 while ( view.NextL() ) // Should find only one certificate |
|
931 { |
|
932 TRAPD(error, view.GetL()); |
|
933 error = error; |
|
934 descarray->AppendL( view.ColDes8( 1 ) ); |
|
935 } |
|
936 CleanupStack::PopAndDestroy( 1 ); //view |
|
937 } |
|
938 |
|
939 // List all of the certificate in trusted server certstore |
|
940 // retrive them one by one to compare the certhash. |
|
941 //if match found, remove the certificate from the trusted server certstore |
|
942 |
|
943 CCertAttributeFilter* filter = CCertAttributeFilter::NewL(); |
|
944 CleanupStack::PushL(filter); |
|
945 filter->SetOwnerType(EPeerCertificate); |
|
946 RPointerArray<CDevTokenCertInfo> certs; |
|
947 RMessage2 message; |
|
948 iServer->CertStoreServerL().ListL(*filter,certs,message, ETrue ); |
|
949 |
|
950 CleanupStack::PopAndDestroy(); //filter |
|
951 |
|
952 if ( certs.Count() ) |
|
953 { |
|
954 for ( TInt i= 0; i < certs.Count(); i++ ) |
|
955 { |
|
956 HBufC8* certData = iServer->CertStoreServerL().RetrieveLC( certs[i]->CertificateId(),message,ETrue ); |
|
957 |
|
958 TPtr8 certPtr = certData->Des(); |
|
959 |
|
960 CX509Certificate* cert = CX509Certificate::NewLC( certPtr ); |
|
961 |
|
962 for ( TInt ii=0; ii < descarray->Count(); ii++ ) |
|
963 { |
|
964 if ( (*descarray)[ii] == cert->Fingerprint() ) |
|
965 { |
|
966 //match, then delete it |
|
967 iServer->CertStoreServerL().RemoveL( certs[i]->CertificateId(),message,ETrue ); |
|
968 break; |
|
969 } |
|
970 } |
|
971 |
|
972 CleanupStack::PopAndDestroy( 2 ); //certData, cert |
|
973 } |
|
974 } |
|
975 descarray->Reset(); //clean the buffer for certhash |
|
976 CleanupStack::PopAndDestroy(); //descarray |
|
977 |
|
978 //delete the cert record in Certificates Table |
|
979 for ( TInt i = 0; i < certIDs2.Count(); i++ ) |
|
980 { |
|
981 _LIT( KSQLDelete1, "DELETE FROM Certificates WHERE CertID = " ); |
|
982 SQLStatement.Zero(); |
|
983 |
|
984 SQLStatement.Copy( KSQLDelete1 ); |
|
985 SQLStatement.AppendNum( certIDs2[i] ); |
|
986 TPtrC sqlStat2( SQLStatement.PtrZ() ); |
|
987 |
|
988 User::LeaveIfError( iDatabase.Execute( sqlStat2 ) ); |
|
989 } |
|
990 } |
|
991 |
|
992 //now corresponding certificates are deleted, it is time to delete the sitenames |
|
993 // just delete all of the record where uid matched with the client uid |
|
994 |
|
995 _LIT( KSQLDelete2, "DELETE FROM TrustedSites WHERE ClientUID = " ); |
|
996 |
|
997 SQLStatement.Zero(); |
|
998 SQLStatement.Copy( KSQLDelete2 ); |
|
999 SQLStatement.AppendNum( aMessage.SecureId().iId ); |
|
1000 |
|
1001 TPtrC sqlStat3( SQLStatement.PtrZ() ); |
|
1002 |
|
1003 User::LeaveIfError( iDatabase.Execute( sqlStat3) ); |
|
1004 } |
|
1005 |
|
1006 |
|
1007 |
|
1008 // --------------------------------------------------------------------------- |
|
1009 // CTrustedSitesServer::RemoveThisForgivenSiteL() |
|
1010 // --------------------------------------------------------------------------- |
|
1011 // |
|
1012 void CTrustedSitesServer::RemoveThisForgivenSiteL( const TDesC& aSite, const RMessage2& aMessage ) |
|
1013 { |
|
1014 // API policing,WriteDeviceData |
|
1015 if (!KWriteSecurityPolicy.CheckPolicy(aMessage)) |
|
1016 { |
|
1017 User::Leave(KErrPermissionDenied); |
|
1018 } |
|
1019 |
|
1020 // get certid arrays for the ClientUID |
|
1021 RDbView view; |
|
1022 _LIT( KSQLQuery, |
|
1023 "SELECT CertID FROM TrustedSites WHERE ClientUID = " ); |
|
1024 |
|
1025 TBuf<KMaxSQLLength> SQLStatement; |
|
1026 SQLStatement.Copy( KSQLQuery ); |
|
1027 |
|
1028 SQLStatement.AppendNum( aMessage.SecureId().iId ); |
|
1029 SQLStatement.Append(_L(" AND SiteName = '") ); |
|
1030 SQLStatement.Append( aSite ); |
|
1031 SQLStatement.Append(_L("'")); |
|
1032 |
|
1033 |
|
1034 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
1035 |
|
1036 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ), TDbWindow::EUnlimited ); |
|
1037 |
|
1038 if ( err != KErrNone ) |
|
1039 { |
|
1040 User::Leave( KErrArgument ); |
|
1041 } |
|
1042 |
|
1043 err = view.EvaluateAll(); |
|
1044 |
|
1045 |
|
1046 CleanupClosePushL( view ); |
|
1047 |
|
1048 RArray<TInt> certIDs; |
|
1049 RArray<TInt> certIDs2; |
|
1050 if ( view.CountL() ) |
|
1051 { |
|
1052 // Get data |
|
1053 while ( view.NextL() ) // Should find only one certificate |
|
1054 { |
|
1055 TRAPD(error, view.GetL()); |
|
1056 error = error; |
|
1057 certIDs.Append( view.ColInt( 1 ) ); |
|
1058 } |
|
1059 CleanupStack::PopAndDestroy( 1 ); //view |
|
1060 } |
|
1061 else |
|
1062 { |
|
1063 CleanupStack::PopAndDestroy( 1 ); //view |
|
1064 //no match found, nothing to delete |
|
1065 return; |
|
1066 } |
|
1067 |
|
1068 if ( certIDs.Count() == 0 ) |
|
1069 { |
|
1070 //no cert id, just return; |
|
1071 return; |
|
1072 } |
|
1073 |
|
1074 //the certID may contain -1, then no need to delete the certificate but go to delete the site directly |
|
1075 //filter the -1 out. |
|
1076 for ( TInt i = 0; i < certIDs.Count() ; i++ ) |
|
1077 { |
|
1078 if ( certIDs[i] != -1 ) |
|
1079 { |
|
1080 certIDs2.Append( certIDs[i] ); |
|
1081 } |
|
1082 } |
|
1083 // if certid2 is not empty, it means there are certificates associated with the sitename, |
|
1084 // we should delete it. |
|
1085 |
|
1086 if ( certIDs2.Count() > 0 ) |
|
1087 { |
|
1088 // get the certhash array for certid arrays |
|
1089 //the desc8 array to put the certhash |
|
1090 TInt num = certIDs2.Count(); |
|
1091 CDesC8ArrayFlat* descarray = new (ELeave) CDesC8ArrayFlat( num ); |
|
1092 CleanupStack::PushL( descarray ); |
|
1093 |
|
1094 for ( TInt i = 0; i < certIDs2.Count(); i++ ) |
|
1095 { |
|
1096 //empty the previous command |
|
1097 SQLStatement.Zero(); |
|
1098 _LIT( KSQLQuery, "SELECT CertHash FROM Certificates WHERE CertID =" ); |
|
1099 SQLStatement.Append( KSQLQuery ); |
|
1100 SQLStatement.AppendNum( certIDs2[i] ); |
|
1101 |
|
1102 TPtrC sqlStat1( SQLStatement.PtrZ() ); |
|
1103 |
|
1104 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat1 ), TDbWindow::EUnlimited ); |
|
1105 |
|
1106 if ( err != KErrNone ) |
|
1107 { |
|
1108 User::Leave( KErrArgument ); |
|
1109 } |
|
1110 |
|
1111 err = view.EvaluateAll(); |
|
1112 |
|
1113 CleanupClosePushL( view ); |
|
1114 |
|
1115 //put the hash into array |
|
1116 while ( view.NextL() ) // Should find only one certificate |
|
1117 { |
|
1118 TRAPD(error, view.GetL()); |
|
1119 error = error; |
|
1120 descarray->AppendL( view.ColDes8( 1 ) ); |
|
1121 } |
|
1122 CleanupStack::PopAndDestroy( 1 ); //view |
|
1123 } |
|
1124 |
|
1125 // List all of the certificate in trusted server certstore |
|
1126 // retrive them one by one to compare the certhash. |
|
1127 //if match found, remove the certificate from the trusted server certstore |
|
1128 |
|
1129 CCertAttributeFilter* filter = CCertAttributeFilter::NewL(); |
|
1130 CleanupStack::PushL(filter); |
|
1131 filter->SetOwnerType(EPeerCertificate); |
|
1132 RPointerArray<CDevTokenCertInfo> certs; |
|
1133 RMessage2 message; |
|
1134 iServer->CertStoreServerL().ListL(*filter,certs,message, ETrue ); |
|
1135 |
|
1136 CleanupStack::PopAndDestroy(); //filter |
|
1137 |
|
1138 if ( certs.Count() ) |
|
1139 { |
|
1140 for ( TInt i= 0; i < certs.Count(); i++ ) |
|
1141 { |
|
1142 HBufC8* certData = iServer->CertStoreServerL().RetrieveLC( certs[i]->CertificateId(),message,ETrue ); |
|
1143 |
|
1144 TPtr8 certPtr = certData->Des(); |
|
1145 |
|
1146 CX509Certificate* cert = CX509Certificate::NewLC( certPtr ); |
|
1147 |
|
1148 for ( TInt ii=0; ii < descarray->Count(); ii++ ) |
|
1149 { |
|
1150 if ( (*descarray)[ii] == cert->Fingerprint() ) |
|
1151 { |
|
1152 //match, then delete it |
|
1153 iServer->CertStoreServerL().RemoveL( certs[i]->CertificateId(),message,ETrue ); |
|
1154 break; |
|
1155 } |
|
1156 } |
|
1157 |
|
1158 CleanupStack::PopAndDestroy( 2 ); //certData, cert |
|
1159 } |
|
1160 } |
|
1161 descarray->Reset(); //clean the buffer for certhash |
|
1162 CleanupStack::PopAndDestroy(); //descarray |
|
1163 |
|
1164 //delete the cert record in Certificates Table |
|
1165 for ( TInt i = 0; i < certIDs2.Count(); i++ ) |
|
1166 { |
|
1167 _LIT( KSQLDelete1, "DELETE FROM Certificates WHERE CertID = " ); |
|
1168 SQLStatement.Zero(); |
|
1169 |
|
1170 SQLStatement.Copy( KSQLDelete1 ); |
|
1171 SQLStatement.AppendNum( certIDs2[i] ); |
|
1172 TPtrC sqlStat2( SQLStatement.PtrZ() ); |
|
1173 |
|
1174 User::LeaveIfError( iDatabase.Execute( sqlStat2 ) ); |
|
1175 } |
|
1176 } |
|
1177 //now corresponding certificates are deleted, it is time to delete the sitenames |
|
1178 // just delete all of the record where uid matched with the client uid |
|
1179 |
|
1180 _LIT( KSQLDelete2, "DELETE FROM TrustedSites WHERE ClientUID = " ); |
|
1181 |
|
1182 SQLStatement.Zero(); |
|
1183 SQLStatement.Copy( KSQLDelete2 ); |
|
1184 SQLStatement.AppendNum( aMessage.SecureId().iId ); |
|
1185 SQLStatement.Append(_L("AND SiteName = '")); |
|
1186 SQLStatement.Append( aSite ); |
|
1187 SQLStatement.Append(_L("'")); |
|
1188 |
|
1189 TPtrC sqlStat3( SQLStatement.PtrZ() ); |
|
1190 |
|
1191 //TInt errr = iDatabase.Execute( sqlStat3); |
|
1192 User::LeaveIfError( iDatabase.Execute( sqlStat3) ); |
|
1193 } |
|
1194 |
|
1195 |
|
1196 // --------------------------------------------------------------------------- |
|
1197 // CTrustedSitesServer::DoAddForgivenSiteL() |
|
1198 // --------------------------------------------------------------------------- |
|
1199 // |
|
1200 void CTrustedSitesServer::DoAddForgivenSiteL(const TDesC& aSite, const TBool& aOutOfDateAllowed, const TUint32& aUID ) |
|
1201 { |
|
1202 TBuf<KMaxSQLLength> SQLStatement; |
|
1203 |
|
1204 _LIT( KSQLInsert1, |
|
1205 "INSERT INTO TrustedSites (CertID,SiteName,Forgiven,OutOfDate,ClientUID) VALUES (" ); |
|
1206 |
|
1207 SQLStatement.Copy(KSQLInsert1); |
|
1208 SQLStatement.AppendNum(-1); |
|
1209 SQLStatement.Append(_L(",'")); |
|
1210 |
|
1211 SQLStatement.Append( aSite ); |
|
1212 SQLStatement.Append(_L("',")); |
|
1213 SQLStatement.AppendNum(1); |
|
1214 SQLStatement.Append(_L(",")); |
|
1215 //add record for forgiven sites, check if outofdate is allowed also |
|
1216 if ( aOutOfDateAllowed ) |
|
1217 { |
|
1218 SQLStatement.AppendNum(1); |
|
1219 } |
|
1220 else |
|
1221 { |
|
1222 SQLStatement.AppendNum(0); |
|
1223 } |
|
1224 SQLStatement.Append(_L(",")); |
|
1225 //append the client UID. |
|
1226 SQLStatement.AppendNum( aUID ); |
|
1227 SQLStatement.Append(_L(")")); |
|
1228 |
|
1229 User::LeaveIfError( iDatabase.Execute( SQLStatement ) ); |
|
1230 } |
|
1231 |
|
1232 |
|
1233 // --------------------------------------------------------------------------- |
|
1234 // CTrustedSitesServer::IsForgivenSiteAlreadyL() |
|
1235 // --------------------------------------------------------------------------- |
|
1236 // |
|
1237 TBool CTrustedSitesServer::IsForgivenSiteAlreadyL( const TDesC& aSite ) |
|
1238 { |
|
1239 RDbView view; |
|
1240 _LIT( KSQLCheckForgivenSite, |
|
1241 "SELECT SiteName, Forgiven, OutOfDate FROM TrustedSites" ); |
|
1242 |
|
1243 TInt err = view.Prepare( iDatabase, TDbQuery( KSQLCheckForgivenSite ) ); |
|
1244 |
|
1245 if( err == KErrNotFound ) |
|
1246 { |
|
1247 return EFalse; |
|
1248 } |
|
1249 |
|
1250 if ( err != KErrNone ) |
|
1251 { |
|
1252 User::Leave( KErrArgument ); |
|
1253 } |
|
1254 |
|
1255 err = view.EvaluateAll(); |
|
1256 |
|
1257 CleanupClosePushL( view ); |
|
1258 |
|
1259 TBool alreadyExist = EFalse; |
|
1260 |
|
1261 // looking for a match |
|
1262 while ( view.NextL() ) // Should find only one certificate |
|
1263 { |
|
1264 TRAPD(error, view.GetL()); |
|
1265 error = error; |
|
1266 |
|
1267 // sitename is matched and forgiven is set to 1 |
|
1268 if( (!aSite.Compare(view.ColDes( 1 ))) && ( view.ColInt( 2 ) == 1 ) ) |
|
1269 { |
|
1270 alreadyExist = ETrue; |
|
1271 break; |
|
1272 } |
|
1273 } |
|
1274 |
|
1275 CleanupStack::PopAndDestroy( 1 ); |
|
1276 return alreadyExist; |
|
1277 } |
|
1278 |
|
1279 |
|
1280 // --------------------------------------------------------------------------- |
|
1281 // CTrustedSitesServer::DoOverWriteOutOfDateL() |
|
1282 // --------------------------------------------------------------------------- |
|
1283 // |
|
1284 void CTrustedSitesServer::DoOverWriteOutOfDateL(const TDesC& aSite, const TBool& aOutOfDateAllowed ) |
|
1285 { |
|
1286 _LIT( KSQLOverWrite1, "UPDATE TrustedSites SET OutOfDate = "); |
|
1287 |
|
1288 _LIT( KSQLOverWrite2, " WHERE SiteName = '"); |
|
1289 |
|
1290 TBuf<KMaxSQLLength> SQLStatement; |
|
1291 SQLStatement.Copy(KSQLOverWrite1); |
|
1292 SQLStatement.AppendNum(aOutOfDateAllowed); |
|
1293 SQLStatement.Append(KSQLOverWrite2); |
|
1294 SQLStatement.Append(aSite); |
|
1295 SQLStatement.Append(_L("'")); |
|
1296 |
|
1297 User::LeaveIfError( iDatabase.Execute( SQLStatement ) ); |
|
1298 } |
|
1299 |
|
1300 |
|
1301 // --------------------------------------------------------------------------- |
|
1302 // CTrustedSitesServer::DoOverWriteCertL() |
|
1303 // --------------------------------------------------------------------------- |
|
1304 // |
|
1305 void CTrustedSitesServer::DoOverWriteCertL( const TDesC& aSiteName, const TDesC8& aCertHash ) |
|
1306 { |
|
1307 TInt certID; |
|
1308 GetCertIDL( aCertHash, certID ); |
|
1309 |
|
1310 if( certID == KErrNotFound ) |
|
1311 { |
|
1312 DoAddCertL( aCertHash ); |
|
1313 GetCertIDL(aCertHash, certID); |
|
1314 } |
|
1315 |
|
1316 RDbView view; |
|
1317 TBuf<KMaxSQLLength> SQLStatement; |
|
1318 |
|
1319 _LIT( KSQLQuery2, "SELECT Forgiven, OutOfDate FROM TrustedSites WHERE CertID = "); |
|
1320 |
|
1321 _LIT( KSQLQuery3," AND SiteName ='" ); |
|
1322 |
|
1323 SQLStatement.Copy( KSQLQuery2 ); |
|
1324 SQLStatement.AppendNum(certID); |
|
1325 SQLStatement.Append( KSQLQuery3); |
|
1326 SQLStatement.Append( aSiteName ); |
|
1327 SQLStatement.Append(_L("'")); |
|
1328 |
|
1329 TPtrC sqlStat( SQLStatement.PtrZ() ); |
|
1330 TInt err = view.Prepare( iDatabase, TDbQuery( sqlStat ) ); |
|
1331 |
|
1332 err = view.EvaluateAll(); |
|
1333 |
|
1334 CleanupClosePushL( view ); |
|
1335 |
|
1336 //same sitename and same cert exists |
|
1337 if (view.FirstL()) |
|
1338 { |
|
1339 //already exist, no need to add |
|
1340 CleanupStack::PopAndDestroy(); //view |
|
1341 return; |
|
1342 } |
|
1343 |
|
1344 CleanupStack::PopAndDestroy(); //view |
|
1345 |
|
1346 // no match found |
|
1347 |
|
1348 _LIT( KSQLQuery, |
|
1349 "SELECT Forgiven,OutOfDate, CertID FROM TrustedSites WHERE CertId = -1 AND SiteName = '" ); |
|
1350 |
|
1351 SQLStatement.Zero(); |
|
1352 SQLStatement.Copy( KSQLQuery ); |
|
1353 |
|
1354 SQLStatement.Append( aSiteName ); |
|
1355 SQLStatement.Append(_L("'")); |
|
1356 |
|
1357 sqlStat.Set( SQLStatement.PtrZ() ); |
|
1358 err = view.Prepare( iDatabase, TDbQuery( sqlStat ) ); |
|
1359 |
|
1360 err = view.EvaluateAll(); |
|
1361 |
|
1362 CleanupClosePushL( view ); |
|
1363 |
|
1364 if ( view.CountL() ) |
|
1365 { |
|
1366 //record is there but certid is empty, add the certid into the record |
|
1367 //Update the certid |
|
1368 _LIT( KSQLOverWrite1, "UPDATE TrustedSites SET CertID = "); |
|
1369 _LIT( KSQLOverWrite2, " WHERE SiteName = '"); |
|
1370 |
|
1371 SQLStatement.Zero(); |
|
1372 SQLStatement.Copy(KSQLOverWrite1); |
|
1373 SQLStatement.AppendNum( certID ); |
|
1374 SQLStatement.Append(KSQLOverWrite2); |
|
1375 SQLStatement.Append(aSiteName); |
|
1376 SQLStatement.Append(_L("'")); |
|
1377 |
|
1378 User::LeaveIfError( iDatabase.Execute(SQLStatement) ); |
|
1379 } |
|
1380 else |
|
1381 { |
|
1382 //retrive the forgiven and outofdate, and make a same record for the certificate |
|
1383 TInt forgiven = 0; |
|
1384 TInt OutOfDate = 0; |
|
1385 TInt ClientUID = 0; |
|
1386 _LIT( KSQLQuery, |
|
1387 "SELECT Forgiven,OutOfDate,CertID,ClientUID FROM TrustedSites WHERE SiteName = '" ); |
|
1388 |
|
1389 SQLStatement.Zero(); |
|
1390 SQLStatement.Copy( KSQLQuery ); |
|
1391 |
|
1392 SQLStatement.Append( aSiteName ); |
|
1393 SQLStatement.Append(_L("'")); |
|
1394 |
|
1395 sqlStat.Set( SQLStatement.PtrZ() ); |
|
1396 err = view.Prepare( iDatabase, TDbQuery( sqlStat ) ); |
|
1397 |
|
1398 err = view.EvaluateAll(); |
|
1399 |
|
1400 CleanupClosePushL( view ); |
|
1401 |
|
1402 // looking for a match |
|
1403 if ( view.FirstL() ) // Should find only one certificate |
|
1404 { |
|
1405 TRAPD(error, view.GetL()); |
|
1406 error = error; |
|
1407 //add a new record |
|
1408 forgiven = view.ColInt( 1 ); |
|
1409 OutOfDate = view.ColInt( 2 ); |
|
1410 ClientUID = view.ColInt( 4 ); |
|
1411 |
|
1412 //set the new record with new certid and same forgiven and outofdate settings |
|
1413 _LIT( KSQLInsert, |
|
1414 "INSERT INTO TrustedSites (CertID,SiteName,Forgiven,OutOfDate,ClientUID) VALUES(" ); |
|
1415 SQLStatement.Zero(); |
|
1416 SQLStatement.Copy(KSQLInsert); |
|
1417 SQLStatement.AppendNum(certID); |
|
1418 SQLStatement.Append(_L(",'")); |
|
1419 |
|
1420 SQLStatement.Append( aSiteName ); |
|
1421 SQLStatement.Append(_L("',")); |
|
1422 SQLStatement.AppendNum(forgiven); |
|
1423 SQLStatement.Append(_L(",")); |
|
1424 SQLStatement.AppendNum(OutOfDate); |
|
1425 SQLStatement.Append(_L(",")); |
|
1426 SQLStatement.AppendNum(ClientUID); |
|
1427 SQLStatement.Append(_L(")")); |
|
1428 |
|
1429 User::LeaveIfError( iDatabase.Execute( SQLStatement ) ); |
|
1430 } |
|
1431 CleanupStack::PopAndDestroy(); |
|
1432 } |
|
1433 CleanupStack::PopAndDestroy(); //view |
|
1434 } |
|
1435 //EOF |
|
1436 |