omadrm/drmengine/server/src/drmrightsdb.cpp
branchRCL_3
changeset 26 1221b68b8a5f
parent 22 ad2863178d17
child 27 1481bf457703
equal deleted inserted replaced
25:50c53e893c3f 26:1221b68b8a5f
    20 
    20 
    21 #include <e32std.h>     // RPointerArray
    21 #include <e32std.h>     // RPointerArray
    22 #include <e32def.h>     // Type definitions
    22 #include <e32def.h>     // Type definitions
    23 #include <hash.h>       // MD5 Algorithm
    23 #include <hash.h>       // MD5 Algorithm
    24 // #include <SysUtil.h>    // Disk space checking
    24 // #include <SysUtil.h>    // Disk space checking
    25 #include <featmgr.h>    // Feature Manager 
       
    26 #include <f32file.h>
    25 #include <f32file.h>
    27 #include <s32strm.h>
    26 #include <s32strm.h>
    28 #include <s32file.h>
    27 #include <s32file.h>
    29 #include <caf/caf.h>
    28 #include <caf/caf.h>
    30 #include <caf/cafplatform.h>
    29 #include <caf/cafplatform.h>
    69 // MACROS
    68 // MACROS
    70 
    69 
    71 // LOCAL CONSTANTS AND MACROS
    70 // LOCAL CONSTANTS AND MACROS
    72 #ifdef RD_MULTIPLE_DRIVE
    71 #ifdef RD_MULTIPLE_DRIVE
    73 // Backup Directory
    72 // Backup Directory
    74 #ifdef __DRM_OMA2
       
    75 _LIT( KBackupDir, "%c:\\private\\101F51F2\\backup\\" );
    73 _LIT( KBackupDir, "%c:\\private\\101F51F2\\backup\\" );
    76 #endif
       
    77 #endif
    74 #endif
    78 
    75 
    79 
    76 
    80 _LIT(KJavaExtension, ".jar");
    77 _LIT(KJavaExtension, ".jar");
    81 _LIT(KSISExtension, ".sis");
    78 _LIT(KSISExtension, ".sis");
    91 
    88 
    92 _LIT(KRODirName, "DomainROs\\");
    89 _LIT(KRODirName, "DomainROs\\");
    93 _LIT(KCorruptionFlagFile, "invalid");
    90 _LIT(KCorruptionFlagFile, "invalid");
    94 
    91 
    95 #ifdef __DRM_OMA2
    92 #ifdef __DRM_OMA2
       
    93 const TInt KMaxUDTDataSize = 256;
       
    94 const TInt KDeviceDataSize = 256;
    96 const TInt KDeviceDataBlock = 128;
    95 const TInt KDeviceDataBlock = 128;
       
    96 #else
       
    97 const TInt KMaxUDTDataSize = 0;
       
    98 const TInt KDeviceDataSize = 0;
       
    99 const TInt KDeviceDataBlock = 0;
    97 #endif
   100 #endif
       
   101 const TInt KUdtDataPos = 4 + KDeviceDataSize;
       
   102 
    98 
   103 
    99 const TInt KEncryptionKeySize = 16;
   104 const TInt KEncryptionKeySize = 16;
   100 const TInt KMaxEncryptionSize = 8192;
   105 const TInt KMaxEncryptionSize = 8192;
   101 
   106 
   102 const TInt KMaxUpdateTime = 3000000; // consider updates to be finished within
   107 const TInt KMaxUpdateTime = 3000000; // consider updates to be finished within
   105 const TUint32 KMaxTIntVal = 2147483647; // 2^31-1
   110 const TUint32 KMaxTIntVal = 2147483647; // 2^31-1
   106 
   111 
   107 // MODULE DATA STRUCTURES
   112 // MODULE DATA STRUCTURES
   108 
   113 
   109 // LOCAL FUNCTION PROTOTYPES
   114 // LOCAL FUNCTION PROTOTYPES
   110 #ifdef __DRM_OMA2
       
   111 LOCAL_C void CleanupData( TAny* aPtr );
   115 LOCAL_C void CleanupData( TAny* aPtr );
   112 LOCAL_C void WriteIntToBlock( TInt aValue, TDes8& aBlock, TInt aOffset );
   116 LOCAL_C void WriteIntToBlock( TInt aValue, TDes8& aBlock, TInt aOffset );
   113 #endif
       
   114 LOCAL_C void DeleteObject( TAny* aObject );
   117 LOCAL_C void DeleteObject( TAny* aObject );
   115 
   118 
   116 // FORWARD DECLARATIONS
   119 // FORWARD DECLARATIONS
   117 
   120 
   118 // ============================= LOCAL FUNCTIONS ===============================
   121 // ============================= LOCAL FUNCTIONS ===============================
   120 // -----------------------------------------------------------------------------
   123 // -----------------------------------------------------------------------------
   121 // CleanupData
   124 // CleanupData
   122 // Used to catch errors and delete the file if it's needed
   125 // Used to catch errors and delete the file if it's needed
   123 // -----------------------------------------------------------------------------
   126 // -----------------------------------------------------------------------------
   124 //
   127 //
   125 #ifdef __DRM_OMA2
       
   126 LOCAL_C void CleanupData( TAny* aPtr )
   128 LOCAL_C void CleanupData( TAny* aPtr )
   127     {
   129     {
   128     CDRMRightsDB* rdb = reinterpret_cast<CDRMRightsDB*>( aPtr );
   130     CDRMRightsDB* rdb = reinterpret_cast<CDRMRightsDB*>( aPtr );
   129 
   131 
   130     rdb->CleanUdtData();
   132     rdb->CleanUdtData();
   136     aBlock[aOffset] =     (aValue & 0xff000000) >> 24;
   138     aBlock[aOffset] =     (aValue & 0xff000000) >> 24;
   137     aBlock[aOffset + 1] = (aValue & 0x00ff0000) >> 16;
   139     aBlock[aOffset + 1] = (aValue & 0x00ff0000) >> 16;
   138     aBlock[aOffset + 2] = (aValue & 0x0000ff00) >> 8;
   140     aBlock[aOffset + 2] = (aValue & 0x0000ff00) >> 8;
   139     aBlock[aOffset + 3] = (aValue & 0x000000ff);
   141     aBlock[aOffset + 3] = (aValue & 0x000000ff);
   140     }
   142     }
   141 #endif
   143 
   142 // ----------------------------------------------------------------------------
   144 // ----------------------------------------------------------------------------
   143 // DeleteObject
   145 // DeleteObject
   144 // Deletes the file by TFileName presented by aHandle
   146 // Deletes the file by TFileName presented by aHandle
   145 // ----------------------------------------------------------------------------
   147 // ----------------------------------------------------------------------------
   146 //
   148 //
   203         iFileServer.Delete( name );
   205         iFileServer.Delete( name );
   204         DeleteDBL();
   206         DeleteDBL();
   205         }
   207         }
   206 
   208 
   207     InitializeDatabaseL();
   209     InitializeDatabaseL();
   208     
       
   209     FeatureManager::InitializeLibL();
       
   210     
       
   211     };
   210     };
   212 
   211 
   213 // -----------------------------------------------------------------------------
   212 // -----------------------------------------------------------------------------
   214 // CDRMRightsDB::NewLC
   213 // CDRMRightsDB::NewLC
   215 // Two-phased constructor.
   214 // Two-phased constructor.
   271         delete iImei;
   270         delete iImei;
   272         iImei = NULL;
   271         iImei = NULL;
   273         }
   272         }
   274 
   273 
   275     iMemStream.Close();
   274     iMemStream.Close();
   276     
       
   277     FeatureManager::UnInitializeLib();
       
   278 
   275 
   279     DRMLOG( _L( "RDB Closing." ) );
   276     DRMLOG( _L( "RDB Closing." ) );
   280     };
   277     };
   281 
   278 
   282 
   279 
  1156 // -----------------------------------------------------------------------------
  1153 // -----------------------------------------------------------------------------
  1157 //
  1154 //
  1158 HBufC8* CDRMRightsDB::GetUdtDataLC()
  1155 HBufC8* CDRMRightsDB::GetUdtDataLC()
  1159     {
  1156     {
  1160 #ifdef __DRM_OMA2
  1157 #ifdef __DRM_OMA2
  1161     
       
  1162     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  1163         {
       
  1164         User::Leave( KErrNotSupported );
       
  1165         }
       
  1166     
       
  1167     const TInt KMaxUDTDataSize( 256 );
       
  1168     const TInt KDeviceDataSize( 256 ); 
       
  1169     const TInt KUdtDataPos = 4 + KDeviceDataSize;
       
  1170     
       
  1171     HBufC8* udtData = HBufC8::NewMaxLC( KMaxUDTDataSize );
  1158     HBufC8* udtData = HBufC8::NewMaxLC( KMaxUDTDataSize );
  1172     TFileName backupFile;
  1159     TFileName backupFile;
  1173     RFile input;
  1160     RFile input;
  1174     TInt pos = KUdtDataPos;
  1161     TInt pos = KUdtDataPos;
  1175     TPtr8 inRead( udtData->Des() );
  1162     TPtr8 inRead( udtData->Des() );
  1221 #else
  1208 #else
  1222 void CDRMRightsDB::InitiateUdtL( const TDesC8& )
  1209 void CDRMRightsDB::InitiateUdtL( const TDesC8& )
  1223 #endif // __DRM_OMA2
  1210 #endif // __DRM_OMA2
  1224     {
  1211     {
  1225 #ifdef __DRM_OMA2
  1212 #ifdef __DRM_OMA2
  1226     
       
  1227     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  1228         {
       
  1229         User::Leave( KErrNotSupported );
       
  1230         }
       
  1231     
       
  1232     TFileName backupFile;
  1213     TFileName backupFile;
  1233     RFile input;
  1214     RFile input;
  1234     HBufC8* keyData = NULL;
  1215     HBufC8* keyData = NULL;
  1235 
  1216 
  1236     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1217     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1462 
  1443 
  1463     // first write the first 4 bytes empty, in the end the size is written there
  1444     // first write the first 4 bytes empty, in the end the size is written there
  1464     stream.WriteInt32L( fileSize );
  1445     stream.WriteInt32L( fileSize );
  1465 
  1446 
  1466 #ifdef __DRM_OMA2
  1447 #ifdef __DRM_OMA2
  1467     if ( FeatureManager::FeatureSupported ( KFeatureIdFfOmadrm2Support ) )
  1448     AddUDTDataL( stream );
  1468         {
       
  1469         AddUDTDataL( stream );
       
  1470         }
       
  1471 #endif
  1449 #endif
  1472 
  1450 
  1473     encIV.SetLength(KEncryptionKeySize);
  1451     encIV.SetLength(KEncryptionKeySize);
  1474     // fill the iv with rnd data and write it to stream:
  1452     // fill the iv with rnd data and write it to stream:
  1475     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1453     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1712 // The file will be opened and closed in CDRMBackup
  1690 // The file will be opened and closed in CDRMBackup
  1713 // -----------------------------------------------------------------------------
  1691 // -----------------------------------------------------------------------------
  1714 //
  1692 //
  1715 void CDRMRightsDB::RestoreContentFromFileL( RFile& aBackupFile,
  1693 void CDRMRightsDB::RestoreContentFromFileL( RFile& aBackupFile,
  1716                                             const TDesC8& aEncryptionKey,
  1694                                             const TDesC8& aEncryptionKey,
  1717                                             const TInt aMode )																																									
  1695                                             const TInt aMode )
  1718     {
  1696     {
  1719 	
       
  1720     // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL\n\r"));
  1697     // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL\n\r"));
  1721     TInt8 continueMarker = 1;
  1698     TInt8 continueMarker = 1;
  1722     TBuf8<16> key;
  1699     TBuf8<16> key;
  1723     TBuf8<16> encryptionKey;
  1700     TBuf8<16> encryptionKey;
  1724     TInt permissions = 0;
  1701     TInt permissions = 0;
  1725     
       
  1726     // Default values when OMA2 DRM is not supported
       
  1727     TInt maxUDTDataSize( 0 );
       
  1728     TInt deviceDataSize( 0 );
       
  1729     TInt udtDataPos( 4 + deviceDataSize );
       
  1730     
       
  1731     CDRMPermission* permission = CDRMPermission::NewLC();
  1702     CDRMPermission* permission = CDRMPermission::NewLC();
  1732     CDRMCommonData *commonData = NULL;
  1703     CDRMCommonData *commonData = NULL;
  1733     CDRMPointerArray<CDRMPermission> *permissionArray = CDRMPointerArray<CDRMPermission>::NewLC();
  1704     CDRMPointerArray<CDRMPermission> *permissionArray = CDRMPointerArray<CDRMPermission>::NewLC();
  1734     permissionArray->SetAutoCleanup( ETrue );
  1705     permissionArray->SetAutoCleanup( ETrue );
  1735     TDRMUniqueID uniqueID = 0;
  1706     TDRMUniqueID uniqueID = 0;
  1736     RFile fileHandle;
  1707     RFile fileHandle;
  1737     TInt readPos = 0;
  1708     TInt readPos = 0;
  1738     TInt size = 0;
  1709     TInt size = 0;
  1739     TInt dataLeft = 0;
  1710     TInt dataLeft = 0;
  1740     TPtr8 keyData(NULL,0,0);
  1711     TPtr8 keyData(NULL,0,0);
  1741       
  1712 
  1742 #ifdef __DRM_OMA2    
       
  1743     if ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) )
       
  1744         {
       
  1745     		maxUDTDataSize = 256;
       
  1746     		deviceDataSize = 256; 
       
  1747     		udtDataPos = 4 + deviceDataSize;	
       
  1748     		}    
       
  1749 #endif 
       
  1750  
       
  1751     // maintain knowledge about stateful rights not being restored
  1713     // maintain knowledge about stateful rights not being restored
  1752     TBool stateful = EFalse;
  1714     TBool stateful = EFalse;
       
  1715 
  1753 
  1716 
  1754     DRMLOG( _L( "CDRMRightsDB::RestoreContentFromFileL ->" ) );
  1717     DRMLOG( _L( "CDRMRightsDB::RestoreContentFromFileL ->" ) );
  1755 
  1718 
  1756     // Indicate that the DB is updated
  1719     // Indicate that the DB is updated
  1757     iLastUpdate.HomeTime();
  1720     iLastUpdate.HomeTime();
  1796     encIV.SetLength( KEncryptionKeySize );
  1759     encIV.SetLength( KEncryptionKeySize );
  1797 
  1760 
  1798     // Check that the decryption works, if it doesn't then the
  1761     // Check that the decryption works, if it doesn't then the
  1799     // key is faulty
  1762     // key is faulty
  1800     User::LeaveIfError( aBackupFile.Size( readPos ) );
  1763     User::LeaveIfError( aBackupFile.Size( readPos ) );
  1801     if( readPos < udtDataPos+maxUDTDataSize+(KEncryptionKeySize*2) )
  1764     if( readPos < KUdtDataPos+KMaxUDTDataSize+(KEncryptionKeySize*2) )
  1802         {
  1765         {
  1803           // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL : corrupt\n\r"));
  1766           // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL : corrupt\n\r"));
  1804         User::Leave(KErrCorrupt);
  1767         User::Leave(KErrCorrupt);
  1805         }
  1768         }
  1806     readPos -= KEncryptionKeySize*2;
  1769     readPos -= KEncryptionKeySize*2;
  1825 
  1788 
  1826     // Now we are ready to go through the file
  1789     // Now we are ready to go through the file
  1827     //-----------------------------------------------------------------------
  1790     //-----------------------------------------------------------------------
  1828 
  1791 
  1829     // Duplicate file handle
  1792     // Duplicate file handle
  1830     readPos = udtDataPos+maxUDTDataSize;
  1793     readPos = KUdtDataPos+KMaxUDTDataSize;
  1831     User::LeaveIfError( fileHandle.Seek( ESeekStart, readPos ) );
  1794     User::LeaveIfError( fileHandle.Seek( ESeekStart, readPos ) );
  1832     iMemStream.Close();
  1795     iMemStream.Close();
  1833 
  1796 
  1834     ReadDataL( fileHandle, encIV, readData, dataBuffer,
  1797     ReadDataL( fileHandle, encIV, readData, dataBuffer,
  1835                dataLeft, size, ETrue, encryptionKey );
  1798                dataLeft, size, ETrue, encryptionKey );
  1921                 {
  1884                 {
  1922                 keyExists = ETrue;
  1885                 keyExists = ETrue;
  1923                 counter = KEncryptionKeySize;
  1886                 counter = KEncryptionKeySize;
  1924                 }
  1887                 }
  1925             }
  1888             }
       
  1889 
  1926 
  1890 
  1927         // Encrypt the key
  1891         // Encrypt the key
  1928         if( keyExists )
  1892         if( keyExists )
  1929             {
  1893             {
  1930             ModifyKey( key );
  1894             ModifyKey( key );
  1987         if( !rights )
  1951         if( !rights )
  1988             {
  1952             {
  1989             User::Leave( KErrGeneral );
  1953             User::Leave( KErrGeneral );
  1990             }
  1954             }
  1991 
  1955 
       
  1956 
       
  1957 
       
  1958 
  1992         if( insertPerm == -1 ) // Add everything no checks needed
  1959         if( insertPerm == -1 ) // Add everything no checks needed
  1993             {
  1960             {
  1994             for( TInt count = 0; count < permissions; count++ )
  1961             for( TInt count = 0; count < permissions; count++ )
  1995                 {
  1962                 {
  1996                 if( dataLeft < 4 )
  1963                 if( dataLeft < 4 )
  2043                     User::LeaveIfError(error);
  2010                     User::LeaveIfError(error);
  2044                     }
  2011                     }
  2045 
  2012 
  2046                 }
  2013                 }
  2047 
  2014 
       
  2015 
  2048             for( TInt count = 0; count < permissions; count++ )
  2016             for( TInt count = 0; count < permissions; count++ )
  2049                 {
  2017                 {
  2050                 if( dataLeft < 4 )
  2018                 if( dataLeft < 4 )
  2051                     {
  2019                     {
  2052                     size = 4;
  2020                     size = 4;
  2139     // to enable UDT
  2107     // to enable UDT
  2140     if( stateful )
  2108     if( stateful )
  2141         {
  2109         {
  2142         User::Leave( KErrPermissionDenied );
  2110         User::Leave( KErrPermissionDenied );
  2143         }
  2111         }
  2144 		
       
  2145     };
  2112     };
  2146 
  2113 
  2147 
  2114 
  2148 
  2115 
  2149 // -----------------------------------------------------------------------------
  2116 // -----------------------------------------------------------------------------
  2293 #else
  2260 #else
  2294 void CDRMRightsDB::AddUDTDataL( RWriteStream& )
  2261 void CDRMRightsDB::AddUDTDataL( RWriteStream& )
  2295 #endif // __DRM_OMA2
  2262 #endif // __DRM_OMA2
  2296     {
  2263     {
  2297 #ifdef __DRM_OMA2
  2264 #ifdef __DRM_OMA2
  2298     
       
  2299     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2300         {
       
  2301         User::Leave( KErrNotSupported );
       
  2302         }
       
  2303     
       
  2304     const TInt KMaxUDTDataSize( 256 );
       
  2305     TBuf8<MDrmKeyStorage::KRdbSerialNumberLength> serialNumber;
  2265     TBuf8<MDrmKeyStorage::KRdbSerialNumberLength> serialNumber;
  2306     TBuf8<KMaxUDTDataSize> buffer;
  2266     TBuf8<KMaxUDTDataSize> buffer;
  2307     TUint8* ptr = const_cast<TUint8*>(buffer.Ptr());
  2267     TUint8* ptr = const_cast<TUint8*>(buffer.Ptr());
  2308     HBufC8* result = NULL;
  2268     HBufC8* result = NULL;
  2309     TPtr8 udtData( ptr+KDeviceDataBlock, KDeviceDataBlock, KDeviceDataBlock );
  2269     TPtr8 udtData( ptr+KDeviceDataBlock, KDeviceDataBlock, KDeviceDataBlock );
  2470 
  2430 
  2471 // test function
  2431 // test function
  2472 void CDRMRightsDB::CreateDummyUDTFileL()
  2432 void CDRMRightsDB::CreateDummyUDTFileL()
  2473     {
  2433     {
  2474 #ifdef __DRM_OMA2
  2434 #ifdef __DRM_OMA2
  2475     
       
  2476     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2477         {
       
  2478         User::Leave( KErrNotSupported );
       
  2479         }
       
  2480     
       
  2481     TFileName backupFile;
  2435     TFileName backupFile;
  2482     RFile input;
  2436     RFile input;
  2483     TInt fileSize = 4 + 256 + 256;
  2437     TInt fileSize = 4 + 256 + 256;
  2484 
  2438 
  2485     backupFile.Copy( _L("c:\\") );
  2439     backupFile.Copy( _L("c:\\") );
  2505 // -----------------------------------------------------------------------------
  2459 // -----------------------------------------------------------------------------
  2506 //
  2460 //
  2507 void CDRMRightsDB::CleanUdtData()
  2461 void CDRMRightsDB::CleanUdtData()
  2508     {
  2462     {
  2509 #ifdef __DRM_OMA2
  2463 #ifdef __DRM_OMA2
  2510     
       
  2511     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2512         {
       
  2513         return;
       
  2514         }
       
  2515     
       
  2516     TFileName backupFile;
  2464     TFileName backupFile;
  2517 
  2465 
  2518 #ifndef RD_MULTIPLE_DRIVE
  2466 #ifndef RD_MULTIPLE_DRIVE
  2519 
  2467 
  2520     backupFile.Copy( KBackupDirectory );
  2468     backupFile.Copy( KBackupDirectory );