omadrm/drmengine/server/src/drmrightsdb.cpp
branchRCL_3
changeset 32 457cd4423b8c
parent 18 8a03a285ab14
child 55 ad2863178d17
equal deleted inserted replaced
25:04da681812a9 32:457cd4423b8c
    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>
    67 // MACROS
    68 // MACROS
    68 
    69 
    69 // LOCAL CONSTANTS AND MACROS
    70 // LOCAL CONSTANTS AND MACROS
    70 #ifdef RD_MULTIPLE_DRIVE
    71 #ifdef RD_MULTIPLE_DRIVE
    71 // Backup Directory
    72 // Backup Directory
       
    73 #ifdef __DRM_OMA2
    72 _LIT( KBackupDir, "%c:\\private\\101F51F2\\backup\\" );
    74 _LIT( KBackupDir, "%c:\\private\\101F51F2\\backup\\" );
       
    75 #endif
    73 #endif
    76 #endif
    74 
    77 
    75 
    78 
    76 _LIT(KJavaExtension, ".jar");
    79 _LIT(KJavaExtension, ".jar");
    77 _LIT(KSISExtension, ".sis");
    80 _LIT(KSISExtension, ".sis");
    87 
    90 
    88 _LIT(KRODirName, "DomainROs\\");
    91 _LIT(KRODirName, "DomainROs\\");
    89 _LIT(KCorruptionFlagFile, "invalid");
    92 _LIT(KCorruptionFlagFile, "invalid");
    90 
    93 
    91 #ifdef __DRM_OMA2
    94 #ifdef __DRM_OMA2
    92 const TInt KMaxUDTDataSize = 256;
       
    93 const TInt KDeviceDataSize = 256;
       
    94 const TInt KDeviceDataBlock = 128;
    95 const TInt KDeviceDataBlock = 128;
    95 #else
       
    96 const TInt KMaxUDTDataSize = 0;
       
    97 const TInt KDeviceDataSize = 0;
       
    98 const TInt KDeviceDataBlock = 0;
       
    99 #endif
    96 #endif
   100 const TInt KUdtDataPos = 4 + KDeviceDataSize;
       
   101 
       
   102 
    97 
   103 const TInt KEncryptionKeySize = 16;
    98 const TInt KEncryptionKeySize = 16;
   104 const TInt KMaxEncryptionSize = 8192;
    99 const TInt KMaxEncryptionSize = 8192;
   105 
   100 
   106 const TInt KMaxUpdateTime = 3000000; // consider updates to be finished within
   101 const TInt KMaxUpdateTime = 3000000; // consider updates to be finished within
   109 const TUint32 KMaxTIntVal = 2147483647; // 2^31-1
   104 const TUint32 KMaxTIntVal = 2147483647; // 2^31-1
   110 
   105 
   111 // MODULE DATA STRUCTURES
   106 // MODULE DATA STRUCTURES
   112 
   107 
   113 // LOCAL FUNCTION PROTOTYPES
   108 // LOCAL FUNCTION PROTOTYPES
       
   109 #ifdef __DRM_OMA2
   114 LOCAL_C void CleanupData( TAny* aPtr );
   110 LOCAL_C void CleanupData( TAny* aPtr );
   115 LOCAL_C void WriteIntToBlock( TInt aValue, TDes8& aBlock, TInt aOffset );
   111 LOCAL_C void WriteIntToBlock( TInt aValue, TDes8& aBlock, TInt aOffset );
       
   112 #endif
   116 LOCAL_C void DeleteObject( TAny* aObject );
   113 LOCAL_C void DeleteObject( TAny* aObject );
   117 
   114 
   118 // FORWARD DECLARATIONS
   115 // FORWARD DECLARATIONS
   119 
   116 
   120 // ============================= LOCAL FUNCTIONS ===============================
   117 // ============================= LOCAL FUNCTIONS ===============================
   122 // -----------------------------------------------------------------------------
   119 // -----------------------------------------------------------------------------
   123 // CleanupData
   120 // CleanupData
   124 // Used to catch errors and delete the file if it's needed
   121 // Used to catch errors and delete the file if it's needed
   125 // -----------------------------------------------------------------------------
   122 // -----------------------------------------------------------------------------
   126 //
   123 //
       
   124 #ifdef __DRM_OMA2
   127 LOCAL_C void CleanupData( TAny* aPtr )
   125 LOCAL_C void CleanupData( TAny* aPtr )
   128     {
   126     {
   129     CDRMRightsDB* rdb = reinterpret_cast<CDRMRightsDB*>( aPtr );
   127     CDRMRightsDB* rdb = reinterpret_cast<CDRMRightsDB*>( aPtr );
   130 
   128 
   131     rdb->CleanUdtData();
   129     rdb->CleanUdtData();
   137     aBlock[aOffset] =     (aValue & 0xff000000) >> 24;
   135     aBlock[aOffset] =     (aValue & 0xff000000) >> 24;
   138     aBlock[aOffset + 1] = (aValue & 0x00ff0000) >> 16;
   136     aBlock[aOffset + 1] = (aValue & 0x00ff0000) >> 16;
   139     aBlock[aOffset + 2] = (aValue & 0x0000ff00) >> 8;
   137     aBlock[aOffset + 2] = (aValue & 0x0000ff00) >> 8;
   140     aBlock[aOffset + 3] = (aValue & 0x000000ff);
   138     aBlock[aOffset + 3] = (aValue & 0x000000ff);
   141     }
   139     }
   142 
   140 #endif
   143 // ----------------------------------------------------------------------------
   141 // ----------------------------------------------------------------------------
   144 // DeleteObject
   142 // DeleteObject
   145 // Deletes the file by TFileName presented by aHandle
   143 // Deletes the file by TFileName presented by aHandle
   146 // ----------------------------------------------------------------------------
   144 // ----------------------------------------------------------------------------
   147 //
   145 //
   203         iFileServer.Delete( name );
   201         iFileServer.Delete( name );
   204         DeleteDBL();
   202         DeleteDBL();
   205         }
   203         }
   206 
   204 
   207     InitializeDatabaseL();
   205     InitializeDatabaseL();
       
   206     
       
   207     FeatureManager::InitializeLibL();
       
   208     
   208     };
   209     };
   209 
   210 
   210 // -----------------------------------------------------------------------------
   211 // -----------------------------------------------------------------------------
   211 // CDRMRightsDB::NewLC
   212 // CDRMRightsDB::NewLC
   212 // Two-phased constructor.
   213 // Two-phased constructor.
   266         delete iImei;
   267         delete iImei;
   267         iImei = NULL;
   268         iImei = NULL;
   268         }
   269         }
   269 
   270 
   270     iMemStream.Close();
   271     iMemStream.Close();
       
   272     
       
   273     FeatureManager::UnInitializeLib();
   271 
   274 
   272     DRMLOG( _L( "RDB Closing." ) );
   275     DRMLOG( _L( "RDB Closing." ) );
   273     };
   276     };
   274 
   277 
   275 
   278 
  1078 // -----------------------------------------------------------------------------
  1081 // -----------------------------------------------------------------------------
  1079 //
  1082 //
  1080 HBufC8* CDRMRightsDB::GetUdtDataLC()
  1083 HBufC8* CDRMRightsDB::GetUdtDataLC()
  1081     {
  1084     {
  1082 #ifdef __DRM_OMA2
  1085 #ifdef __DRM_OMA2
       
  1086     
       
  1087     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  1088         {
       
  1089         User::Leave( KErrNotSupported );
       
  1090         }
       
  1091     
       
  1092     const TInt KMaxUDTDataSize( 256 );
       
  1093     const TInt KDeviceDataSize( 256 ); 
       
  1094     const TInt KUdtDataPos = 4 + KDeviceDataSize;
       
  1095     
  1083     HBufC8* udtData = HBufC8::NewMaxLC( KMaxUDTDataSize );
  1096     HBufC8* udtData = HBufC8::NewMaxLC( KMaxUDTDataSize );
  1084     TFileName backupFile;
  1097     TFileName backupFile;
  1085     RFile input;
  1098     RFile input;
  1086     TInt pos = KUdtDataPos;
  1099     TInt pos = KUdtDataPos;
  1087     TPtr8 inRead( udtData->Des() );
  1100     TPtr8 inRead( udtData->Des() );
  1133 #else
  1146 #else
  1134 void CDRMRightsDB::InitiateUdtL( const TDesC8& )
  1147 void CDRMRightsDB::InitiateUdtL( const TDesC8& )
  1135 #endif // __DRM_OMA2
  1148 #endif // __DRM_OMA2
  1136     {
  1149     {
  1137 #ifdef __DRM_OMA2
  1150 #ifdef __DRM_OMA2
       
  1151     
       
  1152     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  1153         {
       
  1154         User::Leave( KErrNotSupported );
       
  1155         }
       
  1156     
  1138     TFileName backupFile;
  1157     TFileName backupFile;
  1139     RFile input;
  1158     RFile input;
  1140     HBufC8* keyData = NULL;
  1159     HBufC8* keyData = NULL;
  1141 
  1160 
  1142     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1161     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1368 
  1387 
  1369     // first write the first 4 bytes empty, in the end the size is written there
  1388     // first write the first 4 bytes empty, in the end the size is written there
  1370     stream.WriteInt32L( fileSize );
  1389     stream.WriteInt32L( fileSize );
  1371 
  1390 
  1372 #ifdef __DRM_OMA2
  1391 #ifdef __DRM_OMA2
  1373     AddUDTDataL( stream );
  1392     if ( FeatureManager::FeatureSupported ( KFeatureIdFfOmadrm2Support ) )
       
  1393         {
       
  1394         AddUDTDataL( stream );
       
  1395         }
  1374 #endif
  1396 #endif
  1375 
  1397 
  1376     encIV.SetLength(KEncryptionKeySize);
  1398     encIV.SetLength(KEncryptionKeySize);
  1377     // fill the iv with rnd data and write it to stream:
  1399     // fill the iv with rnd data and write it to stream:
  1378     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1400     MDrmKeyStorage* storage = DrmKeyStorageNewL();
  1615 // The file will be opened and closed in CDRMBackup
  1637 // The file will be opened and closed in CDRMBackup
  1616 // -----------------------------------------------------------------------------
  1638 // -----------------------------------------------------------------------------
  1617 //
  1639 //
  1618 void CDRMRightsDB::RestoreContentFromFileL( RFile& aBackupFile,
  1640 void CDRMRightsDB::RestoreContentFromFileL( RFile& aBackupFile,
  1619                                             const TDesC8& aEncryptionKey,
  1641                                             const TDesC8& aEncryptionKey,
  1620                                             const TInt aMode )
  1642                                             const TInt aMode )																																									
  1621     {
  1643     {
       
  1644 	
  1622     // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL\n\r"));
  1645     // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL\n\r"));
  1623     TInt8 continueMarker = 1;
  1646     TInt8 continueMarker = 1;
  1624     TBuf8<16> key;
  1647     TBuf8<16> key;
  1625     TBuf8<16> encryptionKey;
  1648     TBuf8<16> encryptionKey;
  1626     TInt permissions = 0;
  1649     TInt permissions = 0;
       
  1650     
       
  1651     // Default values when OMA2 DRM is not supported
       
  1652     TInt maxUDTDataSize( 0 );
       
  1653     TInt deviceDataSize( 0 );
       
  1654     TInt udtDataPos( 4 + deviceDataSize );
       
  1655     
  1627     CDRMPermission* permission = CDRMPermission::NewLC();
  1656     CDRMPermission* permission = CDRMPermission::NewLC();
  1628     CDRMCommonData *commonData = NULL;
  1657     CDRMCommonData *commonData = NULL;
  1629     CDRMPointerArray<CDRMPermission> *permissionArray = CDRMPointerArray<CDRMPermission>::NewLC();
  1658     CDRMPointerArray<CDRMPermission> *permissionArray = CDRMPointerArray<CDRMPermission>::NewLC();
  1630     permissionArray->SetAutoCleanup( ETrue );
  1659     permissionArray->SetAutoCleanup( ETrue );
  1631     TDRMUniqueID uniqueID = 0;
  1660     TDRMUniqueID uniqueID = 0;
  1632     RFile fileHandle;
  1661     RFile fileHandle;
  1633     TInt readPos = 0;
  1662     TInt readPos = 0;
  1634     TInt size = 0;
  1663     TInt size = 0;
  1635     TInt dataLeft = 0;
  1664     TInt dataLeft = 0;
  1636     TPtr8 keyData(NULL,0,0);
  1665     TPtr8 keyData(NULL,0,0);
  1637 
  1666       
       
  1667 #ifdef __DRM_OMA2    
       
  1668     if ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) )
       
  1669         {
       
  1670     		maxUDTDataSize = 256;
       
  1671     		deviceDataSize = 256; 
       
  1672     		udtDataPos = 4 + deviceDataSize;	
       
  1673     		}    
       
  1674 #endif 
       
  1675  
  1638     // maintain knowledge about stateful rights not being restored
  1676     // maintain knowledge about stateful rights not being restored
  1639     TBool stateful = EFalse;
  1677     TBool stateful = EFalse;
  1640 
       
  1641 
  1678 
  1642     DRMLOG( _L( "CDRMRightsDB::RestoreContentFromFileL ->" ) );
  1679     DRMLOG( _L( "CDRMRightsDB::RestoreContentFromFileL ->" ) );
  1643 
  1680 
  1644     // Indicate that the DB is updated
  1681     // Indicate that the DB is updated
  1645     iLastUpdate.HomeTime();
  1682     iLastUpdate.HomeTime();
  1684     encIV.SetLength( KEncryptionKeySize );
  1721     encIV.SetLength( KEncryptionKeySize );
  1685 
  1722 
  1686     // Check that the decryption works, if it doesn't then the
  1723     // Check that the decryption works, if it doesn't then the
  1687     // key is faulty
  1724     // key is faulty
  1688     User::LeaveIfError( aBackupFile.Size( readPos ) );
  1725     User::LeaveIfError( aBackupFile.Size( readPos ) );
  1689     if( readPos < KUdtDataPos+KMaxUDTDataSize+(KEncryptionKeySize*2) )
  1726     if( readPos < udtDataPos+maxUDTDataSize+(KEncryptionKeySize*2) )
  1690         {
  1727         {
  1691           // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL : corrupt\n\r"));
  1728           // RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("RestoreContentFromFileL : corrupt\n\r"));
  1692         User::Leave(KErrCorrupt);
  1729         User::Leave(KErrCorrupt);
  1693         }
  1730         }
  1694     readPos -= KEncryptionKeySize*2;
  1731     readPos -= KEncryptionKeySize*2;
  1713 
  1750 
  1714     // Now we are ready to go through the file
  1751     // Now we are ready to go through the file
  1715     //-----------------------------------------------------------------------
  1752     //-----------------------------------------------------------------------
  1716 
  1753 
  1717     // Duplicate file handle
  1754     // Duplicate file handle
  1718     readPos = KUdtDataPos+KMaxUDTDataSize;
  1755     readPos = udtDataPos+maxUDTDataSize;
  1719     User::LeaveIfError( fileHandle.Seek( ESeekStart, readPos ) );
  1756     User::LeaveIfError( fileHandle.Seek( ESeekStart, readPos ) );
  1720     iMemStream.Close();
  1757     iMemStream.Close();
  1721 
  1758 
  1722     ReadDataL( fileHandle, encIV, readData, dataBuffer,
  1759     ReadDataL( fileHandle, encIV, readData, dataBuffer,
  1723                dataLeft, size, ETrue, encryptionKey );
  1760                dataLeft, size, ETrue, encryptionKey );
  1809                 {
  1846                 {
  1810                 keyExists = ETrue;
  1847                 keyExists = ETrue;
  1811                 counter = KEncryptionKeySize;
  1848                 counter = KEncryptionKeySize;
  1812                 }
  1849                 }
  1813             }
  1850             }
  1814 
       
  1815 
  1851 
  1816         // Encrypt the key
  1852         // Encrypt the key
  1817         if( keyExists )
  1853         if( keyExists )
  1818             {
  1854             {
  1819             ModifyKey( key );
  1855             ModifyKey( key );
  1876         if( !rights )
  1912         if( !rights )
  1877             {
  1913             {
  1878             User::Leave( KErrGeneral );
  1914             User::Leave( KErrGeneral );
  1879             }
  1915             }
  1880 
  1916 
  1881 
       
  1882 
       
  1883 
       
  1884         if( insertPerm == -1 ) // Add everything no checks needed
  1917         if( insertPerm == -1 ) // Add everything no checks needed
  1885             {
  1918             {
  1886             for( TInt count = 0; count < permissions; count++ )
  1919             for( TInt count = 0; count < permissions; count++ )
  1887                 {
  1920                 {
  1888                 if( dataLeft < 4 )
  1921                 if( dataLeft < 4 )
  1935                     User::LeaveIfError(error);
  1968                     User::LeaveIfError(error);
  1936                     }
  1969                     }
  1937 
  1970 
  1938                 }
  1971                 }
  1939 
  1972 
  1940 
       
  1941             for( TInt count = 0; count < permissions; count++ )
  1973             for( TInt count = 0; count < permissions; count++ )
  1942                 {
  1974                 {
  1943                 if( dataLeft < 4 )
  1975                 if( dataLeft < 4 )
  1944                     {
  1976                     {
  1945                     size = 4;
  1977                     size = 4;
  2032     // to enable UDT
  2064     // to enable UDT
  2033     if( stateful )
  2065     if( stateful )
  2034         {
  2066         {
  2035         User::Leave( KErrPermissionDenied );
  2067         User::Leave( KErrPermissionDenied );
  2036         }
  2068         }
       
  2069 		
  2037     };
  2070     };
  2038 
  2071 
  2039 
  2072 
  2040 
  2073 
  2041 // -----------------------------------------------------------------------------
  2074 // -----------------------------------------------------------------------------
  2185 #else
  2218 #else
  2186 void CDRMRightsDB::AddUDTDataL( RWriteStream& )
  2219 void CDRMRightsDB::AddUDTDataL( RWriteStream& )
  2187 #endif // __DRM_OMA2
  2220 #endif // __DRM_OMA2
  2188     {
  2221     {
  2189 #ifdef __DRM_OMA2
  2222 #ifdef __DRM_OMA2
       
  2223     
       
  2224     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2225         {
       
  2226         User::Leave( KErrNotSupported );
       
  2227         }
       
  2228     
       
  2229     const TInt KMaxUDTDataSize( 256 );
  2190     TBuf8<MDrmKeyStorage::KRdbSerialNumberLength> serialNumber;
  2230     TBuf8<MDrmKeyStorage::KRdbSerialNumberLength> serialNumber;
  2191     TBuf8<KMaxUDTDataSize> buffer;
  2231     TBuf8<KMaxUDTDataSize> buffer;
  2192     TUint8* ptr = const_cast<TUint8*>(buffer.Ptr());
  2232     TUint8* ptr = const_cast<TUint8*>(buffer.Ptr());
  2193     HBufC8* result = NULL;
  2233     HBufC8* result = NULL;
  2194     TPtr8 udtData( ptr+KDeviceDataBlock, KDeviceDataBlock, KDeviceDataBlock );
  2234     TPtr8 udtData( ptr+KDeviceDataBlock, KDeviceDataBlock, KDeviceDataBlock );
  2355 
  2395 
  2356 // test function
  2396 // test function
  2357 void CDRMRightsDB::CreateDummyUDTFileL()
  2397 void CDRMRightsDB::CreateDummyUDTFileL()
  2358     {
  2398     {
  2359 #ifdef __DRM_OMA2
  2399 #ifdef __DRM_OMA2
       
  2400     
       
  2401     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2402         {
       
  2403         User::Leave( KErrNotSupported );
       
  2404         }
       
  2405     
  2360     TFileName backupFile;
  2406     TFileName backupFile;
  2361     RFile input;
  2407     RFile input;
  2362     TInt fileSize = 4 + 256 + 256;
  2408     TInt fileSize = 4 + 256 + 256;
  2363 
  2409 
  2364     backupFile.Copy( _L("c:\\") );
  2410     backupFile.Copy( _L("c:\\") );
  2384 // -----------------------------------------------------------------------------
  2430 // -----------------------------------------------------------------------------
  2385 //
  2431 //
  2386 void CDRMRightsDB::CleanUdtData()
  2432 void CDRMRightsDB::CleanUdtData()
  2387     {
  2433     {
  2388 #ifdef __DRM_OMA2
  2434 #ifdef __DRM_OMA2
       
  2435     
       
  2436     if ( ! ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) ) )
       
  2437         {
       
  2438         return;
       
  2439         }
       
  2440     
  2389     TFileName backupFile;
  2441     TFileName backupFile;
  2390 
  2442 
  2391 #ifndef RD_MULTIPLE_DRIVE
  2443 #ifndef RD_MULTIPLE_DRIVE
  2392 
  2444 
  2393     backupFile.Copy( KBackupDirectory );
  2445     backupFile.Copy( KBackupDirectory );