rtp/srtpstack/src/srtpcryptohandler.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:    .
*
*/




// INCLUDES
#include <e32std.h>
#include "srtpcryptohandler.h"
#include "srtpcryptocontext.h"
#include "srtputils.h"
#include "srtpstream.h"
#include "srtpaesctrcrypto.h"
#include "srtpkeyderivation_aescm128.h"
#include "srtpcipher_aescm128.h"
#include "srtpcipher_null.h"
#include "srtpauthentication_hmac_sha1.h"
#include "srtpauthentication_null.h"
#include "srtpauthentication_rcc.h"
#include "srtppacket.h"
#include "srtpmasterkey.h"
#include "srtpmastersalt.h"

// CONSTANTS
const TInt CSRTPCryptoHandler::iHandlerOffset = _FOFF( CSRTPCryptoHandler, iHandlerLink );

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::CSrtpCryptoHandler
// ---------------------------------------------------------------------------
//
CSRTPCryptoHandler::CSRTPCryptoHandler( CSRTPStream& aStream ) :
        iStream(aStream),
        iCurrentPacket(NULL),
        iKeyDeriver(NULL),
        iAuthenticator(NULL),
        iCipher(NULL),
        iMasterDataUpdated(EFalse),
        iSessionEncrKey(NULL),
        iSessionAuthKey(NULL),
        iSessionSaltKey(NULL),
        iReKey(EFalse),
        iBitmap(NULL),
        iNumPackets(NULL)
    {
        
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::~CSrtpCryptoHandler
// ---------------------------------------------------------------------------
//
CSRTPCryptoHandler::~CSRTPCryptoHandler( )
    { 
    if (&Context())
    	{
    	Context().RemoveCryptoChangeObserver(this);	
    	}
    
    delete iCurrentPacket; iCurrentPacket=NULL;
    delete iKeyDeriver; iKeyDeriver=NULL;
    delete iCipher; iCipher=NULL;
    delete iAuthenticator; iAuthenticator=NULL;        
    
    DeleteSessionKeys();
    }

// ---------------------------------------------------------------------------
// void CSRTPCryptoHandler::SRTPMasterKeyChanged()
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::SRTPMasterKeyChanged()
    {
     iMasterDataUpdated = ETrue;
    //RFC 3711 8.1
     iReKey= EFalse;
    }
        
// ---------------------------------------------------------------------------
// void CSRTPCryptoHandler::SRTPMasterSaltChanged()
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::SRTPMasterSaltChanged()
    {
     iMasterDataUpdated = ETrue;   
         
    }
    

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::ConstructL
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::ConstructL( )
    {  
    if (!Context().Valid())
    	{
    	User::Leave(KErrArgument);
    	}
    Context().AddCryptoChangeObserver(this);	
    
    iKeyDeriver = CSRTPKeyDerivation_AESCM128::NewL();
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::SetEncAndAlg ();
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::SetEncAndAuthL (TSRTPEncAlg aEngAlg, 
										TSRTPAuthAlg aAuthAlg)
	{
	delete iCipher; iCipher=NULL;
    delete iAuthenticator; iAuthenticator=NULL;  
	switch (aEngAlg)
	    {
	    case EEncAES_CM:
	        {
	        iCipher = CSRTPCipherAESCM128::NewL();        
	        break;                
	        }      
	    case ENullAlg:
	        {
	        iCipher = CSRTPCipherNULL::NewL();        
	        break;                
	        }      
	    default:          
	        {
	        User::Leave(KErrNotSupported);
	        }
	    }  

	switch (aAuthAlg)
	    {
	    case EAuthHMAC_SHA1:
	   
	        {
	        iAuthenticator = CSRTPAuthentication_HMAC_SHA1::NewL();        
	        break;                
	        }     
	    case EAuthNull:  
	    case EAuthRCCm3:
	    	{
	        iAuthenticator = CSRTPAuthentication_NULL::NewL();        
	        break;                
	        } 
	    case EAuthRCCm1: 
	    case EAuthRCCm2: 	                 
	       	{
	        iAuthenticator = CSrtpAuthentication_RCC::NewL();        
	        break;                
	        } 

	    default:          
	        {
	        User::Leave(KErrNotSupported);
	        }
	    }  
	}
	
// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::Context()
// ---------------------------------------------------------------------------
//
CSRTPCryptoContext& CSRTPCryptoHandler::Context()
    {
    return iStream.GetCryptoContext();
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::CryptoParams()
// ---------------------------------------------------------------------------
//
const TSrtpCryptoParams& CSRTPCryptoHandler::CryptoParams()
    {
    return Context().CryptoParams();
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::DeleteSessionKeys()
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::DeleteSessionKeys()
    {
     delete iSessionEncrKey;
     delete iSessionAuthKey;
     delete iSessionSaltKey;  
     iSessionEncrKey = NULL;
     iSessionAuthKey = NULL;
     iSessionSaltKey = NULL;
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::Count_X()
// ---------------------------------------------------------------------------
//
void CSRTPCryptoHandler::Count_X(TUint64 a_R,
                                TInt8 aLabel, 
                                const TUint64 aIndexLength, 
                                TDes8 &aRes)
    {
        
    TUint64 key_id = TSRTPUtils::Cnt_key_id(aLabel, a_R, aIndexLength);
    
    TSRTPUtils::Cnt_x(key_id, aRes, Context().MasterSalt().MasterSalt());
    
    } 



// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::SetROC()
// 
// ---------------------------------------------------------------------------
//

void CSRTPCryptoHandler::SetROC(  TUint32 aROC )
    {
    iStream.SetROC(aROC);
    }

// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::ROC
// 
// ---------------------------------------------------------------------------
//
TUint32 CSRTPCryptoHandler::ROC() const
    {
    return iStream.ROC();
    }


// ---------------------------------------------------------------------------
// CSRTPCryptoHandler::SSRC
// 
// ---------------------------------------------------------------------------
//
TUint CSRTPCryptoHandler::SSRC() const
    {
    return iStream.SSRC();
    }



// ---------------------------------------------------------------------------
// TInt CSRTPCryptoHandler::ReplayCheck()
// ---------------------------------------------------------------------------
//
TInt CSRTPCryptoHandler::ReplayCheck(TInt aDelta) 
	{
	TInt replayWSH= KReplayWindowSize;
	if (CryptoParams().iReplayWindowSizeHint!=replayWSH)
		{
		replayWSH= CryptoParams().iReplayWindowSizeHint;
		}
	//Remember to initial bitmask at the first time
  	//if aDelta ==0 it might be just re-sent	
	if (aDelta > 0) 
		{                
		/* new larger sequence number */
        
        if (aDelta < replayWSH) 
        	{  /* In window */
            iBitmap <<= aDelta;
            iBitmap |= 1;  /* set bit for this packet */
        	} else iBitmap = 1; /* This packet has a "way larger" */
        
        return KErrNone;   /* larger is good */
    	}
    TInt diff= -(aDelta);
    if (diff >= replayWSH) return KErrArgument; /* too old or wrapped */

    if (iBitmap & ((TUint64)1 << diff)) return KErrArgument; /* already seen */
    //if none of above then will mark as seen in the AddReplayIndex() function             
    return KErrNone;  /* out of order but good */

	}