rtp/srtpstack/src/srtppacketsrtcp.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:    SRTCP-specific packet handling routines.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "srtppacketsrtcp.h"
       
    23 #include "srtpcryptohandler.h"
       
    24 #include "srtpcryptocontext.h"
       
    25 #include "srtputils.h"
       
    26 #include "srtpmasterkey.h"
       
    27 #include "srtpmastersalt.h"
       
    28 
       
    29 _LIT8( KMKINULL, "NULL");
       
    30 // -----------------------------------------------------------------------------
       
    31 // CSRTPPacketSRTCP::CSRTPPacketSRTCP
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CSRTPPacketSRTCP::CSRTPPacketSRTCP(const TDesC8& aPacket,
       
    35                       CSRTPCryptoHandler& aHandler)
       
    36     : CSRTPPacketRTCP(aPacket, aHandler),
       
    37         iAuthenticationTag(TPtrC8()),       
       
    38         iMasterKeyIdentifier(TPtrC8())
       
    39             {
       
    40             
       
    41             }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSRTPPacketSRTCP::~CSRTPPacketSRTCP
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CSRTPPacketSRTCP::~CSRTPPacketSRTCP()
       
    48             {
       
    49             
       
    50             }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Two-phased constructor. 
       
    54 // 
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CSRTPPacketSRTCP* CSRTPPacketSRTCP::NewL(const TDesC8& aPacket,
       
    58                       CSRTPCryptoHandler& aHandler)
       
    59     {
       
    60     CSRTPPacketSRTCP* self = new( ELeave )CSRTPPacketSRTCP( aPacket, aHandler);
       
    61     CleanupStack::PushL( self );    
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop( self );        
       
    64     return self;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // void CSRTPPacketSRTCP::ConstructL
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void CSRTPPacketSRTCP::ConstructL()
       
    72     {            
       
    73     CSRTPPacketRTCP::ConstructL();
       
    74     UpdatePayloadLength();
       
    75     UpdatePayload();               
       
    76     UpdateAuthenticationTag();    
       
    77     UpdateMasterKeyIdentifier();        
       
    78     UpdatePacketIndex();     
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CSRTPPacketSRTCP::AuthenticationTag()
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 TPtrC8 CSRTPPacketSRTCP::AuthenticationTag()
       
    86     {
       
    87     return iAuthenticationTag;
       
    88     }
       
    89 
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CSRTPPacketSRTCP::MasterKeyIdentifier()
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 TPtrC8 CSRTPPacketSRTCP::MasterKeyIdentifier()
       
    96     {
       
    97     return iMasterKeyIdentifier;
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // CSRTPPacketSRTCP::UpdatePayloadLength()
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void CSRTPPacketSRTCP::UpdatePayloadLength()
       
   105     {    
       
   106      // the real RTCP packet length is 
       
   107      // the packet size - header - MKI - Auth tag- packetIndex
       
   108      TUint authTagLen = iHandler.Context().CryptoParams().iSrtcpAuthTagLen/8;
       
   109      TUint mkiLen=0;
       
   110      mkiLen=  iHandler.Context().MasterKey().MKI().Length();  
       
   111      TUint deductedLength = iHeaderLength + mkiLen + authTagLen + KSRTCPPacketIndexLength4; 
       
   112      iPayloadLength = iPacket.Length() - deductedLength;  
       
   113      
       
   114      if ( iPacket.Length() <= iHeaderLength )
       
   115      	{
       
   116      	iPayloadLength = 0;
       
   117      	}
       
   118     // iPayloadLength should be bigger than 0 
       
   119     }
       
   120     
       
   121     
       
   122 // ---------------------------------------------------------------------------
       
   123 // CSRTPPacketSRTCP::CountDecryptedPacketSize()
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 TUint CSRTPPacketSRTCP::CountDecryptedPacketSize()
       
   127     {
       
   128     return iHeaderLength + iPayloadLength;    
       
   129     }    
       
   130     
       
   131 // ---------------------------------------------------------------------------
       
   132 // CSRTPPacketSRTCP::CreateDecryptedPacketL()
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 HBufC8* CSRTPPacketSRTCP::CreateDecryptedPacketL(TUint8* aDecryptedPayloadPtr)
       
   136     {
       
   137     // create decrypted RTCP packet
       
   138     // first count needed packet size
       
   139     TUint rtpPacketSize = CountDecryptedPacketSize();
       
   140     
       
   141     return CopyHeaderAndPayloadL(rtpPacketSize, aDecryptedPayloadPtr);
       
   142     
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CSRTPPacketSRTCP::UpdateAuthenticationTag()
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CSRTPPacketSRTCP::UpdateAuthenticationTag()
       
   150     {       
       
   151        TPtrC8 authTag = GetAuthenticationTag();    
       
   152        if (authTag.Compare(KNullDesC8))
       
   153        	{
       
   154        	iAuthenticationTag.Set(authTag.Ptr(), authTag.Length());	
       
   155        	}
       
   156        
       
   157     }    
       
   158 // ---------------------------------------------------------------------------
       
   159 // CSRTPPacketSRTCP::GetAuthenticationTagL()
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 TPtrC8 CSRTPPacketSRTCP::GetAuthenticationTag()
       
   163     { 
       
   164     // for RTCP, authTagLength existed only with HMAC_SHA1     
       
   165     if ((iHandler.Context().CryptoParams().iSrtcpAuthTagLen > 0) && 
       
   166         (iHandler.Context().CryptoParams().iSrtcpAuthAlg != EAuthNull))
       
   167         {
       
   168          TUint authTagLength = iHandler.Context().CryptoParams().iSrtcpAuthTagLen;
       
   169          
       
   170          TUint authTagLenInBytes = authTagLength/8;
       
   171          
       
   172          if (iHandler.Context().CryptoParams().iSrtcpAuthAlg != EAuthHMAC_SHA1)
       
   173          	{
       
   174          	authTagLenInBytes = KSRTPAuthTagLength80/8;
       
   175          	}
       
   176          		
       
   177          TUint packetLength = iPacket.Length();
       
   178          TUint position = packetLength - authTagLenInBytes;
       
   179          
       
   180          return iPacket.Mid(position,authTagLenInBytes);
       
   181         }
       
   182     //In case of the other Authentication mode
       
   183     TPtrC8 authTagNull(KNullDesC8);
       
   184     return  authTagNull;    
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CSRTPPacketSRTCP::GetMasterKeyIdentifier()
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 TPtrC8 CSRTPPacketSRTCP::GetMasterKeyIdentifier()
       
   192     {
       
   193     if(iHandler.Context().MasterKey().MKI()!= KNullDesC8)
       
   194     	{    
       
   195 	    TInt mkiLength = iHandler.Context().MasterKey().MKI().Length();
       
   196 	    if (iHandler.Context().MasterKey().MKI().Length() > 0)
       
   197 	        {
       
   198 	         TUint authTagLength = iHandler.Context().CryptoParams().iSrtcpAuthTagLen;
       
   199 	         TUint authTagLenInBytes = authTagLength/8;
       
   200 	         TUint position = iPacket.Length() - authTagLenInBytes - mkiLength;
       
   201 	                                
       
   202 	         return iPacket.Mid(position,mkiLength);
       
   203 	        }
       
   204     	}
       
   205     TPtrC8 mkiNull( KMKINULL );
       
   206     return  mkiNull; 
       
   207     }    
       
   208             
       
   209 // ---------------------------------------------------------------------------
       
   210 // CSRTPPacketSRTCP::UpdateMasterKeyIdentifier()
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CSRTPPacketSRTCP::UpdateMasterKeyIdentifier()
       
   214     {    
       
   215      TPtrC8 mki = GetMasterKeyIdentifier();
       
   216      if (mki.Compare( KMKINULL ))
       
   217      	{
       
   218      	iMasterKeyIdentifier.Set(mki.Ptr(),mki.Length());	
       
   219      	}
       
   220     }    
       
   221     
       
   222 // ---------------------------------------------------------------------------
       
   223 // CSRTPPacketRTP::UpdatePacketIndex()
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 void CSRTPPacketSRTCP::UpdatePacketIndex()
       
   227     {
       
   228     const TUint8 *pointer = iDataP;	
       
   229 	pointer+=iHeaderLength;
       
   230 	pointer+= iPayloadLength;//payload length as SRTCP report length
       
   231     
       
   232      
       
   233     iPacketIndex=static_cast<TUint32>(TSRTPUtils::Read32(pointer));
       
   234     }