omadrm/drmengine/roapstorage/src/DRMDomainContext.cpp
changeset 0 95b198f216e5
child 84 b09186059647
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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:  Datatype for the Common Rights Database Data
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32strm.h>
       
    22 #include <s32mem.h>
       
    23 #include <caf/caf.h>
       
    24 #include <asymmetric.h>
       
    25 
       
    26 #include "DRMDomainContext.h"
       
    27 #include "OmaCrypto.h"
       
    28 #include "drmlog.h"
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // MACROS
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 const TInt KMaxUrlLength = 32768;
       
    40 const TUint8 KMinDomainIdSize = 4;
       
    41 const TUint8 KMaxDomainIdSize = 20;
       
    42 
       
    43 // MODULE DATA STRUCTURES
       
    44 
       
    45 // LOCAL FUNCTION PROTOTYPES
       
    46 
       
    47 // FORWARD DECLARATIONS
       
    48 
       
    49 // ============================= LOCAL FUNCTIONS ===============================
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CDRMDomainContext::NewLC
       
    54 // Two-phased constructor.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CDRMDomainContext* CDRMDomainContext::NewLC( const TDesC8& aDomainID,
       
    58                                              const TTime& aExpiryTime,
       
    59                                              const TBool aHashChained,
       
    60                                              const RPointerArray< HBufC8 >& aDomainKeys,
       
    61                                              const TDesC8& aRiId,
       
    62                                              const TDesC8& aRightsIssuerURL )
       
    63     {
       
    64     CDRMDomainContext* self = new( ELeave ) CDRMDomainContext( aExpiryTime,
       
    65                                                                aHashChained );
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL( aDomainID, aDomainKeys,
       
    68                       aRiId, aRightsIssuerURL );
       
    69 
       
    70     return self;
       
    71     };
       
    72 
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CDRMDomainContext::NewL
       
    76 // Two-phased constructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CDRMDomainContext* CDRMDomainContext::NewL(const TDesC8& aDomainID,
       
    80                                            const TTime& aExpiryTime,
       
    81                                            const TBool aHashChained,
       
    82                                            const RPointerArray< HBufC8 >& aDomainKeys,
       
    83                                            const TDesC8& aRiId,
       
    84                                            const TDesC8& aRightsIssuerURL  )
       
    85     {
       
    86     CDRMDomainContext* self = NewLC( aDomainID, aExpiryTime,
       
    87                                      aHashChained, aDomainKeys,
       
    88                                      aRiId, aRightsIssuerURL );
       
    89     CleanupStack::Pop();
       
    90 
       
    91     return self;
       
    92     };
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CDRMDomainContext::NewLC
       
    96 // Two-phased constructor.
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C CDRMDomainContext* CDRMDomainContext::NewLC()
       
   100     {
       
   101     CDRMDomainContext* self = new( ELeave ) CDRMDomainContext();
       
   102     CleanupStack::PushL( self );
       
   103 
       
   104     return self;
       
   105     };
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CDRMDomainContext::NewL
       
   110 // Two-phased constructor.
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C CDRMDomainContext* CDRMDomainContext::NewL()
       
   114     {
       
   115     CDRMDomainContext* self = NewLC();
       
   116     CleanupStack::Pop();
       
   117 
       
   118     return self;
       
   119     };
       
   120 
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // Default Constructor - First phase.
       
   124 // Can be used by itself to generate an empty object
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 CDRMDomainContext::CDRMDomainContext():
       
   128     iHashChained( EFalse ),
       
   129     iRightsIssuerURL( NULL )
       
   130     {
       
   131     iDomainID.SetLength( 0 );
       
   132     iRightsIssuerID.SetLength( 0 );
       
   133     iDomainKeys.Reset();
       
   134     };
       
   135 
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // Default Constructor - First phase.
       
   139 // Can be used by itself to generate an empty object
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C CDRMDomainContext::CDRMDomainContext( const TTime& aExpiryTime,
       
   143                                       const TBool aHashChained ) :
       
   144     iExpiryTime( aExpiryTime ),
       
   145     iHashChained( aHashChained )
       
   146     {
       
   147     iDomainID.SetLength( 0 );
       
   148     iRightsIssuerID.SetLength( 0 );
       
   149     iDomainKeys.Reset();
       
   150     };
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // Destructor
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C CDRMDomainContext::~CDRMDomainContext()
       
   157     {
       
   158     // Empty and destroy the pointer array
       
   159     iDomainKeys.ResetAndDestroy();
       
   160 
       
   161     if( iRightsIssuerURL )
       
   162         {
       
   163         delete iRightsIssuerURL;
       
   164         iRightsIssuerURL = NULL;
       
   165         }
       
   166     };
       
   167 
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CDRMDomainContext::DomainID
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 EXPORT_C const TPtrC8 CDRMDomainContext::DomainID() const
       
   174     {
       
   175     // last three digits presents the domain generation
       
   176     if ( iDomainID.Length() >= KMinDomainIdSize &&
       
   177         iDomainID.Length() <= KMaxDomainIdSize  )
       
   178         {
       
   179         return iDomainID.Left( iDomainID.Length() - 3 );
       
   180         }
       
   181     else
       
   182         {
       
   183         return KNullDesC8();
       
   184         }
       
   185     };
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CDRMDomainContext::ExpiryTime
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C const TTime CDRMDomainContext::ExpiryTime() const
       
   192     {
       
   193     return iExpiryTime;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CDRMDomainContext::HashChained
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C TBool CDRMDomainContext::HashChained() const
       
   201     {
       
   202     return iHashChained;
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CDRMDomainContext::DomainKeys
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 EXPORT_C const RPointerArray< HBufC8 >& CDRMDomainContext::DomainKeys() const
       
   210     {
       
   211     return iDomainKeys;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CDRMDomainContext::DomainKey
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 EXPORT_C HBufC8* CDRMDomainContext::DomainKeyL( const TInt aGeneration ) const
       
   219     {
       
   220     TInt newestGeneration( DomainGeneration() );
       
   221     TInt loops( newestGeneration - aGeneration );
       
   222     HBufC8* domainKey( NULL );
       
   223     HBufC8* temp( NULL );
       
   224 
       
   225     if ( loops < 0 || aGeneration > newestGeneration )
       
   226         {
       
   227         User::Leave( KErrArgument );
       
   228         }
       
   229 
       
   230     if ( iHashChained )
       
   231         {
       
   232         domainKey = iDomainKeys[0]->AllocL();
       
   233         for ( TInt i = 0; i < loops; i++ )
       
   234             {
       
   235             CleanupStack::PushL( domainKey );
       
   236             temp = OmaCrypto::KdfL( *domainKey, KNullDesC8, OmaCrypto::KKeySize );
       
   237             CleanupStack::PopAndDestroy( domainKey );
       
   238             domainKey = temp;
       
   239             }
       
   240         }
       
   241     else
       
   242         {
       
   243         domainKey = iDomainKeys[aGeneration]->AllocL();
       
   244         }
       
   245 
       
   246     return domainKey;
       
   247     }
       
   248 
       
   249 /// -----------------------------------------------------------------------------
       
   250 // CDRMDomainContext::DomainGeneration
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 EXPORT_C TInt CDRMDomainContext::DomainGeneration() const
       
   254     {
       
   255     // last three digits presents the domain generation
       
   256     TInt generation = 0;
       
   257     TLex8 lex( iDomainID.Right( 3 ) );
       
   258     lex.Val( generation );
       
   259     return generation;
       
   260     };
       
   261 
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CDRMDomainContext::PublicKey
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 EXPORT_C const TDesC8& CDRMDomainContext::RightsIssuerID() const
       
   268     {
       
   269     return iRightsIssuerID;
       
   270     };
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CDRMDomainContext::RightsIssuerURL
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 EXPORT_C const TDesC8& CDRMDomainContext::RightsIssuerURL() const
       
   277     {
       
   278     return *iRightsIssuerURL;
       
   279     };
       
   280 
       
   281 
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CDRMDomainContext::SetDomainIDL
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 EXPORT_C void CDRMDomainContext::SetDomainIDL( const TDesC8& aDomainID )
       
   288     {
       
   289     if ( aDomainID.Length() >= KMinDomainIdSize &&
       
   290         aDomainID.Length() <= KMaxDomainIdSize  )
       
   291         {
       
   292     iDomainID.Copy(aDomainID);
       
   293         }
       
   294     else
       
   295         {
       
   296         User::Leave( KErrArgument );
       
   297         }
       
   298     };
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // CDRMDomainContext::SetExpiryTimeL
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 EXPORT_C void CDRMDomainContext::SetExpiryTimeL( const TTime& aExpiryTime )
       
   305     {
       
   306     iExpiryTime = aExpiryTime;
       
   307     };
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CDRMDomainContext::SetHashChained
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 EXPORT_C void CDRMDomainContext::SetHashChainedL( const TBool& aHashChained )
       
   314     {
       
   315     iHashChained = aHashChained;
       
   316     };
       
   317 
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CDRMDomainContext::SetDomainKeysL
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 EXPORT_C void CDRMDomainContext::SetDomainKeysL(
       
   324     const RPointerArray< HBufC8 >& aDomainKeys )
       
   325     {
       
   326     HBufC8* tempData = NULL;
       
   327 
       
   328     // Clear the old algorithms
       
   329     iDomainKeys.ResetAndDestroy();
       
   330 
       
   331     // Append the new stuff
       
   332     for( TInt i = 0; i < aDomainKeys.Count(); i++ )
       
   333         {
       
   334         tempData = aDomainKeys[i]->AllocLC();
       
   335         iDomainKeys.AppendL( tempData );
       
   336         CleanupStack::Pop();
       
   337         }
       
   338     };
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CDRMDomainContext::SetPublicKeyL
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C void CDRMDomainContext::SetRightsIssuerIDL(
       
   345     const TDesC8& aRightsIssuerID )
       
   346     {
       
   347     iRightsIssuerID.Copy( aRightsIssuerID );
       
   348     };
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CDRMDomainContext::SetRightsIssuerURLL
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 EXPORT_C void CDRMDomainContext::SetRightsIssuerURLL(
       
   355     const TDesC8& aRightsIssuerURL )
       
   356     {
       
   357     HBufC8* newRightsIssuerURL = aRightsIssuerURL.AllocL();
       
   358 
       
   359     if( iRightsIssuerURL )
       
   360         {
       
   361         delete iRightsIssuerURL;
       
   362         iRightsIssuerURL = NULL;
       
   363         }
       
   364     iRightsIssuerURL = newRightsIssuerURL;
       
   365     };
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CDRMDomainContext::ExternalizeL
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 EXPORT_C void CDRMDomainContext::ExternalizeL( RWriteStream& aStream ) const
       
   372     {
       
   373     TUint8 flag;
       
   374 
       
   375     // Write the ContentID
       
   376     aStream << iDomainID;
       
   377 
       
   378     // expiry time
       
   379     WriteInt64L( iExpiryTime.Int64(), aStream );
       
   380 
       
   381     // hash chained
       
   382     aStream.WriteInt8L( iHashChained );
       
   383 
       
   384     // write the domain keys
       
   385 
       
   386     // How many:
       
   387     aStream.WriteInt32L( iDomainKeys.Count() );
       
   388 
       
   389     // for each in rpointerarray
       
   390     for( TInt i = 0; i < iDomainKeys.Count(); i++ )
       
   391         {
       
   392         aStream << *(iDomainKeys[i]);
       
   393         }
       
   394 
       
   395     // Write the rights issuer ID
       
   396     aStream << iRightsIssuerID;
       
   397 
       
   398     // Write the rights issuer url
       
   399     if (iRightsIssuerURL)
       
   400         {
       
   401         flag = ETrue;
       
   402         aStream << flag;
       
   403         aStream << *iRightsIssuerURL;
       
   404         }
       
   405     else
       
   406         {
       
   407         flag = EFalse;
       
   408         aStream << flag;
       
   409         }
       
   410     };
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CDRMDomainContext::InternalizeL
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 EXPORT_C void CDRMDomainContext::InternalizeL( RReadStream& aStream )
       
   417     {
       
   418     HBufC8* dataPart = 0;
       
   419     TPtr8 dataBuffer(NULL,0,0);
       
   420     TInt count = 0;
       
   421     TInt64 timeData;
       
   422     TUint8 flag;
       
   423 
       
   424     // Read the DomainID
       
   425     aStream >> iDomainID;
       
   426 
       
   427     // read the expiry time
       
   428     ReadInt64L( timeData, aStream );
       
   429     iExpiryTime = timeData;
       
   430 
       
   431     // read the hash chained
       
   432     iHashChained = aStream.ReadInt8L();
       
   433 
       
   434     // read the algorithms
       
   435 
       
   436     // How many:
       
   437     count = aStream.ReadInt32L();
       
   438 
       
   439     // for each in rpointerarray
       
   440     for( TInt i = 0; i < count; i++ )
       
   441         {
       
   442         // Read the ContentID
       
   443         dataPart = HBufC8::NewL( aStream, KMaxUrlLength );
       
   444 
       
   445         // assign the new content id
       
   446         iDomainKeys.AppendL( dataPart );
       
   447         }
       
   448 
       
   449     // Read the rights issuer ID
       
   450     aStream >> iRightsIssuerID;
       
   451 
       
   452     // Read the rights issuer url
       
   453     if( iRightsIssuerURL )
       
   454         {
       
   455         delete iRightsIssuerURL;
       
   456         iRightsIssuerURL = NULL;
       
   457         }
       
   458 
       
   459     aStream >> flag;
       
   460     if (flag)
       
   461         {
       
   462         iRightsIssuerURL = HBufC8::NewL( aStream, KMaxUrlLength );
       
   463         }
       
   464     };
       
   465 
       
   466 
       
   467 // -----------------------------------------------------------------------------
       
   468 // CDRMDomainContext::ImportL
       
   469 // -----------------------------------------------------------------------------
       
   470 //
       
   471 EXPORT_C void CDRMDomainContext::ImportL( const TDesC8& aBuffer )
       
   472     {
       
   473     TInt size = aBuffer.Size();
       
   474     RMemReadStream stream( (TAny*)( aBuffer.Ptr() ), size );
       
   475     CleanupClosePushL( stream );
       
   476 
       
   477     InternalizeL( stream );
       
   478 
       
   479     CleanupStack::PopAndDestroy(); // Stream
       
   480     };
       
   481 
       
   482 
       
   483 // -----------------------------------------------------------------------------
       
   484 // CDRMDomainContext::ExportL
       
   485 // -----------------------------------------------------------------------------
       
   486 //
       
   487 EXPORT_C HBufC8* CDRMDomainContext::ExportL() const
       
   488     {
       
   489     TInt size = Size();
       
   490     HBufC8* data = HBufC8::NewMaxLC( size );
       
   491 
       
   492     RMemWriteStream stream( (TAny*)( data->Ptr() ), size );
       
   493     CleanupClosePushL( stream );
       
   494 
       
   495     ExternalizeL( stream );
       
   496 
       
   497     CleanupStack::PopAndDestroy(); // Stream
       
   498     CleanupStack::Pop();
       
   499     return data;
       
   500     };
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // CDRMDomainContext::Size
       
   504 // -----------------------------------------------------------------------------
       
   505 //
       
   506 EXPORT_C TInt CDRMDomainContext::Size() const
       
   507     {
       
   508     TInt size = 0;
       
   509 
       
   510     // domain identifier
       
   511     size += sizeof(TInt32);
       
   512     size += iDomainID.Length();
       
   513 
       
   514     // size of the expiry time
       
   515     size += sizeof(TInt64);
       
   516 
       
   517     // size of the boolean
       
   518     size += sizeof(TBool);
       
   519 
       
   520     // How many:
       
   521     size += sizeof(TInt32);
       
   522 
       
   523     // for each in rpointerarray
       
   524     for( TInt i = 0; i < iDomainKeys.Count(); i++ )
       
   525         {
       
   526         size += sizeof(TInt32);
       
   527         size += iDomainKeys[i]->Size();
       
   528         }
       
   529 
       
   530     // Domain generation
       
   531     size += sizeof(TInt32);
       
   532 
       
   533     // rights issuer ID
       
   534     size += sizeof(TInt32);
       
   535     size += iRightsIssuerID.Length();
       
   536 
       
   537     // rights issuer url
       
   538     size += sizeof(TUint8);
       
   539     if( iRightsIssuerURL )
       
   540         {
       
   541         size += sizeof(TInt32);
       
   542         size += iRightsIssuerURL->Size();
       
   543         }
       
   544 
       
   545     return size;
       
   546     };
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CDRMDomainContext::ConstructL
       
   550 // Second phase constructor
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 void CDRMDomainContext::ConstructL( const TDesC8& aDomainID,
       
   554                                     const RPointerArray< HBufC8 > aDomainKeys,
       
   555                                     const TDesC8& aRightsIssuerID,
       
   556                                     const TDesC8& aRightsIssuerURL )
       
   557     {
       
   558     HBufC8* domainKey = NULL;
       
   559 
       
   560     // Copy the domain identifier
       
   561     if ( aDomainID.Length() >= KMinDomainIdSize &&
       
   562         aDomainID.Length() <= KMaxDomainIdSize  )
       
   563         {
       
   564     iDomainID.Copy( aDomainID );
       
   565         }
       
   566     else
       
   567         {
       
   568         User::Leave( KErrArgument );
       
   569         }
       
   570 
       
   571     // Copy the domain keys
       
   572     for( TInt i = 0; i < aDomainKeys.Count();i++ )
       
   573         {
       
   574         domainKey = aDomainKeys[i]->AllocLC();
       
   575         iDomainKeys.AppendL( domainKey );
       
   576         CleanupStack::Pop();
       
   577         }
       
   578 
       
   579     // copy the rights issuer ID
       
   580     iRightsIssuerID.Copy( aRightsIssuerID );
       
   581 
       
   582     // copy the rights issuer URL
       
   583     iRightsIssuerURL = aRightsIssuerURL.AllocL();
       
   584     };
       
   585 
       
   586 // -----------------------------------------------------------------------------
       
   587 // CDRMRIContext::WriteInt64L
       
   588 // -----------------------------------------------------------------------------
       
   589 //
       
   590 void CDRMDomainContext::WriteInt64L( const TInt64& aWrite, RWriteStream& aStream ) const
       
   591     {
       
   592     TPtr8 output( reinterpret_cast<TUint8*>(const_cast<TInt64*>(&aWrite)),
       
   593                   sizeof(TInt64), sizeof(TInt64) );
       
   594 
       
   595     aStream.WriteL( output, sizeof(TInt64) );
       
   596     }
       
   597 
       
   598 // -----------------------------------------------------------------------------
       
   599 // CDRMRIContext::ReadInt64L
       
   600 // -----------------------------------------------------------------------------
       
   601 //
       
   602 void CDRMDomainContext::ReadInt64L( TInt64& aRead, RReadStream& aStream )
       
   603     {
       
   604     TPtr8 input( reinterpret_cast<TUint8*>(&aRead), 0, sizeof(TInt64) );
       
   605 
       
   606     aStream.ReadL( input, sizeof(TInt64) );
       
   607     };
       
   608 
       
   609 // End of File