omadrm/drmengine/dm/src/Oma1DcfCreator.cpp
changeset 23 493788a4a8a4
parent 0 95b198f216e5
child 84 b09186059647
equal deleted inserted replaced
5:79d62d1d7957 23:493788a4a8a4
    28 #include <s32mem.h>
    28 #include <s32mem.h>
    29 #include <apmstd.h>
    29 #include <apmstd.h>
    30 #include <symmetric.h>
    30 #include <symmetric.h>
    31 #include "Oma1DcfCreator.h"
    31 #include "Oma1DcfCreator.h"
    32 #include "DRMRightsClient.h"
    32 #include "DRMRightsClient.h"
    33 #include "DRMPermission.h"
    33 #include "DrmPermission.h"
    34 #include "DRMConstraint.h"
    34 #include "DrmConstraint.h"
    35 #include "DRMCommon.h"
    35 #include "DRMCommon.h"
    36 #include "DrmKeyStorage.h"
    36 #include "DrmKeyStorage.h"
    37 
    37 
    38 
    38 
    39 // EXTERNAL DATA STRUCTURES
    39 // EXTERNAL DATA STRUCTURES
    40 
    40 
    41 // EXTERNAL FUNCTION PROTOTYPES  
    41 // EXTERNAL FUNCTION PROTOTYPES
    42 
    42 
    43 // CONSTANTS
    43 // CONSTANTS
    44 
    44 
    45 static const TInt KMaxEncryptionSize = 2048;    
    45 static const TInt KMaxEncryptionSize = 2048;
    46 
    46 
    47 // MACROS
    47 // MACROS
    48 
    48 
    49 // LOCAL CONSTANTS AND MACROS
    49 // LOCAL CONSTANTS AND MACROS
    50 
    50 
    62 // -----------------------------------------------------------------------------
    62 // -----------------------------------------------------------------------------
    63 EXPORT_C COma1DcfCreator::COma1DcfCreator(void)
    63 EXPORT_C COma1DcfCreator::COma1DcfCreator(void)
    64     {
    64     {
    65     iEncryptionStream = NULL;
    65     iEncryptionStream = NULL;
    66     iEncryptionBuffer = NULL;
    66     iEncryptionBuffer = NULL;
    67     } 
    67     }
    68 
    68 
    69 // -----------------------------------------------------------------------------
    69 // -----------------------------------------------------------------------------
    70 // COma1DcfCreator::ConstructL
    70 // COma1DcfCreator::ConstructL
    71 // Symbian 2nd phase constructor can leave.
    71 // Symbian 2nd phase constructor can leave.
    72 // -----------------------------------------------------------------------------
    72 // -----------------------------------------------------------------------------
    81 // Two-phased constructor.
    81 // Two-phased constructor.
    82 // -----------------------------------------------------------------------------
    82 // -----------------------------------------------------------------------------
    83 EXPORT_C COma1DcfCreator* COma1DcfCreator::NewL()
    83 EXPORT_C COma1DcfCreator* COma1DcfCreator::NewL()
    84     {
    84     {
    85     COma1DcfCreator* self = new COma1DcfCreator();
    85     COma1DcfCreator* self = new COma1DcfCreator();
    86     
    86 
    87     User::LeaveIfNull(self);
    87     User::LeaveIfNull(self);
    88     
    88 
    89     CleanupStack::PushL(self);
    89     CleanupStack::PushL(self);
    90     self->ConstructL();
    90     self->ConstructL();
    91     CleanupStack::Pop();
    91     CleanupStack::Pop();
    92     
    92 
    93     return self;
    93     return self;
    94     }
    94     }
    95 
    95 
    96 
    96 
    97 // Destructor
    97 // Destructor
   103 
   103 
   104 // ================= MEMBER FUNCTIONS =======================
   104 // ================= MEMBER FUNCTIONS =======================
   105 
   105 
   106 // -----------------------------------------------------------------------------
   106 // -----------------------------------------------------------------------------
   107 // COma1DcfCreator::
   107 // COma1DcfCreator::
   108 // 
   108 //
   109 // -----------------------------------------------------------------------------
   109 // -----------------------------------------------------------------------------
   110 EXPORT_C void COma1DcfCreator::EncryptInitializeL(
   110 EXPORT_C void COma1DcfCreator::EncryptInitializeL(
   111     RWriteStream& anOutput,
   111     RWriteStream& anOutput,
   112     const TDesC8& aMIMEType, 
   112     const TDesC8& aMIMEType,
   113     CDRMRights* aRightsObject)
   113     CDRMRights* aRightsObject)
   114     {
   114     {
   115     const TUint KVersion = 1;
   115     const TUint KVersion = 1;
   116     CDRMPermission *rights = CDRMPermission::NewLC();
   116     CDRMPermission *rights = CDRMPermission::NewLC();
   117     TInt err = 0;
   117     TInt err = 0;
   128         uri.Set(aRightsObject->GetAsset().iUid->Des());
   128         uri.Set(aRightsObject->GetAsset().iUid->Des());
   129         err = iRdb.InitializeKey(uri);
   129         err = iRdb.InitializeKey(uri);
   130         if (err == KErrCANoRights)
   130         if (err == KErrCANoRights)
   131             {
   131             {
   132             key.SetLength(KDCFKeySize);
   132             key.SetLength(KDCFKeySize);
   133             iRdb.GetRandomDataL(key);    
   133             iRdb.GetRandomDataL(key);
   134             User::LeaveIfError(iRdb.AddRecord(key, aRightsObject->GetPermission(),
   134             User::LeaveIfError(iRdb.AddRecord(key, aRightsObject->GetPermission(),
   135                 uri, id));
   135                 uri, id));
   136             User::LeaveIfError(iRdb.InitializeKey(uri));
   136             User::LeaveIfError(iRdb.InitializeKey(uri));
   137             }
   137             }
   138         else
   138         else
   179     anOutput.WriteL(KLDFHeader);
   179     anOutput.WriteL(KLDFHeader);
   180     anOutput.WriteL(iIv);
   180     anOutput.WriteL(iIv);
   181     anOutput.CommitL();
   181     anOutput.CommitL();
   182     iBytesWritten += 3 + aMIMEType.Size() + 2 + KLDFHeader().Size() +
   182     iBytesWritten += 3 + aMIMEType.Size() + 2 + KLDFHeader().Size() +
   183         iIv.Size();
   183         iIv.Size();
   184     
   184 
   185     CleanupStack::PopAndDestroy(); // rights
   185     CleanupStack::PopAndDestroy(); // rights
   186     } 
   186     }
   187 
   187 
   188 // -----------------------------------------------------------------------------
   188 // -----------------------------------------------------------------------------
   189 // COma1DcfCreator::EncryptUpdateL
   189 // COma1DcfCreator::EncryptUpdateL
   190 // update the encryption
   190 // update the encryption
   191 // -----------------------------------------------------------------------------
   191 // -----------------------------------------------------------------------------
   194     TInt i;
   194     TInt i;
   195     TInt n;
   195     TInt n;
   196     TInt size;
   196     TInt size;
   197     TPtr8 ptr(iEncryptionBuffer->Des());
   197     TPtr8 ptr(iEncryptionBuffer->Des());
   198     TPtrC8 data;
   198     TPtrC8 data;
   199     
   199 
   200     data.Set(aMessageData);
   200     data.Set(aMessageData);
   201     if (iRemainder.Size() > 0 && iRemainder.Size() + data.Size() >= KDCFKeySize)
   201     if (iRemainder.Size() > 0 && iRemainder.Size() + data.Size() >= KDCFKeySize)
   202         {
   202         {
   203         ptr.Copy(iRemainder);
   203         ptr.Copy(iRemainder);
   204         n = Min(KDCFKeySize - iRemainder.Size(), data.Size());
   204         n = Min(KDCFKeySize - iRemainder.Size(), data.Size());
   208         iBytesWritten += ptr.Size();
   208         iBytesWritten += ptr.Size();
   209         iIv.Copy(ptr.Right(KDCFKeySize));
   209         iIv.Copy(ptr.Right(KDCFKeySize));
   210         data.Set(data.Right(data.Size() - n));
   210         data.Set(data.Right(data.Size() - n));
   211         iRemainder.SetLength(0);
   211         iRemainder.SetLength(0);
   212         }
   212         }
   213     
   213 
   214     size = data.Size();
   214     size = data.Size();
   215     for (i = 0; size > KDCFKeySize; i += KMaxEncryptionSize)
   215     for (i = 0; size > KDCFKeySize; i += KMaxEncryptionSize)
   216         {
   216         {
   217         n = Min(KMaxEncryptionSize, ((size / KDCFKeySize) * KDCFKeySize));
   217         n = Min(KMaxEncryptionSize, ((size / KDCFKeySize) * KDCFKeySize));
   218         ptr.Copy(data.Mid(i, n));
   218         ptr.Copy(data.Mid(i, n));
   231 // finalize the encryption
   231 // finalize the encryption
   232 // -----------------------------------------------------------------------------
   232 // -----------------------------------------------------------------------------
   233 EXPORT_C void COma1DcfCreator::EncryptFinalizeL()
   233 EXPORT_C void COma1DcfCreator::EncryptFinalizeL()
   234     {
   234     {
   235     TPtr8 ptr(iEncryptionBuffer->Des());
   235     TPtr8 ptr(iEncryptionBuffer->Des());
   236     
   236 
   237     ptr.Copy(iRemainder);
   237     ptr.Copy(iRemainder);
   238     iRdb.Encrypt(iIv, ptr, ETrue);
   238     iRdb.Encrypt(iIv, ptr, ETrue);
   239     iEncryptionStream->WriteL(ptr);
   239     iEncryptionStream->WriteL(ptr);
   240     iEncryptionStream->CommitL();
   240     iEncryptionStream->CommitL();
   241 
   241 
   247 // COma1DcfCreator::EncryptContent
   247 // COma1DcfCreator::EncryptContent
   248 // Encrypt plain content to a DCF. Replaces the input buffer with the plain
   248 // Encrypt plain content to a DCF. Replaces the input buffer with the plain
   249 // content with the DCF.
   249 // content with the DCF.
   250 // -----------------------------------------------------------------------------
   250 // -----------------------------------------------------------------------------
   251 EXPORT_C void COma1DcfCreator::EncryptContentL(
   251 EXPORT_C void COma1DcfCreator::EncryptContentL(
   252     HBufC8*& aContent, 
   252     HBufC8*& aContent,
   253     const TDesC8& aMIMEType, 
   253     const TDesC8& aMIMEType,
   254     CDRMRights* aRightsObject)
   254     CDRMRights* aRightsObject)
   255     {
   255     {
   256     RMemWriteStream output;
   256     RMemWriteStream output;
   257     TAny* buffer;
   257     TAny* buffer;
   258     TInt size;
   258     TInt size;
   259 
   259 
   260     size = aContent->Length() +
   260     size = aContent->Length() +
   261         aMIMEType.Length() +
   261         aMIMEType.Length() +
   262         KLDContentURI().Length() + 
   262         KLDContentURI().Length() +
   263         KLDFHeader().Length() + 256;
   263         KLDFHeader().Length() + 256;
   264     buffer = User::Alloc(size);
   264     buffer = User::Alloc(size);
   265     User::LeaveIfNull(buffer);
   265     User::LeaveIfNull(buffer);
   266     CleanupStack::PushL(buffer);
   266     CleanupStack::PushL(buffer);
   267     output.Open(buffer, size);
   267     output.Open(buffer, size);
   280 // -----------------------------------------------------------------------------
   280 // -----------------------------------------------------------------------------
   281 // COma1DcfCreator::EncryptFileL
   281 // COma1DcfCreator::EncryptFileL
   282 // Turn a plain content file into a DCF.
   282 // Turn a plain content file into a DCF.
   283 // -----------------------------------------------------------------------------
   283 // -----------------------------------------------------------------------------
   284 EXPORT_C void COma1DcfCreator::EncryptFileL(
   284 EXPORT_C void COma1DcfCreator::EncryptFileL(
   285     const TDesC& aFileName, 
   285     const TDesC& aFileName,
   286     const TDesC& aDCFFileName, 
   286     const TDesC& aDCFFileName,
   287     const TDesC8& aMIMEType, 
   287     const TDesC8& aMIMEType,
   288     CDRMRights* aRightsObject)
   288     CDRMRights* aRightsObject)
   289     {
   289     {
   290     TInt r = KErrNone;    
   290     TInt r = KErrNone;
   291     RFs fs;
   291     RFs fs;
   292     RFile input;
   292     RFile input;
   293     RFileWriteStream output;
   293     RFileWriteStream output;
   294     HBufC8* buf = HBufC8::NewLC(8000);
   294     HBufC8* buf = HBufC8::NewLC(8000);
   295     TPtr8 buffer(const_cast<TUint8*>(buf->Ptr()),0,8000);
   295     TPtr8 buffer(const_cast<TUint8*>(buf->Ptr()),0,8000);
   296    
   296 
   297 
   297 
   298     User::LeaveIfError(fs.Connect());
   298     User::LeaveIfError(fs.Connect());
   299     CleanupClosePushL(fs);
   299     CleanupClosePushL(fs);
   300 
   300 
   301     r = input.Open(fs, aFileName, EFileRead|EFileShareReadersOrWriters);
   301     r = input.Open(fs, aFileName, EFileRead|EFileShareReadersOrWriters);
   302     if(r == KErrInUse)
   302     if(r == KErrInUse)
   303         {
   303         {
   304         r = input.Open(fs, aFileName, EFileRead | EFileShareAny);
   304         r = input.Open(fs, aFileName, EFileRead | EFileShareAny);
   305                                                       
   305 
   306         if(r == KErrInUse)
   306         if(r == KErrInUse)
   307             {
   307             {
   308             User::LeaveIfError(input.Open(fs, aFileName, EFileRead | 
   308             User::LeaveIfError(input.Open(fs, aFileName, EFileRead |
   309                                                          EFileShareReadersOnly));
   309                                                          EFileShareReadersOnly));
   310             r = KErrNone;                                             
   310             r = KErrNone;
   311             }
   311             }
   312         }
   312         }
   313         
   313 
   314     User::LeaveIfError( r );        
   314     User::LeaveIfError( r );
   315     
   315 
   316     CleanupClosePushL(input);
   316     CleanupClosePushL(input);
   317     
   317 
   318     User::LeaveIfError(output.Replace(fs, aDCFFileName, EFileWrite));
   318     User::LeaveIfError(output.Replace(fs, aDCFFileName, EFileWrite));
   319     CleanupClosePushL(output);    
   319     CleanupClosePushL(output);
   320     
   320 
   321    
   321 
   322     EncryptInitializeL(output, aMIMEType, aRightsObject);
   322     EncryptInitializeL(output, aMIMEType, aRightsObject);
   323 
   323 
   324     
   324 
   325     do
   325     do
   326         {
   326         {
   327         input.Read(buffer, buffer.MaxSize());
   327         input.Read(buffer, buffer.MaxSize());
   328         if (buffer.Size() > 0)
   328         if (buffer.Size() > 0)
   329             {
   329             {
   331             EncryptUpdateL(buffer);
   331             EncryptUpdateL(buffer);
   332 
   332 
   333             }
   333             }
   334         }
   334         }
   335     while (buffer.Size() > 0);
   335     while (buffer.Size() > 0);
   336  
   336 
   337     EncryptFinalizeL();
   337     EncryptFinalizeL();
   338 
   338 
   339     CleanupStack::PopAndDestroy(4); // output, input, fs, buf
   339     CleanupStack::PopAndDestroy(4); // output, input, fs, buf
   340     }
   340     }
   341 
   341 
   342 // -----------------------------------------------------------------------------
   342 // -----------------------------------------------------------------------------
   343 // COma1DcfCreator::EncryptContentToFile
   343 // COma1DcfCreator::EncryptContentToFile
   344 // Encrypt plain content to a DCF and stores it in a file.
   344 // Encrypt plain content to a DCF and stores it in a file.
   345 // -----------------------------------------------------------------------------
   345 // -----------------------------------------------------------------------------
   346 EXPORT_C void COma1DcfCreator::EncryptContentToFileL(
   346 EXPORT_C void COma1DcfCreator::EncryptContentToFileL(
   347     const TDesC8& aContent, 
   347     const TDesC8& aContent,
   348     const TDesC& aFileName, 
   348     const TDesC& aFileName,
   349     const TDesC8& aMIMEType, 
   349     const TDesC8& aMIMEType,
   350     CDRMRights* aRightsObject)
   350     CDRMRights* aRightsObject)
   351     {
   351     {
   352     RFs fs;
   352     RFs fs;
   353     RFileWriteStream output;
   353     RFileWriteStream output;
   354 
   354