rtp/srtpstack/src/srtpcryptohandler.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 "srtpcryptohandler.h"
       
    24 #include "srtpcryptocontext.h"
       
    25 #include "srtputils.h"
       
    26 #include "srtpstream.h"
       
    27 #include "srtpaesctrcrypto.h"
       
    28 #include "srtpkeyderivation_aescm128.h"
       
    29 #include "srtpcipher_aescm128.h"
       
    30 #include "srtpcipher_null.h"
       
    31 #include "srtpauthentication_hmac_sha1.h"
       
    32 #include "srtpauthentication_null.h"
       
    33 #include "srtpauthentication_rcc.h"
       
    34 #include "srtppacket.h"
       
    35 #include "srtpmasterkey.h"
       
    36 #include "srtpmastersalt.h"
       
    37 
       
    38 // CONSTANTS
       
    39 const TInt CSRTPCryptoHandler::iHandlerOffset = _FOFF( CSRTPCryptoHandler, iHandlerLink );
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CSRTPCryptoHandler::CSrtpCryptoHandler
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CSRTPCryptoHandler::CSRTPCryptoHandler( CSRTPStream& aStream ) :
       
    46         iStream(aStream),
       
    47         iCurrentPacket(NULL),
       
    48         iKeyDeriver(NULL),
       
    49         iAuthenticator(NULL),
       
    50         iCipher(NULL),
       
    51         iMasterDataUpdated(EFalse),
       
    52         iSessionEncrKey(NULL),
       
    53         iSessionAuthKey(NULL),
       
    54         iSessionSaltKey(NULL),
       
    55         iReKey(EFalse),
       
    56         iBitmap(NULL),
       
    57         iNumPackets(NULL)
       
    58     {
       
    59         
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CSRTPCryptoHandler::~CSrtpCryptoHandler
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CSRTPCryptoHandler::~CSRTPCryptoHandler( )
       
    67     { 
       
    68     if (&Context())
       
    69     	{
       
    70     	Context().RemoveCryptoChangeObserver(this);	
       
    71     	}
       
    72     
       
    73     delete iCurrentPacket; iCurrentPacket=NULL;
       
    74     delete iKeyDeriver; iKeyDeriver=NULL;
       
    75     delete iCipher; iCipher=NULL;
       
    76     delete iAuthenticator; iAuthenticator=NULL;        
       
    77     
       
    78     DeleteSessionKeys();
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // void CSRTPCryptoHandler::SRTPMasterKeyChanged()
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 void CSRTPCryptoHandler::SRTPMasterKeyChanged()
       
    86     {
       
    87      iMasterDataUpdated = ETrue;
       
    88     //RFC 3711 8.1
       
    89      iReKey= EFalse;
       
    90     }
       
    91         
       
    92 // ---------------------------------------------------------------------------
       
    93 // void CSRTPCryptoHandler::SRTPMasterSaltChanged()
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CSRTPCryptoHandler::SRTPMasterSaltChanged()
       
    97     {
       
    98      iMasterDataUpdated = ETrue;   
       
    99          
       
   100     }
       
   101     
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CSRTPCryptoHandler::ConstructL
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 void CSRTPCryptoHandler::ConstructL( )
       
   108     {  
       
   109     if (!Context().Valid())
       
   110     	{
       
   111     	User::Leave(KErrArgument);
       
   112     	}
       
   113     Context().AddCryptoChangeObserver(this);	
       
   114     
       
   115     iKeyDeriver = CSRTPKeyDerivation_AESCM128::NewL();
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CSRTPCryptoHandler::SetEncAndAlg ();
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CSRTPCryptoHandler::SetEncAndAuthL (TSRTPEncAlg aEngAlg, 
       
   123 										TSRTPAuthAlg aAuthAlg)
       
   124 	{
       
   125 	delete iCipher; iCipher=NULL;
       
   126     delete iAuthenticator; iAuthenticator=NULL;  
       
   127 	switch (aEngAlg)
       
   128 	    {
       
   129 	    case EEncAES_CM:
       
   130 	        {
       
   131 	        iCipher = CSRTPCipherAESCM128::NewL();        
       
   132 	        break;                
       
   133 	        }      
       
   134 	    case ENullAlg:
       
   135 	        {
       
   136 	        iCipher = CSRTPCipherNULL::NewL();        
       
   137 	        break;                
       
   138 	        }      
       
   139 	    default:          
       
   140 	        {
       
   141 	        User::Leave(KErrNotSupported);
       
   142 	        }
       
   143 	    }  
       
   144 
       
   145 	switch (aAuthAlg)
       
   146 	    {
       
   147 	    case EAuthHMAC_SHA1:
       
   148 	   
       
   149 	        {
       
   150 	        iAuthenticator = CSRTPAuthentication_HMAC_SHA1::NewL();        
       
   151 	        break;                
       
   152 	        }     
       
   153 	    case EAuthNull:  
       
   154 	    case EAuthRCCm3:
       
   155 	    	{
       
   156 	        iAuthenticator = CSRTPAuthentication_NULL::NewL();        
       
   157 	        break;                
       
   158 	        } 
       
   159 	    case EAuthRCCm1: 
       
   160 	    case EAuthRCCm2: 	                 
       
   161 	       	{
       
   162 	        iAuthenticator = CSrtpAuthentication_RCC::NewL();        
       
   163 	        break;                
       
   164 	        } 
       
   165 
       
   166 	    default:          
       
   167 	        {
       
   168 	        User::Leave(KErrNotSupported);
       
   169 	        }
       
   170 	    }  
       
   171 	}
       
   172 	
       
   173 // ---------------------------------------------------------------------------
       
   174 // CSRTPCryptoHandler::Context()
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177 CSRTPCryptoContext& CSRTPCryptoHandler::Context()
       
   178     {
       
   179     return iStream.GetCryptoContext();
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CSRTPCryptoHandler::CryptoParams()
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 const TSrtpCryptoParams& CSRTPCryptoHandler::CryptoParams()
       
   187     {
       
   188     return Context().CryptoParams();
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CSRTPCryptoHandler::DeleteSessionKeys()
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 void CSRTPCryptoHandler::DeleteSessionKeys()
       
   196     {
       
   197      delete iSessionEncrKey;
       
   198      delete iSessionAuthKey;
       
   199      delete iSessionSaltKey;  
       
   200      iSessionEncrKey = NULL;
       
   201      iSessionAuthKey = NULL;
       
   202      iSessionSaltKey = NULL;
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CSRTPCryptoHandler::Count_X()
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 void CSRTPCryptoHandler::Count_X(TUint64 a_R,
       
   210                                 TInt8 aLabel, 
       
   211                                 const TUint64 aIndexLength, 
       
   212                                 TDes8 &aRes)
       
   213     {
       
   214         
       
   215     TUint64 key_id = TSRTPUtils::Cnt_key_id(aLabel, a_R, aIndexLength);
       
   216     
       
   217     TSRTPUtils::Cnt_x(key_id, aRes, Context().MasterSalt().MasterSalt());
       
   218     
       
   219     } 
       
   220 
       
   221 
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CSRTPCryptoHandler::SetROC()
       
   225 // 
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 
       
   229 void CSRTPCryptoHandler::SetROC(  TUint32 aROC )
       
   230     {
       
   231     iStream.SetROC(aROC);
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CSRTPCryptoHandler::ROC
       
   236 // 
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 TUint32 CSRTPCryptoHandler::ROC() const
       
   240     {
       
   241     return iStream.ROC();
       
   242     }
       
   243 
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // CSRTPCryptoHandler::SSRC
       
   247 // 
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 TUint CSRTPCryptoHandler::SSRC() const
       
   251     {
       
   252     return iStream.SSRC();
       
   253     }
       
   254 
       
   255 
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // TInt CSRTPCryptoHandler::ReplayCheck()
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 TInt CSRTPCryptoHandler::ReplayCheck(TInt aDelta) 
       
   262 	{
       
   263 	TInt replayWSH= KReplayWindowSize;
       
   264 	if (CryptoParams().iReplayWindowSizeHint!=replayWSH)
       
   265 		{
       
   266 		replayWSH= CryptoParams().iReplayWindowSizeHint;
       
   267 		}
       
   268 	//Remember to initial bitmask at the first time
       
   269   	//if aDelta ==0 it might be just re-sent	
       
   270 	if (aDelta > 0) 
       
   271 		{                
       
   272 		/* new larger sequence number */
       
   273         
       
   274         if (aDelta < replayWSH) 
       
   275         	{  /* In window */
       
   276             iBitmap <<= aDelta;
       
   277             iBitmap |= 1;  /* set bit for this packet */
       
   278         	} else iBitmap = 1; /* This packet has a "way larger" */
       
   279         
       
   280         return KErrNone;   /* larger is good */
       
   281     	}
       
   282     TInt diff= -(aDelta);
       
   283     if (diff >= replayWSH) return KErrArgument; /* too old or wrapped */
       
   284 
       
   285     if (iBitmap & ((TUint64)1 << diff)) return KErrArgument; /* already seen */
       
   286     //if none of above then will mark as seen in the AddReplayIndex() function             
       
   287     return KErrNone;  /* out of order but good */
       
   288 
       
   289 	}