rtp/srtpstack/inc/srtpcryptohandler.h
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 cryptographic handler for SRTP/SRTCP streams.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #ifndef __SRTP_CRYPTOHANDLER_H__
       
    22 #define __SRTP_CRYPTOHANDLER_H__
       
    23 
       
    24 // INCLUDES
       
    25 #include <e32base.h>
       
    26 #include "srtpdef.h"
       
    27 #include "msrtpcryptohandlercontext.h"
       
    28 #include "srtpcryptocontext.h"
       
    29 #include "srtpcryptoparams.h"
       
    30 
       
    31 // FORWARD DECLARATIONS
       
    32 class CSRTPMasterKey;
       
    33 class CSRTPMasterSalt;
       
    34 class CSRTPCryptoContext;
       
    35 class CSRTPStream;
       
    36 class MSRTPCipher;
       
    37 class MSRTPKeyDerivation;
       
    38 class MSRTPAuthentication;
       
    39 class CSRTPPacket;
       
    40 
       
    41 /**
       
    42 * Contains RTP/RTCP common handling of cryptographic context.
       
    43 */
       
    44 class CSRTPCryptoHandler : public CBase, public MSRTPCryptoHandlerContext
       
    45     {
       
    46         
       
    47    public: // from MSRTPCryptoHandlerContext   
       
    48       
       
    49         /*
       
    50         * virtual function for initializing 
       
    51         * plain packet class (CSRTPPacketRTP or CSRTPPacketRTCP) 
       
    52         * @param aPacket   RTP/RTCP packet.
       
    53         * @leave KErrNone if success, system-wide error code otherwise
       
    54         * @return void
       
    55         */        
       
    56         virtual void InitializePlainPacketL(const TDesC8& aPacket)=0;
       
    57 
       
    58         /*
       
    59         * virtual function for initializing 
       
    60         * encrypted packet class (CSRTPPacketSRTP or CSRTPPacketSRTCP) 
       
    61         * @param aPacket   SRTP/SRTCP packet.
       
    62         * @leave KErrNone if success, system-wide error code otherwise
       
    63         * @return void
       
    64         */        
       
    65         virtual void InitializeEncryptedPacketL(const TDesC8& aPacket)=0;
       
    66         /* 
       
    67         * Virtual function for deriving session 
       
    68         * encryption, salting and authentication keys
       
    69         * for RTP or RTCP 
       
    70         * @leave KErrNone if success, system-wide error code otherwise
       
    71         * @return void                
       
    72         */
       
    73         virtual void DeriveSessionKeysL()=0; 
       
    74                
       
    75         /*
       
    76         * virtual function for checking that crypto context MKI and 
       
    77         * packet MKI match.
       
    78         * @leave KErrNone if success, system-wide error code otherwise
       
    79         * @return void
       
    80         */        
       
    81         virtual void CheckMasterKeyIdentifierL()=0;   
       
    82              
       
    83         /*
       
    84         * virtual function for performing the authentication step
       
    85         * @leave KErrNone if success, system-wide error code otherwise
       
    86         * @return void
       
    87         */        
       
    88         virtual void AuthenticateL()=0;
       
    89         
       
    90         /*
       
    91         * virtual function for performing the replay protection step
       
    92         * @leave KErrNone if success, system-wide error code otherwise
       
    93         * @return void
       
    94         */        
       
    95         virtual void ReplayProtectionL()=0;
       
    96 
       
    97         /*
       
    98         * virtual function for encrypting plain packet
       
    99         * @leave KErrNone if success, system-wide error code otherwise
       
   100         * @return encrypted packet
       
   101         */        
       
   102         virtual HBufC8* EncryptL()=0;
       
   103         
       
   104         /*
       
   105         * virtual function for decrypting ciphered packet
       
   106         * @leave KErrNone if success, system-wide error code otherwise
       
   107         * @return plain packet
       
   108         */        
       
   109         virtual HBufC8* DecryptL()=0;       
       
   110         
       
   111         /*
       
   112         * function for querying whether master key or salt has been updated
       
   113         * @return master keys update status
       
   114         */        
       
   115         virtual TBool MasterKeysUpdated()=0;
       
   116  
       
   117    public:  // from CSRTPCryptoContext           
       
   118         /**
       
   119         * Callback from CSRTPCryptoContext when a master key is changed.
       
   120         */
       
   121         void SRTPMasterKeyChanged();        
       
   122         /**
       
   123         * Callback from CSRTPCryptoContext when a master salt is changed.
       
   124         */
       
   125         void SRTPMasterSaltChanged();
       
   126         
       
   127         
       
   128         
       
   129    public:                    
       
   130         /**
       
   131         * Get roll-over counter value.
       
   132         * @return current ROC value. 
       
   133         */
       
   134         TUint32 ROC( ) const;  
       
   135         /*
       
   136         * Get corresponding CSRTPCryptoContext
       
   137         * (either session or stream specific)
       
   138         * @return CSRTPCryptoContext
       
   139         */                                            
       
   140         CSRTPCryptoContext& Context();
       
   141         
       
   142         const TSrtpCryptoParams& CryptoParams();
       
   143    
       
   144         virtual ~CSRTPCryptoHandler( );                
       
   145                                                  
       
   146     protected: // methods   
       
   147     
       
   148         CSRTPCryptoHandler( CSRTPStream& aStream);
       
   149                 
       
   150         void ConstructL( );
       
   151                 
       
   152                         
       
   153         /*
       
   154         * Deletes session keys (encryption, 
       
   155         * salting and authentication) and sets them NULL
       
   156         * @return void
       
   157         */                                            
       
   158         void DeleteSessionKeys();    
       
   159 
       
   160         /**
       
   161         * Set roll-over counter value.
       
   162         * @param aROC The new ROC value. 
       
   163         * @return void
       
   164         */
       
   165         void SetROC( TUint32 aROC );
       
   166 
       
   167         /**
       
   168         * Get 32 bit SSRC value
       
   169         * @return SSRC 
       
   170         */
       
   171         TUint SSRC() const;
       
   172              
       
   173         /*
       
   174         * Count X value, 
       
   175         * which  used in session key derivation
       
   176         * Common to RTP and RTCP.
       
   177         * The length of X is the same as master key length
       
   178         * (padding will be performed later)
       
   179         * @param a_R, r, index DIV key_derivation_rate
       
   180         * @param aLabel, label 8 bit RTP/RTCP label 
       
   181         * @param aIndexLength, 
       
   182         * @param &aRes  resulting n bit x value
       
   183         * @return void
       
   184         */
       
   185         void Count_X(TUint64 a_R,
       
   186                     TInt8 aLabel, 
       
   187                     const TUint64 aIndexLength, 
       
   188                     TDes8 &aRes);                
       
   189         
       
   190 
       
   191         
       
   192         /*
       
   193         * Check Packet Index with Replay window
       
   194         * Common for RTP and RTCP.
       
   195         * @param aDelta  the difference between the stored packet Index and 
       
   196         *  current Packet Index
       
   197         * @return KErrNone if checks Ok, if not, return KErrArgument
       
   198         */
       
   199         TInt ReplayCheck(TInt aDelta) ;
       
   200         
       
   201         /*
       
   202         * Set Encryptor and Auth
       
   203         * Common for RTP and RTCP.
       
   204         * @param aEngAlg  Encrption Alg using for cryptor handler
       
   205         * @param aAuthAlg Authentication Algorithem using for crypto handler
       
   206         * @return void
       
   207         */
       
   208         void SetEncAndAuthL (TSRTPEncAlg aEngAlg, 
       
   209 										TSRTPAuthAlg aAuthAlg);
       
   210 
       
   211     public: 
       
   212         /**
       
   213         * iHandlerOffset, List offset.
       
   214         * Owned by CCryptoContext object.
       
   215         */
       
   216         static const TInt iHandlerOffset;
       
   217 
       
   218         /**
       
   219         * iHandlerLink, Link to list.
       
   220         * Owned by CSRTPCryptoHandler object.
       
   221         */
       
   222         TSglQueLink iHandlerLink;
       
   223         
       
   224     protected: // data
       
   225     
       
   226         CSRTPStream&            iStream;         // stream
       
   227         
       
   228         CSRTPPacket*            iCurrentPacket;   // packet class        
       
   229         // algorithms: 
       
   230         MSRTPKeyDerivation*     iKeyDeriver;     // key divider algorithm
       
   231         MSRTPAuthentication*    iAuthenticator;  // authentication algorithm
       
   232         MSRTPCipher*            iCipher;         // encryption/decryption alg.
       
   233         TBool                   iMasterDataUpdated; // is master key or salt updated   
       
   234                     
       
   235         // session keys:
       
   236         HBufC8*                 iSessionEncrKey;  // session encryption/decryption key (k_e)
       
   237         HBufC8*                 iSessionAuthKey;  // session authentication key (k_a)
       
   238         HBufC8*                 iSessionSaltKey;  // session salting key (k_s)
       
   239 		TBool                   iReKey; // is master key or salt updated    
       
   240 		TUint64			  		iBitmap;		//Replay List
       
   241 		TUint64					iNumPackets; //Number of packets has been recived / sending
       
   242     	
       
   243     private:
       
   244     #ifdef EUNIT_TESTING
       
   245         friend class UT_CSRTPCryptoHandler;
       
   246         friend class UT_CSRTPCryptoHandlerSRTP;
       
   247         friend class UT_CSRTPStreamIn;
       
   248         friend class UT_CSRTPStreamOut;
       
   249     #endif 
       
   250                    
       
   251     };
       
   252 #endif // __SRTP_CRYPTOHANDLER_H__