omadrm/drmengine/ro/src/DrmProtectedRoParser.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2005 - 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:  Implementation of a parser for protected OMA DRM rights
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32math.h>
       
    22 #include <apmstd.h>
       
    23 #include <utf.h>
       
    24 #include <stringpool.h>
       
    25 #include <xml/ParserFeature.h>
       
    26 
       
    27 #ifdef _DEBUG
       
    28 #include <flogger.h>
       
    29 #endif // _DEBUG
       
    30 #include "base64.h"
       
    31 #include "DcfCommon.h"
       
    32 #include "DrmRightsParser.h"
       
    33 #include "DrmProtectedRoParser.h"
       
    34 #include "DrmRightsClient.h"
       
    35 #include "hash.h"
       
    36 #include "DRMCommon.h"
       
    37 #include "OmaCrypto.h"
       
    38 #include "CmlaCrypto.h"
       
    39 #include "DrmPermission.h"
       
    40 #include "RoapMessage.h"
       
    41 #include "DcfRep.h"
       
    42 #include "DcfEntry.h"
       
    43 #include "Oma2Dcf.h"
       
    44 
       
    45 using namespace Roap;
       
    46 using namespace Xml;
       
    47 
       
    48 // MODULE DATA STRUCTURES
       
    49 NONSHARABLE_CLASS(CDrmProtectedRoParser::CParsedProtectedRo): public CBase
       
    50     {
       
    51 public:
       
    52     static CParsedProtectedRo* NewL();
       
    53     ~CParsedProtectedRo();
       
    54 
       
    55 protected:
       
    56     CParsedProtectedRo();
       
    57 
       
    58 public:
       
    59     TKeyTransportScheme iTransportScheme;
       
    60     HBufC8* iKey;
       
    61     TBool iDomainRo;
       
    62     HBufC8* iId;
       
    63     HBufC8* iRiUrl;
       
    64     HBufC8* iDomainId;
       
    65     HBufC8* iRightsIssuerId;
       
    66     TTime iTimeStamp;
       
    67     HBufC8* iMac;
       
    68     HBufC8* iMacDigest;
       
    69     };
       
    70 
       
    71 enum TElementEnum
       
    72     {
       
    73     EProtectedRo = 0,
       
    74     ERo,
       
    75     EDomainRo,
       
    76     EId,
       
    77     EStateful,
       
    78     EVersion,
       
    79     ERiId,
       
    80     EKeyIdentifier,
       
    81     EHash,
       
    82     ETimeStamp,
       
    83     EEncKey,
       
    84     EEncryptionMethod,
       
    85     EKeyInfo,
       
    86     EX509SPKIHash,
       
    87     ECipherData,
       
    88     ECipherValue,
       
    89     EMac,
       
    90     ESignedInfo,
       
    91     ECanonicalizationMethod,
       
    92     ESignatureMethod,
       
    93     EReference,
       
    94     EDigestMethod,
       
    95     EDigestValue,
       
    96     ESignatureValue,
       
    97     ERetreivalMethod,
       
    98     ERoapDomainId,
       
    99     EDomainIdentifier,
       
   100     ERoapDomainIdentifier,
       
   101     ERoResponse,
       
   102     ELast,
       
   103     };
       
   104 
       
   105 enum TParserStackState
       
   106     {
       
   107     ERoState,
       
   108     ECipherValueState,
       
   109     EDomainIdentifierState,
       
   110     EEncryptionMethodState,
       
   111     ERightsIssuerIdState,
       
   112     ETimeStampState,
       
   113     EMacValueState,
       
   114     EMacDigestValueState,
       
   115     EUnknownState = -1,
       
   116     };
       
   117 
       
   118 struct TElements
       
   119     {
       
   120     const TText8* iString;
       
   121     TInt iNumber;
       
   122     };
       
   123 
       
   124 // MACROS
       
   125 #define ELEMENT_COUNT(x) static_cast<TInt>((sizeof(x) / sizeof (x[0])))
       
   126 
       
   127 #ifdef _DEBUG
       
   128 _LIT(KParseLogDir, "DRM");
       
   129 _LIT(KParseLogFile, "Parse.log");
       
   130 #define DEBUGLOG( a ) { _LIT(KDebugString, a); RFileLogger::Write( KParseLogDir(), KParseLogFile(), EFileLoggingModeAppend, KDebugString() ); }
       
   131 #define DEBUGLOGHEX( ptr, len ) RFileLogger::HexDump( KParseLogDir(), KParseLogFile(), EFileLoggingModeAppend, _S(""), _S(""), ptr, len );
       
   132 #else
       
   133 #define DEBUGLOG( a )
       
   134 #define DEBUGLOGHEX( ptr, len )
       
   135 #endif // _DEBUG
       
   136 
       
   137 // LOCAL CONSTANTS AND MACROS
       
   138 const TInt KParserChunkSize = 512;
       
   139 const TInt KMaxElementNesting = 24;
       
   140 const TInt KMaxElementLen = 80;
       
   141 
       
   142 _LIT8(KXmlParserMimeType, "text/xml");
       
   143 _LIT8(KIdAttr, "id");
       
   144 _LIT8(KDomainRoAttr, "domainRO");
       
   145 _LIT8(KRiUrlAttr, "riURL");
       
   146 _LIT8(KAlgorithmAttr, "Algorithm");
       
   147 _LIT8(KProtectedRoElement, "protectedRO");
       
   148 _LIT8(KRoapPrefix, "roap:");
       
   149 _LIT8(KStartElementStart, "<");
       
   150 _LIT8(KEndElementStart, "</");
       
   151 _LIT8(KRoElement, "roap:ro");
       
   152 _LIT8( KElementEnd, ">" );
       
   153 _LIT8(KQualifiedProtectedRo, "roap:protectedRO");
       
   154 _LIT8(KSignedInfoElement, "ds:SignedInfo");
       
   155 _LIT8(KRoapXmlNs, " xmlns:roap=\"urn:oma:bac:dldrm:roap-1.0\"");
       
   156 _LIT8(KRoapXmlNsUri, "urn:oma:bac:dldrm:roap-1.0");
       
   157 _LIT8(KMacStart, "<mac");
       
   158 
       
   159 static const TElements KElements[] =
       
   160     {
       
   161         {
       
   162         _S8("roResponse"), ERoResponse
       
   163         },
       
   164         {
       
   165         _S8("protectedRo"), EProtectedRo
       
   166         },
       
   167         {
       
   168         _S8("ro"), ERo
       
   169         },
       
   170         {
       
   171         _S8("domainRo"), EDomainRo
       
   172         },
       
   173         {
       
   174         _S8("id"), EId
       
   175         },
       
   176         {
       
   177         _S8("stateful"), EStateful
       
   178         },
       
   179         {
       
   180         _S8("version"), EVersion
       
   181         },
       
   182         {
       
   183         _S8("riID"), ERiId
       
   184         },
       
   185         {
       
   186         _S8("keyIdentifier"), EKeyIdentifier
       
   187         },
       
   188         {
       
   189         _S8("hash"), EHash
       
   190         },
       
   191         {
       
   192         _S8("timeStamp"), ETimeStamp
       
   193         },
       
   194         {
       
   195         _S8("encKey"), EEncKey
       
   196         },
       
   197         {
       
   198         _S8("EncryptionMethod"), EEncryptionMethod
       
   199         },
       
   200         {
       
   201         _S8("KeyInfo"), EKeyInfo
       
   202         },
       
   203         {
       
   204         _S8("roap:X509SPKIHash"), EX509SPKIHash
       
   205         },
       
   206         {
       
   207         _S8("CipherData"), ECipherData
       
   208         },
       
   209         {
       
   210         _S8("CipherValue"), ECipherValue
       
   211         },
       
   212         {
       
   213         _S8("mac"), EMac
       
   214         },
       
   215         {
       
   216         _S8("SignedInfo"), ESignedInfo
       
   217         },
       
   218         {
       
   219         _S8("CanonicalizationMethod"), ECanonicalizationMethod
       
   220         },
       
   221         {
       
   222         _S8("SignatureMethod"), ESignatureMethod
       
   223         },
       
   224         {
       
   225         _S8("Reference"), EReference
       
   226         },
       
   227         {
       
   228         _S8("DigestMethod"), EDigestMethod
       
   229         },
       
   230         {
       
   231         _S8("DigestValue"), EDigestValue
       
   232         },
       
   233         {
       
   234         _S8("SignatureValue"), ESignatureValue
       
   235         },
       
   236         {
       
   237         _S8("RetreivalMethod"), ERetreivalMethod
       
   238         },
       
   239         {
       
   240         _S8("domainID"), ERoapDomainId
       
   241         },
       
   242         {
       
   243         _S8("DomainIdentifier"), EDomainIdentifier
       
   244         },
       
   245         {
       
   246         _S8("roap:DomainIdentifier"), ERoapDomainIdentifier
       
   247         }
       
   248     };
       
   249 
       
   250 struct TStackState
       
   251     {
       
   252     TParserStackState iState;
       
   253     TElementEnum iStack[KMaxElementNesting];
       
   254     };
       
   255 
       
   256 static const TStackState KParserStackStates[] =
       
   257     {
       
   258         {
       
   259         ERightsIssuerIdState,
       
   260             {
       
   261             EHash, EKeyIdentifier, ERiId, ERo, ELast
       
   262             }
       
   263         },
       
   264         {
       
   265         EDomainIdentifierState,
       
   266             {
       
   267             ERoapDomainId, EKeyInfo, EEncKey, ERo, ELast
       
   268             }
       
   269         },
       
   270         {
       
   271         EDomainIdentifierState,
       
   272             {
       
   273             EDomainIdentifier, EKeyInfo, EEncKey, ERo, ELast
       
   274             }
       
   275         },
       
   276         {
       
   277         EDomainIdentifierState,
       
   278             {
       
   279             ERoapDomainIdentifier, EKeyInfo, EEncKey, ERo, ELast
       
   280             }
       
   281         },
       
   282         {
       
   283         ECipherValueState,
       
   284             {
       
   285             ECipherValue, ECipherData, EEncKey, ERo, ELast
       
   286             }
       
   287         },
       
   288         {
       
   289         EEncryptionMethodState,
       
   290             {
       
   291             EEncryptionMethod, EEncKey, ERo, ELast
       
   292             }
       
   293         },
       
   294         {
       
   295         ETimeStampState,
       
   296             {
       
   297             ETimeStamp, ERo, ELast
       
   298             }
       
   299         },
       
   300         {
       
   301         EMacValueState,
       
   302             {
       
   303             ESignatureValue, EMac, ELast
       
   304             }
       
   305         },
       
   306         {
       
   307         EMacDigestValueState,
       
   308             {
       
   309             EDigestValue, EReference, ESignedInfo, EMac, ELast
       
   310             }
       
   311         },
       
   312         {
       
   313         ERoState,
       
   314             {
       
   315             ERo, ELast
       
   316             }
       
   317         }
       
   318     };
       
   319 
       
   320 // LOCAL FUNCTION PROTOTYPES
       
   321 TTime Iso8601ToTime(TDesC8& aTimeString);
       
   322 
       
   323 // ============================= LOCAL FUNCTIONS ===============================
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // Iso8601ToTime
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TTime Iso8601ToTime(TDesC8& aTimeString)
       
   330     {
       
   331     TLex8 lex;
       
   332     TInt year = 0;
       
   333     TInt month = 0;
       
   334     TInt day = 0;
       
   335     TInt hour = 0;
       
   336     TInt minute = 0;
       
   337     TInt second = 0;
       
   338     TTime r = Time::NullTTime();
       
   339     TLocale l;
       
   340     TTimeIntervalSeconds offset(l.UniversalTimeOffset());
       
   341 
       
   342     if (aTimeString.Length() > 0)
       
   343         {
       
   344         lex = aTimeString;
       
   345         lex.Val(year);
       
   346         lex.Inc();
       
   347         lex.Val(month);
       
   348         lex.Inc();
       
   349         lex.Val(day);
       
   350         lex.Inc();
       
   351         lex.Val(hour);
       
   352         lex.Inc();
       
   353         lex.Val(minute);
       
   354         lex.Inc();
       
   355         lex.Val(second);
       
   356         r = TTime(TDateTime(year, static_cast<TMonth>(month - 1), day - 1,
       
   357                 hour, minute, second, 0));
       
   358         if (lex.Get() != 'Z')
       
   359             {
       
   360             r += offset;
       
   361             }
       
   362         }
       
   363     return r;
       
   364     }
       
   365 
       
   366 // ============================ MEMBER FUNCTIONS ===============================
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CDrmProtectedRo::NewL
       
   370 // Allocate a new protected RO object
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 CDrmProtectedRoParser::CParsedProtectedRo* CDrmProtectedRoParser::CParsedProtectedRo::NewL()
       
   374     {
       
   375     CParsedProtectedRo* self = new(ELeave) CParsedProtectedRo;
       
   376     return self;
       
   377     }
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CParsedProtectedRo::~CParsedProtectedRo
       
   381 // Release all data
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 CDrmProtectedRoParser::CParsedProtectedRo::~CParsedProtectedRo()
       
   385     {
       
   386     delete iKey;
       
   387     delete iId;
       
   388     delete iRiUrl;
       
   389     delete iDomainId;
       
   390     delete iRightsIssuerId;
       
   391     delete iMac;
       
   392     delete iMacDigest;
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CParsedProtectedRo::CParsedProtectedRo
       
   397 // Initialize all member data
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 CDrmProtectedRoParser::CParsedProtectedRo::CParsedProtectedRo() :
       
   401     iTransportScheme(EOma), iKey(NULL), iDomainRo(EFalse), iId(NULL),
       
   402             iRiUrl(NULL), iDomainId(NULL), iRightsIssuerId(NULL), iMac(NULL)
       
   403     {
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CDrmProtectedRoParser::CDrmProtectedRoParser
       
   408 // Reset all member variables
       
   409 // -----------------------------------------------------------------------------
       
   410 //
       
   411 CDrmProtectedRoParser::CDrmProtectedRoParser() :
       
   412     iParser(NULL), iRights(NULL), iContent(NULL)
       
   413     {
       
   414     }
       
   415 
       
   416 // -----------------------------------------------------------------------------
       
   417 // CDrmProtectedRoParser::ConstructL
       
   418 // Initialize the string pool elements
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 void CDrmProtectedRoParser::ConstructL()
       
   422     {
       
   423     iParser = CParser::NewL(KXmlParserMimeType, *this);
       
   424     for (TInt i = 0; i < ELEMENT_COUNT(KElements); i++)
       
   425         {
       
   426         TPtrC8 ptr(KElements[i].iString,
       
   427                 User::StringLength(KElements[i].iString));
       
   428         iElements[KElements[i].iNumber] = iParser->StringPool().OpenStringL(ptr);
       
   429         }
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CDrmProtectedRoParser::NewL
       
   434 // Two-phased constructor.
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 EXPORT_C CDrmProtectedRoParser* CDrmProtectedRoParser::NewL()
       
   438     {
       
   439     CDrmProtectedRoParser* self = new(ELeave) CDrmProtectedRoParser;
       
   440 
       
   441     CleanupStack::PushL( self );
       
   442     self->ConstructL();
       
   443     CleanupStack::Pop( self );
       
   444 
       
   445     return self;
       
   446     }
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // CDrmProtectedRoParser::~CDrmProtectedRoParser
       
   450 // Release the allocated strings
       
   451 // -----------------------------------------------------------------------------
       
   452 //
       
   453 EXPORT_C CDrmProtectedRoParser::~CDrmProtectedRoParser()
       
   454     {
       
   455     for (TInt i = 0; i < ELEMENT_COUNT(KElements); i++)
       
   456         {
       
   457         iElements[KElements[i].iNumber].Close();
       
   458         }
       
   459     delete iParser;
       
   460     delete iRights;
       
   461     delete iContent;
       
   462     }
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CDrmProtectedRoParser::ParseL
       
   466 // Parses one <protectedRo> element
       
   467 // -----------------------------------------------------------------------------
       
   468 //
       
   469 void CDrmProtectedRoParser::ParseL(const TDesC8& aProtectedRo,
       
   470         CParsedProtectedRo*& aResultRights)
       
   471     {
       
   472     TInt i( 0 );
       
   473 
       
   474     iElementStackDepth = 0;
       
   475     delete iRights;
       
   476     delete iContent;
       
   477     iContent = NULL;
       
   478     iRights = NULL;
       
   479     iRights = CParsedProtectedRo::NewL();
       
   480     iParser->ParseBeginL();
       
   481 
       
   482     i = aProtectedRo.Find( KRoapXmlNsUri );
       
   483 
       
   484     if ( i == KErrNotFound )
       
   485         {
       
   486         // Feed in the ROAP XML namespace declaration
       
   487         i = aProtectedRo.Find(KProtectedRoElement);
       
   488         if ( i == KErrNotFound )
       
   489             {
       
   490             User::Leave(KErrCorrupt);
       
   491             }
       
   492         i += KProtectedRoElement().Length();
       
   493         iParser->ParseL( aProtectedRo.Left( i ) );
       
   494         iParser->ParseL( KRoapXmlNs() );
       
   495         }
       
   496     else
       
   497         {
       
   498         i = 0;
       
   499         }
       
   500 
       
   501     while ( i < aProtectedRo.Length() )
       
   502         {
       
   503         TInt n( Min( aProtectedRo.Length() - i, KParserChunkSize ) );
       
   504         iParser->ParseL( aProtectedRo.Mid( i, n ) );
       
   505         i += n;
       
   506         }
       
   507     iParser->ParseEndL();
       
   508     aResultRights = iRights;
       
   509     iRights = NULL;
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CDrmProtectedRoParser::GetRiUrlL
       
   514 // Parse the RO and return the RI URL
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 EXPORT_C HBufC8* CDrmProtectedRoParser::GetRiUrlL(
       
   518         const TDesC8& aProtectedRo)
       
   519     {
       
   520     TInt i( 0 );
       
   521     HBufC8* r( NULL );
       
   522 
       
   523     iElementStackDepth = 0;
       
   524     delete iRights;
       
   525     delete iContent;
       
   526     iRights = NULL;
       
   527     iContent = NULL;
       
   528     iRights = CParsedProtectedRo::NewL();
       
   529     iParser->ParseBeginL();
       
   530     while (i < aProtectedRo.Length())
       
   531         {
       
   532         TInt n( Min( aProtectedRo.Length() - i, KParserChunkSize ) );
       
   533         iParser->ParseL( aProtectedRo.Mid( i, n ) );
       
   534         i += n;
       
   535         }
       
   536     iParser->ParseEndL();
       
   537     if ( iRights->iRiUrl )
       
   538         {
       
   539         r = iRights->iRiUrl->AllocL();
       
   540         }
       
   541     delete iRights;
       
   542     iRights = NULL;
       
   543     return r;
       
   544     }
       
   545 
       
   546 // -----------------------------------------------------------------------------
       
   547 // CDrmProtectedRoParser::GetRiUrlL
       
   548 // Parse the RO and return the RI ID
       
   549 // -----------------------------------------------------------------------------
       
   550 //
       
   551 EXPORT_C HBufC8* CDrmProtectedRoParser::GetRiIdL(
       
   552         const TDesC8& aProtectedRo)
       
   553     {
       
   554     HBufC8* r = NULL;
       
   555 
       
   556     iElementStackDepth = 0;
       
   557     delete iRights;
       
   558     delete iContent;
       
   559     iRights = NULL;
       
   560     iContent = NULL;
       
   561     iRights = CParsedProtectedRo::NewL();
       
   562     iParser->ParseBeginL();
       
   563     TInt i( 0 );
       
   564     while (i < aProtectedRo.Length())
       
   565         {
       
   566         TInt n( Min( aProtectedRo.Length() - i, KParserChunkSize ) );
       
   567         iParser->ParseL(aProtectedRo.Mid(i, n));
       
   568         i += n;
       
   569         }
       
   570     iParser->ParseEndL();
       
   571     if ( iRights->iRightsIssuerId )
       
   572         {
       
   573         r = iRights->iRightsIssuerId->AllocL();
       
   574         }
       
   575     delete iRights;
       
   576     iRights = NULL;
       
   577     return r;
       
   578     }
       
   579 
       
   580 // -----------------------------------------------------------------------------
       
   581 // CDrmProtectedRoParser::GetDomainIdL
       
   582 // Parse the RO and return the domain ID
       
   583 // -----------------------------------------------------------------------------
       
   584 //
       
   585 EXPORT_C HBufC8* CDrmProtectedRoParser::GetDomainIdL(
       
   586         const TDesC8& aProtectedRo)
       
   587     {
       
   588     HBufC8* r = NULL;
       
   589 
       
   590     iElementStackDepth = 0;
       
   591     delete iRights;
       
   592     iRights = NULL;
       
   593     delete iContent;
       
   594     iContent = NULL;
       
   595     iRights = CParsedProtectedRo::NewL();
       
   596     iParser->ParseBeginL();
       
   597     TInt i( 0 );
       
   598     while (i < aProtectedRo.Length())
       
   599         {
       
   600         TInt n( Min(aProtectedRo.Length() - i, KParserChunkSize) );
       
   601         iParser->ParseL(aProtectedRo.Mid(i, n));
       
   602         i += n;
       
   603         }
       
   604     iParser->ParseEndL();
       
   605     if ( iRights->iDomainId )
       
   606         {
       
   607         r = iRights->iDomainId->AllocL();
       
   608         }
       
   609     delete iRights;
       
   610     iRights = NULL;
       
   611     return r;
       
   612     }
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // CDrmProtectedRoParser::ParseAndStoreL
       
   616 // -----------------------------------------------------------------------------
       
   617 //
       
   618 EXPORT_C void CDrmProtectedRoParser::ParseAndStoreL(
       
   619         const TDesC8& aRightsObject,
       
   620         RPointerArray<CDRMRights>& aResultRights)
       
   621     {
       
   622     RDRMRightsClient client;
       
   623     CDrmRightsParser* parser = NULL;
       
   624     CDRMRights* rights = NULL;
       
   625     CParsedProtectedRo* parsedRo = NULL;
       
   626     TDRMUniqueID id = 0;
       
   627     HBufC8 *cid = NULL;
       
   628     TPtr8 ptr(0, 0);
       
   629     TBool domain = EFalse;
       
   630     TPtrC8 riId(0, 0);
       
   631     TInt start = 0;
       
   632     TInt end = 0;
       
   633     TInt roStart = 0;
       
   634     TInt roEnd = 0;
       
   635     TInt signedInfoStart = 0;
       
   636     TInt signedInfoEnd = 0;
       
   637     TInt n( 0 );
       
   638     TInt r( 0 );
       
   639     TBool inCache = EFalse;
       
   640     CSHA1* sha1 = NULL;
       
   641     TBool integrityCheck = ETrue;
       
   642     HBufC8* roWithNs = NULL;
       
   643     TPtr8 roWithNsPtr(NULL, NULL);
       
   644 
       
   645     User::LeaveIfError(client.Connect());
       
   646     CleanupClosePushL(client);
       
   647 
       
   648     // Loop through the complete roResponse and find all embedded protected
       
   649     // rights objects
       
   650     while (GetElementStartEnd(KProtectedRoElement, aRightsObject, start, end))
       
   651         {
       
   652         TPtrC8 protectedRo = aRightsObject.Mid(start, end - start);
       
   653         if (GetElementStartEnd(KRoElement, protectedRo, roStart, roEnd) == EFalse)
       
   654             {
       
   655             User::Leave(KErrCorrupt);
       
   656             }
       
   657         TPtrC8 ro = protectedRo.Mid(roStart, roEnd - roStart);
       
   658 
       
   659         ParseL(protectedRo, parsedRo);
       
   660         User::LeaveIfNull(parsedRo);
       
   661         CleanupStack::PushL(parsedRo);
       
   662 
       
   663         parser = CDrmRightsParser::NewL();
       
   664         CleanupStack::PushL(parser);
       
   665         parser->ParseL(protectedRo, aResultRights);
       
   666         CleanupStack::PopAndDestroy( parser );
       
   667 
       
   668         //////////////////////////
       
   669         // Replay protection
       
   670         //////////////////////////
       
   671 
       
   672         if (parsedRo->iTimeStamp == Time::NullTTime())
       
   673             {
       
   674             User::LeaveIfError(client.IsInCache(*parsedRo->iId, inCache));
       
   675             if (inCache)
       
   676                 {
       
   677                 User::Leave(KErrAlreadyExists);
       
   678                 }
       
   679             }
       
   680         else
       
   681             {
       
   682             User::LeaveIfError(client.IsInCache(*parsedRo->iId, parsedRo->iTimeStamp, inCache));
       
   683             if (inCache)
       
   684                 {
       
   685                 User::Leave(KErrAlreadyExists);
       
   686                 }
       
   687             }
       
   688 
       
   689         ///////////////////////////////////////////////////////
       
   690         // Unwrap MAC and REK
       
   691         // The plain keys are stored on the server side session
       
   692         ///////////////////////////////////////////////////////
       
   693         if (parsedRo->iDomainRo)
       
   694             {
       
   695             User::LeaveIfError( client.UnwrapMacAndRek(*parsedRo->iKey,
       
   696                             parsedRo->iTransportScheme,
       
   697                             *parsedRo->iRightsIssuerId,
       
   698                             *parsedRo->iDomainId) );
       
   699             }
       
   700         else
       
   701             {
       
   702             User::LeaveIfError( client.UnwrapMacAndRek(*parsedRo->iKey,
       
   703                             parsedRo->iTransportScheme,
       
   704                             *parsedRo->iRightsIssuerId,
       
   705                             KNullDesC8) );
       
   706             }
       
   707 
       
   708         //////////////////////////
       
   709         // Validate digest and MAC
       
   710         //////////////////////////
       
   711         roWithNs = AddRoapNamespaceL(ro);
       
   712         if (roWithNs)
       
   713             {
       
   714             CleanupStack::PushL(roWithNs);
       
   715             roWithNsPtr.Set(roWithNs->Des());
       
   716             }
       
   717 
       
   718         // Validate RO digest
       
   719         sha1 = CSHA1::NewL();
       
   720         if (roWithNs)
       
   721             {
       
   722             DEBUGLOGHEX( roWithNsPtr.Ptr(), roWithNsPtr.Length() );
       
   723             sha1->Update(roWithNsPtr);
       
   724             CleanupStack::PopAndDestroy(roWithNs);
       
   725             }
       
   726         else
       
   727             {
       
   728             DEBUGLOGHEX( ro.Ptr(), ro.Length() );
       
   729             sha1->Update(ro);
       
   730             }
       
   731 
       
   732         CleanupStack::PushL(sha1);
       
   733         HBufC8* calculatedHash( sha1->Final().AllocL() );
       
   734         CleanupStack::PopAndDestroy( sha1 );
       
   735         sha1 = NULL;
       
   736         CleanupStack::PushL( calculatedHash );
       
   737 
       
   738         if (calculatedHash->Compare(*parsedRo->iMacDigest) == KErrNone)
       
   739             {
       
   740             // Digest OK -> now verify the MAC
       
   741             signedInfoStart = protectedRo.Find(KMacStart());
       
   742 
       
   743             if (signedInfoStart < KErrNone)
       
   744                 {
       
   745                 integrityCheck = EFalse;
       
   746                 r = KErrCorrupt;
       
   747                 }
       
   748 
       
   749             if (integrityCheck && GetElementStartEnd(KSignedInfoElement, protectedRo, signedInfoStart, signedInfoEnd) == EFalse)
       
   750                 {
       
   751                 integrityCheck = EFalse;
       
   752                 r = KErrCorrupt;
       
   753                 }
       
   754 
       
   755             if(integrityCheck)
       
   756                 {
       
   757                 TPtrC8 signedInfo = protectedRo.Mid(signedInfoStart, signedInfoEnd - signedInfoStart);
       
   758                 r = client.VerifyMacL(signedInfo, *parsedRo->iMac);
       
   759 
       
   760                 if (r == KErrNone)
       
   761                     {
       
   762                     // MAC validation OK
       
   763                     // If we parsed a domain RO, store it in its original form as well
       
   764                     if (parsedRo->iDomainRo)
       
   765                         {
       
   766                         HBufC8* protRoWithNs = NULL;
       
   767                         protRoWithNs = AddRoapNamespaceL(protectedRo);
       
   768                         if(protRoWithNs)
       
   769                             {
       
   770                             client.AddDomainRO(*parsedRo->iId, *protRoWithNs);
       
   771                             delete protRoWithNs;
       
   772                             protRoWithNs = NULL;
       
   773                             }
       
   774                         else
       
   775                             {
       
   776                             client.AddDomainRO(*parsedRo->iId, protectedRo);
       
   777                             }
       
   778                         }
       
   779 
       
   780                     // Add RO to replay cache
       
   781                     if (parsedRo->iTimeStamp == Time::NullTTime())
       
   782                         {
       
   783                         User::LeaveIfError(client.AddToCache(*parsedRo->iId));
       
   784                         }
       
   785                     else
       
   786                         {
       
   787                         User::LeaveIfError(client.AddToCache(*parsedRo->iId, parsedRo->iTimeStamp));
       
   788                         }
       
   789                     }
       
   790                 else
       
   791                     {
       
   792                     integrityCheck = EFalse;
       
   793                     }
       
   794                 }
       
   795             }
       
   796         else
       
   797             {
       
   798             integrityCheck = EFalse;
       
   799             r = KErrCorrupt;
       
   800             }
       
   801         CleanupStack::PopAndDestroy( calculatedHash );
       
   802         calculatedHash = NULL;
       
   803 
       
   804         if ( integrityCheck && !r )
       
   805             {
       
   806             // Only add the rigths which were parsed in this loop
       
   807     
       
   808             for ( TInt i( n ); i < aResultRights.Count(); i++)
       
   809                 {
       
   810                 cid = NULL;
       
   811                 rights = aResultRights[i];
       
   812                 rights->GetContentURI(cid);
       
   813                 CleanupStack::PushL(cid);
       
   814                 rights->GetPermission().iOriginalInsertTime =
       
   815                 parsedRo->iTimeStamp;
       
   816                 rights->GetPermission().iRoID = (*parsedRo->iId).AllocL();
       
   817                 rights->GetPermission().iRiId.Copy(*parsedRo->iRightsIssuerId);
       
   818                 if (parsedRo->iDomainRo)
       
   819                     {
       
   820                     domain = ETrue;
       
   821                     rights->GetPermission().iDomainID =
       
   822                     parsedRo->iDomainId->AllocL();
       
   823                     }
       
   824                 if (parsedRo->iRightsIssuerId)
       
   825                     {
       
   826                     riId.Set(*parsedRo->iRightsIssuerId);
       
   827                     }
       
   828                 if (parsedRo->iTransportScheme == EOma)
       
   829                     {
       
   830                     rights->GetPermission().iRightsObjectVersion.iVersionMain =
       
   831                     EOma2Rights;
       
   832                     }
       
   833                 else
       
   834                     {
       
   835                     rights->GetPermission().iRightsObjectVersion.iVersionMain =
       
   836                     ECmlaRights;
       
   837                     }
       
   838                 r = client.AddProtectedRecord( rights->GetAsset().iProtectedKey,
       
   839                         domain, rights->GetPermission(),
       
   840                         *cid, id );
       
   841                 if (!r)
       
   842                     {
       
   843                     if (rights->GetAsset().iProtectedAuthSeed.Length()> 0)
       
   844                         {
       
   845                         r = client.SetAuthenticationSeed(*cid,
       
   846                                 rights->GetAsset().iProtectedAuthSeed);
       
   847                         }
       
   848                     }
       
   849 
       
   850                 User::LeaveIfError(r);
       
   851                 rights->SetLocalID(id);
       
   852                 CleanupStack::PopAndDestroy(cid);
       
   853                 }
       
   854             }
       
   855         else
       
   856             {
       
   857             User::Leave(KErrCorrupt);
       
   858             }
       
   859 
       
   860         start = end;
       
   861         n = aResultRights.Count();
       
   862         CleanupStack::PopAndDestroy(parsedRo);
       
   863         parsedRo = NULL;
       
   864         }
       
   865     CleanupStack::PopAndDestroy( &client );
       
   866     }
       
   867 
       
   868 // -----------------------------------------------------------------------------
       
   869 // CDrmProtectedRoParser::OnStartDocumentL
       
   870 // -----------------------------------------------------------------------------
       
   871 //
       
   872 void CDrmProtectedRoParser::OnStartDocumentL(
       
   873         const RDocumentParameters& /*aDocParam*/, TInt /*aErrorCode*/)
       
   874     {
       
   875     }
       
   876 
       
   877 // -----------------------------------------------------------------------------
       
   878 // CDrmProtectedRoParser::OnEndDocumentL
       
   879 // -----------------------------------------------------------------------------
       
   880 //
       
   881 void CDrmProtectedRoParser::OnEndDocumentL(TInt /*aErrorCode*/)
       
   882     {
       
   883     }
       
   884 
       
   885 // -----------------------------------------------------------------------------
       
   886 // CDrmProtectedRoParser::OnStartElementL
       
   887 // -----------------------------------------------------------------------------
       
   888 //
       
   889 void CDrmProtectedRoParser::OnStartElementL(const RTagInfo& aElement,
       
   890         const RAttributeArray& aAttributes, TInt /*aErrorCode*/)
       
   891     {
       
   892     HBufC8* b= NULL;
       
   893 
       
   894     if (iContent)
       
   895         {
       
   896         delete iContent;
       
   897         iContent = NULL;
       
   898         iContent = HBufC8::NewL(0);
       
   899         }
       
   900 
       
   901     for (TInt i = 0; i < KMaxElementCount; i++)
       
   902         {
       
   903         if (aElement.LocalName() == iElements[i])
       
   904             {
       
   905             iElementStack[iElementStackDepth] = static_cast<TElementEnum>(i);
       
   906             iElementStackDepth++;
       
   907             if (iElementStackDepth == KMaxElementNesting)
       
   908                 {
       
   909                 User::Leave(EXmlUnexpectedState);
       
   910                 }
       
   911             TInt state( MatchStackState() );
       
   912             if (state == ERoState)
       
   913                 {
       
   914                 _LIT8( KTrue, "true" );
       
   915                 iRights->iId = GetAttributeValueL(aAttributes, KIdAttr);
       
   916                 iRights->iRiUrl = GetAttributeValueL(aAttributes, KRiUrlAttr);
       
   917                 b = GetAttributeValueL(aAttributes, KDomainRoAttr);
       
   918                 if ( b && b->Compare( KTrue() ) == 0 )
       
   919                     {
       
   920                     iRights->iDomainRo = ETrue;
       
   921                     }
       
   922                 else
       
   923                     {
       
   924                     iRights->iDomainRo = EFalse;
       
   925                     }
       
   926                 delete b;
       
   927                 }
       
   928             else
       
   929                 if (state == EEncryptionMethodState)
       
   930                     {
       
   931                     b = GetAttributeValueL(aAttributes, KAlgorithmAttr);
       
   932                     if ( b )
       
   933                         {
       
   934                         iRights->iTransportScheme
       
   935                                 = CmlaCrypto::AlgorithmIdToTransportScheme(*b);
       
   936                         delete b;
       
   937                         }
       
   938                     }
       
   939             }
       
   940         }
       
   941     }
       
   942 
       
   943 // -----------------------------------------------------------------------------
       
   944 // CDrmProtectedRoParser::OnEndElementL
       
   945 // -----------------------------------------------------------------------------
       
   946 //
       
   947 void CDrmProtectedRoParser::OnEndElementL(const RTagInfo& aElement, TInt /*aErrorCode*/)
       
   948     {
       
   949     for (TInt i = 0; i < KMaxElementCount; i++)
       
   950         {
       
   951         if (aElement.LocalName() == iElements[i])
       
   952             {
       
   953             switch (MatchStackState())
       
   954                 {
       
   955                 case ECipherValueState:
       
   956                     iRights->iKey = Base64DecodeL(iContent->Des());
       
   957                     break;
       
   958                 case EDomainIdentifierState:
       
   959                     iRights->iDomainId = iContent->AllocL();
       
   960                     break;
       
   961                 case ERightsIssuerIdState:
       
   962                     iRights->iRightsIssuerId = Base64DecodeL(iContent->Des());
       
   963                     break;
       
   964                 case ETimeStampState:
       
   965                     iRights->iTimeStamp = Iso8601ToTime(*iContent);
       
   966                     break;
       
   967                 case EMacValueState:
       
   968                     iRights->iMac = Base64DecodeL(iContent->Des());
       
   969                     break;
       
   970                 case EMacDigestValueState:
       
   971                     iRights->iMacDigest = Base64DecodeL(iContent->Des());
       
   972                     break;
       
   973                 }
       
   974             iElementStackDepth--;
       
   975             if (iElementStackDepth < 0)
       
   976                 {
       
   977                 User::Leave(EXmlUnexpectedState);
       
   978                 }
       
   979             }
       
   980         }
       
   981     }
       
   982 
       
   983 // -----------------------------------------------------------------------------
       
   984 // CDrmProtectedRoParser::OnContentL
       
   985 // -----------------------------------------------------------------------------
       
   986 //
       
   987 void CDrmProtectedRoParser::OnContentL(const TDesC8& aBytes, TInt /*aErrorCode*/)
       
   988     {
       
   989     if ( !iContent )
       
   990         {
       
   991         iContent = HBufC8::NewL(aBytes.Size());
       
   992         *iContent = aBytes;
       
   993         }
       
   994     else
       
   995         {
       
   996         iContent = iContent->ReAllocL(iContent->Size() + aBytes.Size());
       
   997         TPtr8 c(iContent->Des());
       
   998         c.Append(aBytes);
       
   999         }
       
  1000     }
       
  1001 
       
  1002 // -----------------------------------------------------------------------------
       
  1003 // CDrmProtectedRoParser::OnStartPrefixMappingL
       
  1004 // -----------------------------------------------------------------------------
       
  1005 //
       
  1006 void CDrmProtectedRoParser::OnStartPrefixMappingL(const RString& /*aPrefix*/,
       
  1007         const RString& /*aUri*/, TInt /*aErrorCode*/)
       
  1008     {
       
  1009     }
       
  1010 
       
  1011 // -----------------------------------------------------------------------------
       
  1012 // CDrmProtectedRoParser::OnEndPrefixMappingL
       
  1013 // -----------------------------------------------------------------------------
       
  1014 //
       
  1015 void CDrmProtectedRoParser::OnEndPrefixMappingL(const RString& /*aPrefix*/,
       
  1016         TInt /*aErrorCode*/)
       
  1017     {
       
  1018     }
       
  1019 
       
  1020 // -----------------------------------------------------------------------------
       
  1021 // CDrmProtectedRoParser::OnIgnorableWhiteSpaceL
       
  1022 // -----------------------------------------------------------------------------
       
  1023 //
       
  1024 void CDrmProtectedRoParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/,
       
  1025         TInt /*aErrorCode*/)
       
  1026     {
       
  1027     }
       
  1028 
       
  1029 // -----------------------------------------------------------------------------
       
  1030 // CDrmProtectedRoParser::OnSkippedEntityL
       
  1031 // -----------------------------------------------------------------------------
       
  1032 //
       
  1033 void CDrmProtectedRoParser::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/)
       
  1034     {
       
  1035     }
       
  1036 
       
  1037 // -----------------------------------------------------------------------------
       
  1038 // CDrmProtectedRoParser::OnProcessingInstructionL
       
  1039 // -----------------------------------------------------------------------------
       
  1040 //
       
  1041 void CDrmProtectedRoParser::OnProcessingInstructionL(
       
  1042         const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt /*aErrorCode*/)
       
  1043     {
       
  1044     }
       
  1045 
       
  1046 // -----------------------------------------------------------------------------
       
  1047 // CDrmProtectedRoParser::OnOutOfData
       
  1048 // -----------------------------------------------------------------------------
       
  1049 //
       
  1050 void CDrmProtectedRoParser::OnOutOfData()
       
  1051     {
       
  1052     }
       
  1053 
       
  1054 // -----------------------------------------------------------------------------
       
  1055 // CDrmProtectedRoParser::OnError
       
  1056 // -----------------------------------------------------------------------------
       
  1057 //
       
  1058 void CDrmProtectedRoParser::OnError(TInt /*aErrorCode*/)
       
  1059     {
       
  1060     }
       
  1061 
       
  1062 // -----------------------------------------------------------------------------
       
  1063 // CDrmProtectedRoParser::GetExtendedInterface
       
  1064 // -----------------------------------------------------------------------------
       
  1065 //
       
  1066 TAny* CDrmProtectedRoParser::GetExtendedInterface(const TInt32 /*aUid*/)
       
  1067     {
       
  1068     return NULL;
       
  1069     }
       
  1070 
       
  1071 // -----------------------------------------------------------------------------
       
  1072 // CDrmProtectedRoParser::MatchStackState
       
  1073 // -----------------------------------------------------------------------------
       
  1074 //
       
  1075 TInt CDrmProtectedRoParser::MatchStackState()
       
  1076     {
       
  1077     TInt i( 0 );
       
  1078     TInt j( 0 );
       
  1079     TInt k( 0 );
       
  1080     TParserStackState r = EUnknownState;
       
  1081 
       
  1082     for (i = 0; r == EUnknownState && i < ELEMENT_COUNT(KParserStackStates); i++)
       
  1083         {
       
  1084         for (j = iElementStackDepth - 1, k = 0; j > 0
       
  1085                 && KParserStackStates[i].iStack[k] != ELast; j--, k++)
       
  1086             {
       
  1087             if (iElementStack[j] != KParserStackStates[i].iStack[k])
       
  1088                 {
       
  1089                 break;
       
  1090                 }
       
  1091             }
       
  1092         if ((j == 0 && iElementStack[j] == KParserStackStates[i].iStack[k])
       
  1093                 || KParserStackStates[i].iStack[k] == ELast)
       
  1094             {
       
  1095             r = KParserStackStates[i].iState;
       
  1096             }
       
  1097         }
       
  1098     return r;
       
  1099     }
       
  1100 
       
  1101 // -----------------------------------------------------------------------------
       
  1102 // CDrmProtectedRoParser::GetAttributeValueL
       
  1103 // Allocate the attribute value as a buffer if it is found, otherwise it's NULL
       
  1104 // -----------------------------------------------------------------------------
       
  1105 //
       
  1106 HBufC8* CDrmProtectedRoParser::GetAttributeValueL(
       
  1107         const RAttributeArray& aAttrList, const TDesC8& aAttrName)
       
  1108     {
       
  1109     HBufC8* r( NULL );
       
  1110 
       
  1111     for ( TInt i(0); !r && i < aAttrList.Count(); i++ )
       
  1112         {
       
  1113         const RAttribute& a( aAttrList[i] );
       
  1114         if ( a.Attribute().LocalName().DesC().Compare( aAttrName ) == 0 )
       
  1115             {
       
  1116             r = a.Value().DesC().AllocL();
       
  1117             }
       
  1118         }
       
  1119     return r;
       
  1120     }
       
  1121 
       
  1122 // -----------------------------------------------------------------------------
       
  1123 // CDrmProtectedRoParser::GetElementStartEnd
       
  1124 // Search for the element as a string
       
  1125 // -----------------------------------------------------------------------------
       
  1126 //
       
  1127 TBool CDrmProtectedRoParser::GetElementStartEnd(const TDesC8& aElement,
       
  1128         const TDesC8& aData, TInt& aStart, TInt& aEnd)
       
  1129     {
       
  1130     TInt i( 0 );
       
  1131     TInt j( 0 );
       
  1132     TBool r( EFalse );
       
  1133     TBuf8<KMaxElementLen> element;
       
  1134 
       
  1135     DEBUGLOG( "CDrmProtectedRoParser::GetElementStartEnd ->" );
       
  1136 
       
  1137     DEBUGLOG( "aElement" );
       
  1138     DEBUGLOGHEX( aElement.Ptr(), aElement.Length() );
       
  1139     DEBUGLOG( "aData" );
       
  1140     DEBUGLOGHEX( aData.Ptr(), aData.Length() );
       
  1141     element.Copy(KStartElementStart);
       
  1142     element.Append(aElement);
       
  1143     // Find the start tag, which has to end with a "<" or whitespace
       
  1144     i = aData.Mid(aStart).Find(element);
       
  1145 
       
  1146     if (i == KErrNotFound)
       
  1147         {
       
  1148         // try the same thing with ROAP prefix
       
  1149         element.Copy(KStartElementStart);
       
  1150         element.Append(KRoapPrefix);
       
  1151         element.Append(aElement);
       
  1152         i = aData.Mid(aStart).Find(element);
       
  1153         }
       
  1154 
       
  1155     while (i != KErrNotFound && !(aData[aStart + i + element.Length()] == '>' || aData[aStart + i + element.Length()] <= ' '))
       
  1156         {
       
  1157         aStart += i;
       
  1158         i = aData.Mid(aStart + element.Length() + 1).Find(element);
       
  1159         }
       
  1160 
       
  1161     if (i != KErrNotFound)
       
  1162         {
       
  1163         aStart += i;
       
  1164         i = aStart;
       
  1165         element.Copy(KEndElementStart);
       
  1166         element.Append(aElement);
       
  1167         element.Append(KElementEnd);
       
  1168         j = aData.Mid(i + 1).Find(element);
       
  1169         if (j == KErrNotFound)
       
  1170             {
       
  1171             // try the same thing with ROAP prefix
       
  1172             element.Copy(KEndElementStart);
       
  1173             element.Append(KRoapPrefix);
       
  1174             element.Append(aElement);
       
  1175             element.Append(KElementEnd);
       
  1176             j = aData.Mid(i + 1).Find(element);
       
  1177             }
       
  1178         if (j != KErrNotFound)
       
  1179             {
       
  1180             aEnd = i + 1 + j + element.Length();
       
  1181             r = ETrue;
       
  1182             }
       
  1183         }
       
  1184 #ifdef _DEBUG
       
  1185     if ( r )
       
  1186         {
       
  1187         TPtrC8 extracted( aData.Mid( aStart, aEnd - aStart ) );
       
  1188         DEBUGLOG( "got element: " );
       
  1189         DEBUGLOGHEX( extracted.Ptr(), extracted.Length() );
       
  1190         }
       
  1191 #endif
       
  1192     DEBUGLOG( "-> CDrmProtectedRoParser::GetElementStartEnd" );
       
  1193     return r;
       
  1194     }
       
  1195 
       
  1196 // -----------------------------------------------------------------------------
       
  1197 // CDrmProtectedRoParser::HandleUnregisteredDomainRoL
       
  1198 // Embed the domain RO into all matching DCF files
       
  1199 // -----------------------------------------------------------------------------
       
  1200 //
       
  1201 void CDrmProtectedRoParser::HandleUnregisteredDomainRoL(
       
  1202         const TDesC8& aContentId, const TDesC8& aDomainRoData,
       
  1203         CParsedProtectedRo* /*aDomainRo*/)
       
  1204     {
       
  1205     CDcfRep* rep( NULL );
       
  1206     COma2Dcf* dcf( NULL );
       
  1207     CDcfEntry* entry( NULL );
       
  1208     RFile file;
       
  1209     RFs fs;
       
  1210     TBool exists( EFalse );
       
  1211     TInt r( KErrNone );
       
  1212 
       
  1213     User::LeaveIfError( fs.Connect() );
       
  1214     CleanupClosePushL( fs );
       
  1215     rep = CDcfRep::NewL();
       
  1216     CleanupStack::PushL( rep );
       
  1217     TRAP( r, rep->OrderListL( aContentId ) );
       
  1218     if ( r == KErrNone )
       
  1219         {
       
  1220         entry = rep->NextL();
       
  1221         }
       
  1222     while ( r == KErrNone && entry )
       
  1223         {
       
  1224         CleanupStack::PushL( entry );
       
  1225 
       
  1226         r = file.Open( fs, entry->FileName(), EFileWrite
       
  1227                 | EFileShareReadersOrWriters );
       
  1228         if ( r == KErrInUse )
       
  1229             {
       
  1230             r = file.Open(fs, entry->FileName(), EFileWrite | EFileShareAny);
       
  1231             }
       
  1232 
       
  1233         if ( r == KErrNone )
       
  1234             {
       
  1235             CleanupClosePushL( file );
       
  1236             dcf = COma2Dcf::NewL( file );
       
  1237             CleanupStack::PushL( dcf );
       
  1238             for ( TInt i = 0; !exists && i < dcf->iRightsObjects.Count(); i++ )
       
  1239                 {
       
  1240                 if ( dcf->iRightsObjects[i]->Compare(aDomainRoData) == 0 )
       
  1241                     {
       
  1242                     exists = ETrue;
       
  1243                     }
       
  1244                 }
       
  1245             if ( !exists )
       
  1246                 {
       
  1247                 dcf->iRightsObjects.Append( aDomainRoData.AllocL() );
       
  1248                 dcf->SetRightsObjectsL( dcf->iRightsObjects );
       
  1249                 }
       
  1250             CleanupStack::PopAndDestroy( dcf );
       
  1251             CleanupStack::PopAndDestroy( &file );
       
  1252             }
       
  1253         CleanupStack::PopAndDestroy( entry );
       
  1254         entry = rep->NextL();
       
  1255         }
       
  1256     CleanupStack::PopAndDestroy( rep );
       
  1257     CleanupStack::PopAndDestroy( &fs );
       
  1258     }
       
  1259 
       
  1260 // -----------------------------------------------------------------------------
       
  1261 // CDrmProtectedRoParser::AddRoapNamespaceL
       
  1262 // Add ROAP namespace declaration if needed
       
  1263 // -----------------------------------------------------------------------------
       
  1264 //
       
  1265 HBufC8* CDrmProtectedRoParser::AddRoapNamespaceL(TDesC8& aProtectedRo) const
       
  1266     {
       
  1267     TInt index = 0;
       
  1268     HBufC8* roWithNs= NULL;
       
  1269     TPtr8 roWithNsPtr(NULL, NULL);
       
  1270 
       
  1271     if (aProtectedRo.Find(KRoapXmlNsUri) == KErrNotFound)
       
  1272         {
       
  1273         roWithNs = HBufC8::NewLC(aProtectedRo.Length() + KRoapXmlNs().Length());
       
  1274         roWithNsPtr.Set(roWithNs->Des());
       
  1275         index = aProtectedRo.Find(KQualifiedProtectedRo);
       
  1276         if (index == KErrNotFound)
       
  1277             {
       
  1278             index = aProtectedRo.Find(KRoElement);
       
  1279             if (index == KErrNotFound)
       
  1280                 {
       
  1281                 User::Leave(KErrCorrupt);
       
  1282                 }
       
  1283             index += KRoElement().Length();
       
  1284             }
       
  1285         else
       
  1286             {
       
  1287             index += KQualifiedProtectedRo().Length();
       
  1288             }
       
  1289         roWithNsPtr.Copy(aProtectedRo.Left(index));
       
  1290         roWithNsPtr.Append(KRoapXmlNs());
       
  1291         roWithNsPtr.Append(aProtectedRo.Right(aProtectedRo.Length() - index));
       
  1292         CleanupStack::Pop(roWithNs);
       
  1293         }
       
  1294     return roWithNs;
       
  1295     }
       
  1296 
       
  1297 //  End of File