realtimenetprots/sipfw/SIP/SIPSec/DigestPlugin/src/CSIPSecDigest.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : CSIPSecDigest.cpp
       
    15 // Part of       : SIPDigestPlugin
       
    16 // Version       : SIP/6.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "sipsecurityserverheader.h"
       
    22 #include "CSIPSecDigest.h"
       
    23 #include "sipsecdigestcache.h"
       
    24 #include "CSIPSecChallenge.h"
       
    25 #include "RSIPSecChallengeResolver.h"
       
    26 #include "CSIPSecCredentials.h"
       
    27 #include "sipsecdigestcontext.h"
       
    28 #include "sipstrings.h"
       
    29 #include "sipstrconsts.h"
       
    30 #include "MSIPSecUser.h"
       
    31 #include <hash.h>
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CSIPSecDigest::NewL
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CSIPSecDigest* CSIPSecDigest::NewL()
       
    40     {
       
    41 	CSIPSecDigest* self = new( ELeave )CSIPSecDigest();
       
    42 	CleanupStack::PushL( self );
       
    43 	self->ConstructL();
       
    44 	CleanupStack::Pop( self );
       
    45 	return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CSIPSecDigest::CSIPSecDigest
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CSIPSecDigest::CSIPSecDigest()
       
    53     {
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSIPSecDigest::ConstructL
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 void CSIPSecDigest::ConstructL()
       
    61     {
       
    62     SIPStrings::OpenL();
       
    63     iStringPoolOpened = ETrue;
       
    64 
       
    65     iAlgorithm = SIPStrings::StringF( SipStrConsts::EEmpty );
       
    66 
       
    67     iMessageDigest = CMD5::NewL();
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CSIPSecDigest::~CSIPSecDigest
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CSIPSecDigest::~CSIPSecDigest()
       
    75     {
       
    76     delete iMessageDigest;
       
    77     iAlgorithm.Close();
       
    78 
       
    79     if ( iStringPoolOpened )
       
    80     	{
       
    81     	SIPStrings::Close();
       
    82     	}
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CSIPSecDigest::Name
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 const TDesC8& CSIPSecDigest::Name() const
       
    90     {
       
    91     return KSIPSecDigestName;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CSIPSecDigest::InitializeL
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CSIPSecDigest::InitializeL( CSIPSecurityServerHeader& aSecurityServer )
       
    99     {
       
   100     SetDefaultAlgorithm( aSecurityServer.ParamValue(
       
   101     	SIPStrings::StringF( SipStrConsts::EDigestAlgorithm ) ) );
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CSIPSecDigest::UpdateCacheL
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TBool CSIPSecDigest::UpdateCacheL( TSIPSecPluginCtxResponse& aContext )
       
   109     {
       
   110     TBool mustWait( EFalse );
       
   111     // EFalse = all challenges in the SIP response have found credentials from
       
   112     // the cache, or are already waiting credentials from application.
       
   113     TBool askCredentials( EFalse );
       
   114     CSIPSecChallenge* challenge( NULL );
       
   115     CSIPSecCredentials* credentials( NULL );
       
   116 
       
   117 	RSIPSecChallengeResolver challenges( aContext, *this );
       
   118     CleanupClosePushL( challenges );
       
   119     challenges.OpenL( aContext.SIPSecUser() );
       
   120 
       
   121     // Create one cache entry (CSIPSecCredentials) for each challenge.
       
   122     while ( ( challenge = challenges.Pop() ) != NULL )
       
   123         {
       
   124         CleanupStack::PushL( challenge );
       
   125 		credentials = CSIPSecCredentials::NewL( challenge, *this, aContext );
       
   126 		CleanupStack::Pop( challenge );
       
   127         aContext.Cache().AddEntry( credentials );
       
   128 
       
   129         TSIPSecDigestCtxSetup ctx( *credentials,
       
   130         						   aContext.TransactionId(),
       
   131         						   &aContext );
       
   132 		TBool ask( EFalse );
       
   133 		// AKA challenges set "ask" to EFalse, as they use SIM card
       
   134         mustWait = credentials->DigestUpdateL( ctx, ask ) || mustWait;
       
   135         askCredentials = askCredentials || ask;
       
   136         }
       
   137 
       
   138 	MSIPSecUser& sipSecUser = aContext.SIPSecUser();
       
   139 	MSIPSecUser* trustedUser = const_cast< MSIPSecUser* >
       
   140 		( sipSecUser.TrustedUser( aContext.RegistrationId() ) );
       
   141 	MSIPSecUser* user = trustedUser ? trustedUser : &sipSecUser;
       
   142 	if ( mustWait && !user->PassOnlyRealmsToUser() && askCredentials )
       
   143 		{
       
   144 		user->RequestCredentialsL( aContext.SIPResponse(),
       
   145 								  aContext.TransactionId(),
       
   146 								  KEmptyRefreshId );
       
   147 		}
       
   148 
       
   149     CleanupStack::PopAndDestroy(); // challenges
       
   150     return mustWait;
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CSIPSecDigest::CSIPSecDigest
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 CMD5& CSIPSecDigest::MessageDigest()
       
   158     {
       
   159     return *iMessageDigest;
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CSIPSecDigest::Algorithm
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 RStringF CSIPSecDigest::Algorithm() const
       
   167     {
       
   168     return iAlgorithm;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CSIPSecDigest::SetDefaultAlgorithm
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 void CSIPSecDigest::SetDefaultAlgorithm( RStringF aAlgorithm )
       
   176 	{
       
   177 	iAlgorithm.Close();
       
   178 	iAlgorithm = aAlgorithm.Copy();
       
   179 	}