rtp/srtpstack/src/srtpcryptocontext.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:    .
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include <e32std.h>
       
    23 #include "srtpmasterkey.h"
       
    24 #include "srtpmastersalt.h"
       
    25 #include "srtpcryptohandler.h"
       
    26 #include "srtpdef.h"
       
    27 #include "srtpcryptocontext.h"
       
    28 #include "srtpcryptoparams.h"
       
    29 
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Two-phased constructor. Used when stream uses its own cryptographic context
       
    33 // 
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CSRTPCryptoContext* CSRTPCryptoContext::NewL( const CSRTPMasterKey* aKey,
       
    37                                             const CSRTPMasterSalt* aSalt, 
       
    38                                             const TSrtpCryptoParams& aCryptoParams)
       
    39     {
       
    40     
       
    41     
       
    42     CSRTPCryptoContext* self = CSRTPCryptoContext::NewLC( aKey,
       
    43                                                            aSalt,
       
    44                                                            aCryptoParams );
       
    45     CleanupStack::Pop( self );    
       
    46     
       
    47     return self;
       
    48     }	
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Two-phased constructor. Used when stream uses its own cryptographic context
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CSRTPCryptoContext* CSRTPCryptoContext::NewLC( const CSRTPMasterKey* aKey,
       
    56                                             const CSRTPMasterSalt* aSalt,
       
    57                                             const TSrtpCryptoParams& aCryptoParams)
       
    58     {
       
    59                                           
       
    60     CSRTPCryptoContext* self = new( ELeave )CSRTPCryptoContext( aCryptoParams );
       
    61                                                                 
       
    62                                                       
       
    63     CleanupStack::PushL( self );    
       
    64     self->ConstructL( aKey, aSalt );
       
    65     
       
    66     return self;
       
    67     }	
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CSRTPCryptoContext::CSRTPCryptoContext
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CSRTPCryptoContext::CSRTPCryptoContext(  const TSrtpCryptoParams& aCryptoParams) :
       
    74             iHandlerList(CSRTPCryptoHandler::iHandlerOffset), 
       
    75             iHandlerIter( iHandlerList ),                           
       
    76             iCryptoParams (aCryptoParams)
       
    77             
       
    78             {
       
    79             }
       
    80 
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CSRTPCryptoContext::ConstructL
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CSRTPCryptoContext::ConstructL( const CSRTPMasterKey* aKey,
       
    87                                      const CSRTPMasterSalt* aSalt)
       
    88 	{
       
    89     if( !aKey || !aSalt )
       
    90         {
       
    91         User::Leave( KErrNoMemory );
       
    92         }
       
    93     else
       
    94         {
       
    95         iKey = aKey;
       
    96         iSalt = aSalt;
       
    97         }
       
    98     
       
    99     if (!Valid())
       
   100     	{
       
   101     	User::Leave(KErrArgument);
       
   102     	}
       
   103     }
       
   104 	
       
   105 // ---------------------------------------------------------------------------
       
   106 // CSRTPCryptoContext::CSRTPCryptoContext
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 CSRTPCryptoContext::~CSRTPCryptoContext( )
       
   110            
       
   111     {
       
   112     delete iKey; iKey=NULL;
       
   113     delete iSalt; iSalt=NULL;
       
   114     iHandlerList.Reset();
       
   115     }
       
   116 
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CSRTPCryptoContext::SetMasterKey()
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C void CSRTPCryptoContext::SetMasterKey(const CSRTPMasterKey* aKey) 
       
   123     {
       
   124     if (iKey)
       
   125     	{
       
   126     	delete iKey; 
       
   127     	iKey=NULL;	
       
   128     	}
       
   129     iKey = aKey;
       
   130     
       
   131     CSRTPCryptoHandler* handler;
       
   132     iHandlerIter.SetToFirst();
       
   133     while( ( handler = iHandlerIter++ ) != NULL )
       
   134         {
       
   135         handler->SRTPMasterKeyChanged();        
       
   136         }
       
   137     }
       
   138 
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CSRTPCryptoContext::SetMasterSalt()
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 EXPORT_C  void CSRTPCryptoContext::SetMasterSalt(const CSRTPMasterSalt* aSalt) 
       
   145     {
       
   146     if(iSalt)
       
   147     	{
       
   148     	delete iSalt;
       
   149     	iSalt = NULL;	
       
   150     	}
       
   151     iSalt = aSalt;
       
   152 
       
   153     CSRTPCryptoHandler* handler;
       
   154     iHandlerIter.SetToFirst();
       
   155     while( ( handler = iHandlerIter++ ) != NULL )
       
   156         {
       
   157         handler->SRTPMasterSaltChanged();        
       
   158         }
       
   159     }
       
   160 
       
   161     
       
   162 
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CSRTPCryptoContext::AddCryptoChangeObserver()
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168  void CSRTPCryptoContext::AddCryptoChangeObserver(CSRTPCryptoHandler *aObserver)
       
   169     {
       
   170     iHandlerList.AddLast(*aObserver);
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // CSRTPCryptoContext::RemoveCryptoChangeObserver()
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177  void CSRTPCryptoContext::RemoveCryptoChangeObserver(CSRTPCryptoHandler *aObserver)
       
   178     {   
       
   179     if (!iHandlerList.IsEmpty() && FindHandler(aObserver))
       
   180         {
       
   181         iHandlerList.Remove(*aObserver);
       
   182         }
       
   183     }
       
   184 // ---------------------------------------------------------------------------
       
   185 // CSRTPCryptoContext::MasterKey()
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 EXPORT_C  const CSRTPMasterKey& CSRTPCryptoContext::MasterKey() const 
       
   189     {
       
   190     return *iKey;
       
   191     }
       
   192 // ---------------------------------------------------------------------------
       
   193 // CSRTPCryptoContext::MasterSalt()
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 EXPORT_C  const CSRTPMasterSalt& CSRTPCryptoContext::MasterSalt() const
       
   197     {
       
   198     return *iSalt;
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CSRTPCryptoContext::CryptoParams()
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 EXPORT_C const TSrtpCryptoParams& CSRTPCryptoContext::CryptoParams() 
       
   206     {
       
   207     return iCryptoParams;
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CSRTPCryptoContext::SetRCCm3Sync()
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 EXPORT_C void CSRTPCryptoContext::SetRCCm3Sync(TBool aSync)
       
   215 	{
       
   216 	iCryptoParams.iIsRCCm3Sync = aSync;
       
   217 	}
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CSRTPCryptoContext::IsEqual()
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 TBool CSRTPCryptoContext::IsEqual(const CSRTPCryptoContext& aContext)
       
   224 	{
       
   225 	TBool isEqual= EFalse;
       
   226 	if (!aContext.iKey->iKey->Compare(iKey->iKey->Des()) && 
       
   227 		!aContext.iSalt->iKey->Compare(iSalt->iKey->Des()) &&
       
   228 		!aContext.iKey->iMKI->Compare(iKey->iMKI->Des()))
       
   229 		{
       
   230 		if (aContext.iCryptoParams.iSrtpEncAlg==iCryptoParams.iSrtpEncAlg &&
       
   231 			aContext.iCryptoParams.iSrtpEncAlg==iCryptoParams.iSrtcpEncAlg &&
       
   232 			aContext.iCryptoParams.iSrtpAuthAlg==iCryptoParams.iSrtpAuthAlg  &&
       
   233 			aContext.iCryptoParams.iSrtcpAuthAlg==iCryptoParams.iSrtcpAuthAlg &&
       
   234 			aContext.iCryptoParams.iSrtpAuthTagLen==iCryptoParams.iSrtpAuthTagLen &&
       
   235 			aContext.iCryptoParams.iSrtcpAuthTagLen==iCryptoParams.iSrtcpAuthTagLen &&
       
   236 			aContext.iCryptoParams.iSrtpKeyDervRate==iCryptoParams.iSrtpKeyDervRate &&
       
   237 			aContext.iCryptoParams.iSrtcpKeyDervRate==iCryptoParams.iSrtcpKeyDervRate &&
       
   238 			aContext.iCryptoParams.iSrtpReplayProtection==iCryptoParams.iSrtpReplayProtection &&
       
   239 			aContext.iCryptoParams.iSrtcpReplayProtection==iCryptoParams.iSrtcpReplayProtection &&
       
   240 			aContext.iCryptoParams.iROCTransRate==iCryptoParams.iROCTransRate &&
       
   241 			aContext.iCryptoParams.iMasterKeysLifeTime==iCryptoParams.iMasterKeysLifeTime &&
       
   242 			aContext.iCryptoParams.iReplayWindowSizeHint==iCryptoParams.iReplayWindowSizeHint &&
       
   243 			aContext.iCryptoParams.iIsRCCm3Sync==iCryptoParams.iIsRCCm3Sync)
       
   244 		   		return !isEqual;
       
   245 		}
       
   246 	return isEqual;
       
   247 	}
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // CSRTPCryptoContext::operator=
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 const CSRTPCryptoContext& CSRTPCryptoContext::operator=(const CSRTPCryptoContext& aContext)
       
   254     {
       
   255     return aContext;
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // CSRTPCryptoContext::UpdateCryptoParams
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 void CSRTPCryptoContext::UpdateCryptoParams(const  TSrtpCryptoParams& aCryptoParams )
       
   263 	{
       
   264 	iCryptoParams = aCryptoParams;
       
   265 	}
       
   266 	
       
   267 // ---------------------------------------------------------------------------
       
   268 // CSRTPCryptoContext::FindCryptoHandler
       
   269 // ---------------------------------------------------------------------------
       
   270 //       
       
   271 
       
   272 TBool CSRTPCryptoContext::FindHandler( CSRTPCryptoHandler *aObserver )
       
   273 	{
       
   274 	TBool found=EFalse;
       
   275 	CSRTPCryptoHandler* handler=NULL;
       
   276 	
       
   277 	iHandlerIter.SetToFirst();
       
   278 	handler =iHandlerIter;
       
   279 	while ((handler=iHandlerIter++)!=NULL)
       
   280 			{
       
   281 			if (handler == aObserver)
       
   282 				{
       
   283 				found = ETrue;
       
   284 				return found;
       
   285 				}
       
   286 			}
       
   287 	return found;
       
   288 	}
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // CSRTPCryptoContext::Valid()
       
   292 // ---------------------------------------------------------------------------
       
   293 //        
       
   294 EXPORT_C  TBool CSRTPCryptoContext::Valid() const
       
   295     {
       
   296     if (!(iKey->Valid()))
       
   297         {
       
   298         return EFalse;
       
   299         }    
       
   300         
       
   301     if (!(iSalt->Valid()))
       
   302         {
       
   303         return EFalse;
       
   304         }    
       
   305         
       
   306     if ((iCryptoParams.iSrtpEncAlg!=EEncAES_CM &&
       
   307           iCryptoParams.iSrtpEncAlg!=ENullAlg) ||( 
       
   308          iCryptoParams.iSrtcpEncAlg!=EEncAES_CM &&
       
   309         	iCryptoParams.iSrtcpEncAlg!=ENullAlg))
       
   310         {
       
   311         return EFalse;
       
   312         }  
       
   313 
       
   314     if ((iCryptoParams.iSrtpAuthAlg!=EAuthHMAC_SHA1 &&
       
   315           iCryptoParams.iSrtpAuthAlg!=EAuthNull  &&
       
   316           iCryptoParams.iSrtpAuthAlg!=EAuthRCCm1 &&
       
   317           iCryptoParams.iSrtpAuthAlg!=EAuthRCCm2 &&
       
   318           iCryptoParams.iSrtpAuthAlg!=EAuthRCCm3 ) || 
       
   319           (iCryptoParams.iSrtcpAuthAlg!=EAuthHMAC_SHA1 &&
       
   320           iCryptoParams.iSrtcpAuthAlg!=EAuthNull ))
       
   321         {
       
   322         return EFalse;
       
   323         }  
       
   324 	if (iCryptoParams.iSrtpAuthAlg==EAuthHMAC_SHA1 &&
       
   325 		(iCryptoParams.iSrtpAuthTagLen!=KSRTPAuthTagLength80 &&
       
   326 		iCryptoParams.iSrtpAuthTagLen!=KSRTPAuthTagLength32
       
   327 		))
       
   328 		{
       
   329 		return EFalse;
       
   330 		}
       
   331 	
       
   332 	if (iCryptoParams.iSrtcpAuthAlg==EAuthHMAC_SHA1 &&
       
   333 		(iCryptoParams.iSrtcpAuthTagLen!=KSRTPAuthTagLength80 &&
       
   334 		iCryptoParams.iSrtcpAuthTagLen!=KSRTPAuthTagLength32
       
   335 		))
       
   336 		{
       
   337 		return EFalse;
       
   338 		}	
       
   339     if ((iCryptoParams.iSrtpAuthAlg==EAuthRCCm1 ||
       
   340           iCryptoParams.iSrtpAuthAlg==EAuthRCCm2 ) && 
       
   341           iCryptoParams.iSrtpAuthTagLen != KSRTPAuthTagLength112)
       
   342         {
       
   343         return EFalse;
       
   344         }  
       
   345 	if (iCryptoParams.iSrtpAuthAlg==EAuthRCCm3 && 
       
   346           iCryptoParams.iSrtpAuthTagLen != KSRTPAuthTagLength32)
       
   347         {
       
   348         return EFalse;
       
   349         }  
       
   350 	if ((iCryptoParams.iSrtpAuthAlg==EAuthNull  && 
       
   351           iCryptoParams.iSrtpAuthTagLen != 0)||(
       
   352           iCryptoParams.iSrtcpAuthAlg==EAuthNull  && 
       
   353           iCryptoParams.iSrtcpAuthTagLen != 0))
       
   354         {
       
   355         return EFalse;
       
   356         }  
       
   357 
       
   358     if (iCryptoParams.iPrefixLen!=KSRTPPrefixLength)
       
   359         {
       
   360         return EFalse;
       
   361         }  
       
   362         
       
   363     if (!iCryptoParams.iROCTransRate)
       
   364         {
       
   365         return EFalse;
       
   366         }      
       
   367         
       
   368     return ETrue;
       
   369     }
       
   370