rtp/srtpstack/src/srtpsession.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2004 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:    Contains a default cryptographic context for SRTP streams.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "srtpsession.h"
       
    23 #include "srtpstream.h"
       
    24 #include "srtpcryptocontext.h"
       
    25 #include "srtpstreamin.h"
       
    26 #include "srtpstreamout.h"
       
    27 #include "msrtprekeyingobserver.h"
       
    28 #include "srtputils.h"
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Two-phased constructor. Used when stream uses default cryptographic context
       
    32 // 
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CSRTPSession* CSRTPSession::NewL( const TInetAddr& aDestination )
       
    36     {
       
    37     CSRTPSession* self = CSRTPSession::NewLC( aDestination );
       
    38     CleanupStack::Pop( self );
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Two-phased constructor. Used when stream uses its own cryptographic context
       
    44 // 
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C CSRTPSession* CSRTPSession::NewL( const TInetAddr& aDestination,
       
    48                                      CSRTPCryptoContext* aCon,
       
    49                                      MSRTPReKeyingObserver& aObs ) 
       
    50     {
       
    51     CSRTPSession* self = CSRTPSession::NewLC( aDestination, aCon, aObs );
       
    52     CleanupStack::Pop( self );
       
    53     return self;    
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Two-phased constructor. Used when stream uses its own cryptographic context
       
    59 // 
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C CSRTPSession* CSRTPSession::NewLC( const TInetAddr& aDestination )
       
    63     {                                     
       
    64     CSRTPSession* self = new( ELeave ) CSRTPSession ( aDestination );                                      
       
    65     CleanupStack::PushL( self );    
       
    66     return self;
       
    67     }	
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Two-phased constructor. Used when stream uses its own cryptographic context
       
    71 // 
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C CSRTPSession* CSRTPSession::NewLC( const TInetAddr& aDestination,
       
    75                                      CSRTPCryptoContext* aCon,
       
    76                                      MSRTPReKeyingObserver& aObs )
       
    77     {                                     
       
    78     CSRTPSession* self = new( ELeave ) CSRTPSession ( aDestination, aObs  );
       
    79     CleanupStack::PushL( self ); 
       
    80     self->ConstructL( aCon );                                      
       
    81     return self;
       
    82     }	
       
    83         
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CSRTPSession::CSRTPSession
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89  CSRTPSession::CSRTPSession( const TInetAddr& aDestination,
       
    90                              MSRTPReKeyingObserver& aObs )
       
    91     :   iStreamList(CSRTPStream::iStreamOffset),
       
    92         iStreamIter( iStreamList ),
       
    93         iDestination(aDestination),
       
    94         iObserver(&aObs),
       
    95         iStreamCount(NULL),    
       
    96         iRekey(EFalse)    
       
    97     {
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CSRTPSession::CSRTPSession
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104  CSRTPSession::CSRTPSession(const TInetAddr& aDestination)
       
   105     :   iStreamList(CSRTPStream::iStreamOffset),
       
   106         iStreamIter( iStreamList ),
       
   107         iDestination(aDestination),
       
   108         iContext(NULL),
       
   109         iObserver(NULL),
       
   110         iStreamCount(NULL),    
       
   111         iSesssionCrypto(EFalse),
       
   112         iRekey(EFalse)             
       
   113     {
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CSRTPSession::ConstructL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CSRTPSession::ConstructL( CSRTPCryptoContext* aCon )
       
   121 	{
       
   122 	if( !aCon )
       
   123 	    {
       
   124 	    //delete this;// because of NewLC so we need to delete it
       
   125 	    User::Leave( KErrArgument );
       
   126 	    }
       
   127 	else 
       
   128 	    {
       
   129 	    if (!aCon->Valid())
       
   130 		    {
       
   131 		    User::Leave( KErrArgument );
       
   132 		    }
       
   133 		iSesssionCrypto=ETrue;
       
   134 		iContext = aCon;
       
   135 		    
       
   136 	    }
       
   137 	}
       
   138 	
       
   139 	
       
   140 // ---------------------------------------------------------------------------
       
   141 // CSRTPSession::~CSRTPSession
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 CSRTPSession::~CSRTPSession()
       
   145     {
       
   146     if (iContext)
       
   147     	{
       
   148     	delete iContext;iContext=NULL;	
       
   149     	}
       
   150     if (iStreamCount)
       
   151     	{
       
   152     	RemoveAllStream( );
       
   153     	}
       
   154     iStreamList.Reset();	
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CSRTPSession::UpdateCryptoContextL
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 EXPORT_C void CSRTPSession::SetCryptoContextL(CSRTPCryptoContext* aCon)
       
   162 	{
       
   163 	//Note that ROC is not re-set.
       
   164 	if( !aCon)
       
   165 	    {
       
   166 	    User::Leave( KErrArgument );
       
   167 	    }
       
   168 	if (!iRekey )
       
   169 		{
       
   170 		if (iContext && iSesssionCrypto )
       
   171 			{
       
   172 			delete iContext;iContext=NULL;
       
   173 			}
       
   174 		iSesssionCrypto = ETrue;
       
   175 		iContext = aCon;
       
   176 		//should implement a way that session knows how to updated the stream
       
   177 		if (!iStreamList.IsEmpty())
       
   178 			{
       
   179 			CSRTPStream* item=NULL;
       
   180 	
       
   181 			iStreamIter.SetToFirst();
       
   182 			while ((item=iStreamIter++) != NULL)
       
   183 				{
       
   184 				TRAPD( err, item->UpdateCryptoAndStatesL() );
       
   185 				if ( err )
       
   186 				    {
       
   187 				    iContext = NULL; // Cannot take ownership if leave occurs
       
   188 				    User::Leave( err );
       
   189 				    }
       
   190 				}
       
   191 			}
       
   192 		
       
   193 		}
       
   194 	}
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CSRTPSession::RemoteAddr()
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C const TInetAddr& CSRTPSession::RemoteAddr() 
       
   201 	{
       
   202 	return iDestination;
       
   203 	}
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CSRTPSession::StreamL
       
   207 // ---------------------------------------------------------------------------
       
   208 //       
       
   209 
       
   210 EXPORT_C CSRTPStream& CSRTPSession::StreamL(TUint aSSRC, TBool aIsInStream)
       
   211 	{
       
   212 	CSRTPStream* item=NULL;
       
   213 	
       
   214 	iStreamIter.SetToFirst();
       
   215 	while ((item=iStreamIter++) != NULL)
       
   216 			{
       
   217 			if (item->SSRC()==aSSRC && aIsInStream== item->StreamType())
       
   218 				return *item;
       
   219 			}
       
   220 	User::Leave(KErrNotFound);
       
   221 	return *item;
       
   222 	}
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CSRTPSession::ProtectRTPL
       
   226 // ---------------------------------------------------------------------------
       
   227 // 
       
   228 EXPORT_C HBufC8* CSRTPSession::ProtectRTPL(TUint aSSRC, const TDesC8& aPacket)
       
   229 	{
       
   230 	SRTP_DEBUG_DETAIL( "CSRTPSession::ProtectRTPL ENTRY" );
       
   231   	
       
   232 	//find the matching ssrc stream
       
   233 	CSRTPStreamOut* stream = static_cast<CSRTPStreamOut*> (&StreamL(aSSRC, EFalse));
       
   234 	SRTP_DEBUG_TUINT_VALUE( "aSSRC is", aSSRC);
       
   235      
       
   236 	//If it is streamIn
       
   237 	SRTP_DEBUG_DETAIL( "CSRTPSession::ProtectRTPL EXIT" );
       
   238 
       
   239 	return stream->ProtectRtpL( aPacket );
       
   240 	}
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // CSRTPSession::UnprotectRTPL
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 EXPORT_C HBufC8* CSRTPSession::UnprotectRTPL(TUint aSSRC, const TDesC8& aPacket)
       
   247 	{
       
   248 	SRTP_DEBUG_DETAIL( "CSRTPSession::UnprotectRTPL ENTRY" );
       
   249   	   
       
   250 	//Find Stream by SSRC
       
   251 	CSRTPStreamIn* stream;
       
   252 	if(FindStream(aSSRC, ETrue))
       
   253 		{
       
   254 		SRTP_DEBUG_TUINT_VALUE( "not latebinding and stream found and aSSRC is", aSSRC);
       
   255     
       
   256 		 stream= static_cast<CSRTPStreamIn*> (&StreamL(aSSRC, ETrue));
       
   257 		//If found
       
   258 		return stream->UnprotectSrtpL( aPacket );
       
   259 		}
       
   260 	//If not found, try to find the stream SSRC=0;	
       
   261 	SRTP_DEBUG_DETAIL( "UnprotectRTPL is latebinding and If not found, try to find the stream SSRC=0");
       
   262     
       
   263     SRTP_DEBUG_DETAIL( "CSRTPSession::UnprotectRTPL EXIT" );		
       
   264 	return FindLateBindingStreamAndUnprotectRTPL(aSSRC, aPacket);
       
   265 		
       
   266 	}
       
   267 	
       
   268 // ---------------------------------------------------------------------------
       
   269 // CSRTPSession::ProtectRTCPL
       
   270 // ---------------------------------------------------------------------------
       
   271 // 
       
   272 EXPORT_C HBufC8* CSRTPSession::ProtectRTCPL(TUint aSSRC, const TDesC8& aPacket)
       
   273 	{
       
   274 	//find the matching ssrc stream
       
   275 	CSRTPStreamOut* stream = static_cast<CSRTPStreamOut*> (&StreamL(aSSRC, EFalse));
       
   276 	//If it is streamIn
       
   277 	return stream->ProtectRtcpL( aPacket );
       
   278 	}
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // CSRTPSession::UnprotectRTPL
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C HBufC8* CSRTPSession::UnprotectRTCPL(TUint aSSRC, const TDesC8& aPacket)
       
   285 	{
       
   286 	//Find Stream by SSRC
       
   287 	CSRTPStreamIn* stream;
       
   288 	if(FindStream(aSSRC, ETrue) )
       
   289 		{
       
   290 		 stream= static_cast<CSRTPStreamIn*> (&StreamL(aSSRC, ETrue));
       
   291 		//If found
       
   292 		return stream->UnprotectSrtcpL( aPacket );
       
   293 		}
       
   294 	//If not found, return NULL since there must already some RTP packet received	
       
   295 	// But for outgoing stream to receive RR and SDES so it should be able to 
       
   296 	// decod this RTCP packet as well
       
   297 	return FindLateBindingStreamAndUnprotectRTCPL(aSSRC, aPacket);
       
   298 		
       
   299 	}	
       
   300 // ---------------------------------------------------------------------------
       
   301 // CSRTPSession::FindStream
       
   302 // ---------------------------------------------------------------------------
       
   303 //       
       
   304 
       
   305 TBool CSRTPSession::FindStream(TUint aSSRC, TBool aIsInStream )
       
   306 	{
       
   307 	TBool found=EFalse;
       
   308 	TInt count = iStreamCount;
       
   309 	CSRTPStream* item=NULL;
       
   310 	
       
   311 	iStreamIter.SetToFirst();
       
   312 	item=iStreamIter;
       
   313 	while ((item=iStreamIter++) != NULL && count !=0)
       
   314 			{
       
   315 			if (item->SSRC()==aSSRC &&  (aIsInStream== item->StreamType()))
       
   316 				{
       
   317 				found = ETrue;
       
   318 				return found;
       
   319 				}
       
   320 			count --;
       
   321 			}
       
   322 	return found;
       
   323 	}
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // CSRTPSession::FindLateBindingStreamAndUnprotect
       
   327 // ---------------------------------------------------------------------------
       
   328 //       
       
   329 
       
   330 HBufC8* CSRTPSession::FindLateBindingStreamAndUnprotectRTPL(TUint aSSRC, 
       
   331 													const TDesC8& aPacket)
       
   332 	{
       
   333 	SRTP_DEBUG_DETAIL( "CSRTPSession::FindLateBindingStreamAndUnprotectRTPL ENTRY" );
       
   334   	
       
   335 	CSRTPStream* item=NULL;
       
   336 	
       
   337 	iStreamIter.SetToFirst();
       
   338 	while ((item=iStreamIter++) != NULL)
       
   339 			{
       
   340 			//In the case of late binding state unprotect failed but SSRC 
       
   341     		// has set; we should set it again to the corrected one in the 
       
   342     		// case SSRC is different from previous one 											
       
   343 			if ((item->StreamType()))
       
   344 				{
       
   345 				if ((item->SSRC()==0) || 
       
   346 					(item->SSRC()!=0 && 
       
   347 						static_cast<CSRTPStreamIn*> (item)->iCurrentRTPState== 
       
   348 						MSRTPStreamInContext::ESRTPStreamInLateBinding) 
       
   349 						)
       
   350 					{
       
   351 					SRTP_DEBUG_TUINT_VALUE( "FindLateBindingStreamAndUnprotectRTPL and ssrc is", aSSRC );
       
   352 		  								
       
   353 					item->SetSSRC(aSSRC);
       
   354 					HBufC8* packet=	static_cast<CSRTPStreamIn*> 
       
   355 									(item)->UnprotectSrtpL( aPacket );
       
   356 					return packet;				
       
   357 					}
       
   358 	 			}
       
   359 			}
       
   360 	
       
   361 	SRTP_DEBUG_TUINT_VALUE( "NOT Find any match SSRC in FindLBStrmUnprotectRTPL and ssrc is", aSSRC);
       
   362 				 		
       
   363 	//If not found stream ssrc =0it leaves
       
   364 	User::Leave(KErrCorrupt);
       
   365 	//keep away warnings
       
   366 	SRTP_DEBUG_DETAIL( "CSRTPSession::FindLateBindingStreamAndUnprotectRTPL EXIT" );
       
   367   	
       
   368 	return NULL;
       
   369 	}
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 // CSRTPSession::FindLateBindingStreamAndUnprotectRTCP
       
   373 // ---------------------------------------------------------------------------
       
   374 //       
       
   375 
       
   376 HBufC8* CSRTPSession::FindLateBindingStreamAndUnprotectRTCPL(TUint aSSRC, 
       
   377 													const TDesC8& aPacket)
       
   378 	{
       
   379 	SRTP_DEBUG_DETAIL( "CSRTPSession::FindLateBindingStreamAndUnprotectRTCPL ENTRY" );
       
   380   	
       
   381 	CSRTPStream* item=NULL;
       
   382 	
       
   383 	iStreamIter.SetToFirst();
       
   384 	while ((item=iStreamIter++) != NULL)
       
   385 			{
       
   386 			//Only InStreams are handled for received RTCP Packets.
       
   387 			if (item->StreamType())
       
   388 				{
       
   389 			//In the case of late binding state unprotect failed but SSRC 
       
   390     		// has set; we should set it again to the corrected one in the 
       
   391     		// case SSRC is different from previous one 											
       
   392 		
       
   393 			if ((item->SSRC()==0) || 
       
   394 				(item->SSRC()!=0 && 
       
   395 					static_cast<CSRTPStreamIn*> (item)->iCurrentRTCPState== 
       
   396 					MSRTPStreamInContext::ESRTPStreamInLateBinding))
       
   397 				{
       
   398 					CSRTPStreamIn *pStrmIn = static_cast<CSRTPStreamIn*> (item);
       
   399 
       
   400 					if(pStrmIn->SSRC()!=0 && pStrmIn->iCurrentRTPState == MSRTPStreamInContext::ESRTPStreamInNormal)
       
   401 						{
       
   402 						if(pStrmIn->SSRC() != aSSRC )
       
   403 							{
       
   404 							User::Leave(KErrCorrupt);
       
   405 							}
       
   406 						}
       
   407 				item->SetSSRC(aSSRC);
       
   408 				HBufC8* packet=	static_cast<CSRTPStreamIn*> 
       
   409 								(item)->UnprotectSrtcpL( aPacket );
       
   410 				return packet;				
       
   411 				}
       
   412 	 		SRTP_DEBUG_TUINT_VALUE( "FindLateBindingStreamAndUnprotectRTCPL and ssrc is", aSSRC);
       
   413 				}
       
   414 			}
       
   415 	//If not found stream ssrc =0it leaves
       
   416 	User::Leave(KErrCorrupt);
       
   417 	//keep away warnings
       
   418 	SRTP_DEBUG_DETAIL( "CSRTPSession::FindLateBindingStreamAndUnprotectRTCPL EXIT" );
       
   419   	
       
   420 	return NULL;
       
   421 	}
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // CSRTPSession::AddStreamToList()
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 void CSRTPSession::AddStreamToList(CSRTPStream *aStream)
       
   428     {   
       
   429     iStreamList.AddLast(*aStream);
       
   430     ++iStreamCount;
       
   431     }
       
   432 
       
   433 // ---------------------------------------------------------------------------
       
   434 // CSRTPSession::RemoveStreamFromListL()
       
   435 // ---------------------------------------------------------------------------
       
   436 //
       
   437 void CSRTPSession::RemoveStreamFromList(CSRTPStream *aStream)
       
   438     {   
       
   439     //when there are 2 same stream ...it could not remove
       
   440     TBool found = FindStream(aStream->SSRC(), aStream->StreamType() );
       
   441     if (found)
       
   442     	{
       
   443         iStreamList.Remove(*aStream);
       
   444 	    --iStreamCount;
       
   445     	}
       
   446     }
       
   447 
       
   448 // ---------------------------------------------------------------------------
       
   449 // CSRTPSession::RemoveAllStream
       
   450 // ---------------------------------------------------------------------------
       
   451 //       
       
   452 
       
   453 void CSRTPSession::RemoveAllStream( )
       
   454 	{
       
   455 	CSRTPStream* item=NULL;
       
   456 	iStreamIter.SetToFirst();
       
   457 	item=iStreamIter;
       
   458 	while ((item=iStreamIter++) != NULL)
       
   459 		{
       
   460 		delete item; item=NULL;
       
   461 		}
       
   462 	}
       
   463 // ---------------------------------------------------------------------------
       
   464 // void CSRTPSession::GetCryptoContext()
       
   465 // 
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 CSRTPCryptoContext& CSRTPSession::GetCryptoContext()
       
   469     {
       
   470      return *iContext;
       
   471     }
       
   472 
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // void CSRTPSession::ReKeyNeeded()
       
   476 // 
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 void CSRTPSession::ReKeyNeeded(const CSRTPStream& aStream, TBool aIsStrmCrypto)
       
   480     {
       
   481     if (iObserver)
       
   482 		{
       
   483 		if (aIsStrmCrypto)
       
   484 			{
       
   485 			iObserver->SRTPMasterKeyStaleEvent(aStream);
       
   486 			}
       
   487 		else
       
   488 			{
       
   489 			iObserver->SRTPMasterKeyStaleEvent(*this);
       
   490 			}	
       
   491 		}
       
   492 	}