multimediacommsengine/tsrc/testdriver/siptester/src/CTcSIPIMSAuthenticator.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007 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:  See class definition below.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CTcSIPIMSAuthenticator.h"
       
    19 #include "SipConstants.h"
       
    20 #include "ErrorHandling.h"
       
    21 
       
    22 #include <commsdat.h>
       
    23 #include <CommsDatTypesV1_1.h>
       
    24 #include <MetaDatabase.h>
       
    25 #include <cdblen.h>
       
    26 #include <tconvbase64.h>  
       
    27 
       
    28 
       
    29 #ifdef RD_SIP_TESTER
       
    30 const TInt KAuthenticationTimeout = 30000000; // 30 seconds
       
    31 #endif
       
    32 
       
    33 CTcSIPIMSAuthenticationParams* CTcSIPIMSAuthenticationParams::NewL()
       
    34     {
       
    35     CTcSIPIMSAuthenticationParams* self = 
       
    36         new ( ELeave ) CTcSIPIMSAuthenticationParams();
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 CTcSIPIMSAuthenticationParams::~CTcSIPIMSAuthenticationParams()
       
    44     {
       
    45     delete iRes;
       
    46     delete iIk;
       
    47     delete iCk;
       
    48     delete iAuts;
       
    49     }
       
    50 	
       
    51 void CTcSIPIMSAuthenticationParams::SetResL( const TDesC8& aRes )
       
    52     {
       
    53     delete iRes;
       
    54     iRes = 0;
       
    55     
       
    56     const TInt KBase64EncodedResLength = 20;
       
    57     iRes = EncodeL( aRes, KBase64EncodedResLength );
       
    58     }
       
    59 	    
       
    60 void CTcSIPIMSAuthenticationParams::SetIkL( const TDesC8& aIk )
       
    61     {
       
    62     delete iIk;
       
    63     iIk = 0;
       
    64     
       
    65     const TInt KBase64EncodedIkLength = 20;
       
    66     iIk = EncodeL( aIk, KBase64EncodedIkLength );
       
    67     }
       
    68     
       
    69 void CTcSIPIMSAuthenticationParams::SetCkL( const TDesC8& aCk )
       
    70     {
       
    71     delete iCk;
       
    72     iCk = 0;
       
    73     
       
    74     const TInt KBase64EncodedCkLength = 20;
       
    75 	iCk = EncodeL( aCk, KBase64EncodedCkLength );
       
    76     }
       
    77     
       
    78 void CTcSIPIMSAuthenticationParams::SetAutsL( const TDesC8& aAuts )
       
    79     {
       
    80     delete iAuts;
       
    81     iAuts = 0;
       
    82     
       
    83     const TInt KBase64EncodedAutsLength = 20;
       
    84 	iAuts = EncodeL( aAuts, KBase64EncodedAutsLength );
       
    85     }
       
    86 	    
       
    87 const TDesC8& CTcSIPIMSAuthenticationParams::Res()
       
    88     {
       
    89     return *iRes;
       
    90     }
       
    91 	    
       
    92 const TDesC8& CTcSIPIMSAuthenticationParams::Ik()
       
    93     {
       
    94     return *iIk;
       
    95     }
       
    96 	    
       
    97 const TDesC8& CTcSIPIMSAuthenticationParams::Ck()
       
    98     {
       
    99     return *iCk;
       
   100     }
       
   101 	    
       
   102 const TDesC8& CTcSIPIMSAuthenticationParams::Auts()
       
   103     {
       
   104     return *iAuts;
       
   105     }
       
   106 
       
   107 void CTcSIPIMSAuthenticationParams::ConstructL()
       
   108     {
       
   109     iRes = KNullDesC8().AllocL();
       
   110     iIk = KNullDesC8().AllocL();
       
   111     iCk = KNullDesC8().AllocL();
       
   112     iAuts = KNullDesC8().AllocL();
       
   113     }
       
   114     
       
   115 HBufC8* CTcSIPIMSAuthenticationParams::EncodeL( 
       
   116     const TDesC8& aInput, 
       
   117     TInt aEncodedMaxLen )
       
   118     {
       
   119     if ( aInput.Length() > 0 )
       
   120         {
       
   121     	HBufC8* encoded = HBufC8::NewLC( aEncodedMaxLen );
       
   122     	TPtr8 encodedPtr( encoded->Des() );	
       
   123         TBase64 encoder;
       
   124     	User::LeaveIfError( encoder.Encode( aInput, encodedPtr ) );
       
   125     	CleanupStack::Pop( encoded );
       
   126     	return encoded;
       
   127         }
       
   128     return KNullDesC8().AllocL();
       
   129     }
       
   130         
       
   131 
       
   132 CTcSIPIMSAuthenticator* CTcSIPIMSAuthenticator::NewL()
       
   133     {
       
   134     CTcSIPIMSAuthenticator* self = 
       
   135         new ( ELeave ) CTcSIPIMSAuthenticator();
       
   136     CleanupStack::PushL( self );
       
   137     self->ConstructL();
       
   138     CleanupStack::Pop( self );
       
   139     return self;
       
   140     }
       
   141 
       
   142 CTcSIPIMSAuthenticator::~CTcSIPIMSAuthenticator()
       
   143     {
       
   144     Cancel();
       
   145     delete iTimer;
       
   146     delete iAuthenticationResult;
       
   147     #ifdef RD_SIP_TESTER
       
   148         {
       
   149         iPhone.Close();
       
   150         }
       
   151     #endif // RD_SIP_TESTER
       
   152     iEtelServer.Close();
       
   153     }
       
   154 
       
   155 
       
   156 void CTcSIPIMSAuthenticator::RunL()
       
   157     {
       
   158     #ifdef RD_SIP_TESTER
       
   159         {
       
   160         TInt error( iStatus.Int() );
       
   161         switch ( error )
       
   162     		{
       
   163     		case KErrNone:
       
   164     		    {
       
   165     		    iAuthenticationResult->SetIkL( iData.iIK );
       
   166                 iAuthenticationResult->SetCkL( iData.iCK );
       
   167                 iAuthenticationResult->SetResL( iData.iRES );
       
   168     		    break;
       
   169     		    }
       
   170     		case -3214: 
       
   171     		    {
       
   172     		    // Using harcoded value for KErrMMEtelSqnVerificationFailed
       
   173     		    // to enable building SIP Tester on public SDKs
       
   174     		    // which do not have file etelmmerr.h.
       
   175     		    iAuthenticationResult->SetResL( iData.iRES );
       
   176                 iAuthenticationResult->SetAutsL( iData.iAUTS );
       
   177     			break;
       
   178     		    }
       
   179     		default:
       
   180     		    {
       
   181     		    // KErrMMEtelAuthenticateFailed and KErrMMEtelMacVerificationFailed
       
   182     		    // are handled over here	
       
   183     			iError = error;
       
   184     			break;
       
   185     		    }
       
   186     		}
       
   187         }
       
   188     #endif // RD_SIP_TESTER   
       
   189         
       
   190     if ( iActiveWait.IsStarted() )
       
   191 		{
       
   192 		iTimer->Remove( iReceiveTimeout );
       
   193 		iActiveWait.AsyncStop();
       
   194 		}
       
   195     }
       
   196         
       
   197 void CTcSIPIMSAuthenticator::DoCancel()
       
   198     {
       
   199     #ifdef RD_SIP_TESTER
       
   200         {
       
   201         iPhone.CancelAsyncRequest( EMobilePhoneIMSAuthenticate );
       
   202         }
       
   203     #endif // RD_SIP_TESTER
       
   204     }
       
   205 
       
   206 CTcSIPIMSAuthenticationParams& CTcSIPIMSAuthenticator::AuthenticateL( 
       
   207     const TDesC8& aNonce )
       
   208     {
       
   209     #ifdef RD_SIP_TESTER
       
   210         {
       
   211         __ASSERT_ALWAYS( !IsActive(), User::Leave( KErrInUse ) );
       
   212         
       
   213         CTcSIPIMSAuthenticationParams* tmp = 
       
   214             CTcSIPIMSAuthenticationParams::NewL();
       
   215         delete iAuthenticationResult;
       
   216         iAuthenticationResult = tmp;
       
   217         
       
   218         SetAuthenticationDataL( aNonce );
       
   219         
       
   220         RMobilePhone::TImsAuthenticateDataV5Pckg message =
       
   221             RMobilePhone::TImsAuthenticateDataV5Pckg( iData );
       
   222 
       
   223     	iPhone.ImsAuthenticate( iStatus, message );	
       
   224         SetActive();
       
   225         
       
   226         iTimer->Queue( KAuthenticationTimeout, iReceiveTimeout );
       
   227     	iActiveWait.Start();
       
   228 
       
   229     	if ( !iAuthenticationResult )
       
   230     		{
       
   231     		User::Leave( KErrTimedOut );
       
   232     		}
       
   233     		
       
   234         if ( iError != KErrNone )
       
   235             {
       
   236             TInt err( iError );
       
   237             iError = KErrNone;
       
   238             User::Leave( err );
       
   239             }
       
   240         }
       
   241     #else
       
   242         aNonce.Length(); // Avoid compiler warning   
       
   243         User::Leave( KErrNotSupported );
       
   244     #endif // RD_SIP_TESTER
       
   245     
       
   246     return *iAuthenticationResult;
       
   247     }
       
   248 
       
   249 CTcSIPIMSAuthenticator::CTcSIPIMSAuthenticator() : 
       
   250     CActive( EPriorityStandard )
       
   251     {
       
   252     CActiveScheduler::Add( this );
       
   253     }
       
   254     
       
   255 void CTcSIPIMSAuthenticator::ConstructL()
       
   256     {
       
   257    	iTimer = CDeltaTimer::NewL( CActive::EPriorityStandard );
       
   258 	TCallBack cb( ReceiveTimeout, this );
       
   259 	iReceiveTimeout.Set( cb );
       
   260 	
       
   261 	TBuf<KCommsDbSvrMaxFieldLength> tsyModuleName;
       
   262 	GetTsyModuleNameL( tsyModuleName );
       
   263 	PreparePhoneL( tsyModuleName );
       
   264     }
       
   265         
       
   266 TInt CTcSIPIMSAuthenticator::ReceiveTimeout( TAny* aSelf )
       
   267 	{
       
   268 	CTcSIPIMSAuthenticator& self =
       
   269 				*reinterpret_cast< CTcSIPIMSAuthenticator* >( aSelf );
       
   270 	if( self.iActiveWait.IsStarted() )
       
   271 		{
       
   272 		self.iActiveWait.AsyncStop();
       
   273 		}
       
   274 
       
   275 	return KErrNone;
       
   276 	}
       
   277 	
       
   278 void CTcSIPIMSAuthenticator::GetTsyModuleNameL( TDes& aTsyModuleName ) const
       
   279     {
       
   280     __ASSERT_ALWAYS( aTsyModuleName.MaxSize() >= KCommsDbSvrMaxFieldLength,
       
   281 		User::Leave( KErrArgument ) );
       
   282 
       
   283     using namespace CommsDat;
       
   284 
       
   285     CMDBSession* db = CMDBSession::NewLC( KCDLatestVersion ); 
       
   286 
       
   287     CMDBField<TUint32>* globalSettingField =
       
   288     	new ( ELeave ) CMDBField<TUint32>( KCDTIdModemPhoneServicesSMS );
       
   289     CleanupStack::PushL( globalSettingField );
       
   290 
       
   291     globalSettingField->SetRecordId( 1 );
       
   292     globalSettingField->LoadL( *db );
       
   293     TUint32 modemId = *globalSettingField; 
       
   294 
       
   295     CMDBField<TDesC>* tsyField =
       
   296     	new ( ELeave ) CMDBField<TDesC>( KCDTIdTsyName );
       
   297     CleanupStack::PushL( tsyField );
       
   298 
       
   299     tsyField->SetRecordId( modemId ); 
       
   300     tsyField->LoadL( *db );
       
   301     aTsyModuleName = *tsyField;
       
   302 
       
   303     CleanupStack::PopAndDestroy( 3, db ); // db, tsyField & globalSettingField 
       
   304     }
       
   305 
       
   306 void CTcSIPIMSAuthenticator::PreparePhoneL( const TDes& aTsyModuleName )
       
   307 	{
       
   308 	#ifdef RD_SIP_TESTER
       
   309 	    {
       
   310         User::LeaveIfError( iEtelServer.Connect() );
       
   311         User::LeaveIfError( iEtelServer.LoadPhoneModule( aTsyModuleName ) );
       
   312 
       
   313         //Get total number of phones supported by all currently loaded TSY modules    
       
   314         TInt phoneCount( 0 );
       
   315         User::LeaveIfError( iEtelServer.EnumeratePhones( phoneCount ) );
       
   316 
       
   317         RTelServer::TPhoneInfo phoneInfo;
       
   318     	TName searchTsyName;
       
   319     	TInt phoneIndex( KErrNotFound );
       
   320 
       
   321         while ( phoneIndex == KErrNotFound && phoneCount-- > 0 )
       
   322             {
       
   323             //Check whether this phone belongs to the loaded TSY
       
   324             if ( ( iEtelServer.GetTsyName( phoneCount, searchTsyName ) == KErrNone )
       
   325                  &&
       
   326                  ( searchTsyName.CompareF( aTsyModuleName ) == 0 ) )
       
   327             	{
       
   328             	phoneIndex = phoneCount;        	
       
   329             	}
       
   330             }
       
   331     	if ( phoneIndex == KErrNotFound )
       
   332     		{
       
   333     		User::Leave( KErrNotFound );
       
   334     		}
       
   335 
       
   336         User::LeaveIfError( iEtelServer.GetPhoneInfo( phoneIndex, phoneInfo ) );
       
   337         User::LeaveIfError( iPhone.Open( iEtelServer, phoneInfo.iName ) );	
       
   338 
       
   339         RPhone::TStatus phoneStatus;
       
   340         User::LeaveIfError( iPhone.GetStatus( phoneStatus ) );
       
   341 
       
   342         //Check whether phone has already been initialised
       
   343         if ( phoneStatus.iMode == RPhone::EModeUnknown )
       
   344             {
       
   345             TInt err = iPhone.Initialise();
       
   346 
       
   347             if ( err != KErrNone )
       
   348                 {
       
   349                 iPhone.Close();
       
   350                 User::Leave( err );
       
   351                 }
       
   352             }
       
   353 	    }
       
   354     #else
       
   355         {
       
   356         aTsyModuleName.Length(); // Avoid compiler warning
       
   357         }
       
   358     #endif // RD_SIP_TESTER
       
   359     }
       
   360 
       
   361 void CTcSIPIMSAuthenticator::SetAuthenticationDataL( const TDesC8& aNonce )
       
   362     {
       
   363     #ifdef RD_SIP_TESTER
       
   364         {
       
   365         iData.iRAND.Zero();
       
   366         iData.iAUTN.Zero();
       
   367         iData.iRES.Zero();
       
   368     	iData.iIK.Zero();
       
   369         iData.iCK.Zero();
       
   370         iData.iAUTS.Zero();
       
   371 
       
   372         HBufC8* decoded = HBufC8::NewLC( aNonce.Length() );
       
   373         TPtr8 decodedPtr( decoded->Des() );
       
   374         
       
   375         TBase64 encoder;
       
   376     	User::LeaveIfError( encoder.Decode( aNonce, decodedPtr ) );
       
   377     		       
       
   378         __ASSERT_ALWAYS( decodedPtr.Length() >= 
       
   379                          ( RMobilePhone::KRandLength + RMobilePhone::KAutnLength ),
       
   380                          User::Leave( KErrCorrupt ) );
       
   381 
       
   382         TPtrC8 rand = decodedPtr.Left( RMobilePhone::KRandLength );
       
   383         TPtrC8 autn = decodedPtr.Mid( RMobilePhone::KRandLength,
       
   384                                       RMobilePhone::KAutnLength );
       
   385 
       
   386     	iData.iRAND.Copy( rand );
       
   387     	iData.iAUTN.Copy( autn );
       
   388     	
       
   389     	CleanupStack::PopAndDestroy( decoded );
       
   390         }
       
   391     #else
       
   392         {
       
   393         aNonce.Length(); // Avoid compiler warning
       
   394         }  
       
   395 	#endif // RD_SIP_TESTER
       
   396     }
       
   397