diff -r 000000000000 -r 95b198f216e5 omadrm/drmengine/roap/src/MeteringReportReq.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/omadrm/drmengine/roap/src/MeteringReportReq.cpp Thu Dec 17 08:52:27 2009 +0200 @@ -0,0 +1,362 @@ +/* +* Copyright (c) 2002-2008 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: Class representing metering report request +* +*/ + + + +// INCLUDE FILES +#include +#include "base64.h" +#include "MeteringReportReq.h" +#include "RoapLog.h" + +using namespace Roap; + +// LOCAL CONSTANTS AND MACROS +// XML tag related literals +// Warning: these literals are very fragile in respect to XML canonicalisation. +_LIT8(KReqHeader, ""); +_LIT8(KReqDeviceId, ""); +_LIT8(KReqDeviceIdEnd, ""); +_LIT8(KReqRiId, ""); +_LIT8(KReqRiIdEnd, ""); +_LIT8(KReqNonce, ""); +_LIT8(KReqNonceEnd, ""); +_LIT8(KReqTime, ""); +_LIT8(KReqMeteringReport, ""); + +_LIT8(KReqReportHeader, +"\ +\ +\ +"); + +_LIT8(KReqKeyInfo, "\ +\ +"); + +_LIT8(KReqCipherDataStart, ""); +_LIT8(KReqCipherDataEnd, ""); + +_LIT8(KReqEncKeyAndMethod, ""); + +_LIT8(KReqEncKeyInfoStart, ""); + +// Note the two versions below are because of different namespace +// normalisation of element roap:X509SPKIHash on canonicalisation of +// whole request to be signed and on canonicalisation of element +// to be MAC calculated. +// when changing one of these another one must be cahnges too. +// +// This version is used when canonicalisation is performed over +// +// +_LIT8(KReqRoapX509AndHash, ""); +// +// +// This version is used when canonicalisation is taken over +// (on MAC calculation) +// +_LIT8(KReqRoapX509AndHashNs, ""); + + +_LIT8(KReqEncKeyInfoEnd, "\ +"); + +_LIT8(KReqCipherValueEnd, ""); +_LIT8(KReqMacStart, ""); +_LIT8(KReqMacEnd, ""); +_LIT8(KReqReportHeaderEnd, ""); +_LIT8(KReqCertChain, ""); +_LIT8(KReqCert, ""); +_LIT8(KReqCertEnd, ""); +_LIT8(KReqCertChainEnd, ""); +_LIT8(KReqSig, ""); +_LIT8(KReqSigEnd, ""); +// literals for key transport scheme +_LIT8( KOmaKdf, "http://www.rsasecurity.com/rsalabs/pkcs/schemas/pkcs-1#rsaes-kem-kdf2-kw-aes128"); +_LIT8( KCmlaIp1, "http://www.cm-la.com/tech/cmlaip/cmlaip#cmlaip-1"); +// ============================ LOCAL FUNCTIONS =============================== + +// ---------------------------------------------------------------------------- +// MeteringReportElementAsTextL +// +// write content of to given flat buffer +// Used from MessageAsXmlL and from InsertMacL +// Note content depends on value of +// ---------------------------------------------------------------------------- +LOCAL_C inline void MeteringReportElementAsTextL( + CBufFlat*& aBuf, + const Roap::CMeteringReportReq& aReq + ) + { + Roap::CRoapMessage::BufAppendL( aBuf, KReqMeteringReport ); + Roap::CRoapMessage::BufAppendL( aBuf, KReqReportHeader ); + + Roap::CRoapMessage::BufAppendL( aBuf, KReqKeyInfo ); + + Roap::CRoapMessage::BufAppendL( aBuf, KReqCipherDataStart ); + + // actual encrypted metering data + if ( aReq.iCipherValue ) + { + Roap::CRoapMessage::BufAppendBase64L( aBuf, *aReq.iCipherValue ); + } + + Roap::CRoapMessage::BufAppendL( aBuf, KReqCipherDataEnd ); + + Roap::CRoapMessage::BufAppendL( aBuf, KReqEncKeyAndMethod ); + + // Select KDF algorithm in use + if ( aReq.iAlgorithmInUse == ECmlaIp1 ) + { + Roap::CRoapMessage::BufAppendL( aBuf, KCmlaIp1 ); + } + else if ( aReq.iAlgorithmInUse == EOma ) + { + Roap::CRoapMessage::BufAppendL( aBuf, KOmaKdf ); + } + else + { + User::Leave( KErrNotSupported ); + } + Roap::CRoapMessage::BufAppendL( aBuf, KReqEncKeyAndMethodAfterAlgorithm ); + Roap::CRoapMessage::BufAppendL( aBuf, KReqEncKeyInfoStart ); + + if ( aReq.iMac ) + { + // used as part of roap:Meteringreport submit + // so namespace ROAP already defined in container element + Roap::CRoapMessage::BufAppendL( aBuf, KReqRoapX509AndHash ); + } + else + { + // used in mac calculation + // canonical form requires namespace definition for namespace roap + Roap::CRoapMessage::BufAppendL( aBuf, KReqRoapX509AndHashNs ); + } + + // Insert 128-bit encrypted encryption key PKI hash + Roap::CRoapMessage::BufAppendBase64L( aBuf, aReq.iEncKeyHash ); + + + Roap::CRoapMessage::BufAppendL( aBuf, KReqEncKeyInfoEnd ); + + // key wrapping info + Roap::CRoapMessage::BufAppendBase64L( aBuf, *aReq.iEncryptedMekAndMak ); + + Roap::CRoapMessage::BufAppendL( aBuf, KReqCipherValueEnd ); + + // Insert 128-bit encrypted MAC value + if ( aReq.iMac ) + { + Roap::CRoapMessage::BufAppendL( aBuf, KReqMacStart ); + Roap::CRoapMessage::BufAppendBase64L( aBuf, *aReq.iMac ); + Roap::CRoapMessage::BufAppendL( aBuf, KReqMacEnd ); + } + Roap::CRoapMessage::BufAppendL( aBuf, KReqReportHeaderEnd ); + } + +// ----------------------------------------------------------------------------- +// CalculateMacL +// ----------------------------------------------------------------------------- +// +LOCAL_C HBufC8* CalculateMacL(const TDesC8& aElement, const TDesC8& aMacKey ) + { + + LOG( _L8( "CalculateMacL" ) ); + LOG( _L8( "aMacKey" ) ); + LOGHEX( aMacKey.Ptr(), aMacKey.Length() ); + LOG( _L8( "aElement" ) ); + LOGHEX( aElement.Ptr(), aElement.Length() ); + if( !aMacKey.Length() || !aElement.Length() ) + { + User::Leave(KErrArgument); + } + + CHMAC* hMac = NULL; + CSHA1* sha = NULL; + TPtrC8 hmac_value( KNullDesC8 ); + TPtrC8 sha1_value( KNullDesC8 ); + HBufC8* macValue = NULL; + + sha = CSHA1::NewL(); + CleanupStack::PushL( sha ); + hMac = CHMAC::NewL( aMacKey, sha ); + CleanupStack::Pop( sha ); // sha is now owned by hMac + CleanupStack::PushL( hMac ); + hMac->Update( aElement ); + hmac_value.Set( hMac->Final() ); + + macValue = hmac_value.AllocL(); + LOG( _L8( "macValue" ) ); + LOGHEX( macValue->Ptr(), macValue->Length() ); + + CleanupStack::PopAndDestroy( hMac ); + return macValue; + } + + +// ============================ MEMBER FUNCTIONS =============================== + +// ----------------------------------------------------------------------------- +// CRightsReq::CRightsReq +// C++ default constructor can NOT contain any code, that +// might leave. +// ----------------------------------------------------------------------------- +// +CMeteringReportReq::CMeteringReportReq(): + iNonce(NULL), + iReportNonce(NULL), + iSignature(NULL), + iCipherValue(NULL), + iMac(NULL), + iTriggerNonce(NULL) + { + } + +// ----------------------------------------------------------------------------- +// CRightsReq::ConstructL +// Symbian 2nd phase constructor can leave. +// ----------------------------------------------------------------------------- +// +void CMeteringReportReq::ConstructL() + { + iTime.UniversalTime(); + } + +// ----------------------------------------------------------------------------- +// CRightsReq::NewL +// Two-phased constructor. +// ----------------------------------------------------------------------------- +// +CMeteringReportReq* CMeteringReportReq::NewL() + { + CMeteringReportReq* self = new( ELeave ) CMeteringReportReq; + + CleanupStack::PushL( self ); + self->ConstructL(); + CleanupStack::Pop( self ); + + return self; + } + + +// Destructor +CMeteringReportReq::~CMeteringReportReq() + { + if ( iCertificateChain.Count() ) + { + iCertificateChain.ResetAndDestroy(); + } + delete iSignature; + delete iTriggerNonce; + delete iCipherValue; + delete iEncryptedMekAndMak; + delete iMac; + } + + +// ----------------------------------------------------------------------------- +// CRightsReq::MessageAsXmlL +// Gives XML representation of meteringreport request +// (other items were commented in a header). +// ----------------------------------------------------------------------------- +// +HBufC8* CMeteringReportReq::MessageAsXmlL(void) + { + HBufC8* r( NULL ); + CBufFlat* b( CBufFlat::NewL( 128 ) ); + CleanupStack::PushL( b ); + b->InsertL( 0, KReqHeader ); + if ( iTriggerNonce ) + { + BufAppendL( b, KReqNonceTrigger ); + BufAppendL( b, *iTriggerNonce ); + BufAppendL( b, KReqNonceTriggerEnd ); + } + BufAppendL( b, KReqHeaderEnd ); + + BufAppendL( b, KReqDeviceId ); + BufAppendBase64L( b, iDeviceId ); + BufAppendL( b, KReqDeviceIdEnd ); + + BufAppendL( b, KReqRiId ); + BufAppendBase64L( b, iRiId ); + BufAppendL( b, KReqRiIdEnd ); + + BufAppendL( b, KReqNonce ); + BufAppendBase64L( b, iNonce ); + BufAppendL( b, KReqNonceEnd ); + + BufAppendL( b, KReqTime ); + BufAppendTimeL( b, iTime ); + BufAppendL( b, KReqTimeEnd ); + + MeteringReportElementAsTextL( b, *this ); + + if ( iCertificateChain.Count() > 0 ) + { + BufAppendL( b, KReqCertChain ); + for ( TInt i( 0 ); i < iCertificateChain.Count(); i++ ) + { + BufAppendL( b, KReqCert ); + BufAppendBase64L( b, *iCertificateChain[ i ] ); + BufAppendL( b, KReqCertEnd ); + } + BufAppendL( b, KReqCertChainEnd ); + } + + BufAppendL(b, KReqSig); + BufAppendL(b, KReqSigEnd); + + r = b->Ptr( 0 ).AllocL(); + CleanupStack::PopAndDestroy( b ); + return r; + } + +// ----------------------------------------------------------------------------- +// CMeteringReportReq::InsertMacL( const TDesC8& aMac ) +// Calculate MAC -value over +// (other items were commented in a header). +// ----------------------------------------------------------------------------- +// +void CMeteringReportReq::InsertMacL( const TDesC8& aMacKey ) + { + CBufFlat* b( CBufFlat::NewL( 128 ) ); + CleanupStack::PushL( b ); + + + MeteringReportElementAsTextL( b, *this ); + + // Calculate MAC -value over + // without the actual -element + + iMac = CalculateMacL( b->Ptr( 0 ), aMacKey ); + CleanupStack::PopAndDestroy( b ); + return; + } + +// End of File