realtimenetprots/sipfw/SIP/SIPSec/DigestPlugin/src/CSIPSecSIMCredentialsProvider.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2007-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          : CSIPSecSIMCredentialsProvider.cpp
       
    15 // Part of       : SIPSec
       
    16 // Version       : SIP/6.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include <commsdat.h>
       
    22 #include <commsdattypesv1_1.h>
       
    23 #include <mmretrieve.h>
       
    24 #include <etelmmerr.h>
       
    25 #include <cdblen.h>
       
    26 
       
    27 #include "SipLogs.h"
       
    28 #include "SipAssert.h"
       
    29 #include "CSIPSecSIMCredentialsProvider.h"
       
    30 #include "CSIPSecSIMRecord.h"
       
    31 #include "CSIPSecDigestPlugin.h"
       
    32 #include "sipsecdigestcontext.h"
       
    33 
       
    34 // CONSTANTS
       
    35 _LIT8( KSIPSecCK, "ck" );
       
    36 _LIT8( KSIPSecIK, "ik" );
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // CSIPSecIMSAuthentication::NewL
       
    43 // ----------------------------------------------------------------------------
       
    44 //
       
    45 CSIPSecIMSAuthentication* CSIPSecIMSAuthentication::NewL(
       
    46          MSIPSecIMSAuthenticationDataObserver& aObserver )
       
    47     {
       
    48     return new ( ELeave ) CSIPSecIMSAuthentication( aObserver );    
       
    49     }
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // CSIPSecIMSAuthentication::CSIPSecIMSAuthentication
       
    53 // ----------------------------------------------------------------------------
       
    54 //
       
    55 CSIPSecIMSAuthentication::CSIPSecIMSAuthentication( 
       
    56             MSIPSecIMSAuthenticationDataObserver& aObserver ) :
       
    57     CActive( CActive::EPriorityStandard ),
       
    58     iObserver( aObserver ),
       
    59     iPhone( NULL ),    
       
    60     iMessage( iData )
       
    61     {
       
    62     CActiveScheduler::Add( this );
       
    63     }
       
    64         
       
    65 // ----------------------------------------------------------------------------
       
    66 // CSIPSecIMSAuthentication::~CSIPSecIMSAuthentication
       
    67 // ----------------------------------------------------------------------------
       
    68 //
       
    69 CSIPSecIMSAuthentication::~CSIPSecIMSAuthentication()
       
    70     {
       
    71     Cancel();
       
    72     }
       
    73 
       
    74 #ifndef USE_TEST_STUB
       
    75 // ----------------------------------------------------------------------------
       
    76 // CSIPSecIMSAuthentication::Request
       
    77 // ----------------------------------------------------------------------------
       
    78 //
       
    79 void CSIPSecIMSAuthentication::Request( RMobilePhone& aPhone )
       
    80     {
       
    81     __SIP_LOG( "CSIPSecIMSAuthen:Request" )
       
    82 	__ASSERT_ALWAYS( !IsActive(),
       
    83 					 User::Panic( _L( "IMSAuthentic:Req" ), KErrInUse ) );
       
    84 
       
    85 	iMessage.Copy( RMobilePhone::TImsAuthenticateDataV5Pckg( iData ) );
       
    86 
       
    87     __SIP_LOG( "CSIPSecIMSAuthen:Request Asking ISIM to Authenticate" )
       
    88 	aPhone.ImsAuthenticate( iStatus, iMessage );
       
    89     SetActive();
       
    90     iPhone = &aPhone;	
       
    91     }
       
    92 #endif // #ifndef USE_TEST_STUB
       
    93 
       
    94 // ----------------------------------------------------------------------------
       
    95 // CSIPSecIMSAuthentication::Response
       
    96 // ----------------------------------------------------------------------------
       
    97 //
       
    98 RMobilePhone::TImsAuthenticateDataV5& CSIPSecIMSAuthentication::Response()
       
    99     {
       
   100     return iData;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 // CSIPSecIMSAuthentication::RunL
       
   105 // ----------------------------------------------------------------------------
       
   106 //
       
   107 void CSIPSecIMSAuthentication::RunL()
       
   108     {
       
   109 	__SIP_INT_LOG1( "SIPSecIMSAuthen:RunL status", iStatus.Int() )    
       
   110 
       
   111     if ( iStatus.Int() == KErrNone )
       
   112         {
       
   113         iObserver.AuthenticatedL();
       
   114         }
       
   115     else
       
   116         {
       
   117         iObserver.AuthenticationFailedL( iStatus.Int() );        
       
   118         }
       
   119    	iPhone = NULL;
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CSIPSecIMSAuthentication::RunError
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 TInt CSIPSecIMSAuthentication::RunError( TInt aError )
       
   127     {
       
   128 	return iObserver.PassErrorToFramework( aError, ETrue );
       
   129 	}
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // CSIPSecIMSAuthentication::DoCancel
       
   133 // As there is a pending request, iPhone must be defined.
       
   134 // ----------------------------------------------------------------------------
       
   135 //
       
   136 void CSIPSecIMSAuthentication::DoCancel()
       
   137     {
       
   138 #if defined( USE_TEST_STUB )
       
   139     // If CSIPSecDigestTest::AKAAuthWWWChallengeFailuresTestL leaves before it
       
   140     // called iSIMStub->CallBack, this is needed to cancel the active object.
       
   141 	TRequestStatus* status = &iStatus;
       
   142    	User::RequestComplete( status, KErrCancel );
       
   143 #else
       
   144  	__SIP_LOG( "CSIPSecIMSAuthen:DoCancel" )
       
   145 
       
   146     __SIP_ASSERT_RETURN( iPhone, KErrNotFound );    
       
   147 
       
   148 	iPhone->CancelAsyncRequest( EMobilePhoneIMSAuthenticate );	
       
   149 #endif
       
   150     }
       
   151 
       
   152 // ============================ MEMBER FUNCTIONS ===============================
       
   153 
       
   154 
       
   155 // ----------------------------------------------------------------------------
       
   156 // CSIPSecIMSAuthorization::NewL
       
   157 // ----------------------------------------------------------------------------
       
   158 //
       
   159 CSIPSecIMSAuthorization*
       
   160 CSIPSecIMSAuthorization::NewL( MSIPSecIMSAuthenticationDataObserver& aObserver )
       
   161     {
       
   162     CSIPSecIMSAuthorization* self = 
       
   163         new ( ELeave ) CSIPSecIMSAuthorization( aObserver );
       
   164     CleanupStack::PushL( self );
       
   165     self->ConstructL();
       
   166     CleanupStack::Pop( self );
       
   167     return self;    
       
   168     }
       
   169 
       
   170 // ----------------------------------------------------------------------------
       
   171 // CSIPSecIMSAuthorization::CSIPSecIMSAuthorization
       
   172 // ----------------------------------------------------------------------------
       
   173 //
       
   174 CSIPSecIMSAuthorization::CSIPSecIMSAuthorization( 
       
   175         MSIPSecIMSAuthenticationDataObserver& aObserver ) :
       
   176     CActive( CActive::EPriorityStandard ),
       
   177     iObserver( aObserver )
       
   178     {
       
   179     }
       
   180         
       
   181 // ----------------------------------------------------------------------------
       
   182 // CSIPSecIMSAuthorization::~CSIPSecIMSAuthorization
       
   183 // ----------------------------------------------------------------------------
       
   184 //
       
   185 CSIPSecIMSAuthorization::~CSIPSecIMSAuthorization()
       
   186     {
       
   187     Cancel();
       
   188     delete iRetriever;
       
   189     delete iData;
       
   190     }
       
   191 
       
   192 // ----------------------------------------------------------------------------
       
   193 // CSIPSecIMSAuthorization::ConstructL
       
   194 // ----------------------------------------------------------------------------
       
   195 //
       
   196 void CSIPSecIMSAuthorization::ConstructL()
       
   197     {    
       
   198     iData = RMobilePhone::CImsAuthorizationInfoV5::NewL();
       
   199     CActiveScheduler::Add( this );    
       
   200     }
       
   201 
       
   202 #ifndef USE_TEST_STUB
       
   203 // ----------------------------------------------------------------------------
       
   204 // CSIPSecIMSAuthorization::RequestL
       
   205 // ----------------------------------------------------------------------------
       
   206 //
       
   207 void CSIPSecIMSAuthorization::RequestL( RMobilePhone& aPhone )
       
   208     {
       
   209     delete iRetriever;
       
   210     iRetriever = NULL;
       
   211     iRetriever = CAsyncRetrieveAuthorizationInfo::NewL( aPhone, *iData );
       
   212 
       
   213     iRetriever->Start( iStatus );
       
   214     SetActive();
       
   215     }
       
   216 #endif // #ifndef USE_TEST_STUB
       
   217 
       
   218 // ----------------------------------------------------------------------------
       
   219 // CSIPSecIMSAuthorization::Response
       
   220 // ----------------------------------------------------------------------------
       
   221 //
       
   222 RMobilePhone::CImsAuthorizationInfoV5& CSIPSecIMSAuthorization::Response()
       
   223     {
       
   224     return *iData;
       
   225     }   
       
   226 
       
   227 #ifndef USE_TEST_STUB
       
   228 // ----------------------------------------------------------------------------
       
   229 // CSIPSecIMSAuthorization::RunL
       
   230 // ----------------------------------------------------------------------------
       
   231 //
       
   232 void CSIPSecIMSAuthorization::RunL()
       
   233     {
       
   234 	if ( iStatus.Int() == KErrNone &&
       
   235 		 iRetriever &&
       
   236 		 iRetriever->AuthInfoValid() )
       
   237         {
       
   238         iObserver.Authorized();
       
   239         }
       
   240     else
       
   241         {
       
   242         iObserver.AuthorizationFailedL();
       
   243         }
       
   244     }
       
   245 #endif // #ifndef USE_TEST_STUB
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CSIPSecIMSAuthorization::RunError
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 TInt CSIPSecIMSAuthorization::RunError( TInt aError )	
       
   252     {
       
   253 	return iObserver.PassErrorToFramework( aError, EFalse );	
       
   254 	}
       
   255         
       
   256 // ----------------------------------------------------------------------------
       
   257 // CSIPSecIMSAuthorization::DoCancel
       
   258 // ----------------------------------------------------------------------------
       
   259 //
       
   260 void CSIPSecIMSAuthorization::DoCancel()
       
   261     {
       
   262 #if defined( USE_TEST_STUB )
       
   263     // If using test stub, must cancel own request as iRetriever is always NULL
       
   264 	TRequestStatus* status = &iStatus;
       
   265    	User::RequestComplete( status, KErrCancel );
       
   266 #endif
       
   267     __SIP_LOG( "CSIPSecIMSAuthor:DoCancel" )
       
   268 
       
   269 	if ( iRetriever )
       
   270 		{
       
   271 		iRetriever->Cancel();
       
   272 		}
       
   273     delete iRetriever;
       
   274     iRetriever = NULL;
       
   275     }
       
   276 
       
   277 
       
   278 // ============================ MEMBER FUNCTIONS ===============================
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // CSIPSecSIMCredentialsProvider::NewL
       
   282 // ----------------------------------------------------------------------------
       
   283 //
       
   284 CSIPSecSIMCredentialsProvider*
       
   285 CSIPSecSIMCredentialsProvider::NewL( CSIPSecSIMRecord& aRecord )
       
   286     {
       
   287     CSIPSecSIMCredentialsProvider* self =
       
   288 		new ( ELeave ) CSIPSecSIMCredentialsProvider( aRecord );
       
   289     CleanupStack::PushL( self );
       
   290     self->ConstructL();
       
   291     CleanupStack::Pop( self );
       
   292     return self;
       
   293     }
       
   294 
       
   295 // ----------------------------------------------------------------------------
       
   296 // CSIPSecSIMCredentialsProvider::CSIPSecSIMCredentialsProvider
       
   297 // ----------------------------------------------------------------------------
       
   298 //
       
   299 CSIPSecSIMCredentialsProvider::CSIPSecSIMCredentialsProvider(
       
   300 	CSIPSecSIMRecord& aRecord ) :
       
   301 	CSIPSecCredentialsProvider( aRecord )
       
   302 	{
       
   303 	}
       
   304 
       
   305 #ifndef USE_TEST_STUB
       
   306 // ----------------------------------------------------------------------------
       
   307 // CSIPSecSIMCredentialsProvider::ConstructL
       
   308 // ----------------------------------------------------------------------------
       
   309 //
       
   310 void CSIPSecSIMCredentialsProvider::ConstructL()
       
   311     {
       
   312 	TBuf<KCommsDbSvrMaxFieldLength> tsyModuleName;
       
   313 	GetTsyModuleNameL( tsyModuleName );
       
   314 	PreparePhoneL( tsyModuleName );
       
   315 
       
   316     iAuthentication = CSIPSecIMSAuthentication::NewL( *this );
       
   317     iAuthorization = CSIPSecIMSAuthorization::NewL( *this );
       
   318     }
       
   319 #endif // #ifndef USE_TEST_STUB
       
   320 
       
   321 // ----------------------------------------------------------------------------
       
   322 // CSIPSecSIMCredentialsProvider::~CSIPSecSIMCredentialsProvider
       
   323 // ----------------------------------------------------------------------------
       
   324 //
       
   325 CSIPSecSIMCredentialsProvider::~CSIPSecSIMCredentialsProvider()
       
   326     {
       
   327     __SIP_LOG( "~SIPSecSIMCredProv" )
       
   328 
       
   329     delete iAuthentication;
       
   330     delete iAuthorization;
       
   331     delete iOutboundProxy;
       
   332 
       
   333     iPhone.Close();
       
   334     iEtelServer.Close();
       
   335 
       
   336 	__SIP_LOG( "~SIPSecSIMCredProv ends" )
       
   337     }
       
   338 
       
   339 // ----------------------------------------------------------------------------
       
   340 // CSIPSecSIMCredentialsProvider::GetTsyModuleNameL
       
   341 // ----------------------------------------------------------------------------
       
   342 //
       
   343 void
       
   344 CSIPSecSIMCredentialsProvider::GetTsyModuleNameL( TDes& aTsyModuleName ) const
       
   345     {
       
   346     __ASSERT_ALWAYS( aTsyModuleName.MaxSize() >= KCommsDbSvrMaxFieldLength,
       
   347 		User::Leave( KErrArgument ) );
       
   348 
       
   349     using namespace CommsDat;
       
   350 
       
   351     CMDBSession* db = CMDBSession::NewLC( KCDVersion1_1 ); 
       
   352 
       
   353     CMDBField<TUint32>* globalSettingField =
       
   354     	new ( ELeave ) CMDBField<TUint32>( KCDTIdModemPhoneServicesSMS );
       
   355     CleanupStack::PushL( globalSettingField );
       
   356 
       
   357     globalSettingField->SetRecordId( 1 );
       
   358     globalSettingField->LoadL( *db );
       
   359     TUint32 modemId = *globalSettingField; 
       
   360 
       
   361     CMDBField<TDesC>* tsyField =
       
   362     	new ( ELeave ) CMDBField<TDesC>( KCDTIdTsyName );
       
   363     CleanupStack::PushL( tsyField );
       
   364 
       
   365     tsyField->SetRecordId( modemId ); 
       
   366     tsyField->LoadL( *db );
       
   367     aTsyModuleName = *tsyField;
       
   368 
       
   369     CleanupStack::PopAndDestroy( tsyField );
       
   370     CleanupStack::PopAndDestroy( globalSettingField );
       
   371     CleanupStack::PopAndDestroy( db );
       
   372     }
       
   373 
       
   374 // ----------------------------------------------------------------------------
       
   375 // CSIPSecSIMCredentialsProvider::PreparePhoneL
       
   376 // ----------------------------------------------------------------------------
       
   377 //
       
   378 void CSIPSecSIMCredentialsProvider::PreparePhoneL( const TDes& aTsyModuleName )
       
   379 	{
       
   380     User::LeaveIfError( iEtelServer.Connect() );
       
   381     User::LeaveIfError( iEtelServer.LoadPhoneModule( aTsyModuleName ) );
       
   382 
       
   383     // Get number of phones supported by all currently loaded TSY modules
       
   384     TInt phoneCount( 0 );
       
   385     User::LeaveIfError( iEtelServer.EnumeratePhones( phoneCount ) );
       
   386 
       
   387     RTelServer::TPhoneInfo phoneInfo;
       
   388 	TName searchTsyName;
       
   389 	TInt phoneIndex( KErrNotFound );
       
   390 
       
   391     while ( phoneIndex == KErrNotFound && phoneCount-- > 0 )
       
   392         {
       
   393         // Check if this phone belongs to the loaded TSY
       
   394         if ( ( iEtelServer.GetTsyName( phoneCount, searchTsyName ) == KErrNone )
       
   395              &&
       
   396              ( searchTsyName.CompareF( aTsyModuleName ) == 0 ) )
       
   397         	{
       
   398         	phoneIndex = phoneCount;        	
       
   399         	}
       
   400         }
       
   401 	if ( phoneIndex == KErrNotFound )
       
   402 		{
       
   403 		User::Leave( KErrNotFound );
       
   404 		}
       
   405 
       
   406     User::LeaveIfError( iEtelServer.GetPhoneInfo( phoneIndex, phoneInfo ) );
       
   407     User::LeaveIfError( iPhone.Open( iEtelServer, phoneInfo.iName ) );	
       
   408 
       
   409     RPhone::TStatus phoneStatus;
       
   410     User::LeaveIfError( iPhone.GetStatus( phoneStatus ) );
       
   411 
       
   412     // Check if the phone is already initialised
       
   413     if ( phoneStatus.iMode == RPhone::EModeUnknown )
       
   414         {
       
   415         TInt err = iPhone.Initialise();
       
   416 
       
   417         if ( err != KErrNone )
       
   418             {
       
   419             iPhone.Close();
       
   420             User::Leave( err );
       
   421             }
       
   422         }
       
   423     }
       
   424 
       
   425 // ----------------------------------------------------------------------------
       
   426 // CSIPSecSIMCredentialsProvider::RequestL
       
   427 // ----------------------------------------------------------------------------
       
   428 //
       
   429 void CSIPSecSIMCredentialsProvider::RequestL( TSIPSecDigestCtxSetup& aContext )
       
   430     {
       
   431     iPlugin = &aContext.Parent().Plugin();
       
   432 
       
   433     delete iOutboundProxy;
       
   434     iOutboundProxy = NULL;
       
   435     iOutboundProxy = aContext.Parent().OutboundProxy().AllocL();
       
   436 
       
   437     static_cast<CSIPSecSIMRecord&>( UserRecord() ).SetAuthenticationDataL(
       
   438     	aContext.Nonce() );
       
   439 
       
   440     if ( UserRecord().UserName().Length() == 0 )
       
   441         {
       
   442         RequestUserNameL();
       
   443         }
       
   444     else
       
   445         {
       
   446         RequestPassword();
       
   447         }
       
   448     }
       
   449     
       
   450 // ----------------------------------------------------------------------------
       
   451 // CSIPSecSIMCredentialsProvider::RequestUserNameL
       
   452 // ----------------------------------------------------------------------------
       
   453 //
       
   454 void CSIPSecSIMCredentialsProvider::RequestUserNameL()
       
   455     {
       
   456     RMobilePhone::CImsAuthorizationInfoV5& data = iAuthorization->Response();
       
   457 
       
   458     data.iIMPI.Zero();
       
   459     data.iIMPUArray.Reset();
       
   460     data.iHomeNetworkDomainName.Zero();
       
   461 
       
   462     iAuthorization->RequestL( iPhone );
       
   463     }
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // CSIPSecSIMCredentialsProvider::RequestPassword
       
   467 // ----------------------------------------------------------------------------
       
   468 //
       
   469 void CSIPSecSIMCredentialsProvider::RequestPassword()
       
   470     {
       
   471     CSIPSecSIMRecord& record = static_cast<CSIPSecSIMRecord&>( UserRecord() );
       
   472 
       
   473     RMobilePhone::TImsAuthenticateDataV5& data = iAuthentication->Response();
       
   474     data.iRAND.Zero();
       
   475     data.iAUTN.Zero();
       
   476     data.iRES.Zero();
       
   477 	data.iIK.Zero();
       
   478     data.iCK.Zero();
       
   479     data.iAUTS.Zero();
       
   480 
       
   481     data.iRAND.Copy( record.RAND() );
       
   482 	data.iAUTN.Copy( record.AUTN() );
       
   483 
       
   484 	__SIP_INT_LOG2( "SIPSecSIMCredProv:RequestPassword, RAND len, AUTN len",
       
   485 	                record.RAND().Length(), record.AUTN().Length() )
       
   486 	HexDumpToLog( _L8( "RAND=" ), record.RAND() );	
       
   487 	HexDumpToLog( _L8( "AUTN=" ), record.AUTN() );
       
   488 
       
   489     iAuthentication->Request( iPhone );
       
   490     }
       
   491 
       
   492 // ----------------------------------------------------------------------------
       
   493 // CSIPSecSIMCredentialsProvider::Cancel
       
   494 // ----------------------------------------------------------------------------
       
   495 //
       
   496 void CSIPSecSIMCredentialsProvider::Cancel()
       
   497     {
       
   498     iAuthentication->Cancel();
       
   499     iAuthorization->Cancel();
       
   500     }
       
   501     
       
   502 // ----------------------------------------------------------------------------
       
   503 // CSIPSecSIMCredentialsProvider::AuthenticatedL
       
   504 // For AKA, use the challenge's realm, not the SIM's realm. AUTS does not exist.
       
   505 // ----------------------------------------------------------------------------
       
   506 //
       
   507 void CSIPSecSIMCredentialsProvider::AuthenticatedL()
       
   508     {
       
   509     __SIP_LOG( "SIPSecSIMCredProv:AuthenticatedL" )
       
   510     __SIP_ASSERT_LEAVE( iPlugin && iOutboundProxy, KErrNotFound );
       
   511 
       
   512     RMobilePhone::CImsAuthorizationInfoV5& authorization =
       
   513     	iAuthorization->Response();
       
   514     RMobilePhone::TImsAuthenticateDataV5& authentication =
       
   515 		iAuthentication->Response();
       
   516 
       
   517 	__SIP_INT_LOG2( "Lengths: IK, CK",
       
   518 	                authentication.iIK.Length(),
       
   519 			        authentication.iCK.Length() )
       
   520 	__SIP_INT_LOG2( "Lengths: IMPI, RES",       
       
   521 			        authorization.iIMPI.Length(),
       
   522 			        authentication.iRES.Length() )
       
   523 	__SIP_INT_LOG1( "Lengths: AUTS", authentication.iAUTS.Length() )
       
   524 
       
   525 	HexDumpToLog( _L8( "IK=" ), authentication.iIK );
       
   526 	HexDumpToLog( _L8( "CK=" ), authentication.iCK );
       
   527 	HexDumpToLog( _L8( "RES=" ), authentication.iRES );
       
   528 
       
   529     iPlugin->EngineContext().RemoveValue( KSIPSecIK );
       
   530     iPlugin->EngineContext().RemoveValue( KSIPSecCK );
       
   531    	iPlugin->EngineContext().SetValueL( KSIPSecIK, authentication.iIK );
       
   532    	iPlugin->EngineContext().SetValueL( KSIPSecCK, authentication.iCK );
       
   533     iPlugin->SetCredentialsL( UserRecord().SIPSecUser(),
       
   534                               UserRecord().Realm(),
       
   535                               *iOutboundProxy,
       
   536                               authorization.iIMPI,   // username
       
   537                               authentication.iRES ); // password
       
   538     }
       
   539 
       
   540 // ----------------------------------------------------------------------------
       
   541 // CSIPSecSIMCredentialsProvider::AuthenticationFailedL
       
   542 // If SQN failure, AUTS is set. Compute credentials using an empty password.
       
   543 // ----------------------------------------------------------------------------
       
   544 //
       
   545 void CSIPSecSIMCredentialsProvider::AuthenticationFailedL( TInt aError )
       
   546     {
       
   547 	__SIP_LOG( "SIPSecSIMCredProv:AuthenticFailedL" )
       
   548 	__SIP_ASSERT_RETURN( aError != KErrNone, KErrArgument );
       
   549     __SIP_ASSERT_RETURN( iPlugin && iOutboundProxy, KErrNotFound );    
       
   550 
       
   551 	const TDesC8& res = iAuthentication->Response().iRES;	
       
   552 	const TDesC8& auts = iAuthentication->Response().iAUTS;
       
   553 
       
   554 	__SIP_INT_LOG2( "Lengths RES, AUTS", res.Length(), auts.Length() )
       
   555 	HexDumpToLog( _L8( "AUTS=" ), auts );
       
   556 
       
   557 	__ASSERT_ALWAYS( res.Length() == 0, User::Leave( KErrGeneral ) );
       
   558 
       
   559 	CSIPSecSIMRecord& record = static_cast<CSIPSecSIMRecord&>( UserRecord() );
       
   560 	switch ( aError )
       
   561 		{
       
   562 		case KErrMMEtelMacVerificationFailed:
       
   563 			record.UserCredentialsFailed( CSIPSecSIMRecord::EMAC );
       
   564 			break;
       
   565 
       
   566 		case KErrMMEtelSqnVerificationFailed:			
       
   567 			__ASSERT_ALWAYS( auts.Length() > 0, User::Leave( KErrGeneral ) );
       
   568 
       
   569 			record.UserCredentialsFailed( CSIPSecSIMRecord::ESQN );
       
   570         	record.SetResynchronizationDataL( auts );
       
   571 			break;
       
   572 
       
   573 		case KErrMMEtelAuthenticateFailed: // Authenticate command failed		
       
   574 		default: // System wide errors
       
   575 			User::Leave( aError );
       
   576 		}
       
   577 
       
   578 	iPlugin->EngineContext().RemoveValue( KSIPSecIK );
       
   579     iPlugin->EngineContext().RemoveValue( KSIPSecCK );
       
   580 
       
   581 	// Though using record.TransactionId() works too, it is better to use
       
   582 	// userRecord().SIPSecUser() as transaction id variant of SetCredentialsL
       
   583 	// will be removed.
       
   584 	iPlugin->SetCredentialsL( record.SIPSecUser(),
       
   585 	                          record.Realm(),
       
   586                               *iOutboundProxy,
       
   587                               iAuthorization->Response().iIMPI, // username
       
   588                               KNullDesC8 ); // empty password ("")
       
   589     }
       
   590 
       
   591 // ----------------------------------------------------------------------------
       
   592 // CSIPSecSIMCredentialsProvider::Authorized
       
   593 // ----------------------------------------------------------------------------
       
   594 //
       
   595 void CSIPSecSIMCredentialsProvider::Authorized()
       
   596     {    
       
   597     RequestPassword();    
       
   598     }
       
   599 
       
   600 // ----------------------------------------------------------------------------
       
   601 // CSIPSecSIMCredentialsProvider::AuthorizationFailedL
       
   602 // ----------------------------------------------------------------------------
       
   603 //
       
   604 void CSIPSecSIMCredentialsProvider::AuthorizationFailedL()
       
   605     {
       
   606     __SIP_LOG( "SIPSecSIMCredProv:AuthorizationFailedL" )
       
   607     __SIP_ASSERT_RETURN( iPlugin && iOutboundProxy, KErrNotFound );
       
   608 
       
   609     CSIPSecSIMRecord& record = static_cast<CSIPSecSIMRecord&>( UserRecord() );
       
   610 
       
   611     record.UserCredentialsFailed( CSIPSecSIMRecord::EPrivateIdentity );
       
   612     iPlugin->SetCredentialsL( record.SIPSecUser(),
       
   613                               record.Realm(),
       
   614                               *iOutboundProxy,
       
   615                               iAuthorization->Response().iIMPI,   // username
       
   616                               iAuthentication->Response().iRES ); // password
       
   617     }
       
   618 
       
   619 // ----------------------------------------------------------------------------
       
   620 // CSIPSecSIMCredentialsProvider::PassErrorToFramework
       
   621 // If aAuthenticate == EFalse, UserCredentialsFailed( EPrivateIdentity ) has
       
   622 // already been done in AuthorizationFailedL.
       
   623 // ----------------------------------------------------------------------------
       
   624 //
       
   625 TInt CSIPSecSIMCredentialsProvider::PassErrorToFramework( TInt aError,
       
   626 	TBool aAuthenticate ) const
       
   627 	{
       
   628 	__SIP_ASSERT_RETURN_VALUE( iPlugin, KErrNotFound );
       
   629 
       
   630 	__SIP_INT_LOG2( "SIPSecSIMCredPr:PassErrorToFramework err, authen",
       
   631 	                aError,	aAuthenticate )
       
   632 
       
   633 	if ( aError == KErrNoMemory )
       
   634 		{
       
   635 		return aError;
       
   636 		}
       
   637 
       
   638 	CSIPSecSIMRecord& record = static_cast<CSIPSecSIMRecord&>( UserRecord() );
       
   639 
       
   640 	if ( aAuthenticate )
       
   641 		{		
       
   642 		// Sets state -> ECredentialsFailed and failure type -> ENoFailure
       
   643 		record.Invalidate( CSIPSecUserRecord::ECredentialsFailed );
       
   644 
       
   645 		// Sets failure type -> EPrivateIdentity. Does not change state.
       
   646 	    record.UserCredentialsFailed( CSIPSecSIMRecord::EPrivateIdentity );
       
   647 		}
       
   648 
       
   649 	// Inform SIPSecFW that the asynchronous request has ended
       
   650 	record.Updated();
       
   651 
       
   652 	// Don't remove anything from cache, but use CleanObservers
       
   653 	iPlugin->ClearCache( NULL );
       
   654 
       
   655 	return KErrNone;
       
   656 	}
       
   657 
       
   658 // ----------------------------------------------------------------------------
       
   659 // CSIPSecSIMCredentialsProvider::HexDumpToLog
       
   660 // ----------------------------------------------------------------------------
       
   661 //
       
   662 void CSIPSecSIMCredentialsProvider::HexDumpToLog( const TDesC8& aMessage,
       
   663 												  const TDesC8& aData ) const
       
   664 	{
       
   665 	__SIP_DES8_LOG( "CSIPSecSIMCredentialsProvider::HexDumpToLog", aMessage )
       
   666 
       
   667 	// Prevent compiler warning in UREL build
       
   668 	aMessage.Length();
       
   669 
       
   670 	// Each byte uses 3 characters (e.g. 0x12 is written as "12 ")
       
   671 	const TInt KLengthOfByte = 3;
       
   672 	const TInt KMaxLineLength = 60;
       
   673 	TBuf8< KMaxLineLength > logBuf;
       
   674 
       
   675 	TInt inputPos( 0 );
       
   676 	while ( inputPos < aData.Length() )
       
   677 		{
       
   678 		logBuf.Zero();
       
   679 
       
   680 		TInt outputPos( 0 );
       
   681 		while ( ( outputPos < KMaxLineLength - KLengthOfByte ) &&
       
   682 		        ( inputPos < aData.Length() ) )
       
   683 			{
       
   684 			logBuf.AppendFormat( _L8( "%02x " ), aData[ inputPos++ ] );
       
   685 			outputPos += KLengthOfByte;
       
   686 			}
       
   687 		__SIP_DES8_LOG( "CSIPSecSIMCredentialsProvider::HexDumpToLog", logBuf )
       
   688 		}
       
   689 	}
       
   690 
       
   691 // End of File