commondrm/drmencryptor/src/DrmEncryptor.cpp
branchRCL_3
changeset 26 1221b68b8a5f
parent 25 50c53e893c3f
child 27 1481bf457703
equal deleted inserted replaced
25:50c53e893c3f 26:1221b68b8a5f
     1 /*
       
     2 * Copyright (c) 2003-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 Encryption tool for DRM5
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <e32base.h>
       
    22 #include <barsc.h>
       
    23 #include <barsread.h>
       
    24 #include <apmstd.h>
       
    25 #include <e32test.h>
       
    26 #include <s32strm.h>
       
    27 #include <s32file.h>
       
    28 #include <bacline.h>
       
    29 #include <e32math.h>
       
    30 
       
    31 
       
    32 #ifdef RD_MULTIPLE_DRIVE
       
    33 #include <driveinfo.h>
       
    34 #endif
       
    35 
       
    36 #include <DRMRights.h>
       
    37 #include <DcfCommon.h>
       
    38 #include <DRMMessageParser.h>
       
    39 #include <Oma1DcfCreator.h>
       
    40 #include <DRMRightsClient.h>
       
    41 #include "DRMClockClient.h"
       
    42 
       
    43 #include <DcfRep.h>
       
    44 #include <DcfEntry.h>
       
    45 
       
    46 #include <DRMEncryptor.rsg>
       
    47 
       
    48 #include <avkon.hrh>
       
    49 #include <aknnotewrappers.h>
       
    50 
       
    51 #include <wmdrmagent.h>
       
    52 
       
    53 // EXTERNAL DATA STRUCTURES
       
    54 
       
    55 // EXTERNAL FUNCTION PROTOTYPES
       
    56 
       
    57 // CONSTANTS
       
    58 
       
    59 _LIT( KWmdrmBd, "c:\\private\\10281e17\\[10282F1B]hds.db" );
       
    60 _LIT( KWmdrmBdBackup, "e:\\[10282F1B]hds.db" );
       
    61 
       
    62 // MACROS
       
    63 
       
    64 // LOCAL CONSTANTS AND MACROS
       
    65 
       
    66 // MODULE DATA STRUCTURES
       
    67 
       
    68 // LOCAL FUNCTION PROTOTYPES
       
    69 
       
    70 // ==================== LOCAL FUNCTIONS ====================
       
    71 
       
    72 LOCAL_C void ReadFileL(HBufC8*& aContent, const TDesC& aName, RFs& aFs)
       
    73     {
       
    74     TInt size = 0;
       
    75     RFile file;
       
    76     User::LeaveIfError(file.Open(aFs, aName, EFileRead));
       
    77     User::LeaveIfError(file.Size(size));
       
    78     aContent = HBufC8::NewLC(size);
       
    79     TPtr8 ptr(aContent->Des());
       
    80     User::LeaveIfError(file.Read(ptr, size));
       
    81     CleanupStack::Pop(); //aContent
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------
       
    85 // UpdateDCFRepositoryL()
       
    86 // Update saved file to DCFRepository
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 LOCAL_C void UpdateDCFRepositoryL( const TDesC& aFileName)
       
    90     {
       
    91     CDcfEntry* dcf( NULL );
       
    92     CDcfRep* dcfRep( NULL );
       
    93 
       
    94     dcf = CDcfEntry::NewL();
       
    95     CleanupStack::PushL( dcf );
       
    96 
       
    97     dcfRep = CDcfRep::NewL();
       
    98     CleanupStack::PushL( dcfRep );
       
    99 
       
   100     dcf->SetLocationL( aFileName, 0 );
       
   101     dcfRep->UpdateL( dcf );
       
   102 
       
   103     CleanupStack::PopAndDestroy(2); // dcf, dcfRep
       
   104     }
       
   105 
       
   106 
       
   107 
       
   108 
       
   109 // ==================== TEST FUNCTIONS =====================
       
   110 
       
   111 const TInt KBufferSize = 20000;
       
   112 
       
   113 void ProcessMessageL(const TDesC& aFile, const TDesC& aOutput)
       
   114     {
       
   115     CDRMMessageParser* c = NULL;
       
   116     HBufC8* d = NULL;
       
   117     RFs fs;
       
   118     TPtr8 inRead(NULL, 0);
       
   119     TInt error = 1;
       
   120     __UHEAP_MARK;
       
   121 
       
   122     User::LeaveIfError(fs.Connect());
       
   123     CleanupClosePushL(fs);
       
   124     c = CDRMMessageParser::NewL();
       
   125     CleanupStack::PushL(c);
       
   126 
       
   127     d = HBufC8::NewLC( KBufferSize );
       
   128 
       
   129     RFile input;
       
   130     User::LeaveIfError(input.Open( fs, aFile, EFileRead ));
       
   131     CleanupClosePushL( input );
       
   132 
       
   133     RFileWriteStream output;
       
   134     output.Replace( fs, aOutput, EFileWrite );
       
   135     CleanupClosePushL( output );
       
   136 
       
   137     c->InitializeMessageParserL( output );
       
   138 
       
   139     while( error )
       
   140         {
       
   141         inRead.Set( const_cast<TUint8*>(d->Ptr()),0,KBufferSize);
       
   142         error = input.Read( inRead );
       
   143 
       
   144         if( error )
       
   145             {
       
   146             c->FinalizeMessageParserL();
       
   147 
       
   148             User::Leave( error );
       
   149             }
       
   150         else
       
   151             {
       
   152             error = inRead.Length();
       
   153             }
       
   154 
       
   155         if( error )
       
   156             {
       
   157             c->ProcessMessageDataL(inRead);
       
   158             }
       
   159         }
       
   160 
       
   161     c->FinalizeMessageParserL();
       
   162 
       
   163 
       
   164     CleanupStack::PopAndDestroy( 5 ); // fs, c, d, input, output
       
   165     UpdateDCFRepositoryL( aOutput );
       
   166     __UHEAP_MARKEND;
       
   167     }
       
   168 
       
   169 void ProcessRightsL(const TDesC& aFile)
       
   170     {
       
   171     CDRMMessageParser* c = NULL;
       
   172     HBufC8* d = NULL;
       
   173     RFs fs;
       
   174     RPointerArray<CDRMRights> rights;
       
   175 
       
   176     User::LeaveIfError(fs.Connect());
       
   177     c = CDRMMessageParser::NewL();
       
   178     ReadFileL(d, aFile, fs);
       
   179     c->ProcessRightsObject(*d, rights);
       
   180     rights.ResetAndDestroy();
       
   181     delete d;
       
   182     delete c;
       
   183     fs.Close();
       
   184     }
       
   185 
       
   186 void EncryptFileL(const TDesC& aFile, TDesC& aOutput, TInt aMultiplier)
       
   187     {
       
   188     COma1DcfCreator* c = NULL;
       
   189     CDRMRights* rights = NULL;
       
   190     TBuf8<64> mime;
       
   191     RFs fs;
       
   192     TFileName aDcfFile;
       
   193     TInt aOriginalFileNameLength(aOutput.Length() - 4);
       
   194 
       
   195     User::LeaveIfError(fs.Connect());
       
   196     if (aFile.Right(3).CompareF(_L("amr")) == 0) //AMR
       
   197         {
       
   198         mime.Copy(_L8("audio/amr"));
       
   199         }
       
   200     else if (aFile.Right(3).CompareF(_L("awb")) == 0) //AMR-AWB
       
   201         {
       
   202         mime.Copy(_L8("audio/amr-wb"));
       
   203         }
       
   204     else if (aFile.Right(3).CompareF(_L("mp3")) == 0) //MP3
       
   205         {
       
   206         mime.Copy(_L8("audio/mpeg"));
       
   207         }
       
   208     else if (aFile.Right(3).CompareF(_L("mp4")) == 0) //MP4
       
   209         {
       
   210         mime.Copy(_L8("audio/mp4"));
       
   211         }
       
   212     else if (aFile.Right(3).CompareF(_L("m4a")) == 0) //M4A
       
   213         {
       
   214         mime.Copy(_L8("audio/mp4"));
       
   215         }
       
   216     else if (aFile.Right(3).CompareF(_L("3gp")) == 0) //3GPP
       
   217         {
       
   218         mime.Copy(_L8("audio/3gpp"));
       
   219         }
       
   220     else if (aFile.Right(3).CompareF(_L("3g2")) == 0) //3GPP2
       
   221         {
       
   222         mime.Copy(_L8("audio/3gpp2"));
       
   223         }
       
   224     else if (aFile.Right(3).CompareF(_L("aac")) == 0) //AAC
       
   225         {
       
   226         mime.Copy(_L8("audio/aac"));
       
   227         }
       
   228     else if (aFile.Right(3).CompareF(_L("mid")) == 0) //MIDI
       
   229         {
       
   230         mime.Copy(_L8("audio/midi"));
       
   231         }
       
   232     else if (aFile.Right(5).CompareF(_L(".spmid")) == 0) //SP-MIDI
       
   233         {
       
   234         mime.Copy(_L8("audio/sp-midi"));
       
   235         }
       
   236     else if (aFile.Right(3).CompareF(_L("rmf")) == 0) //RMF
       
   237         {
       
   238         mime.Copy(_L8("audio/rmf"));
       
   239         }
       
   240     else if (aFile.Right(4).CompareF(_L("mxmf")) == 0) //Mobile-XMF
       
   241         {
       
   242         mime.Copy(_L8("audio/mobile-xmf"));
       
   243         }
       
   244     else if (aFile.Right(3).CompareF(_L("wav")) == 0) //WAV
       
   245         {
       
   246         mime.Copy(_L8("audio/x-wav"));
       
   247         }
       
   248     else if (aFile.Right(3).CompareF(_L("gif")) == 0) // GIF
       
   249         {
       
   250         mime.Copy(_L8("image/gif"));
       
   251         }
       
   252     else if (aFile.Right(3).CompareF(_L("jpg")) == 0) // JPEG
       
   253         {
       
   254         mime.Copy(_L8("image/jpeg"));
       
   255         }
       
   256     else if (aFile.Right(3).CompareF(_L("txt")) == 0) // text
       
   257         {
       
   258         mime.Copy(_L8("text/plain"));
       
   259         }
       
   260     else if (aFile.Right(3).CompareF(_L("pip")) == 0) // PIP
       
   261         {
       
   262         mime.Copy(_L8("application/x-pip"));
       
   263         }
       
   264 
       
   265     aDcfFile.Append(aOutput);
       
   266 
       
   267     for(TInt i = 0; i < aMultiplier ; ++i)
       
   268         {
       
   269         aDcfFile.Delete(aOriginalFileNameLength, aDcfFile.Length());
       
   270         aDcfFile.Append(_L("-"));
       
   271         aDcfFile.AppendNum(i);
       
   272         aDcfFile.Append(_L(".dcf"));
       
   273         c = COma1DcfCreator::NewL();
       
   274         CleanupStack::PushL(c);
       
   275         fs.Delete(aOutput);
       
   276         c->EncryptFileL(aFile, aDcfFile, mime, rights);
       
   277         UpdateDCFRepositoryL( aDcfFile );
       
   278         delete rights;
       
   279         CleanupStack::PopAndDestroy(); // c
       
   280         }
       
   281     fs.Close();
       
   282 
       
   283     }
       
   284 
       
   285 TUint EncryptL(TUint& aEncryptedCount, TUint& aRightsCount, TUint& aMessagesProcessed)
       
   286     {
       
   287     TInt i;
       
   288     CDir* files;
       
   289     TFileName input;
       
   290     TFileName output;
       
   291     TUint inputNameSize = 0;
       
   292     TUint outputNameSize = 0;
       
   293     RFs fs;
       
   294     User::LeaveIfError(fs.Connect());
       
   295     TInt aMultiplier(1);
       
   296 
       
   297 
       
   298 #ifdef __WINS__
       
   299     input.Append(_L("c:\\data\\DRM\\"));
       
   300     output.Append(_L("c:\\data\\Others\\"));
       
   301 #else
       
   302 #ifndef RD_MULTIPLE_DRIVE
       
   303 
       
   304     input.Append(_L("e:\\DRM\\"));
       
   305     output.Append(_L("e:\\Others\\"));
       
   306 
       
   307 #else //RD_MULTIPLE_DRIVE
       
   308 
       
   309     TInt driveNumber( -1 );
       
   310     TChar driveLetter;
       
   311     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultMassStorage, driveNumber );
       
   312     fs.DriveToChar( driveNumber, driveLetter );
       
   313 
       
   314     _LIT( KdrmDir, "%c:\\DRM\\" );
       
   315     input.Format( KdrmDir, (TUint)driveLetter );
       
   316 
       
   317     _LIT( KothersDir, "%c:\\Others\\" );
       
   318     output.Format( KothersDir, (TUint)driveLetter );
       
   319 
       
   320 #endif
       
   321 #endif
       
   322 
       
   323     inputNameSize = input.Length();
       
   324     outputNameSize = output.Length();
       
   325 
       
   326 
       
   327     fs.MkDir(input);
       
   328     fs.MkDir(output);
       
   329 
       
   330     fs.GetDir(input, KEntryAttNormal, ESortNone, files);
       
   331     for (i = 0; i < files->Count(); i++)
       
   332         {
       
   333         input.Append((*files)[i].iName);
       
   334 
       
   335         output.Append((*files)[i].iName);
       
   336 
       
   337         if (input.Right(2).CompareF(_L("dm")) == 0)
       
   338             {
       
   339 
       
   340             for(TInt ii = 0; ii < aMultiplier ; ++ii)
       
   341                 {
       
   342                 output.Delete(outputNameSize +(*files)[i].iName.Length() , output.Length()-1);
       
   343                 output.Append(_L("-"));
       
   344                 output.AppendNum(ii);
       
   345                 output.Append(_L(".dcf"));
       
   346                 ProcessMessageL(input, output);
       
   347                 ++aMessagesProcessed;
       
   348                 }
       
   349             }
       
   350         else if (input.Right(3).CompareF(_L("oro")) == 0 ||
       
   351                 input.Right(3).CompareF(_L("drc")) == 0 ||
       
   352                 input.Right(2).CompareF(_L("ro")) == 0 ||
       
   353                 input.Right(2).CompareF(_L("dr")) == 0 )
       
   354             {
       
   355             for (TInt iii = 0; iii < aMultiplier; ++iii)
       
   356                 {
       
   357                 ProcessRightsL(input);
       
   358                 ++aRightsCount;
       
   359                 }
       
   360             }
       
   361         else if (input.Right(3).CompareF(_L("dcf")) != 0)
       
   362             {
       
   363             output.Append(_L(".dcf"));
       
   364             EncryptFileL(input, output, aMultiplier);
       
   365             ++aEncryptedCount;
       
   366             }
       
   367 
       
   368         //restore paths
       
   369         input.Delete(inputNameSize, input.Length()-1);
       
   370         output.Delete(outputNameSize, output.Length()-1);
       
   371         }
       
   372 
       
   373     fs.Close();
       
   374 
       
   375     TRequestStatus status;
       
   376     CDcfRep* rep = CDcfRep::NewL();
       
   377     CleanupStack::PushL(rep);
       
   378     rep->RefreshDcf(status);
       
   379     User::WaitForRequest( status );
       
   380     CleanupStack::PopAndDestroy( rep );
       
   381 
       
   382     delete files;
       
   383 
       
   384     return (aEncryptedCount*aMultiplier + aRightsCount + aMessagesProcessed);
       
   385     }
       
   386 
       
   387 void DeleteRdbL()
       
   388     {
       
   389     RDRMRightsClient client;
       
   390 
       
   391     User::LeaveIfError(client.Connect());
       
   392     client.DeleteAll();
       
   393     client.Close();
       
   394     }
       
   395 
       
   396 
       
   397 
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // GetCafDataL
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 ContentAccess::CManager* GetCafDataL( TAgent& aAgent )
       
   404     {
       
   405     TPtr8 ptr(NULL, 0, 0);
       
   406     RArray<TAgent> agents;
       
   407     TRequestStatus status;
       
   408     TInt i;
       
   409 
       
   410     CleanupClosePushL( agents );
       
   411     CManager* manager = CManager::NewLC();
       
   412 
       
   413     manager->ListAgentsL( agents );
       
   414 
       
   415     for (i = 0; i < agents.Count(); i++)
       
   416         {
       
   417         if (agents[i].Name().Compare(KWmDrmAgentName) == 0)
       
   418             {
       
   419             aAgent = agents[i];
       
   420             break;
       
   421             }
       
   422         }
       
   423     CleanupStack::Pop( manager );
       
   424     CleanupStack::PopAndDestroy(); // agents
       
   425     return manager;
       
   426     }
       
   427 
       
   428 
       
   429 // -----------------------------------------------------------------------------
       
   430 // DeleteWmDrmRdbL
       
   431 //-----------------------------------------------------------------------------
       
   432 //
       
   433 
       
   434 void DeleteWmDrmRdbL()
       
   435     {
       
   436     // get the data part
       
   437     ContentAccess::CManager* manager = NULL;
       
   438     ContentAccess::TAgent agent;
       
   439     TPtr8 ptr(NULL, 0, 0);
       
   440     TPtrC8 ptr2;
       
   441 
       
   442     // Find the caf agent and create manager
       
   443     manager = GetCafDataL( agent );
       
   444     CleanupStack::PushL( manager );
       
   445 
       
   446     User::LeaveIfError(
       
   447         manager->AgentSpecificCommand( agent,
       
   448                                        (TInt)DRM::EWmDrmDeleteRights,
       
   449                                        ptr2,
       
   450                                        ptr) );
       
   451     CleanupStack::PopAndDestroy( manager );
       
   452     }
       
   453 
       
   454 
       
   455 
       
   456 void GetDrmClockL()
       
   457     {
       
   458     RDRMClockClient client;
       
   459 
       
   460     TTime drmTime;
       
   461     TDateTime date;
       
   462     TInt aTimeZone;
       
   463     DRMClock::ESecurityLevel secLevel = DRMClock::KInsecure;
       
   464     TBuf< 80 > buf;
       
   465 
       
   466 
       
   467 
       
   468     User::LeaveIfError( client.Connect() );
       
   469 
       
   470     client.GetSecureTime(drmTime, aTimeZone, secLevel);
       
   471 
       
   472     client.Close();
       
   473 
       
   474     date = drmTime.DateTime();
       
   475 
       
   476     if(secLevel == DRMClock::KSecure)
       
   477         {
       
   478         _LIT(KFormatTxt,"DRMClock Time:\n%d/%d/%d\n%d:%d:%d\nNitz available");
       
   479         buf.Format( KFormatTxt,
       
   480                 date.Day()+1,
       
   481                 TInt(date.Month()+1),
       
   482                 date.Year(),
       
   483                 date.Hour(),
       
   484                 date.Minute(),
       
   485                 date.Second());
       
   486         }
       
   487     else
       
   488         {
       
   489         _LIT(KFormatTxt,"DRMClock Time:\n%d/%d/%d\n%d:%d:%d\nNitz unavailable");
       
   490         buf.Format( KFormatTxt,
       
   491                 date.Day()+1,
       
   492                 TInt(date.Month()+1),
       
   493                 date.Year(),
       
   494                 date.Hour(),
       
   495                 date.Minute(),
       
   496                 date.Second());
       
   497         }
       
   498 
       
   499     CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
       
   500     informationNote->ExecuteLD(buf);
       
   501 
       
   502 
       
   503     }
       
   504 
       
   505 void SetDrmClockL()
       
   506     {
       
   507     RDRMClockClient client;
       
   508 
       
   509     TTime drmTime (_L("20000111:200600.000000"));
       
   510     TTime aDate (_L("20040000:"));
       
   511     TDateTime date;
       
   512     TInt aTimeZone;
       
   513     DRMClock::ESecurityLevel secLevel = DRMClock::KInsecure;
       
   514 
       
   515     User::LeaveIfError(client.Connect());
       
   516     CleanupClosePushL(client);
       
   517 
       
   518     client.GetSecureTime(drmTime, aTimeZone, secLevel);
       
   519 
       
   520     aDate = drmTime;
       
   521 
       
   522     CAknMultiLineDataQueryDialog* dlg = CAknMultiLineDataQueryDialog::NewL(aDate, drmTime);
       
   523     if ( dlg->ExecuteLD( R_DRM_TIME_QUERY ) )
       
   524         {
       
   525         TTime aTime = aDate.Int64() + drmTime.Int64();
       
   526         client.UpdateSecureTime(aTime, aTimeZone);
       
   527         CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
       
   528         informationNote->ExecuteLD(_L("DRM time changed"));
       
   529         }
       
   530     else
       
   531         {
       
   532         //User pressed cancel on confirmation screen
       
   533         CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
       
   534         informationNote->ExecuteLD(_L("DRM time not changed"));
       
   535         }
       
   536     CleanupStack::PopAndDestroy();
       
   537     }
       
   538 
       
   539 void BackupWmDrmDbL()
       
   540     {
       
   541     RProcess process;
       
   542     TFullName name;
       
   543     TFindProcess wmDrmServerFinder( _L( "*wmdrmserver*" ) );
       
   544     if ( wmDrmServerFinder.Next( name ) == KErrNone && process.Open( name ) == KErrNone )
       
   545         {
       
   546         process.Kill( -1 );
       
   547         process.Close();
       
   548         }
       
   549     RFs fs;
       
   550     User::LeaveIfError( fs.Connect() );
       
   551     CleanupClosePushL( fs );
       
   552     CFileMan* fileMan = CFileMan::NewL( fs );
       
   553     CleanupStack::PushL( fileMan );
       
   554     User::LeaveIfError( fileMan->Copy( KWmdrmBd, KWmdrmBdBackup, CFileMan::EOverWrite ) );
       
   555     CleanupStack::PopAndDestroy( 2, &fs ); //fs, fileMan
       
   556     }
       
   557 
       
   558 void RestoreWmDrmDbL()
       
   559     {
       
   560     RProcess process;
       
   561     TFullName name;
       
   562     TFindProcess wmDrmServerFinder( _L( "*wmdrmserver*" ) );
       
   563     if ( wmDrmServerFinder.Next( name ) == KErrNone && process.Open( name ) == KErrNone )
       
   564         {
       
   565         process.Kill( -1 );
       
   566         process.Close();
       
   567         }
       
   568     RFs fs;
       
   569     User::LeaveIfError( fs.Connect() );
       
   570     CleanupClosePushL( fs );
       
   571     CFileMan* fileMan = CFileMan::NewL( fs );
       
   572     CleanupStack::PushL( fileMan );
       
   573     User::LeaveIfError( fileMan->Copy( KWmdrmBdBackup, KWmdrmBd, CFileMan::EOverWrite ) );
       
   574     CleanupStack::PopAndDestroy( 2, &fs ); //fs, fileMan
       
   575     }
       
   576 
       
   577 // End of File