commondrm/drmutility/src/DrmUtility.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2006-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:  DRM Utility
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <Oma2Dcf.h>
       
    21 #include <drmagents.h>
       
    22 #include <drmutility.h>
       
    23 #include <centralrepository.h>
       
    24 #include <UTF.h>
       
    25 
       
    26 #include "Oma1Dcf.h"
       
    27 
       
    28 #include "DrmUtilityInternalcrkeys.h"      // Cenrep extension for OmaBased
       
    29 
       
    30 // CONSTANTS
       
    31 _LIT8( KASFHeaderObject, "75B22630668E11CFA6D900AA0062CE6C" );
       
    32 _LIT8( KWrmHeader, "W\0R\0M\0H\0E\0A\0D\0E\0R\0" );
       
    33 
       
    34 const TInt KMinContentLength( 16 );
       
    35 const TInt KOma2EncryptionFieldOffset( 8 );
       
    36 const TInt KOmaHeaderLength( 512 );
       
    37 // This constant is for OMA1 case in which a buffer of 35-40
       
    38 // bytes is needed to recognize the mime type.
       
    39 const TInt KMinContentLengthOma1Based( 40 );
       
    40 const TInt KWMHeaderObjectLength( 8 );
       
    41 const TInt KWMTopLevelHeaderObjectLength( 30 );
       
    42 const TInt KMaxWMHeaderLength( ( KMaxTInt32 / 2 ) - 1 );
       
    43 
       
    44 const TInt KCenRepDataLength( 50 );
       
    45 
       
    46 // ============================ LOCAL FUNCTIONS ================================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // FormatGUID
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 LOCAL_C void FormatGUID( TDes8 &aGUID )
       
    53     {
       
    54     TBuf8<16> copyGUID( aGUID );
       
    55     TInt i( 0 );
       
    56     for ( i = 0; i < 4; i++ )
       
    57         {
       
    58         copyGUID[i] = aGUID[3-i];
       
    59         }
       
    60     for ( i = 4; i < 6; i++ )
       
    61         {
       
    62         copyGUID[i] = aGUID[9 - i];
       
    63         }
       
    64     for ( i = 6; i < 8; i++ )
       
    65         {
       
    66         copyGUID[i] = aGUID[13 - i];
       
    67         }
       
    68     for ( i = 8; i < 16 ; i++) 
       
    69         {
       
    70         copyGUID[i] = aGUID[i];
       
    71         }
       
    72     aGUID.Delete( 0, 32 );
       
    73     for ( i = 0; i < 16; i++ )
       
    74         {
       
    75         aGUID.AppendNumFixedWidthUC( copyGUID[i], EHex, 2 );
       
    76         }
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // ConvertToInt64
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 LOCAL_C TInt64 ConvertToInt64( TDesC8& aDes )
       
    84     {
       
    85     TInt64 num( 0 );
       
    86     TInt i( 0 );
       
    87     for ( i = 7 ; i >= 0; i-- )
       
    88         {
       
    89         num <<= 8;
       
    90         num |= aDes[i];
       
    91         }
       
    92     return num;
       
    93     }
       
    94 
       
    95 // ============================ MEMBER FUNCTIONS ===============================
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // Default constructor
       
    99 // -----------------------------------------------------------------------------
       
   100 //  
       
   101 DRM::CDrmUtility::CDrmUtility()
       
   102     {
       
   103     }
       
   104     
       
   105 // -----------------------------------------------------------------------------
       
   106 // Destructor
       
   107 // -----------------------------------------------------------------------------
       
   108 //  
       
   109 DRM::CDrmUtility::~CDrmUtility()
       
   110     {
       
   111     delete iOmaBasedAgentName;
       
   112     delete iOmaBasedMimeType;
       
   113     }
       
   114     
       
   115 // -----------------------------------------------------------------------------
       
   116 // CDrmUtility::NewLC
       
   117 // First phase constructor
       
   118 // -----------------------------------------------------------------------------
       
   119 //  
       
   120 EXPORT_C DRM::CDrmUtility* DRM::CDrmUtility::NewLC()
       
   121     {
       
   122     DRM::CDrmUtility* self( new( ELeave ) CDrmUtility );
       
   123     CleanupStack::PushL( self );
       
   124     self->ConstructL();
       
   125     return self;
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CDrmUtility::NewL
       
   130 // First phase constructor
       
   131 // -----------------------------------------------------------------------------
       
   132 //  
       
   133 EXPORT_C DRM::CDrmUtility* DRM::CDrmUtility::NewL()
       
   134     {
       
   135     DRM::CDrmUtility* self( NewLC() );
       
   136     CleanupStack::Pop();
       
   137     return self;
       
   138     }
       
   139 
       
   140 void DRM::CDrmUtility::ConstructL()
       
   141     {
       
   142     TInt err( KErrNone );
       
   143              
       
   144      TRAP(err, FetchOmaBasedInfoL() );
       
   145      if( err)
       
   146          {
       
   147          if( iOmaBasedAgentName )
       
   148              {
       
   149              delete iOmaBasedAgentName;
       
   150              }
       
   151          iOmaBasedAgentName = NULL;
       
   152          if( iOmaBasedMimeType )
       
   153              {
       
   154              delete iOmaBasedMimeType;
       
   155              }
       
   156          iOmaBasedMimeType = NULL;
       
   157          }   
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CDrmUtility::FetchOmaBasedInfoL
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 void DRM::CDrmUtility::FetchOmaBasedInfoL()
       
   165     {
       
   166     TInt err = KErrNone;
       
   167     CRepository* repository( NULL );
       
   168     RBuf bOmaBasedAgentName;
       
   169     RBuf bOmaBasedMimeType;
       
   170     
       
   171     CleanupClosePushL(bOmaBasedAgentName);
       
   172     CleanupClosePushL(bOmaBasedMimeType);
       
   173     bOmaBasedAgentName.CreateL( KCenRepDataLength );
       
   174     bOmaBasedMimeType.CreateL( KCenRepDataLength );
       
   175  
       
   176     TRAP( err, repository = CRepository::NewL( KCRUidOmaBased ) );
       
   177     if ( !err )
       
   178         {
       
   179         CleanupStack::PushL( repository );
       
   180         
       
   181         err = repository->Get( KDrmOmaBasedName, bOmaBasedAgentName );
       
   182         if( !err )
       
   183             {
       
   184             iOmaBasedAgentName = bOmaBasedAgentName.AllocL(); 
       
   185             }
       
   186         
       
   187         err = repository->Get( KOmaBasedMimeType, bOmaBasedMimeType );
       
   188         if( !err )
       
   189             {
       
   190             iOmaBasedMimeType = CnvUtfConverter::ConvertFromUnicodeToUtf8L( bOmaBasedMimeType );
       
   191             }
       
   192         CleanupStack::PopAndDestroy( repository );
       
   193         }
       
   194     
       
   195     CleanupStack::PopAndDestroy(2);
       
   196     
       
   197     User::LeaveIfError( err );
       
   198     }
       
   199 
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CDrmUtility::GetDrmInfoL
       
   203 // -----------------------------------------------------------------------------
       
   204 //  
       
   205 EXPORT_C TBool DRM::CDrmUtility::GetDrmInfoL( 
       
   206     RFile& aFileHandle,
       
   207     TPtrC& aAgent,
       
   208     DRM::TDrmProtectionStatus& aProtected ) const
       
   209     {
       
   210     TInt r( KErrNone );
       
   211     HBufC8* buffer( NULL );
       
   212     TInt pos( 0 );
       
   213     RFile file;
       
   214     TBuf8< 32 > header;
       
   215     TInt64 headerSize( 0 );
       
   216     TBool isDrmFile( EFalse );
       
   217     TPtr8 headerPtr( NULL, 0 );
       
   218         
       
   219     aProtected = DRM::EUUnprotected;
       
   220     aAgent.Set( KNullDesC );
       
   221     
       
   222     CheckFileHandleL( aFileHandle );
       
   223     User::LeaveIfError( file.Duplicate( aFileHandle ) );
       
   224     CleanupClosePushL( file );
       
   225 
       
   226     User::LeaveIfError( file.Seek( ESeekStart, pos ) );
       
   227     
       
   228     // Check if the file is an ASF file
       
   229     // To be Checked on runtime wether WM DRM is supporeted or not
       
   230     User::LeaveIfError( file.Read( 0, header, KMinContentLength ) );
       
   231     if ( header.Length() < KMinContentLength )
       
   232         {
       
   233         User::Leave( KErrArgument ); 
       
   234         }    
       
   235         
       
   236     FormatGUID( header );
       
   237     
       
   238     if ( header == KASFHeaderObject )
       
   239         {
       
   240         // It's ASF, check still whether it's WM DRM protected or not
       
   241         file.Read( header, KWMHeaderObjectLength );
       
   242         headerSize = ConvertToInt64( header );
       
   243         if( headerSize <= KWMTopLevelHeaderObjectLength || 
       
   244             headerSize > KMaxWMHeaderLength )
       
   245             {
       
   246             User::Leave( KErrArgument );
       
   247             }
       
   248         buffer = HBufC8::NewLC( headerSize );
       
   249 
       
   250         headerPtr.Set( buffer->Des() ); 
       
   251         User::LeaveIfError( file.Read( headerPtr, 
       
   252                 headerSize - ( KMinContentLength + KWMHeaderObjectLength ) ) );
       
   253     
       
   254         r = headerPtr.Find( KWrmHeader );
       
   255         if ( r == KErrNotFound ) 
       
   256             {
       
   257             aProtected = DRM::EUUnprotected;
       
   258             }
       
   259         else
       
   260             {
       
   261             isDrmFile = ETrue;
       
   262             aProtected = DRM::EUProtected;
       
   263             aAgent.Set( DRM::KDrmWMAgentName );                            
       
   264             }
       
   265         CleanupStack::PopAndDestroy( buffer ); // buffer    
       
   266         }
       
   267     else
       
   268         {
       
   269         // Check whether it's OMA DRM protected or not
       
   270         buffer = HBufC8::NewLC( KOmaHeaderLength );
       
   271         
       
   272         headerPtr.Set( buffer->Des() );
       
   273         User::LeaveIfError( file.Read( 0, headerPtr ));
       
   274         
       
   275         if ( COma1Dcf::IsValidDcf( headerPtr ) )
       
   276             {
       
   277             isDrmFile = ETrue;
       
   278             aProtected = DRM::EUProtected;                             
       
   279             aAgent.Set( DRM::KDrmOmaAgentName );                
       
   280             }
       
   281         else if ( COma2Dcf::IsValidDcf( headerPtr ) )
       
   282             {
       
   283             isDrmFile = ETrue;
       
   284             _LIT8( KCommonHeadersBox, "ohdr" );
       
   285             pos = headerPtr.Find( KCommonHeadersBox );
       
   286             
       
   287             // If no box can be found or if there isn't enough data
       
   288             // set protection as unknown
       
   289             if( pos == KErrNotFound || 
       
   290                 headerPtr.Length() < pos + KOma2EncryptionFieldOffset )
       
   291                 {
       
   292                 aProtected = DRM::EUUnknown;
       
   293                 }
       
   294             // If encryption field is 0, then content isn't protected
       
   295             else if ( !headerPtr[pos + KOma2EncryptionFieldOffset] )
       
   296                 {
       
   297                 aProtected = DRM::EUUnprotected;
       
   298                 }
       
   299             else
       
   300                 {
       
   301                 aProtected = DRM::EUProtected;
       
   302                 }                  
       
   303             aAgent.Set( DRM::KDrmOmaAgentName );                
       
   304             }   
       
   305         else if ( (buffer->Des())[0] == 1)
       
   306             {
       
   307             // set the mimetype from the buffer which is in the beginning
       
   308             // starting from byte 3 with the length specified at position 2		
       
   309             TPtrC8 mimeType( buffer->Des().Mid(3, (buffer->Des())[1]) );
       
   310             if( !mimeType.CompareF( *iOmaBasedMimeType ) ) 
       
   311                 {	
       
   312                 aAgent.Set( *DRM::CDrmUtility::iOmaBasedAgentName );
       
   313                 isDrmFile = ETrue;
       
   314                 }
       
   315             }
       
   316         CleanupStack::PopAndDestroy( buffer );
       
   317         }  
       
   318     CleanupStack::PopAndDestroy( &file ); // file
       
   319     return isDrmFile;
       
   320     }  
       
   321   
       
   322 // -----------------------------------------------------------------------------
       
   323 // CDrmUtility::GetDrmInfoL
       
   324 // -----------------------------------------------------------------------------
       
   325 //  
       
   326 EXPORT_C TBool DRM::CDrmUtility::GetDrmInfoL( 
       
   327     const TDesC8& aContent,
       
   328     TPtrC& aAgent,
       
   329     DRM::TDrmProtectionStatus& aProtected ) const
       
   330     {
       
   331     TInt r( KErrNone );
       
   332     RFile file;
       
   333     TPtr8 asfPtr( NULL, 0 );
       
   334     TBuf8< 32 > asfGuidHex;
       
   335     TBool isDrmFile( EFalse );
       
   336     
       
   337     aProtected = DRM::EUUnprotected;
       
   338     aAgent.Set( KNullDesC );
       
   339 
       
   340     if ( aContent.Length() < KMinContentLength )
       
   341         {
       
   342         User::Leave( KErrArgument );   
       
   343         }
       
   344         
       
   345     // Check if the file is an ASF file
       
   346     asfPtr.Set( 
       
   347         const_cast<TUint8*>( asfGuidHex.Ptr() ), 0, KMinContentLength * 2 );
       
   348     asfPtr.Copy( aContent.Left( KMinContentLength ) );    
       
   349     FormatGUID( asfPtr );
       
   350     
       
   351     if ( asfPtr == KASFHeaderObject )
       
   352         {
       
   353         // It's ASF, check still whether it's WM DRM protected or not
       
   354         r = aContent.Find( KWrmHeader );
       
   355         if ( r == KErrNotFound ) 
       
   356             {
       
   357             aProtected = DRM::EUUnprotected;   
       
   358             }
       
   359         else
       
   360             {
       
   361             isDrmFile = ETrue;
       
   362             aProtected = DRM::EUProtected;                               
       
   363             aAgent.Set( DRM::KDrmWMAgentName ); 
       
   364             }   
       
   365         }
       
   366     else
       
   367         {
       
   368         // Check whether it's OMA DRM protected or not.
       
   369         if ( ( aContent.Length() >= KMinContentLengthOma1Based ) && 
       
   370             ( COma1Dcf::IsValidDcf( aContent ) ) )
       
   371             {
       
   372             isDrmFile = ETrue;
       
   373             aProtected = DRM::EUProtected;                          
       
   374             aAgent.Set( DRM::KDrmOmaAgentName );
       
   375             }
       
   376         else if ( COma2Dcf::IsValidDcf( aContent ) )
       
   377             {
       
   378             isDrmFile = ETrue;
       
   379             _LIT8( KCommonHeadersBox, "ohdr" );
       
   380             TInt pos( aContent.Find( KCommonHeadersBox ) );
       
   381             
       
   382             // If no box can be found or if there isn't enough data
       
   383             // set protection as unknown
       
   384             if ( pos == KErrNotFound || 
       
   385                  aContent.Length() < pos + KOma2EncryptionFieldOffset )
       
   386                 {
       
   387                 aProtected = DRM::EUUnknown;
       
   388                 }
       
   389             // If encryption field is 0, then content isn't protected    
       
   390             else if ( !aContent[pos + KOma2EncryptionFieldOffset] )
       
   391                 {
       
   392                 aProtected = DRM::EUUnprotected;    
       
   393                 }
       
   394             else
       
   395                 {
       
   396                 aProtected = DRM::EUProtected;    
       
   397                 }                               
       
   398             aAgent.Set( DRM::KDrmOmaAgentName );            
       
   399             }
       
   400         else if ( (aContent)[0] == 1)
       
   401             {
       
   402             // set the mimetype from the buffer which is in the beginning
       
   403             // starting from byte 3 with the length specified at position 2     
       
   404             TPtrC8 mimeType( aContent.Mid(3, (aContent)[1]) );
       
   405             if( !mimeType.CompareF( *iOmaBasedMimeType ) )
       
   406                 {   
       
   407                 aAgent.Set( *DRM::CDrmUtility::iOmaBasedAgentName );
       
   408                 isDrmFile = ETrue;
       
   409                 aProtected = DRM::EUProtected;
       
   410                 }
       
   411             }
       
   412         }
       
   413     return isDrmFile;
       
   414     }      
       
   415 
       
   416 // -----------------------------------------------------------------------------
       
   417 // CDrmUtility::IsProtectedL
       
   418 // -----------------------------------------------------------------------------
       
   419 //  
       
   420 EXPORT_C TBool DRM::CDrmUtility::IsProtectedL( RFile& aFileHandle ) const
       
   421     {
       
   422     DRM::TDrmProtectionStatus protection( DRM::EUUnknown );
       
   423     TPtrC agent( KNullDesC );
       
   424     TBool isDrmFile( EFalse );
       
   425     TBool isProtected( EFalse );
       
   426     
       
   427     isDrmFile = GetDrmInfoL( aFileHandle, agent, protection );
       
   428     
       
   429     if ( isDrmFile && protection == DRM::EUProtected )
       
   430         {
       
   431         isProtected = ETrue;
       
   432         }
       
   433     return isProtected;
       
   434     }
       
   435 
       
   436     
       
   437 // -----------------------------------------------------------------------------
       
   438 // CDrmUtility::IsProtectedL
       
   439 // -----------------------------------------------------------------------------
       
   440 //  
       
   441 EXPORT_C TBool DRM::CDrmUtility::IsProtectedL( const TDesC8& aContent ) const
       
   442     {
       
   443     DRM::TDrmProtectionStatus protection( DRM::EUUnknown );
       
   444     TPtrC agent( KNullDesC );
       
   445     TBool isDrmFile( EFalse );
       
   446     TBool isProtected( EFalse );
       
   447     
       
   448     isDrmFile = GetDrmInfoL( aContent, agent, protection );
       
   449     
       
   450     if( isDrmFile && protection == DRM::EUProtected )
       
   451         {
       
   452         isProtected = ETrue;
       
   453         }
       
   454     return isProtected;
       
   455     }
       
   456 
       
   457 
       
   458 // -----------------------------------------------------------------------------
       
   459 // CDrmUtility::GetAgentL
       
   460 // -----------------------------------------------------------------------------
       
   461 //  
       
   462 EXPORT_C TBool DRM::CDrmUtility::GetAgentL( 
       
   463     RFile& aFileHandle,
       
   464     TPtrC& aAgent ) const
       
   465     {
       
   466     DRM::TDrmProtectionStatus protection( DRM::EUUnknown );
       
   467     TBool isDrmFile( EFalse );
       
   468 
       
   469     isDrmFile = GetDrmInfoL( aFileHandle, aAgent, protection );
       
   470 
       
   471     return isDrmFile;
       
   472     }
       
   473     
       
   474 // -----------------------------------------------------------------------------
       
   475 // CDrmUtility::GetAgentL
       
   476 // -----------------------------------------------------------------------------
       
   477 //  
       
   478 EXPORT_C TBool DRM::CDrmUtility::GetAgentL( 
       
   479     const TDesC8& aContent,
       
   480     TPtrC& aAgent ) const
       
   481     {
       
   482     DRM::TDrmProtectionStatus protection( DRM::EUUnknown );
       
   483     TBool isDrmFile( EFalse );
       
   484 
       
   485     isDrmFile = GetDrmInfoL( aContent, aAgent, protection );
       
   486 
       
   487     return isDrmFile;
       
   488     }
       
   489  	
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CDrmUtility::CheckFileHandlerL
       
   493 // Checks whether given filehandle is valid if not leaves with KErrArgument
       
   494 // (other items were commented in a header).
       
   495 // -----------------------------------------------------------------------------
       
   496 // 
       
   497 EXPORT_C void DRM::CDrmUtility::CheckFileHandleL( RFile& aFileHandle ) const
       
   498     {
       
   499     if ( !aFileHandle.SubSessionHandle() )
       
   500         {
       
   501         User::Leave( KErrBadHandle );
       
   502         }
       
   503     }
       
   504        
       
   505 //  End of File