realtimenetprots/sipfw/SIP/SIPSec/DigestPlugin/src/CSIPSecChallenge.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          : CSIPSecChallenge.cpp
       
    15 // Part of       : SIPDigestPlugin
       
    16 // Version       : SIP/6.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "CSIPSecChallenge.h"
       
    22 #include "sipproxyauthenticateheader.h"
       
    23 #include "sipwwwauthenticateheader.h"
       
    24 #include "sipstrings.h"
       
    25 #include "sipstrconsts.h"
       
    26 #include "sipresponse.h"
       
    27 #include "SipAssert.h"
       
    28 #include "MSIPSecUser.h"
       
    29 #include "sipsecdigestcontext.h"
       
    30 #include "sipsecdigestcache.h"
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CSIPSecChallenge::CSIPSecChallenge
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CSIPSecChallenge::CSIPSecChallenge( CSIPSecDigest::TChallengeType aType,
       
    41 									CSIPSecRequestData::TQop aQop ) :
       
    42     iType( aType ),
       
    43     iQop( aQop ),
       
    44     iOwner( NULL )
       
    45     {
       
    46     __SIP_ASSERT_RETURN( iQop != CSIPSecRequestData::EUnknown, KErrArgument );
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CSIPSecChallenge::~CSIPSecChallenge
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CSIPSecChallenge::~CSIPSecChallenge()
       
    54     {
       
    55     delete iAuthenticateHeader;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CSIPSecChallenge::Type
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CSIPSecDigest::TChallengeType CSIPSecChallenge::Type() const
       
    63     {
       
    64     return iType;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CSIPSecChallenge::Realm
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 const TDesC8& CSIPSecChallenge::Realm() const
       
    72     {
       
    73 	return TSIPSecDigestContext::GetDesParam( Content(), SipStrConsts::ERealm );
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CSIPSecChallenge::Content
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 CSIPAuthenticateHeaderBase& CSIPSecChallenge::Content()
       
    81     {
       
    82     return *iAuthenticateHeader;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CSIPSecChallenge::Content
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 const CSIPAuthenticateHeaderBase& CSIPSecChallenge::Content() const
       
    90     {
       
    91     return *iAuthenticateHeader;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CSIPSecChallenge::Qop
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 CSIPSecRequestData::TQop CSIPSecChallenge::Qop() const
       
    99 	{
       
   100 	return iQop;	
       
   101 	}
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CSIPSecChallenge::QopDescriptor
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 const TDesC8& CSIPSecChallenge::QopDescriptor() const
       
   108 	{
       
   109 	switch ( iQop )
       
   110 		{
       
   111     case CSIPSecRequestData::EAuth:
       
   112     	return KSIPSecAuth;
       
   113     
       
   114     case CSIPSecRequestData::EAuthInt:
       
   115     	return KSIPSecAuthInt;
       
   116 
       
   117     // Can't be CSIPSecRequestData::EUnknown, it is checked in constructor    	
       
   118     case CSIPSecRequestData::EDoesNotExist:
       
   119     default:
       
   120     	return KNullDesC8;
       
   121 		}
       
   122 	}
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSIPSecChallenge::HasQop
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 TBool CSIPSecChallenge::HasQop() const
       
   129 	{
       
   130 	return iQop != CSIPSecRequestData::EDoesNotExist;
       
   131 	}
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CSIPSecChallenge::Owner
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 const MSIPSecUser* CSIPSecChallenge::Owner() const
       
   138     {
       
   139     return iOwner;
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CSIPSecChallenge::SetOwner
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 void CSIPSecChallenge::SetOwner( const MSIPSecUser* aOwner )
       
   147     {
       
   148     iOwner = aOwner;
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CSIPSecChallenge::ConstructL
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void
       
   156 CSIPSecChallenge::ConstructL( CSIPAuthenticateHeaderBase& aAuthenticateHeader )
       
   157     {
       
   158     __ASSERT_DEBUG( aAuthenticateHeader.HasParam(
       
   159     	SIPStrings::StringF( SipStrConsts::ERealm ) ) &&
       
   160        	aAuthenticateHeader.HasParam(
       
   161        		SIPStrings::StringF( SipStrConsts::ENonce ) ),
       
   162     	User::Leave( KErrArgument ) );
       
   163 	__ASSERT_ALWAYS( iType == CSIPSecDigest::EProxy ||
       
   164     				 iType == CSIPSecDigest::EEndPoint,
       
   165     				 User::Leave( KErrArgument ) );
       
   166 
       
   167     iAuthenticateHeader = dynamic_cast<CSIPAuthenticateHeaderBase*>
       
   168     	( aAuthenticateHeader.CloneL() );
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CSIPSecChallenge::PopulateCredentialsL
       
   173 // Copy parameters from challenge to CSIPSecCredentials' authorization header.
       
   174 // This function is used when handling a SIP response, so transaction id always
       
   175 // exists.
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void
       
   179 CSIPSecChallenge::PopulateCredentialsL( TSIPSecDigestCtxSetup& aContext ) const
       
   180     {
       
   181     __ASSERT_DEBUG( aContext.TransactionId() != KEmptyTransactionId,
       
   182     				User::Panic( _L( "SIPSecChall:PopCreds" ), KErrArgument ) );
       
   183 
       
   184 	FillCredentialParamL( aContext, SipStrConsts::ERealm );
       
   185 	FillCredentialParamL( aContext, SipStrConsts::ENonce );
       
   186 	FillCredentialParamL( aContext, SipStrConsts::EOpaque );
       
   187 	aContext.SetParamValueL( SipStrConsts::EAlgorithm,
       
   188 		Content().ParamValue(
       
   189 			SIPStrings::StringF( SipStrConsts::EAlgorithm ) ).DesC() );
       
   190 
       
   191 
       
   192 	TSIPSecPluginCtxResponse& parent =
       
   193     	static_cast< TSIPSecPluginCtxResponse& >( aContext.Parent() );
       
   194 
       
   195     const TDesC8& realm = Realm();
       
   196     __ASSERT_ALWAYS( realm.Length() > 0, User::Leave( KErrGeneral ) );
       
   197     MSIPSecUser& user = parent.SIPSecUser();
       
   198 
       
   199 	CSIPSecUserRecord* record = parent.Cache().SearchRecordForResponse(
       
   200 		realm, user, parent.RegistrationId() );
       
   201     if ( record )
       
   202         {
       
   203         // Found from cache, use it
       
   204 	// This user will be attached to the record.
       
   205 		VerifyTypeL( record->Type() );
       
   206 		if  ( (aContext.GetParam(SipStrConsts::EAlgorithm) == 
       
   207         SIPStrings::StringF( SipStrConsts::EAKAv1MD5 ).DesC()) &&
       
   208         record->Type() == CSIPSecUserRecord::EIMS)
       
   209 			{
       
   210 			record->Invalidate( CSIPSecUserRecord::ENoState );
       
   211 			record->ClearUsernameAndPassword();	
       
   212 			}
       
   213         }
       
   214     else
       
   215         {
       
   216         // Not found from the cache, create a new one.
       
   217         record = CreateUserRecordL( realm,
       
   218         							user,
       
   219         							parent.RegistrationId(),
       
   220         							aContext.TransactionId(),
       
   221 									parent.SIPResponse().ResponseCode() );
       
   222         parent.Cache().AddUserCredentials( record );
       
   223         }
       
   224 
       
   225     aContext.SetUserData( *record );
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CSIPSecChallenge::FillCredentialParamL
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CSIPSecChallenge::FillCredentialParamL( TSIPSecDigestCtxSetup& aContext,
       
   233 											 TInt aParam ) const
       
   234     {
       
   235     aContext.SetDesParamValueL( aParam,
       
   236 		Content().DesParamValue( SIPStrings::StringF( aParam ) ) );
       
   237     }