rtp/srtpstack/src/srtppacketsrtp.cpp
author Petteri Saari <petteri.saari@digia.com>
Thu, 25 Nov 2010 13:59:42 +0200
branchMSRP_FrameWork
changeset 58 cdb720e67852
parent 0 307788aac0a8
permissions -rw-r--r--
This release addresses the following issues: 1. The crash bug fix when receiving file 2. Now the sending is based on MSRP messages, there is no longer file receiving or sending. Client sends data as MSRP was designed. 3. Soma MSRP stack was created so that the client told the correct session-id, Symbian stack generated it by itself. This is not allowed, it was changed so that clients tell the session-id (same as used in SIP INVITE). 4. Unnecessary division of data to chunks removed when there is no need to interrupt sending. The message is sent in as few chunks as possible. 5. Stack can now receive files and chunks with ?unlimited? size. Old stack wrote the incoming data to memory and did not utilize disk space until the end of chunk was reached (large chunks from another client crashed it). 6. Now when writing the incoming data to file, it will take into account the byte-range header values. So, this complies with the RFC4975 requirements that stack must be able to handle chunks that come in any sequence. 7. Some buffering changes to outgoing/incoming data. 8. The outgoing data is now checked that it does not contain the created transaction-id before sending the data. 9. MSRP success reports are now implemented and tested against servers. 10. Progress report system fixed so progress is now visible on client (all the way to 100%). 11. Message Cancel receiving / Cancel sending now corrected and made to work as rfc4975 requires. (termination from sender and error code from receiver when cancelling). 12. Bug correction related to messages received not belonging to any session, old stack implementation did send error response, but after response was written it did give the buffer to client anyway. Now corrected.

/*
* Copyright (c) 2004 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:    SRTP-specific packet handling routines.
*
*/




// INCLUDES
#include "srtppacketsrtp.h"
#include "srtpcryptohandler.h"
#include "srtpcryptocontext.h"
#include "srtpmasterkey.h"
#include "srtpmastersalt.h"
#include "srtputils.h"

_LIT8( KMKINULL, "NULL");

// -----------------------------------------------------------------------------
// CSRTPPacketSRTP::CSRTPPacketSRTP
// -----------------------------------------------------------------------------
//
CSRTPPacketSRTP::CSRTPPacketSRTP(const TDesC8& aPacket,
                      CSRTPCryptoHandler& aHandler)
    : CSRTPPacketRTP(aPacket, aHandler),
        iAuthenticationTag(TPtrC8()),       
        iMasterKeyIdentifier(TPtrC8())
            {
            
            }

// -----------------------------------------------------------------------------
// CSRTPPacketSRTP::~CSRTPPacketSRTP
// -----------------------------------------------------------------------------
//
CSRTPPacketSRTP::~CSRTPPacketSRTP()
            {
            
            }

// ---------------------------------------------------------------------------
// Two-phased constructor. 
// 
// ---------------------------------------------------------------------------
//
CSRTPPacketSRTP* CSRTPPacketSRTP::NewL(const TDesC8& aPacket,
                      CSRTPCryptoHandler& aHandler)
    {
    CSRTPPacketSRTP* self = new( ELeave )CSRTPPacketSRTP( aPacket, aHandler);
    CleanupStack::PushL( self );    
    self->ConstructL();
    CleanupStack::Pop( self );        
    return self;
    }

// -----------------------------------------------------------------------------
// void CSRTPPacketSRTP::ConstructL
// -----------------------------------------------------------------------------
//
void CSRTPPacketSRTP::ConstructL()
    {            
    CSRTPPacketRTP::ConstructL();
    UpdatePayloadLengthL();
    SRTP_DEBUG_TINT_VALUE( "SRTP payload length", iPayloadLength );
    
    UpdatePayload();               
    UpdateAuthenticationTagL();    
    UpdateMasterKeyIdentifier();        
    }

// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::AuthenticationTag()
// ---------------------------------------------------------------------------
//
TPtrC8 CSRTPPacketSRTP::AuthenticationTag()
    {
    SRTP_DEBUG_DETAIL( "SRTP HMAC authen tag and the tag in the packet" );
    SRTP_DEBUG_PACKET( iAuthenticationTag );    
    		
    return iAuthenticationTag;
    }


// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::MasterKeyIdentifier()
// ---------------------------------------------------------------------------
//
TPtrC8 CSRTPPacketSRTP::MasterKeyIdentifier()
    {
    return iMasterKeyIdentifier;
    }

// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::UpdatePayloadLengthL()
// ---------------------------------------------------------------------------
//
void CSRTPPacketSRTP::UpdatePayloadLengthL()
    {    
    SRTP_DEBUG_DETAIL( "CSRTPPacketSRTP::UpdatePayloadLengthL ENTRY" );

     // the real RTP packet length is 
     // the packet size - header - MKI - Auth tag
     // Need implementation of caculating RCC
     // when RCC is not included but the tag length is 14 or 8
     TUint rocLength=KSRTPROCLength4;
     TUint authTagLen=0;
     authTagLen = iHandler.Context().CryptoParams().iSrtpAuthTagLen/8;	
     if (!TagWithROCLengthL() && (
     	iHandler.Context().CryptoParams().iSrtpAuthAlg  == EAuthRCCm2 ||
     	iHandler.Context().CryptoParams().iSrtpAuthAlg  == EAuthRCCm3))
     	{
     	authTagLen -= rocLength;
     	}
     if (!TagWithROCLengthL() && (
     	iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm1))
     	{
     	authTagLen=0;
     	}	
     TUint deductedLen = HeaderLength() + iHandler.Context().MasterKey().MKI().Length() + authTagLen;     
     
     iPayloadLength = iPacket.Length() - deductedLen;    
     if ( iPacket.Length() <= HeaderLength() )
     	{
     	iPayloadLength = 0;
     	}	
    SRTP_DEBUG_TINT_VALUE( "SRTP payload length", iPayloadLength);
    SRTP_DEBUG_DETAIL( "CSRTPPacketSRTP::UpdatePayloadLengthL EXIT" );
    }
    
    
// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::CountDecryptedPacketSize()
// ---------------------------------------------------------------------------
//
TUint CSRTPPacketSRTP::CountDecryptedPacketSize()
    {
    return iHeaderLength + iPayloadLength;    
    }    
    
// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::CreateDecryptedPacketL()
// ---------------------------------------------------------------------------
//
HBufC8* CSRTPPacketSRTP::CreateDecryptedPacketL(TUint8* aDecryptedPayloadPtr)
    {
    // create decrypted RTP packet
    // first count needed packet size
    TUint rtpPacketSize = CountDecryptedPacketSize();
    
    return CopyHeaderAndPayloadL(rtpPacketSize, aDecryptedPayloadPtr);
    
    }

// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::UpdateAuthenticationTag()
// ---------------------------------------------------------------------------
//
void CSRTPPacketSRTP::UpdateAuthenticationTagL()
    {       
    //Here means MAC part
    TPtrC8 authTag = GetAuthenticationTagL();  
    if (authTag.Compare(KNullDesC8))
       	{     
    	iAuthenticationTag.Set(authTag.Ptr(), authTag.Length());
       	}
    }    

// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::GetAuthenticationTag()
// ---------------------------------------------------------------------------
//
TPtrC8 CSRTPPacketSRTP::GetAuthenticationTagL()
    {       
    if ((iHandler.Context().CryptoParams().iSrtpAuthTagLen > 0) && 
        iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthHMAC_SHA1)
        {
         TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
         TUint authTagLenInBytes = authTagLength/8;
         TUint packetLength = iPacket.Length();
         TUint position = packetLength - authTagLenInBytes;
         
         return iPacket.Mid(position,authTagLenInBytes);
        }
     if ((iHandler.Context().CryptoParams().iSrtpAuthTagLen > 0) &&
     	((iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm1 && 
     		TagWithROCLengthL()) ||
        iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm2))
     	{
     	TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
     	TUint authTagLenInBytes = authTagLength/8;//14
     	//Here we only need MAC part
     	authTagLenInBytes-= 4;
     	TUint packetLength = iPacket.Length();
        TUint position = packetLength - authTagLenInBytes;
         
        return iPacket.Mid(position,authTagLenInBytes);
     	}  
    // in case of EAuthRCCm3 and EAuthNULL  	 
    TPtrC8 authTagNull(KNullDesC8);
    return  authTagNull;    
    } 

// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::GetMasterKeyIdentifier()
// ---------------------------------------------------------------------------
//
TPtrC8 CSRTPPacketSRTP::GetMasterKeyIdentifier()
    {
    if(iHandler.Context().MasterKey().MKI()!= KNullDesC8)
    	{    
	    TInt mkiLength = iHandler.Context().MasterKey().MKI().Length();
	    if (iHandler.Context().MasterKey().MKI().Length() > 0)
	        {
	         TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
	         TUint authTagLenInBytes = authTagLength/8;
	         TUint position = iPacket.Length() - authTagLenInBytes - mkiLength;
	                                
	         return iPacket.Mid(position,mkiLength);
	        }
    	}
    TPtrC8 mkiNull( KMKINULL );
    return  mkiNull; 
    }    
           
// ---------------------------------------------------------------------------
// CSRTPPacketSRTP::UpdateMasterKeyIdentifierL()
// ---------------------------------------------------------------------------
//
void CSRTPPacketSRTP::UpdateMasterKeyIdentifier()
    {    
     TPtrC8 mki = GetMasterKeyIdentifier();
     if (mki.Compare( KMKINULL ))
     	{
     	iMasterKeyIdentifier.Set(mki.Ptr(),mki.Length());	
     	}
    }