boottimeintegritycheck/src/IntegrityCheck.cpp
changeset 15 318c4eab2439
parent 0 164170e6151a
equal deleted inserted replaced
14:b75757c81051 15:318c4eab2439
    75 using namespace Swi;
    75 using namespace Swi;
    76 
    76 
    77 // DATA TYPES
    77 // DATA TYPES
    78 
    78 
    79 // FUNCTION PROTOTYPES
    79 // FUNCTION PROTOTYPES
       
    80 
       
    81 template <class T>
       
    82 class CleanupResetAndDestroy
       
    83     {
       
    84 public:
       
    85     inline static void PushL(T& aRef);
       
    86 private:
       
    87     static void ResetAndDestroy(TAny *aPtr);
       
    88     };
       
    89     
       
    90 template <class T>
       
    91 inline void CleanupResetAndDestroyPushL(T& aRef);
       
    92 
       
    93 template <class T>
       
    94 inline void CleanupResetAndDestroy<T>::PushL(T& aRef)
       
    95     {
       
    96     CleanupStack::PushL(TCleanupItem(&ResetAndDestroy,&aRef));
       
    97     }
       
    98 
       
    99 template <class T>
       
   100 void CleanupResetAndDestroy<T>::ResetAndDestroy(TAny *aPtr)
       
   101     {
       
   102     static_cast<T*>(aPtr)->ResetAndDestroy();
       
   103     }
       
   104 
       
   105 template <class T>
       
   106 inline void CleanupResetAndDestroyPushL(T& aRef)
       
   107     {
       
   108     CleanupResetAndDestroy<T>::PushL(aRef);
       
   109     }
       
   110 
    80 
   111 
    81 LOCAL_C TInt ThreadStartL();
   112 LOCAL_C TInt ThreadStartL();
    82 
   113 
    83 LOCAL_C TInt ReadCertificatesL( 
   114 LOCAL_C TInt ReadCertificatesL( 
    84     RFs& aFs, 
   115     RFs& aFs, 
   388         }
   419         }
   389                
   420                
   390  
   421  
   391     BTIC_TRACE_PRINT_NUM("Updated store read enabled(1) = %d",certFileFound);
   422     BTIC_TRACE_PRINT_NUM("Updated store read enabled(1) = %d",certFileFound);
   392     BTIC_TRACE_PRINT_NUM("Binaries check enabled(0) = %d",binariesCheckEnbled);           
   423     BTIC_TRACE_PRINT_NUM("Binaries check enabled(0) = %d",binariesCheckEnbled);           
   393    
   424 //FIX   
   394     // Create pointer array for root certificates.  
   425     // Create pointer array for root certificates.  
   395     RPointerArray<CX509Certificate> x509CertArray;     
   426     RPointerArray<CX509Certificate> x509CertArray; 
       
   427     CleanupResetAndDestroyPushL( x509CertArray );
       
   428     pushToStack++;
   396     RPointerArray<TCapabilitySet> certCapaArray;
   429     RPointerArray<TCapabilitySet> certCapaArray;
       
   430     CleanupResetAndDestroyPushL( certCapaArray );
       
   431     pushToStack++;
   397     TInt fileError = 0;
   432     TInt fileError = 0;
   398      
   433      
   399     // Read root certificates if needed.      
   434     // Read root certificates if needed.      
   400     if ( certFileFound || binariesCheckEnbled )
   435     if ( certFileFound || binariesCheckEnbled )
   401         {                                    
   436         {                                    
   411         // Let's return and try this in next boot.    
   446         // Let's return and try this in next boot.    
   412         if ( fileError || x509CertArray.Count() == 0 )
   447         if ( fileError || x509CertArray.Count() == 0 )
   413             {
   448             {
   414             BTIC_TRACE_PRINT("ERROR Can't read root certificates ! ! !");
   449             BTIC_TRACE_PRINT("ERROR Can't read root certificates ! ! !");
   415             CleanupStack::PopAndDestroy( pushToStack );
   450             CleanupStack::PopAndDestroy( pushToStack );
   416             x509CertArray.ResetAndDestroy();                   
   451 
   417             certCapaArray.ResetAndDestroy();
       
   418             User::Leave( fileError );
   452             User::Leave( fileError );
   419             }               
   453             }               
   420         }
   454         }
   421               
   455             
   422     // Create temporary pointer array for C-drive updated certificates. 
   456     // Create temporary pointer array for C-drive updated certificates. 
   423     RPointerArray<CX509Certificate> tempCertArray;  
   457     RPointerArray<CX509Certificate> tempCertArray; 
   424     RPointerArray<TCapabilitySet> tempCapaArray;    
   458     CleanupResetAndDestroyPushL( tempCertArray );
   425              
   459     pushToStack++;
       
   460     RPointerArray<TCapabilitySet> tempCapaArray; 
       
   461     CleanupResetAndDestroyPushL( tempCapaArray );
       
   462     pushToStack++;
       
   463     
   426     // If candidate for certstore file is found read it and validate file.
   464     // If candidate for certstore file is found read it and validate file.
   427     if ( certFileFound )
   465     if ( certFileFound )
   428         {        
   466         {        
   429         BTIC_TRACE_PRINT("Read certificates from C");       
   467         BTIC_TRACE_PRINT("Read certificates from C");       
   430                                         
   468                                         
   510                                   
   548                                   
   511 #endif //__BTIC_BINARIES_CHECK_ENABLED
   549 #endif //__BTIC_BINARIES_CHECK_ENABLED
   512 */      
   550 */      
   513                             
   551                             
   514     BTIC_TRACE_PRINT("\nProgram complete\n");
   552     BTIC_TRACE_PRINT("\nProgram complete\n");
   515     // DELETE ALL ARRAYS AND ARRAY CONTENT    
   553     // DELETE ALL ARRAYS AND ARRAY CONTENT  
   516     x509CertArray.ResetAndDestroy();                
   554     // x509CertArray, tempCertArray, certCapaArray, tempCapaArray, fs,  buffers
   517     tempCertArray.ResetAndDestroy();
   555     CleanupStack::PopAndDestroy( pushToStack ); 
   518     certCapaArray.ResetAndDestroy();
       
   519     tempCapaArray.ResetAndDestroy();  
       
   520   
       
   521     CleanupStack::PopAndDestroy( pushToStack ); //fs, buffers
       
   522       
   556       
   523     BTIC_TRACE_PRINT("UHEAP MARK END");
   557     BTIC_TRACE_PRINT("UHEAP MARK END");
   524     __UHEAP_MARKEND;
   558     __UHEAP_MARKEND;
   525     BTIC_TRACE_PRINT_ALLOC_CELLS("End Cell count = %d");
   559     BTIC_TRACE_PRINT_ALLOC_CELLS("End Cell count = %d");
   526      
   560      
   681     {
   715     {
   682     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateCertStoreL v2->");
   716     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateCertStoreL v2->");
   683     __UHEAP_MARK;
   717     __UHEAP_MARK;
   684                           
   718                           
   685     TBool storeOK = EFalse;                  
   719     TBool storeOK = EFalse;                  
   686     
   720  
   687     RPointerArray<HBufC> updaterExePaths;
   721     RPointerArray<HBufC> updaterExePaths;
       
   722     CleanupResetAndDestroyPushL( updaterExePaths );
       
   723     
       
   724     RPointerArray<CSisRegistryPackage> sisPackages;
       
   725     CleanupResetAndDestroyPushL( sisPackages );
   688          
   726          
   689     Swi::RSisRegistrySession sisRegSession;  
   727     Swi::RSisRegistrySession sisRegSession;  
   690 
   728 
   691     // Leave if we can not connect to sis register. Othervice we have a risk 
   729     // Leave if we can not connect to sis register. Othervice we have a risk 
   692     // that valid files will be renamed if updated certstore is left out.             
   730     // that valid files will be renamed if updated certstore is left out.             
   693     User::LeaveIfError( sisRegSession.Connect() );
   731     User::LeaveIfError( sisRegSession.Connect() );
   694         
   732         
   695     CleanupClosePushL( sisRegSession );
   733     CleanupClosePushL( sisRegSession );
   696     
       
   697     RPointerArray<CSisRegistryPackage> sisPackages;
       
   698                 
   734                 
   699     // Find installed packages.
   735     // Find installed packages.
   700     sisRegSession.InstalledPackagesL( sisPackages );            
   736     sisRegSession.InstalledPackagesL( sisPackages );            
   701             
   737             
   702     Swi::RSisRegistryEntry entry;        
   738     Swi::RSisRegistryEntry entry;        
   776                     entry,
   812                     entry,
   777                     updaterExePaths );
   813                     updaterExePaths );
   778                 }                
   814                 }                
   779             }                                                        
   815             }                                                        
   780         }                       
   816         }                       
   781                                                    
   817                                          
   782     // Cleanup array.           
       
   783     sisPackages.ResetAndDestroy();                                           
       
   784 
       
   785     if ( entryOpen )
   818     if ( entryOpen )
   786         {        
   819         {        
   787         CleanupStack::PopAndDestroy(); //RSisRegistryEntry               
   820         CleanupStack::PopAndDestroy(); //RSisRegistryEntry               
   788         }
   821         }
   789                        
   822                        
   794     // close SisRegistryEntry before running updater. 
   827     // close SisRegistryEntry before running updater. 
   795     if ( storeOK && packageUID == KBTICSymbianSWIUpdaterUID )
   828     if ( storeOK && packageUID == KBTICSymbianSWIUpdaterUID )
   796         {              
   829         {              
   797         storeOK = RunUpdaterL( aFs, updaterExePaths );                        
   830         storeOK = RunUpdaterL( aFs, updaterExePaths );                        
   798         }
   831         }
   799         
   832     
   800     updaterExePaths.ResetAndDestroy();    
   833     CleanupStack::PopAndDestroy( 1, &sisPackages );    
       
   834     CleanupStack::PopAndDestroy( 1, &updaterExePaths ); 
   801                   
   835                   
   802     BTIC_TRACE_PRINT("UHEAP MARK END");     
   836     BTIC_TRACE_PRINT("UHEAP MARK END");     
   803     __UHEAP_MARKEND;   
   837     __UHEAP_MARKEND;   
   804     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateCertStoreL v2 <-");        
   838     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateCertStoreL v2 <-");        
   805     
   839     
   825     const TDesC& aCertStoreFilePath,
   859     const TDesC& aCertStoreFilePath,
   826     RSisRegistryEntry& aEntry )
   860     RSisRegistryEntry& aEntry )
   827     {
   861     {
   828     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateProvisonerStoreL --->");
   862     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] ValidateProvisonerStoreL --->");
   829     __UHEAP_MARK;
   863     __UHEAP_MARK;
   830         
   864        
   831     TBool retOK = EFalse;                      
   865     TBool retOK = EFalse;                      
   832     // installedFiles array owns entry's files.
   866     // installedFiles array owns entry's files.
   833     RPointerArray<HBufC> installedFilesArray;
   867     RPointerArray<HBufC> installedFilesArray;
   834     RPointerArray<HBufC> foundFilesArray;  
   868     RPointerArray<HBufC> foundFilesArray;  
   835     RPointerArray<HBufC> fileArray;  // Files that need to be cheked.
   869     RPointerArray<HBufC> fileArray;  // Files that need to be cheked.
   836     RPointerArray<HBufC8> hashArray; // Hash for each fileArray's file.
   870     RPointerArray<HBufC8> hashArray; // Hash for each fileArray's file.
   837     RArray<TUint64> capaArray;              
   871     RArray<TUint64> capaArray;              
   838     RPointerArray<HBufC> stringArray;
   872     RPointerArray<HBufC> stringArray;
   839     HBufC* string = NULL;
   873     HBufC* string = NULL;
       
   874     
       
   875     CleanupResetAndDestroyPushL( installedFilesArray );
       
   876     CleanupResetAndDestroyPushL( foundFilesArray );
       
   877     CleanupResetAndDestroyPushL( fileArray );
       
   878     CleanupResetAndDestroyPushL( hashArray );
       
   879     CleanupResetAndDestroyPushL( stringArray );
   840     
   880     
   841     TUint64* capaVector = new( ELeave ) TUint64[KBTICCapaCount];
   881     TUint64* capaVector = new( ELeave ) TUint64[KBTICCapaCount];
   842     CleanupArrayDeletePushL( capaVector );
   882     CleanupArrayDeletePushL( capaVector );
   843     capaVector[0] = KBTIC_TCB;
   883     capaVector[0] = KBTIC_TCB;
   844                                                                                     
   884                                                                                     
  1048                          
  1088                          
  1049             }                                                                       
  1089             }                                                                       
  1050         }
  1090         }
  1051     
  1091     
  1052     CleanupStack::PopAndDestroy(); //capaVector   
  1092     CleanupStack::PopAndDestroy(); //capaVector   
  1053                                               
  1093 
       
  1094     CleanupStack::Pop( &stringArray );
       
  1095     CleanupStack::Pop( &hashArray );
       
  1096     CleanupStack::Pop( &fileArray );
       
  1097     CleanupStack::Pop( &foundFilesArray );
       
  1098     CleanupStack::Pop( &installedFilesArray );
       
  1099                  
  1054     // Reset only. InstalledFiles array owns buffers.
  1100     // Reset only. InstalledFiles array owns buffers.
  1055     foundFilesArray.Reset();
  1101     foundFilesArray.Reset();
  1056     fileArray.Reset(); 
  1102     fileArray.Reset(); 
  1057     hashArray.ResetAndDestroy();                                    
  1103     hashArray.ResetAndDestroy();                                    
  1058     installedFilesArray.ResetAndDestroy();         
  1104     installedFilesArray.ResetAndDestroy();         
  1094     RPointerArray<HBufC> fileArray;  // Files that need to be cheked.
  1140     RPointerArray<HBufC> fileArray;  // Files that need to be cheked.
  1095     RPointerArray<HBufC8> hashArray; // Hash for each fileArray's file.
  1141     RPointerArray<HBufC8> hashArray; // Hash for each fileArray's file.
  1096     RArray<TUint64> capaArray;              
  1142     RArray<TUint64> capaArray;              
  1097     RPointerArray<HBufC> stringArray;
  1143     RPointerArray<HBufC> stringArray;
  1098     HBufC* string = NULL;
  1144     HBufC* string = NULL;
       
  1145    
       
  1146     CleanupResetAndDestroyPushL( installedFilesArray );
       
  1147     CleanupResetAndDestroyPushL( foundFilesArray );
       
  1148     CleanupResetAndDestroyPushL( fileArray );
       
  1149     CleanupResetAndDestroyPushL( hashArray );
       
  1150     CleanupResetAndDestroyPushL( stringArray );
  1099     
  1151     
  1100     TUint64* capaVector = new( ELeave ) TUint64[KBTICCapaCount];
  1152     TUint64* capaVector = new( ELeave ) TUint64[KBTICCapaCount];
  1101     CleanupArrayDeletePushL( capaVector );
  1153     CleanupArrayDeletePushL( capaVector );
  1102     capaVector[0] = KBTIC_TCB;
  1154     capaVector[0] = KBTIC_TCB;
  1103                                                                                    
  1155                                                                                    
  1159         User::LeaveIfError( aUpdaterExePaths.Append( path ) );         
  1211         User::LeaveIfError( aUpdaterExePaths.Append( path ) );         
  1160         CleanupStack::Pop(); //path          
  1212         CleanupStack::Pop(); //path          
  1161         }
  1213         }
  1162    
  1214    
  1163     CleanupStack::PopAndDestroy(); //capaVector   
  1215     CleanupStack::PopAndDestroy(); //capaVector   
  1164                                               
  1216     
       
  1217     CleanupStack::Pop( &stringArray ); 
       
  1218     CleanupStack::Pop( &hashArray );
       
  1219     CleanupStack::Pop( &fileArray );
       
  1220     CleanupStack::Pop( &foundFilesArray );
       
  1221     CleanupStack::Pop( &installedFilesArray );
       
  1222                                                                
  1165     // Reset only. InstalledFiles array owns buffers.
  1223     // Reset only. InstalledFiles array owns buffers.
  1166     foundFilesArray.Reset();
  1224     foundFilesArray.Reset();
  1167     fileArray.Reset(); 
  1225     fileArray.Reset(); 
  1168     hashArray.ResetAndDestroy();                                    
  1226     hashArray.ResetAndDestroy();                                    
  1169     installedFilesArray.ResetAndDestroy();         
  1227     installedFilesArray.ResetAndDestroy();         
  1390     TUint64 aCapability )                           // Needed capabiltiy
  1448     TUint64 aCapability )                           // Needed capabiltiy
  1391     {
  1449     {
  1392     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] SearchValidCertificate v2.1 --->");      
  1450     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] SearchValidCertificate v2.1 --->");      
  1393 
  1451 
  1394     RPointerArray<HBufC8> binaryCertChainArray;
  1452     RPointerArray<HBufC8> binaryCertChainArray;
       
  1453     CleanupResetAndDestroyPushL( binaryCertChainArray );
       
  1454     
  1395     // Get certificates in binary format.
  1455     // Get certificates in binary format.
  1396     aEntry.CertificateChainsL( binaryCertChainArray );
  1456     aEntry.CertificateChainsL( binaryCertChainArray );
  1397                             
  1457                             
  1398     TInt chainCount = binaryCertChainArray.Count();
  1458     TInt chainCount = binaryCertChainArray.Count();
  1399     BTIC_TRACE_PRINT_NUM("Entry chain count (1) = %d", chainCount );
  1459     BTIC_TRACE_PRINT_NUM("Entry chain count (1) = %d", chainCount );
  1400         
  1460         
  1401     TBool retVal = EFalse;                                                                  
  1461     TBool retVal = EFalse;                                                                  
  1402 
  1462 
  1403     RPointerArray<CX509Certificate> chainCertArray;
  1463     RPointerArray<CX509Certificate> chainCertArray;
  1404 
  1464     CleanupResetAndDestroyPushL( chainCertArray );
       
  1465     
  1405     // Check all entry's chains    
  1466     // Check all entry's chains    
  1406     for ( TInt chain = 0; chain < chainCount; chain++ )
  1467     for ( TInt chain = 0; chain < chainCount; chain++ )
  1407         {  
  1468         {  
  1408         BTIC_TRACE_PRINT_NUM("Check chain nr = %d", chain);
  1469         BTIC_TRACE_PRINT_NUM("Check chain nr = %d", chain);
  1409                                    
  1470                                    
  1544         
  1605         
  1545         // If this is a valid certificate chain, break to loop.   
  1606         // If this is a valid certificate chain, break to loop.   
  1546         if ( retVal )
  1607         if ( retVal )
  1547             {                
  1608             {                
  1548             break;
  1609             break;
  1549             }
  1610             }           
  1550             
       
  1551         } // FOR LOOP
  1611         } // FOR LOOP
  1552                     
  1612 
  1553     binaryCertChainArray.ResetAndDestroy();
  1613     CleanupStack::PopAndDestroy( &chainCertArray );
  1554     
  1614     CleanupStack::PopAndDestroy( &binaryCertChainArray );
  1555                                         
  1615                                             
  1556     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] SearchValidCertificate v2.1 <---");         
  1616     BTIC_TRACE_PRINT("[BOOT INTECRITY CHECK] SearchValidCertificate v2.1 <---");         
  1557     
  1617     
  1558     return retVal;
  1618     return retVal;
  1559     }
  1619     }
  1560 
  1620