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