rtp/srtpstack/src/srtppacketsrtp.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:    SRTP-specific packet handling routines.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "srtppacketsrtp.h"
       
    23 #include "srtpcryptohandler.h"
       
    24 #include "srtpcryptocontext.h"
       
    25 #include "srtpmasterkey.h"
       
    26 #include "srtpmastersalt.h"
       
    27 #include "srtputils.h"
       
    28 
       
    29 _LIT8( KMKINULL, "NULL");
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CSRTPPacketSRTP::CSRTPPacketSRTP
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CSRTPPacketSRTP::CSRTPPacketSRTP(const TDesC8& aPacket,
       
    36                       CSRTPCryptoHandler& aHandler)
       
    37     : CSRTPPacketRTP(aPacket, aHandler),
       
    38         iAuthenticationTag(TPtrC8()),       
       
    39         iMasterKeyIdentifier(TPtrC8())
       
    40             {
       
    41             
       
    42             }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CSRTPPacketSRTP::~CSRTPPacketSRTP
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CSRTPPacketSRTP::~CSRTPPacketSRTP()
       
    49             {
       
    50             
       
    51             }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // Two-phased constructor. 
       
    55 // 
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CSRTPPacketSRTP* CSRTPPacketSRTP::NewL(const TDesC8& aPacket,
       
    59                       CSRTPCryptoHandler& aHandler)
       
    60     {
       
    61     CSRTPPacketSRTP* self = new( ELeave )CSRTPPacketSRTP( aPacket, aHandler);
       
    62     CleanupStack::PushL( self );    
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop( self );        
       
    65     return self;
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // void CSRTPPacketSRTP::ConstructL
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void CSRTPPacketSRTP::ConstructL()
       
    73     {            
       
    74     CSRTPPacketRTP::ConstructL();
       
    75     UpdatePayloadLengthL();
       
    76     SRTP_DEBUG_TINT_VALUE( "SRTP payload length", iPayloadLength );
       
    77     
       
    78     UpdatePayload();               
       
    79     UpdateAuthenticationTagL();    
       
    80     UpdateMasterKeyIdentifier();        
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CSRTPPacketSRTP::AuthenticationTag()
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 TPtrC8 CSRTPPacketSRTP::AuthenticationTag()
       
    88     {
       
    89     SRTP_DEBUG_DETAIL( "SRTP HMAC authen tag and the tag in the packet" );
       
    90     SRTP_DEBUG_PACKET( iAuthenticationTag );    
       
    91     		
       
    92     return iAuthenticationTag;
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CSRTPPacketSRTP::MasterKeyIdentifier()
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 TPtrC8 CSRTPPacketSRTP::MasterKeyIdentifier()
       
   101     {
       
   102     return iMasterKeyIdentifier;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // CSRTPPacketSRTP::UpdatePayloadLengthL()
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CSRTPPacketSRTP::UpdatePayloadLengthL()
       
   110     {    
       
   111     SRTP_DEBUG_DETAIL( "CSRTPPacketSRTP::UpdatePayloadLengthL ENTRY" );
       
   112 
       
   113      // the real RTP packet length is 
       
   114      // the packet size - header - MKI - Auth tag
       
   115      // Need implementation of caculating RCC
       
   116      // when RCC is not included but the tag length is 14 or 8
       
   117      TUint rocLength=KSRTPROCLength4;
       
   118      TUint authTagLen=0;
       
   119      authTagLen = iHandler.Context().CryptoParams().iSrtpAuthTagLen/8;	
       
   120      if (!TagWithROCLengthL() && (
       
   121      	iHandler.Context().CryptoParams().iSrtpAuthAlg  == EAuthRCCm2 ||
       
   122      	iHandler.Context().CryptoParams().iSrtpAuthAlg  == EAuthRCCm3))
       
   123      	{
       
   124      	authTagLen -= rocLength;
       
   125      	}
       
   126      if (!TagWithROCLengthL() && (
       
   127      	iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm1))
       
   128      	{
       
   129      	authTagLen=0;
       
   130      	}	
       
   131      TUint deductedLen = HeaderLength() + iHandler.Context().MasterKey().MKI().Length() + authTagLen;     
       
   132      
       
   133      iPayloadLength = iPacket.Length() - deductedLen;    
       
   134      if ( iPacket.Length() <= HeaderLength() )
       
   135      	{
       
   136      	iPayloadLength = 0;
       
   137      	}	
       
   138     SRTP_DEBUG_TINT_VALUE( "SRTP payload length", iPayloadLength);
       
   139     SRTP_DEBUG_DETAIL( "CSRTPPacketSRTP::UpdatePayloadLengthL EXIT" );
       
   140     }
       
   141     
       
   142     
       
   143 // ---------------------------------------------------------------------------
       
   144 // CSRTPPacketSRTP::CountDecryptedPacketSize()
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 TUint CSRTPPacketSRTP::CountDecryptedPacketSize()
       
   148     {
       
   149     return iHeaderLength + iPayloadLength;    
       
   150     }    
       
   151     
       
   152 // ---------------------------------------------------------------------------
       
   153 // CSRTPPacketSRTP::CreateDecryptedPacketL()
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 HBufC8* CSRTPPacketSRTP::CreateDecryptedPacketL(TUint8* aDecryptedPayloadPtr)
       
   157     {
       
   158     // create decrypted RTP packet
       
   159     // first count needed packet size
       
   160     TUint rtpPacketSize = CountDecryptedPacketSize();
       
   161     
       
   162     return CopyHeaderAndPayloadL(rtpPacketSize, aDecryptedPayloadPtr);
       
   163     
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // CSRTPPacketSRTP::UpdateAuthenticationTag()
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CSRTPPacketSRTP::UpdateAuthenticationTagL()
       
   171     {       
       
   172     //Here means MAC part
       
   173     TPtrC8 authTag = GetAuthenticationTagL();  
       
   174     if (authTag.Compare(KNullDesC8))
       
   175        	{     
       
   176     	iAuthenticationTag.Set(authTag.Ptr(), authTag.Length());
       
   177        	}
       
   178     }    
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // CSRTPPacketSRTP::GetAuthenticationTag()
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 TPtrC8 CSRTPPacketSRTP::GetAuthenticationTagL()
       
   185     {       
       
   186     if ((iHandler.Context().CryptoParams().iSrtpAuthTagLen > 0) && 
       
   187         iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthHMAC_SHA1)
       
   188         {
       
   189          TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
       
   190          TUint authTagLenInBytes = authTagLength/8;
       
   191          TUint packetLength = iPacket.Length();
       
   192          TUint position = packetLength - authTagLenInBytes;
       
   193          
       
   194          return iPacket.Mid(position,authTagLenInBytes);
       
   195         }
       
   196      if ((iHandler.Context().CryptoParams().iSrtpAuthTagLen > 0) &&
       
   197      	((iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm1 && 
       
   198      		TagWithROCLengthL()) ||
       
   199         iHandler.Context().CryptoParams().iSrtpAuthAlg == EAuthRCCm2))
       
   200      	{
       
   201      	TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
       
   202      	TUint authTagLenInBytes = authTagLength/8;//14
       
   203      	//Here we only need MAC part
       
   204      	authTagLenInBytes-= 4;
       
   205      	TUint packetLength = iPacket.Length();
       
   206         TUint position = packetLength - authTagLenInBytes;
       
   207          
       
   208         return iPacket.Mid(position,authTagLenInBytes);
       
   209      	}  
       
   210     // in case of EAuthRCCm3 and EAuthNULL  	 
       
   211     TPtrC8 authTagNull(KNullDesC8);
       
   212     return  authTagNull;    
       
   213     } 
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CSRTPPacketSRTP::GetMasterKeyIdentifier()
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 TPtrC8 CSRTPPacketSRTP::GetMasterKeyIdentifier()
       
   220     {
       
   221     if(iHandler.Context().MasterKey().MKI()!= KNullDesC8)
       
   222     	{    
       
   223 	    TInt mkiLength = iHandler.Context().MasterKey().MKI().Length();
       
   224 	    if (iHandler.Context().MasterKey().MKI().Length() > 0)
       
   225 	        {
       
   226 	         TUint authTagLength = iHandler.Context().CryptoParams().iSrtpAuthTagLen;
       
   227 	         TUint authTagLenInBytes = authTagLength/8;
       
   228 	         TUint position = iPacket.Length() - authTagLenInBytes - mkiLength;
       
   229 	                                
       
   230 	         return iPacket.Mid(position,mkiLength);
       
   231 	        }
       
   232     	}
       
   233     TPtrC8 mkiNull( KMKINULL );
       
   234     return  mkiNull; 
       
   235     }    
       
   236            
       
   237 // ---------------------------------------------------------------------------
       
   238 // CSRTPPacketSRTP::UpdateMasterKeyIdentifierL()
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CSRTPPacketSRTP::UpdateMasterKeyIdentifier()
       
   242     {    
       
   243      TPtrC8 mki = GetMasterKeyIdentifier();
       
   244      if (mki.Compare( KMKINULL ))
       
   245      	{
       
   246      	iMasterKeyIdentifier.Set(mki.Ptr(),mki.Length());	
       
   247      	}
       
   248     }    
       
   249