wim/WimServer/src/WimTrustSettingsStore.cpp
changeset 0 164170e6151a
child 42 82671cd8994b
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2003-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 "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:  Implements WIM TrustSettingsStore for WIM certificates
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "WimTrustSettingsStore.h"
       
    22 #include "WimCertInfo.h"
       
    23 #include "WimCertUtil.h"
       
    24 #include <x509cert.h>
       
    25 #include <x509certext.h>
       
    26 #include <hash.h>
       
    27 #include "WimTrace.h"
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CWimTrustSettingsStore::CWimTrustSettingsStore()
       
    33 // Default constructor.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CWimTrustSettingsStore::CWimTrustSettingsStore() 
       
    37                       : CActive( EPriorityNormal ),
       
    38                         iCompact ( EFalse )
       
    39     {
       
    40     CActiveScheduler::Add( this );
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CWimTrustSettingsStore::ConstructL()
       
    45 // Second phase constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 void CWimTrustSettingsStore::ConstructL()
       
    49     {
       
    50     _WIMTRACE ( _L( "CWimTrustSettingsStore::ConstructL()" ) );
       
    51     // Connect to DB server
       
    52     User::LeaveIfError( iDBSession.Connect() );
       
    53 
       
    54     // combine trust settings DB path & name
       
    55 
       
    56     HBufC* dbPathBuffer = HBufC::NewLC( KWimTrustSettingsStorePath().Length() +
       
    57                                         KWimTrustSettingsDBFile().Length() );
       
    58     TPtr wimTrustSettingsDB( dbPathBuffer->Des() );
       
    59     wimTrustSettingsDB = KWimTrustSettingsStorePath;
       
    60     wimTrustSettingsDB.Append( KWimTrustSettingsDBFile );
       
    61 
       
    62     // Open database. Note: err used because of create is done if not found
       
    63     TInt err = iDatabase.Open( iDBSession, wimTrustSettingsDB );
       
    64 
       
    65     if ( err != KErrNone ) //Database could not open, create new
       
    66         {
       
    67         _WIMTRACE2( _L( "CWimTrustSettingsStore::ConstructL()|DB open error %d" ), err ); 
       
    68         CreateDBL(); // Create DB
       
    69         User::LeaveIfError( iDatabase.Open( iDBSession, wimTrustSettingsDB ) );
       
    70         }
       
    71 
       
    72     iPhase = EIdle;
       
    73     CleanupStack::PopAndDestroy( dbPathBuffer );
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CWimTrustSettingsStore::NewL()
       
    78 // Two-phased constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CWimTrustSettingsStore* CWimTrustSettingsStore::NewL()
       
    82     {
       
    83     _WIMTRACE ( _L( "CWimTrustSettingsStore::NewL()" ) );
       
    84     CWimTrustSettingsStore* self = new( ELeave ) CWimTrustSettingsStore;
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL();
       
    87     CleanupStack::Pop( self );
       
    88     return self;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CWimTrustSettingsStore::~CWimTrustSettingsStore()
       
    93 // Destructor. If updates has been done, compact databse.
       
    94 // -----------------------------------------------------------------------------
       
    95 
       
    96 CWimTrustSettingsStore::~CWimTrustSettingsStore()
       
    97     {
       
    98     _WIMTRACE ( _L( "CWimTrustSettingsStore::~CWimTrustSettingsStore()" ) );
       
    99     Cancel();
       
   100 
       
   101     if ( iCompact ) // Is compact needed
       
   102         {
       
   103         iDatabase.Compact(); //Compact database
       
   104         }
       
   105 
       
   106     iDatabase.Close();  // Close database
       
   107     iDBSession.Close(); // Close session to DB server
       
   108 
       
   109     if ( iWimCertUtil )
       
   110         {
       
   111         delete iWimCertUtil;
       
   112         }
       
   113 
       
   114     delete iEncodedCertBuf;
       
   115     delete iEncodedCertPtr;
       
   116 
       
   117     iCertInfos.ResetAndDestroy();
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CWimTrustSettingsStore::CloseD()
       
   122 // Closes all open resources and deletes this instance of the TrustSettingsStore
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CWimTrustSettingsStore::CloseD()
       
   126     {
       
   127     _WIMTRACE ( _L( "CWimTrustSettingsStore::CloseD()" ) );
       
   128     delete ( this );
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CWimTrustSettingsStore::GetTrustSettings()
       
   133 // Return trust settings for given certificate.
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CWimTrustSettingsStore::GetTrustSettings( const CWimCertInfo& aCert,
       
   137                                                TBool& aTrusted,
       
   138                                                RArray<TUid>& aApplications, 
       
   139                                                TRequestStatus& aStatus )
       
   140     {
       
   141     _WIMTRACE ( _L( "CWimTrustSettingsStore::GetTrustSettings()" ) );
       
   142     TRAPD( err, DoGetTrustSettingsL( aCert, aTrusted, aApplications ) );
       
   143 
       
   144     TRequestStatus* status = &aStatus;
       
   145     User::RequestComplete( status, err );
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CWimTrustSettingsStore::SetApplicability()
       
   150 // Set applicability for certificate.
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CWimTrustSettingsStore::SetApplicability( const CWimCertInfo& aCert,
       
   154                                               const RArray<TUid>& aApplications,
       
   155                                               TRequestStatus& aStatus )
       
   156     {
       
   157     _WIMTRACE ( _L( "CWimTrustSettingsStore::SetApplicability()" ) );
       
   158     TRAPD( err, DoSetApplicabilityL( aCert, aApplications ) );
       
   159 
       
   160     TRequestStatus* status = &aStatus;
       
   161     User::RequestComplete( status, err );
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CWimTrustSettingsStore::SetTrust()
       
   166 // Set trust flag for certificate.
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CWimTrustSettingsStore::SetTrust( const CWimCertInfo& aCert,
       
   170                                        TBool aTrusted,
       
   171                                        TRequestStatus& aStatus )
       
   172     {
       
   173     _WIMTRACE ( _L( "CWimTrustSettingsStore::SetTrust()" ) );
       
   174     TRAPD( err, DoSetTrustL( aCert, aTrusted ) );
       
   175 
       
   176     TRequestStatus* status = &aStatus;
       
   177     User::RequestComplete( status, err );
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CWimTrustSettingsStore::SetDefaultTrustSettings()
       
   182 // Set default trust settings for certificate.
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CWimTrustSettingsStore::SetDefaultTrustSettings( const CWimCertInfo& aCert,
       
   186                                                       TBool aAddApps,
       
   187                                                       TRequestStatus& aStatus )
       
   188     {
       
   189     _WIMTRACE ( _L( "CWimTrustSettingsStore::SetDefaultTrustSettings()" ) );
       
   190 
       
   191     iWimCert = &aCert;
       
   192     iAddApps = aAddApps;
       
   193     iOriginalRequestStatus = &aStatus;
       
   194 
       
   195     iCert = aCert.CctCert();
       
   196 
       
   197     //Certificate owner type
       
   198     TCertificateOwnerType ownerType;
       
   199     ownerType = iCert->CertificateOwnerType();
       
   200 
       
   201     //Certificate format
       
   202     TCertificateFormat certFormat;
       
   203     certFormat = iCert->CertificateFormat();
       
   204     
       
   205     // If CA/X509 certificate locates in trusted certs area
       
   206     // check if certificate extensions must be fetched
       
   207     if ( ownerType == ECACertificate &&
       
   208          certFormat == EX509Certificate &&
       
   209          iWimCert->CDFRefs() == EWimTrustedCertsCDF )
       
   210         {
       
   211         // Check if Java Midlet OID is present
       
   212         RArray<HBufC*> arr = iWimCert->TrustedUsage();
       
   213         TInt oidCount = arr.Count();
       
   214         TInt i = 0;
       
   215 
       
   216         for ( ; i < oidCount; i++ )
       
   217             {
       
   218             if ( !arr[i]->Compare( KIpKpJavaMidlet ) )
       
   219                 {
       
   220                 i = oidCount + 1;
       
   221                 }
       
   222             }
       
   223         // If OID is not present, extensions must be fetched
       
   224         if ( i == oidCount )
       
   225             {
       
   226             // Fetch extensions only for CA X509 certificates         
       
   227             iPhase = ERetrieve;
       
   228             TRequestStatus* status = &iStatus;
       
   229             User::RequestComplete( status, KErrNone );
       
   230             SetActive();
       
   231             }
       
   232         else
       
   233             {
       
   234             TRAPD( err, DoSetDefaultTrustSettingsL( aCert, aAddApps ) );
       
   235             TRequestStatus* status = &aStatus;
       
   236             User::RequestComplete( status, err );
       
   237             }
       
   238         }
       
   239     else
       
   240         {
       
   241         TRAPD( err, DoSetDefaultTrustSettingsL( aCert, aAddApps ) );
       
   242         TRequestStatus* status = &aStatus;
       
   243         User::RequestComplete( status, err );
       
   244         }
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CWimTrustSettingsStore::RemoveTrustSettings()
       
   249 // Remove trust settings of given certificate
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 void CWimTrustSettingsStore::RemoveTrustSettings( const CWimCertInfo& aCert,
       
   253                                                   TRequestStatus& aStatus )
       
   254     {
       
   255     _WIMTRACE ( _L( "CWimTrustSettingsStore::RemoveTrustSettings()" ) );
       
   256     TRAPD( err, DoRemoveTrustSettingsL( aCert ) );
       
   257     iCompact = ETrue;
       
   258 
       
   259     TRequestStatus* status = &aStatus;
       
   260     User::RequestComplete( status, err );
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CWimTrustSettingsStore::DoGetTrustSettingsL ()
       
   265 // Get trust settings for given certificate.
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CWimTrustSettingsStore::DoGetTrustSettingsL( const CWimCertInfo& aCert,
       
   269                                                   TBool& aTrusted,
       
   270                                                   RArray<TUid>& aApplications )
       
   271     {
       
   272     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoGetTrustSettingsL()" ) );
       
   273     TInt certID;
       
   274     GetCertificateIDL( aCert, certID ); // Get certificate ID
       
   275 
       
   276     if ( certID == KErrNotFound ) //Not found settings
       
   277         {
       
   278         User::Leave( KErrNotFound );
       
   279         }
       
   280     GetTrustedL( certID, aTrusted ); //Get trusted flag
       
   281     GetApplicationsL( certID, aApplications ); //Get applications
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CWimTrustSettingsStore::DoSetApplicabilityL()
       
   286 // Set applicability for certificate.
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CWimTrustSettingsStore::DoSetApplicabilityL( const CWimCertInfo& aCert,
       
   290                                             const RArray<TUid>& aApplications )
       
   291     {
       
   292     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoSetApplicabilityL()" ) );    
       
   293     TInt certID;
       
   294     GetCertificateIDL( aCert, certID ); // Get certificate ID
       
   295 
       
   296     iDatabase.Begin(); // Begin transaction
       
   297 
       
   298     TRAPD( err,
       
   299     if ( certID == KErrNotFound ) //Certificate not found -> Insert new one
       
   300         {
       
   301         InsertCertificateL( aCert, EFalse ); //Insert + set flag (EFalse)
       
   302         GetCertificateIDL( aCert, certID );    //Now get certificate ID
       
   303         }
       
   304     else // Certificate found
       
   305         {
       
   306         RemoveApplicationsL( certID );   //Remove old ones before inserting
       
   307         }
       
   308     InsertApplicationsL( certID, aApplications ); // Insert new apps
       
   309     ); //TRAPD
       
   310 
       
   311     if ( err != KErrNone ) //Some error occured
       
   312         {
       
   313         iDatabase.Rollback(); //Do rollback = restore changes
       
   314         User::Leave ( err );
       
   315         }
       
   316     else // No errors -> commit changes
       
   317         {
       
   318         iDatabase.Commit(); // Commit changes
       
   319         }
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CWimTrustSettingsStore::DoSetTrustL()
       
   324 // Set trust flag for certificate.
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 void CWimTrustSettingsStore::DoSetTrustL( const CWimCertInfo& aCert,
       
   328                                           TBool aTrusted )
       
   329     {
       
   330     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoSetTrustL()" ) );    
       
   331     TInt certID;
       
   332     GetCertificateIDL( aCert, certID ); // Get certificate ID
       
   333 
       
   334     if ( certID == KErrNotFound ) // Certificate not found -> Insert new
       
   335        {
       
   336        InsertCertificateL( aCert, aTrusted ); // Insert + set flag
       
   337        }
       
   338     else // Certificate found
       
   339        {
       
   340         SetTrustedL( certID, aTrusted ); // Set trusted flag
       
   341        }
       
   342     }
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CWimTrustSettingsStore::DoSetDefaultTrustSettingsL()
       
   346 // Set default trust settings for certificate
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 void CWimTrustSettingsStore::DoSetDefaultTrustSettingsL( 
       
   350                                                     const CWimCertInfo& aCert,
       
   351                                                     TBool aAddApps )
       
   352     {
       
   353     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoSetDefaultTrustSettingsL()" ) );    
       
   354     TInt certID;
       
   355     GetCertificateIDL( aCert, certID ); // Get certificate ID
       
   356 
       
   357     iDatabase.Begin(); // Begin transaction
       
   358 
       
   359     TRAPD( err,
       
   360         if ( certID == KErrNotFound ) // Certificate not found -> Insert new one
       
   361             {
       
   362             InsertCertificateL( aCert, KDefaultTrustFlag ); // Set default
       
   363             GetCertificateIDL( aCert, certID );
       
   364             }
       
   365         else // Certificate allready exists, set default trusted flag
       
   366             {
       
   367             SetTrustedL( certID, KDefaultTrustFlag );
       
   368             }
       
   369         //
       
   370         // Applications
       
   371         //
       
   372         if ( aAddApps ) // Default applications required
       
   373             {
       
   374             RArray<TUid> applications;
       
   375             CleanupClosePushL( applications );
       
   376 
       
   377             DefaultApplicationsL( aCert, applications ); 
       
   378             RemoveApplicationsL( certID ); // Remove previous applications
       
   379             if ( applications.Count() > 0 ) // There is apps to insert
       
   380                 {
       
   381                 InsertApplicationsL( certID, applications );
       
   382                 }
       
   383 
       
   384             CleanupStack::PopAndDestroy( &applications );
       
   385             }
       
   386     );
       
   387 
       
   388     if ( err != KErrNone ) // Some error occured
       
   389         {
       
   390         iDatabase.Rollback(); // Do rollback = restore changes
       
   391         User::Leave( err );
       
   392         }
       
   393     else // No errors
       
   394         {
       
   395         iDatabase.Commit(); // Commit changes
       
   396         }
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CWimTrustSettingsStore::DoRemoveTrustSettingsL()
       
   401 // Remove trust settings of given certificate
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CWimTrustSettingsStore::DoRemoveTrustSettingsL( const CWimCertInfo& aCert )
       
   405     {
       
   406     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoRemoveTrustSettingsL()" ) );    
       
   407     TInt certID;
       
   408     GetCertificateIDL( aCert, certID ); // Get certificate ID
       
   409 
       
   410     if ( certID == KErrNotFound ) // Not found
       
   411         {
       
   412         User::Leave( KErrNotFound );
       
   413         }
       
   414 
       
   415     iDatabase.Begin(); // Begin transaction
       
   416 
       
   417     TRAPD( err, 
       
   418         RemoveApplicationsL( certID );  // Remove applications
       
   419         RemoveCertificateL( certID );   // Remove certificate
       
   420     );
       
   421 
       
   422     if ( err ) // Some error occurred
       
   423         {
       
   424         iDatabase.Rollback(); // Rollback changes
       
   425         User::Leave ( err );
       
   426         }
       
   427     else
       
   428         {
       
   429         iDatabase.Commit(); // Commit changes
       
   430         }
       
   431     }
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CWimTrustSettingsStore::GetCertificateIDL()
       
   435 // Get database ID for given certificate. Return KErrNotFound, if not found
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 void CWimTrustSettingsStore::GetCertificateIDL( const CWimCertInfo& aCert,
       
   439                                                 TInt& aCertificateID )
       
   440     {
       
   441     _WIMTRACE ( _L( "CWimTrustSettingsStore::GetCertificateIDL()" ) );    
       
   442     _LIT( KSQLQuery, "SELECT CertificateID,\
       
   443                              CertHash,\
       
   444                              TokenSerial,\
       
   445                              LabelHash,\
       
   446                              IsTrusted\
       
   447                       FROM Certificates" );
       
   448 
       
   449     TBuf8<KSHA1HashLengthBytes> hash;
       
   450     aCert.GetCertHash( hash );
       
   451 
       
   452     HBufC8* buf8 = HBufC8::NewLC( aCert.CctCert()->Label().Length() );
       
   453     TPtr8* ptr8 = new( ELeave ) TPtr8( buf8->Des() );
       
   454     CleanupStack::PushL( ptr8 );
       
   455 
       
   456     ptr8->Copy( aCert.CctCert()->Label() );
       
   457 
       
   458     HBufC8* labelHash = GetHashL( *ptr8 );
       
   459     CleanupStack::PushL( labelHash );
       
   460 
       
   461     RDbView view;
       
   462     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( KSQLQuery ) ) );
       
   463     CleanupClosePushL( view );
       
   464 
       
   465     TInt i = 0;
       
   466     while ( i == 0 && view.NextL() ) // Should find only one ID
       
   467         {
       
   468         view.GetL();
       
   469         // Compare given hash, token serial number, cert label hash
       
   470         if ( !view.ColDes8( 2 ).Compare( hash ) &&
       
   471              !view.ColDes( 3 ).Compare( aCert.CctCert()->
       
   472                                Token().Information( MCTToken::ESerialNo ) ) &&
       
   473              !view.ColDes8( 4 ).Compare( *labelHash ) )
       
   474             {
       
   475             aCertificateID = view.ColUint32( 1 );
       
   476             i++;
       
   477             }
       
   478         }
       
   479             
       
   480     if ( i == 0 ) // Not found any matching rows
       
   481         {
       
   482         aCertificateID = KErrNotFound;
       
   483         }
       
   484 
       
   485     CleanupStack::PopAndDestroy( 4, buf8 ); // view, labelHash, ptr8, buf8
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CWimTrustSettingsStore::InsertCertificateL()
       
   490 // Insert certificate into the Certificates table
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 void CWimTrustSettingsStore::InsertCertificateL( const CWimCertInfo& aCert, 
       
   494                                                  TBool aTrusted )
       
   495     {
       
   496     _WIMTRACE ( _L( "CWimTrustSettingsStore::InsertCertificateL()" ) );    
       
   497     TBuf8<KSHA1HashLengthBytes> hash;
       
   498     aCert.GetCertHash( hash );
       
   499     
       
   500     HBufC8* buf8 = HBufC8::NewLC( aCert.CctCert()->Label().Length() );
       
   501     TPtr8* ptr8 = new(ELeave) TPtr8( buf8->Des() );
       
   502     CleanupStack::PushL( ptr8 );
       
   503 
       
   504     ptr8->Copy( aCert.CctCert()->Label() );
       
   505 
       
   506     HBufC8* labelHash = GetHashL( *ptr8 );
       
   507     CleanupStack::PushL( labelHash );
       
   508 
       
   509     RDbView view;
       
   510 
       
   511     _LIT( KSQLInsertCert, "SELECT CertHash,\
       
   512                                   TokenSerial,\
       
   513                                   LabelHash,\
       
   514                                   IsTrusted\
       
   515                            FROM Certificates" );
       
   516 
       
   517     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( KSQLInsertCert ),
       
   518         TDbWindow::EUnlimited, RDbView::EInsertOnly ) );
       
   519 
       
   520     CleanupClosePushL( view );
       
   521     
       
   522     view.InsertL();
       
   523     view.SetColL( 1, hash );
       
   524     view.SetColL( 2, aCert.CctCert()->
       
   525                            Token().Information( MCTToken::ESerialNo ) );
       
   526     view.SetColL( 3, *labelHash );
       
   527     view.SetColL( 4, aTrusted );
       
   528     view.PutL();
       
   529 
       
   530     CleanupStack::PopAndDestroy( 4, buf8 ); // view, labelHash, ptr8, buf8
       
   531     }
       
   532 
       
   533 // -----------------------------------------------------------------------------
       
   534 // CWimTrustSettingsStore::GetHash()
       
   535 // Return hash of 8 bit string
       
   536 // -----------------------------------------------------------------------------
       
   537 //
       
   538 HBufC8* CWimTrustSettingsStore::GetHashL( TDesC8& aString )
       
   539     {
       
   540     CSHA1* sha = CSHA1::NewL();
       
   541     CleanupStack::PushL( sha );
       
   542     TPtrC8 shaFingerPrint = sha->Hash( aString );
       
   543     HBufC8* fingerPrint = HBufC8::NewL( shaFingerPrint.Length() ); //make a copy
       
   544     TPtr8 fingerPrintDes = fingerPrint->Des();
       
   545     fingerPrintDes.Copy( shaFingerPrint );
       
   546     CleanupStack::PopAndDestroy( sha );
       
   547     return fingerPrint;
       
   548     }
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // CWimTrustSettingsStore::GetTrustedL()
       
   552 // Get trusted flag of given certificate
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void CWimTrustSettingsStore::GetTrustedL( TInt aCertificateID,  
       
   556                                           TBool& aTrusted )
       
   557     {
       
   558     _WIMTRACE ( _L( "CWimTrustSettingsStore::GetTrustedL()" ) );    
       
   559     _LIT( KSQLQuery, 
       
   560          "SELECT IsTrusted FROM Certificates WHERE CertificateID = " );
       
   561 
       
   562     TBuf<KMaxSQLLength> SQLStatement;
       
   563     SQLStatement.Copy( KSQLQuery );
       
   564 
       
   565     SQLStatement.AppendNum( aCertificateID );
       
   566     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   567 
       
   568     RDbView view;
       
   569     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( sqlStat ) ) );
       
   570     CleanupClosePushL( view );
       
   571 
       
   572     while ( view.NextL() ) // Should find only one certificate
       
   573         {
       
   574         view.GetL();
       
   575         aTrusted = ( TBool )view.ColUint( 1 );
       
   576         }
       
   577     
       
   578     CleanupStack::PopAndDestroy( &view );
       
   579     }
       
   580 
       
   581 // -----------------------------------------------------------------------------
       
   582 // CWimTrustSettingsStore::SetTrustedL()
       
   583 // Set trusted flag for given certificate
       
   584 // -----------------------------------------------------------------------------
       
   585 //
       
   586 void CWimTrustSettingsStore::SetTrustedL( TInt aCertificateID,  
       
   587                                           TBool aTrusted )
       
   588     {
       
   589     _WIMTRACE ( _L( "CWimTrustSettingsStore::SetTrustedL()" ) );    
       
   590     _LIT( KSQLUpdate1, "UPDATE Certificates SET IsTrusted = " );
       
   591     _LIT( KSQLUpdate2, " WHERE CertificateID = " );
       
   592     TBuf<KMaxSQLLength> SQLStatement;
       
   593     SQLStatement.Copy( KSQLUpdate1 );
       
   594     SQLStatement.AppendNum( aTrusted );
       
   595     SQLStatement.Append( KSQLUpdate2 );
       
   596     SQLStatement.AppendNum( aCertificateID );
       
   597 
       
   598     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   599 
       
   600     User::LeaveIfError( iDatabase.Execute( sqlStat ) );
       
   601     }
       
   602 
       
   603 // -----------------------------------------------------------------------------
       
   604 // CWimTrustSettingsStore::RemoveCertificateL()
       
   605 // Delete certificate from Certificates table
       
   606 // -----------------------------------------------------------------------------
       
   607 //
       
   608 void CWimTrustSettingsStore::RemoveCertificateL( TInt aCertificateID )
       
   609     {
       
   610     _WIMTRACE ( _L( "CWimTrustSettingsStore::RemoveCertificateL()" ) );    
       
   611     _LIT( KSQLDelete, "DELETE FROM Certificates WHERE CertificateID = " );
       
   612 
       
   613     TBuf<60> SQLStatement;
       
   614     SQLStatement.Copy( KSQLDelete );
       
   615     SQLStatement.AppendNum( aCertificateID );
       
   616     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   617     User::LeaveIfError( iDatabase.Execute( sqlStat ) );
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // CWimTrustSettingsStore::RemoveApplicationsL()
       
   622 // Delete all applications of given certificate
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 void CWimTrustSettingsStore::RemoveApplicationsL( TInt aCertificateID )
       
   626     {
       
   627     _WIMTRACE ( _L( "CWimTrustSettingsStore::RemoveApplicationsL()" ) );    
       
   628     _LIT( KSQLDelete, 
       
   629          "DELETE FROM CertificateApplications WHERE CertificateID = " );
       
   630 
       
   631     TBuf<KMaxSQLLength> SQLStatement;
       
   632     SQLStatement.Copy( KSQLDelete );
       
   633     SQLStatement.AppendNum( aCertificateID );
       
   634     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   635     User::LeaveIfError( iDatabase.Execute( sqlStat ) );
       
   636     iCompact = ETrue;
       
   637     }
       
   638 
       
   639 // -----------------------------------------------------------------------------
       
   640 // CWimTrustSettingsStore::GetApplicationsL()
       
   641 // Get applications of given certificate
       
   642 // -----------------------------------------------------------------------------
       
   643 //
       
   644 void CWimTrustSettingsStore::GetApplicationsL( TInt aCertificateID, 
       
   645                                                RArray<TUid>& aApplications )
       
   646     {
       
   647     _WIMTRACE ( _L( "CWimTrustSettingsStore::GetApplicationsL()" ) );    
       
   648     _LIT( KSQLQuery, "SELECT AppUid FROM CertificateApplications " );
       
   649     _LIT( KSQLQuery2, "WHERE CertificateID = " );
       
   650     
       
   651     TBuf<KMaxSQLLength> SQLStatement;
       
   652     SQLStatement.Copy( KSQLQuery );
       
   653     SQLStatement.Append( KSQLQuery2 );
       
   654     SQLStatement.AppendNum( aCertificateID );
       
   655     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   656 
       
   657     RDbView view;
       
   658     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( sqlStat ) ) );
       
   659     CleanupClosePushL( view );
       
   660 
       
   661     TUid uid;
       
   662     while ( view.NextL() )
       
   663         {
       
   664         view.GetL();
       
   665         uid.iUid = view.ColInt32( 1 );
       
   666         aApplications.AppendL( uid );
       
   667         }
       
   668     
       
   669     CleanupStack::PopAndDestroy( &view );
       
   670     }
       
   671 
       
   672 // -----------------------------------------------------------------------------
       
   673 // CWimTrustSettingsStore::InsertApplicationsL()
       
   674 // Insert applications to certificate
       
   675 // -----------------------------------------------------------------------------
       
   676 //
       
   677 void CWimTrustSettingsStore::InsertApplicationsL( TInt aCertificateID, 
       
   678                                             const RArray<TUid>& aApplications )
       
   679     {
       
   680     _WIMTRACE ( _L( "CWimTrustSettingsStore::InsertApplicationsL()" ) );    
       
   681     RDbView view;
       
   682 
       
   683     // Insert applications
       
   684     _LIT( KSQLInsert, "SELECT CertificateID, AppUid FROM\
       
   685         CertificateApplications" );
       
   686 
       
   687     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( KSQLInsert ),
       
   688                         TDbWindow::EUnlimited, RDbView::EInsertOnly ) );
       
   689     CleanupClosePushL( view );
       
   690     
       
   691     // Loop application list and insert each to database
       
   692     for ( TInt i = 0; i < aApplications.Count(); i++ )
       
   693         {
       
   694         view.InsertL();
       
   695         view.SetColL( 1, aCertificateID );          // CertificateID
       
   696         view.SetColL( 2, aApplications[i].iUid );   // AppUid
       
   697         view.PutL();
       
   698         }
       
   699     
       
   700     CleanupStack::PopAndDestroy( &view );
       
   701     }
       
   702 
       
   703 // -----------------------------------------------------------------------------
       
   704 // CWimTrustSettingsStore::DefaultApplicationsL()
       
   705 //
       
   706 // Decide default applications for certificate regarding certain rules
       
   707 // Format
       
   708 //    EX509Certificate    = 0x00,
       
   709 //    EWTLSCertificate    = 0x01,
       
   710 //    EX968Certificate    = 0x02,
       
   711 //    EUnknownCertificate = 0x0f,
       
   712 //    EX509CertificateUrl = 0x10,
       
   713 //    EWTLSCertificateUrl = 0x11,
       
   714 //    EX968CertificateUrl = 0x12
       
   715 //
       
   716 // Trusting applications
       
   717 //    KTrustUidWapBearer     
       
   718 //    KTrustUidAppController
       
   719 //    KTrustUidInternet
       
   720 //    KTrustUidJavaMidlet
       
   721 //
       
   722 // -----------------------------------------------------------------------------
       
   723 //
       
   724 void CWimTrustSettingsStore::DefaultApplicationsL( const CWimCertInfo& aCert,
       
   725                                                    RArray<TUid>& aApplications )
       
   726     {
       
   727     _WIMTRACE ( _L( "CWimTrustSettingsStore::DefaultApplicationsL()" ) );    
       
   728     const CCTCertInfo* cctCert = aCert.CctCert();
       
   729 
       
   730     //Certificate owner type
       
   731     TCertificateOwnerType ownerType;
       
   732     ownerType = cctCert->CertificateOwnerType();
       
   733 
       
   734     //Certificate format
       
   735     TCertificateFormat certFormat;
       
   736     certFormat = cctCert->CertificateFormat();
       
   737     TInt32 appUid;
       
   738     
       
   739     // Applications only for CA certificates
       
   740     switch ( ownerType )
       
   741         {
       
   742         case ECACertificate:
       
   743             {   
       
   744             // Applications by certificate format
       
   745             switch ( certFormat )    
       
   746                 {
       
   747                 case EX509Certificate: // For Url not the same handling
       
   748                     {
       
   749                     DefaultAppsByOIDsL( aCert, aApplications );
       
   750                     break;
       
   751                     }
       
   752                 case EWTLSCertificate: //All WTLSs has same apps, flow through!
       
   753                 case EWTLSCertificateUrl:
       
   754                     {
       
   755                     appUid = KTrustUidWapBearer; //WAP
       
   756                     TUid uid = {appUid};
       
   757                     User::LeaveIfError( aApplications.Append( uid ) );
       
   758                     break;
       
   759                     }             
       
   760                 default:
       
   761                     {
       
   762                     break;
       
   763                     }
       
   764                 }
       
   765             break;
       
   766             }
       
   767         case EUserCertificate: // User certificate: no defaults
       
   768             {
       
   769             break;
       
   770             }
       
   771         case EPeerCertificate: // Peer certificate: no defaults
       
   772             {
       
   773             break;
       
   774             }
       
   775         default:
       
   776             {
       
   777             break;
       
   778             }
       
   779         }
       
   780     }
       
   781 
       
   782 // -----------------------------------------------------------------------------
       
   783 // CWimTrustSettingsStore::DefaultAppsByOIDsL()
       
   784 // Get Default AppUid(s) from database regarding the OIDs from certificate
       
   785 // -----------------------------------------------------------------------------
       
   786 //
       
   787 void CWimTrustSettingsStore::DefaultAppsByOIDsL( const CWimCertInfo& aCert,
       
   788                                                  RArray<TUid>& aApplications )
       
   789     {
       
   790     _WIMTRACE ( _L( "CWimTrustSettingsStore::DefaultAppsByOIDsL()" ) );    
       
   791     // Get trusted usage OID(s) of certificate
       
   792     RArray<HBufC*> certTrustedUsage = aCert.TrustedUsage();
       
   793     // Get extended key usage OID(s) of certificate
       
   794     RArray<HBufC*> certExtendedKeyUsage = aCert.ExtendedKeyUsage();
       
   795     // Get certificate location 
       
   796     TUint8 certLocation = aCert.CDFRefs();
       
   797 
       
   798     TInt certTrustedCount;
       
   799     TInt certExtendedUsageCount;
       
   800 
       
   801     certTrustedCount = certTrustedUsage.Count();
       
   802     certExtendedUsageCount = certExtendedKeyUsage.Count();
       
   803    
       
   804     // Select all rows of table
       
   805     _LIT( KSQLQuery1, 
       
   806         "SELECT TrustedUsage, ExtendedKeyUsage, Location, AppUid " );
       
   807     _LIT( KSQLQuery2, "FROM CertificateDefaultApps " );
       
   808     TBuf<KMaxSQLLength> SQLStatement;
       
   809     SQLStatement.Copy( KSQLQuery1 );
       
   810     SQLStatement.Append( KSQLQuery2 );
       
   811     
       
   812     TPtrC sqlStat( SQLStatement.PtrZ() );
       
   813     RDbView view;
       
   814     User::LeaveIfError( view.Prepare( iDatabase, TDbQuery( sqlStat ) ) );
       
   815     CleanupClosePushL( view );
       
   816     
       
   817     TInt32  appUid = 0;
       
   818     TUid    uid;
       
   819     TBool   found = EFalse;
       
   820     TBool   canContinue = EFalse;
       
   821     TPtrC   defaultTrustedUsage;
       
   822     TPtrC   defaultExtendedKeyUsage;
       
   823     TInt    defaultLocation;
       
   824 
       
   825     // Check first if certificate's trusted usage field matches
       
   826     // After that check extended key usage
       
   827     while ( view.NextL() )
       
   828         {
       
   829         view.GetL();
       
   830 
       
   831         defaultTrustedUsage.Set( view.ColDes( 1 ) );
       
   832         defaultExtendedKeyUsage.Set( view.ColDes( 2 ) );
       
   833         defaultLocation = view.ColInt( 3 ); // Not used yet
       
   834         
       
   835         if ( certTrustedCount == 0 ) // No trusted usages in the cert
       
   836             {
       
   837             if ( defaultTrustedUsage.Length() == 0 ) // Either in defaults
       
   838                 {
       
   839                 canContinue = ETrue; 
       
   840                 }
       
   841             }
       
   842         else
       
   843             {
       
   844             for ( TInt i = 0; i < certTrustedCount; i++ )
       
   845                 {
       
   846                 if ( !defaultTrustedUsage.
       
   847                       Compare( certTrustedUsage[i]->Des() ) )
       
   848                     {
       
   849                     canContinue = ETrue;
       
   850                     }
       
   851                 }
       
   852             }
       
   853 
       
   854         if ( canContinue )
       
   855             {
       
   856             canContinue = EFalse;
       
   857 
       
   858             if ( certExtendedUsageCount == 0 ) // No ext.key usages in the cert
       
   859                 {
       
   860                 if ( defaultExtendedKeyUsage.Length() == 0 ) // Either in defs
       
   861                     {
       
   862                     canContinue = ETrue;
       
   863                     }
       
   864                 }
       
   865             else
       
   866                 {
       
   867                 for ( TInt i = 0; i < certExtendedUsageCount; i++ )
       
   868                     {
       
   869                     if ( !defaultExtendedKeyUsage.
       
   870                           Compare( certExtendedKeyUsage[i]->Des() ) )
       
   871                         {
       
   872                         canContinue = ETrue;
       
   873                         }
       
   874                     }
       
   875                 }
       
   876             }
       
   877 
       
   878         if ( canContinue )
       
   879             {
       
   880             if ( defaultLocation == EWimUnknownCDF ) // No default location
       
   881                 {
       
   882                 appUid = view.ColInt32( 4 );
       
   883                 }
       
   884             else
       
   885                 {
       
   886                 if ( defaultLocation == certLocation )
       
   887                     {
       
   888                     appUid = view.ColInt32( 4 );
       
   889                     }
       
   890                 }
       
   891             }
       
   892 
       
   893         if ( appUid > 0 ) // Application found -> append it
       
   894             {
       
   895             //Check if app UID allready exists in array
       
   896             for ( TInt i = 0; !found && i < aApplications.Count(); i++ )
       
   897                 {
       
   898                 if ( aApplications[i].iUid == appUid )
       
   899                     {
       
   900                     found = ETrue;
       
   901                     }
       
   902                 }
       
   903             if ( !found ) // AppUid not added to array yet
       
   904                 {
       
   905                 uid.iUid = appUid;
       
   906                 aApplications.AppendL( uid );
       
   907                 }
       
   908             found = EFalse;
       
   909             }
       
   910         appUid = 0;
       
   911         canContinue = EFalse;
       
   912         }
       
   913 
       
   914     CleanupStack::PopAndDestroy( &view );
       
   915     }
       
   916 
       
   917 // -----------------------------------------------------------------------------
       
   918 // CWimTrustSettingsStore::CreateDBL()
       
   919 // Create database for trust settings
       
   920 // -----------------------------------------------------------------------------
       
   921 //
       
   922 void CWimTrustSettingsStore::CreateDBL() const
       
   923     {
       
   924     _WIMTRACE ( _L( "CWimTrustSettingsStore::CreateDBL()" ) );    
       
   925     RFs fsSession;
       
   926     User::LeaveIfError( fsSession.Connect() );
       
   927     CleanupClosePushL( fsSession );
       
   928     
       
   929     RDbNamedDatabase database;
       
   930 
       
   931     // combine trust settings DB path & name
       
   932 
       
   933     TInt pathError = fsSession.MkDirAll( KWimTrustSettingsStorePath );
       
   934     if (( pathError != KErrNone ) && ( pathError != KErrAlreadyExists ))
       
   935         {
       
   936         User::Leave( pathError ); // Dir does not exist and can't be created
       
   937         }
       
   938     HBufC* dbPathBuffer = HBufC::NewLC( KWimTrustSettingsStorePath().Length() +
       
   939                                         KWimTrustSettingsDBFile().Length() );
       
   940     TPtr wimTrustSettingsDB( dbPathBuffer->Des() );
       
   941     wimTrustSettingsDB = KWimTrustSettingsStorePath;
       
   942     wimTrustSettingsDB.Append( KWimTrustSettingsDBFile );
       
   943 
       
   944     User::LeaveIfError( database.Replace( fsSession, wimTrustSettingsDB ) );
       
   945     CleanupClosePushL( database );
       
   946 
       
   947     // Create tables
       
   948 
       
   949     // Certificates
       
   950     _LIT( KSQLCreateTable1, "CREATE TABLE Certificates (\
       
   951                                           CertificateID COUNTER NOT NULL,\
       
   952                                           CertHash CHAR(20) NOT NULL,\
       
   953                                           TokenSerial CHAR(20) NOT NULL,\
       
   954                                           LabelHash CHAR(20) NOT NULL,\
       
   955                                           IsTrusted BIT NOT NULL)" );
       
   956     // CertificateApplications
       
   957     _LIT( KSQLCreateTable2, "CREATE TABLE CertificateApplications (\
       
   958                                           CertificateID INTEGER NOT NULL,\
       
   959                                           AppUid INTEGER NOT NULL )" );
       
   960     // CertificateDefaultApps
       
   961     _LIT( KSQLCreateTable3, "CREATE TABLE CertificateDefaultApps (\
       
   962                                           TrustedUsage VARCHAR(200),\
       
   963                                           ExtendedKeyUsage VARCHAR(200),\
       
   964                                           Location INTEGER NOT NULL,\
       
   965                                           AppUid INTEGER NOT NULL )" );
       
   966 
       
   967     User::LeaveIfError( database.Execute( KSQLCreateTable1 ) );
       
   968     User::LeaveIfError( database.Execute( KSQLCreateTable2 ) );
       
   969     User::LeaveIfError( database.Execute( KSQLCreateTable3 ) );
       
   970 
       
   971     // Create unique index for CertHash
       
   972     _LIT( KSQLTableIndex, "CREATE UNIQUE INDEX hash_index ON\
       
   973         Certificates (CertHash, TokenSerial, LabelHash)" );
       
   974     User::LeaveIfError( database.Execute( KSQLTableIndex ) );
       
   975    
       
   976     CleanupStack::PopAndDestroy( 3, &fsSession ); // database, dbPathBuffer, fsSession
       
   977 
       
   978     InsertDefaultAppRulesL();
       
   979 
       
   980     _WIMTRACE ( _L( "CWimTrustSettingsStore::CreateDBL()|End" ) );
       
   981     }
       
   982 
       
   983 // -----------------------------------------------------------------------------
       
   984 // CWimTrustSettingsStore::InsertDefaultAppRulesL()
       
   985 // Insert default application UID selection rules to the database
       
   986 // -----------------------------------------------------------------------------
       
   987 //
       
   988 void CWimTrustSettingsStore::InsertDefaultAppRulesL() const
       
   989     {
       
   990     _WIMTRACE ( _L( "CWimTrustSettingsStore::InsertDefaultAppRulesL()" ) );
       
   991     RDbView view;
       
   992 
       
   993     RFs fsSession;
       
   994     User::LeaveIfError( fsSession.Connect() );
       
   995     CleanupClosePushL( fsSession );
       
   996 
       
   997     RDbNamedDatabase database;
       
   998 
       
   999     // combine trust settings DB path & name
       
  1000     HBufC* dbPathBuffer = HBufC::NewLC( KWimTrustSettingsStorePath().Length() +
       
  1001                                         KWimTrustSettingsDBFile().Length() );
       
  1002     TPtr wimTrustSettingsDB( dbPathBuffer->Des() );
       
  1003     wimTrustSettingsDB = KWimTrustSettingsStorePath;
       
  1004     wimTrustSettingsDB.Append( KWimTrustSettingsDBFile );
       
  1005 
       
  1006     User::LeaveIfError( database.Open( fsSession, wimTrustSettingsDB ) );
       
  1007     CleanupClosePushL( database );
       
  1008 
       
  1009     _LIT( KSQLInsertRules, "SELECT TrustedUsage, ExtendedKeyUsage,\
       
  1010         Location, AppUid FROM CertificateDefaultApps" );
       
  1011 
       
  1012     User::LeaveIfError( view.Prepare( database, TDbQuery( KSQLInsertRules ),
       
  1013         TDbWindow::EUnlimited, RDbView::EInsertOnly ) );
       
  1014 
       
  1015     CleanupClosePushL( view );
       
  1016 
       
  1017     view.InsertL();
       
  1018     view.SetColL( 1, _L("") );                  // TrustedUsage OID
       
  1019     view.SetColL( 2, _L("") );                  // ExtendedUsage OID
       
  1020     view.SetColL( 3, TInt( EWimUnknownCDF ) );  // Location
       
  1021     view.SetColL( 4, KTrustUidInternet );       // Application Uid
       
  1022     view.PutL();
       
  1023 
       
  1024     view.InsertL();
       
  1025     view.SetColL( 1, KIpKpServerAuth );         // TrustedUsage OID
       
  1026     view.SetColL( 2, KIpKpServerAuth );         // ExtendedUsage OID
       
  1027     view.SetColL( 3, TInt( EWimUnknownCDF ) );  // Location
       
  1028     view.SetColL( 4, KTrustUidInternet );       // Application Uid
       
  1029     view.PutL();
       
  1030 
       
  1031     view.InsertL();
       
  1032     view.SetColL( 1, _L("") );                  // TrustedUsage OID
       
  1033     view.SetColL( 2, KIpKpServerAuth );         // ExtendedUsage OID
       
  1034     view.SetColL( 3, TInt( EWimUnknownCDF ) );  // Application Uid
       
  1035     view.SetColL( 4, KTrustUidInternet );       // Application Uid
       
  1036     view.PutL();
       
  1037 
       
  1038     view.InsertL();
       
  1039     view.SetColL( 1, KIpKpServerAuth );         // TrustedUsage OID
       
  1040     view.SetColL( 2, _L("") );                  // ExtendedUsage OID
       
  1041     view.SetColL( 3, TInt( EWimUnknownCDF ) );  // Location
       
  1042     view.SetColL( 4, KTrustUidInternet );       // Application Uid
       
  1043     view.PutL();
       
  1044 
       
  1045     view.InsertL();
       
  1046     view.SetColL( 1, KIpKpJavaMidlet );         // TrustedUsage OID
       
  1047     view.SetColL( 2, _L("") );                  // ExtendedUsage OID
       
  1048     view.SetColL( 3, TInt( EWimTrustedCertsCDF ) ); // Location
       
  1049     view.SetColL( 4, KTrustUidJavaMidlet );     // Application Uid
       
  1050     view.PutL();
       
  1051 
       
  1052     view.InsertL();
       
  1053     view.SetColL( 1, _L("") );                  // TrustedUsage OID
       
  1054     view.SetColL( 2, KWimCodeSigningOID );      // ExtendedUsage OID
       
  1055     view.SetColL( 3, TInt( EWimTrustedCertsCDF ) );  // Location
       
  1056     view.SetColL( 4, KTrustUidJavaMidlet );     // Application Uid
       
  1057     view.PutL();
       
  1058 
       
  1059     CleanupStack::PopAndDestroy( 4, &fsSession ); // view, database, dbPathBuffer, fsSession
       
  1060     }
       
  1061 
       
  1062 
       
  1063 // -----------------------------------------------------------------------------
       
  1064 // CWimTrustSettingsStore::RunL()
       
  1065 // Not used.
       
  1066 // -----------------------------------------------------------------------------
       
  1067 //
       
  1068 void CWimTrustSettingsStore::RunL()
       
  1069     {
       
  1070 
       
  1071     switch ( iPhase )
       
  1072         {
       
  1073         case ERetrieve:
       
  1074             {
       
  1075             iPhase = ECheckRestore;
       
  1076 
       
  1077             if ( !iWimCertUtil )
       
  1078                 {
       
  1079                 iWimCertUtil = CWimCertUtil::NewL( iCert->Token() );
       
  1080                 iCertInfos.ResetAndDestroy();
       
  1081                 iWimCertUtil->Restore( iCertInfos, iStatus );
       
  1082                 }
       
  1083             else
       
  1084                 {
       
  1085                 TRequestStatus* status = &iStatus;
       
  1086                 User::RequestComplete( status, KErrNone );
       
  1087                 }
       
  1088             SetActive();
       
  1089             break;
       
  1090             }
       
  1091         case ECheckRestore:
       
  1092             {
       
  1093             if ( iStatus.Int() == KErrNone )
       
  1094                 {
       
  1095                 // Allocate space for certificate data
       
  1096                 delete iEncodedCertBuf;
       
  1097                 iEncodedCertBuf = 0;
       
  1098                 delete iEncodedCertPtr;
       
  1099                 iEncodedCertPtr = 0;
       
  1100                 iEncodedCertBuf = HBufC8::NewL( iCert->Size() );
       
  1101                 iEncodedCertPtr = new( ELeave ) TPtr8( iEncodedCertBuf->Des() );
       
  1102                 iPhase = ECheckRetrieve;
       
  1103 
       
  1104                 iWimCertUtil->RetrieveCertByIndexL( iCert->Handle().iObjectId,
       
  1105                                                     *iEncodedCertPtr, 
       
  1106                                                     iStatus ); 
       
  1107                 SetActive();
       
  1108                 }
       
  1109             else
       
  1110                 {
       
  1111                 iPhase = EIdle;
       
  1112                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
  1113                 }
       
  1114             break;
       
  1115             }
       
  1116         case ECheckRetrieve:
       
  1117             {
       
  1118             if ( iStatus.Int() == KErrNone )
       
  1119                 {
       
  1120                 ExtractExtensionL();
       
  1121                 DoSetDefaultTrustSettingsL( *iWimCert, iAddApps );
       
  1122                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
  1123                 }
       
  1124             else
       
  1125                 {
       
  1126                 iPhase = EIdle;
       
  1127                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
  1128                 }
       
  1129             break;
       
  1130             }
       
  1131         default:
       
  1132             {
       
  1133             // Here we should not be
       
  1134             User::RequestComplete( iOriginalRequestStatus, KErrCorrupt );
       
  1135             break;
       
  1136             }
       
  1137         }
       
  1138     }
       
  1139 
       
  1140 // -----------------------------------------------------------------------------
       
  1141 // CWimTrustSettingsStore::ExtractExtensionL()
       
  1142 // If the certificate extension contains extended key usage and certificate 
       
  1143 // locates in the trusted certificates area, add OID to extended key usages
       
  1144 // -----------------------------------------------------------------------------
       
  1145 //
       
  1146 void CWimTrustSettingsStore::ExtractExtensionL()
       
  1147     {
       
  1148     _WIMTRACE ( _L( "CWimTrustSettingsStore::ExtractExtensionL()" ) );    
       
  1149     CX509Certificate* cert = CX509Certificate::NewLC( *iEncodedCertPtr );
       
  1150 
       
  1151     const CX509CertExtension* ext = cert->Extension( KExtendedKeyUsage );
       
  1152 
       
  1153     if ( ext )
       
  1154         {
       
  1155         TInt dummy = 0;
       
  1156         CX509ExtendedKeyUsageExt* extendedKeyUsage = 
       
  1157                 CX509ExtendedKeyUsageExt::NewL( ext->Data(), dummy );
       
  1158         CleanupStack::PushL( extendedKeyUsage );
       
  1159         const CArrayPtrFlat<HBufC>& usages = extendedKeyUsage->KeyUsages();
       
  1160 
       
  1161         TInt count = usages.Count();
       
  1162         for ( TInt i = 0; i < count; i++ )
       
  1163             {
       
  1164             HBufC* buf = usages[i]->AllocLC();
       
  1165             CWimCertInfo* tmp = const_cast<CWimCertInfo*>( iWimCert );
       
  1166             User::LeaveIfError( tmp->AddExtendedKeyUsage( buf ) ); 
       
  1167             CleanupStack::Pop( buf ); // Take the ownership
       
  1168             }
       
  1169 
       
  1170         CleanupStack::PopAndDestroy( extendedKeyUsage );
       
  1171         }
       
  1172 
       
  1173     CleanupStack::PopAndDestroy( cert );
       
  1174     }
       
  1175 
       
  1176 // -----------------------------------------------------------------------------
       
  1177 // CWimTrustSettingsStore::CancelDoing()
       
  1178 // Not used.
       
  1179 // -----------------------------------------------------------------------------
       
  1180 //
       
  1181 void CWimTrustSettingsStore::CancelDoing()
       
  1182     {
       
  1183     _WIMTRACE ( _L( "CWimTrustSettingsStore::Cancel()" ) );
       
  1184     Cancel();
       
  1185     }
       
  1186 // -----------------------------------------------------------------------------
       
  1187 // CWimTrustSettingsStore::DoCancel()
       
  1188 // Not used.
       
  1189 // -----------------------------------------------------------------------------
       
  1190 //
       
  1191 void CWimTrustSettingsStore::DoCancel()
       
  1192     {
       
  1193     _WIMTRACE ( _L( "CWimTrustSettingsStore::DoCancel()" ) );
       
  1194     iPhase = EIdle;
       
  1195     User::RequestComplete( iOriginalRequestStatus, KErrCancel );
       
  1196     }
       
  1197 
       
  1198 // -----------------------------------------------------------------------------
       
  1199 // void CWimTrustSettingsStore::RunError()
       
  1200 // Leave leads us here.
       
  1201 // -----------------------------------------------------------------------------
       
  1202 //
       
  1203 TInt CWimTrustSettingsStore::RunError( TInt aError )
       
  1204     {
       
  1205     _WIMTRACE ( _L( "CWimTrustSettingsStore::RunError()" ) );
       
  1206     iPhase = EIdle;
       
  1207     User::RequestComplete( iOriginalRequestStatus, aError );
       
  1208     return KErrNone;
       
  1209     }
       
  1210 
       
  1211 //  End of File