syncmlfw/common/syncagent/src/nsmlauth.cpp
changeset 0 b497e44ab2fc
child 14 504e41245867
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Server authentication
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <imcvcodc.h>
       
    21 #include <e32math.h>
       
    22 #include "NSmlAuth.h"
       
    23 #include "nsmlcliagconstants.h"
       
    24 #include "NSmlAgentBase.h"
       
    25 #include "nsmlagenttestdefines.h"
       
    26 
       
    27 
       
    28 
       
    29 // ---------------------------------------------------------
       
    30 // CNSmlAuth::CNSmlAuth()
       
    31 // Constructor, nothing special in here.
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 CNSmlAuth::CNSmlAuth()
       
    35 	{
       
    36 	}
       
    37 
       
    38 // ---------------------------------------------------------
       
    39 // CNSmlAuth::~CNSmlAuth()
       
    40 // Destructor
       
    41 // ---------------------------------------------------------
       
    42 //
       
    43 CNSmlAuth::~CNSmlAuth()
       
    44 	{
       
    45 	delete iCredential;
       
    46 	delete iType;
       
    47 	delete iFormat;
       
    48 	delete iNonce;
       
    49 	delete iB64Nonce;
       
    50 	}
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // CNSmlAuth::NewL()
       
    54 // Creates new instance of CNSmlAuth. 
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CNSmlAuth* CNSmlAuth::NewL()
       
    58 	{
       
    59 	CNSmlAuth* self = new (ELeave) CNSmlAuth();
       
    60 	CleanupStack::PushL( self );
       
    61 	self->ConstructL();
       
    62 	CleanupStack::Pop();
       
    63 	return( self );
       
    64 	}
       
    65 // ---------------------------------------------------------
       
    66 // CNSmlAuth::SetSyncAgent()
       
    67 // 
       
    68 // ---------------------------------------------------------
       
    69 EXPORT_C void CNSmlAuth::SetSyncAgent( CNSmlAgentBase* aAgent ) 
       
    70 	{
       
    71 	iAgent = aAgent;
       
    72 	}
       
    73 // ---------------------------------------------------------
       
    74 // CNSmlAuth::SetCredentialL()
       
    75 // 
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 EXPORT_C void CNSmlAuth::SetCredentialL( const TDesC8& aCredential )
       
    79 	{
       
    80 	delete iCredential;
       
    81 	iCredential = NULL;
       
    82 	iCredential = aCredential.AllocL();
       
    83 	
       
    84 	}
       
    85 // ---------------------------------------------------------
       
    86 // CNSmlAuth::SetTypeL()
       
    87 // 
       
    88 // ---------------------------------------------------------
       
    89 //	
       
    90 void CNSmlAuth::SetTypeL( const TDesC8& aType )
       
    91 	{
       
    92 	delete iType;
       
    93 	iType = NULL;
       
    94 	iType = aType.AllocL();
       
    95 	}
       
    96 // ---------------------------------------------------------
       
    97 // CNSmlAuth::SetFormatL()
       
    98 // 
       
    99 // ---------------------------------------------------------
       
   100 //	
       
   101 void CNSmlAuth::SetFormatL( const TDesC8& aFormat )
       
   102 	{
       
   103 	delete iFormat;
       
   104 	iFormat = NULL;
       
   105 	iFormat = aFormat.AllocL();
       
   106 	}
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CNSmlAuth::SetAuthRequiredL()
       
   110 // 
       
   111 // ---------------------------------------------------------
       
   112 //	
       
   113 EXPORT_C void CNSmlAuth::SetAuthRequiredL()
       
   114 	{
       
   115 	iAuthState = ENotAuthenticated;
       
   116 	HBufC* unicodeNonce = iAgent->ServerNonceAllocL();
       
   117 	CleanupStack::PushL( unicodeNonce );
       
   118 	iNonce = HBufC8::NewL( unicodeNonce->Length() );
       
   119 	iNonce->Des().Copy( *unicodeNonce ); 
       
   120 	CleanupStack::PopAndDestroy(); //unicodeNonce 
       
   121 	if ( iNonce->Length() == 0 )
       
   122 		{
       
   123 		CreateAndSaveNewNonceL();
       
   124 		}
       
   125 	}
       
   126 // ---------------------------------------------------------
       
   127 // CNSmlAuth::SetChallenced()
       
   128 // 
       
   129 // ---------------------------------------------------------
       
   130 //	
       
   131 void CNSmlAuth::SetChallenced()
       
   132 	{
       
   133 	if ( iAuthState == ENotAuthenticated )
       
   134 		{
       
   135 		iAuthState = EAuthChallenced;
       
   136 		}
       
   137 	if ( iAuthState == EAuthenticated )
       
   138 		{
       
   139 		iAuthState = EChallencedForNext;
       
   140 		}
       
   141 	}
       
   142 
       
   143 // ---------------------------------------------------------
       
   144 // CNSmlAuth::CreateAndSaveNewNonceL()
       
   145 // ---------------------------------------------------------
       
   146 void CNSmlAuth::CreateAndSaveNewNonceL()
       
   147 	{
       
   148 	delete iNonce;
       
   149 	iNonce = NULL;
       
   150 	iNonce = HBufC8::NewL( 5 );
       
   151 	_LIT8( KNSmlNonceMask, "%05d" );
       
   152 	TInt64 seed = User::TickCount();
       
   153 	TInt nonce = Math::Rand( seed );
       
   154 	iNonce->Des().Format( KNSmlNonceMask, nonce );
       
   155 #ifdef __CONSTANT_NONCE
       
   156 	*iNonce = _L8("nonce");
       
   157 #endif
       
   158 	HBufC* unicodeNonce = HBufC::NewLC( iNonce->Length() );
       
   159 	unicodeNonce->Des().Copy( *iNonce );
       
   160 	iAgent->SetServerNonceL( *unicodeNonce );
       
   161 	CleanupStack::PopAndDestroy(); //unicodeNonce 
       
   162 	}
       
   163 
       
   164 // ---------------------------------------------------------
       
   165 // CNSmlAuth::NonceL()
       
   166 // 
       
   167 // ---------------------------------------------------------
       
   168 //
       
   169 TPtrC8 CNSmlAuth::NonceL()
       
   170 	{
       
   171 	if ( iAuthState == EAuthenticated )
       
   172 		{
       
   173 		CreateAndSaveNewNonceL();
       
   174 		}
       
   175 	TImCodecB64 B64Coder;
       
   176 	delete iB64Nonce;
       
   177 	iB64Nonce = NULL;
       
   178 	iB64Nonce = HBufC8::NewL( iNonce->Length() * 2 + 1 );
       
   179 	TPtr8 B64NoncePtr( iB64Nonce->Des() );
       
   180 	User::LeaveIfError( B64Coder.Encode( *iNonce, B64NoncePtr ) );
       
   181 	return *iB64Nonce;
       
   182 	}
       
   183 
       
   184 // ---------------------------------------------------------
       
   185 // CNSmlAuth::ChallengeNeeded()
       
   186 // 
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 TBool CNSmlAuth::ChallengeNeeded() const
       
   190 	{
       
   191 	if ( iAuthState == ENotAuthenticated || iAuthState == EAuthenticated )
       
   192 		{
       
   193 		return ETrue;
       
   194 		}
       
   195 	return EFalse;
       
   196 	}
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CNSmlAuth::Challenged()
       
   200 // 
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 EXPORT_C TBool CNSmlAuth::Challenged() const
       
   204 	{
       
   205 	if ( iAuthState == EAuthChallenced )
       
   206 		{
       
   207 		return ETrue;
       
   208 		}
       
   209 	return EFalse;
       
   210 	}
       
   211 
       
   212 // ---------------------------------------------------------
       
   213 // CNSmlAuth::StatusCodeL()
       
   214 // 
       
   215 // ---------------------------------------------------------
       
   216 //
       
   217 TNSmlError::TNSmlSyncMLStatusCode CNSmlAuth::StatusCodeL()
       
   218 	{
       
   219 	if ( iAuthState == ENotInUse )
       
   220 		{
       
   221 		return TNSmlError::ESmlStatusOK;
       
   222 		}
       
   223 	if ( iAuthState == EAuthenticated || iAuthState == EChallencedForNext )
       
   224 		{
       
   225 		return TNSmlError::ESmlStatusAuthenticationAccepted;
       
   226 		}
       
   227 	TNSmlError::TNSmlSyncMLStatusCode statusCode = TNSmlError::ESmlStatusOK;
       
   228 	if ( iCredential == NULL || iType == NULL )
       
   229 		{
       
   230 		statusCode = TNSmlError::ESmlStatusClientAuthenticationRequired;
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 		if ( iType->Des() !=  KNSmlAgentAuthMD5 )
       
   235 			{
       
   236 			statusCode = TNSmlError::ESmlStatusClientAuthenticationRequired;
       
   237 			}
       
   238 		else
       
   239 			{
       
   240             statusCode = CheckCredentialL();
       
   241 			if ( statusCode == TNSmlError::ESmlStatusAuthenticationAccepted )
       
   242 				{
       
   243 				iAuthState = EAuthenticated;
       
   244 				}
       
   245 			}
       
   246 		}	
       
   247 	if ( iAuthState == EAuthChallenced && statusCode == TNSmlError::ESmlStatusClientAuthenticationRequired )
       
   248 		{
       
   249 		statusCode = TNSmlError::ESmlStatusUnauthorized;
       
   250 		}
       
   251 	return statusCode;
       
   252 	}
       
   253 // ---------------------------------------------------------
       
   254 // CNSmlAuth::ConstructL()
       
   255 // ---------------------------------------------------------
       
   256 //
       
   257 void CNSmlAuth::ConstructL()
       
   258 	{
       
   259 	iAuthState = ENotInUse;
       
   260 	iType = KNSmlAgentAuthMD5().AllocL();
       
   261 	iFormat = HBufC8::NewL(0);
       
   262 	}
       
   263 
       
   264 // ---------------------------------------------------------
       
   265 // CNSmlAuth::CheckCredentialL()
       
   266 // ---------------------------------------------------------
       
   267 TNSmlError::TNSmlSyncMLStatusCode CNSmlAuth::CheckCredentialL() 
       
   268 	{
       
   269 	HBufC8* B64Cred;
       
   270 	if ( iFormat->Des() != KNSmlAgentBase64Format )
       
   271 		{
       
   272 		TImCodecB64 B64Coder;
       
   273 		B64Cred = HBufC8::NewLC( iCredential->Length() * 2 + 1 );
       
   274 		TPtr8 B64CredPtr( B64Cred->Des() );
       
   275 		User::LeaveIfError( B64Coder.Encode( *iCredential, B64CredPtr ) );
       
   276 		}
       
   277 	else
       
   278 		{
       
   279 		B64Cred = iCredential->AllocLC();
       
   280 		}
       
   281 	HBufC8* digest = iAgent->Md5CredentialL( ETrue );
       
   282 	TNSmlError::TNSmlSyncMLStatusCode statusCode = TNSmlError::ESmlStatusAuthenticationAccepted;
       
   283 	if ( *digest != *B64Cred )
       
   284 		{
       
   285 		statusCode = TNSmlError::ESmlStatusClientAuthenticationRequired;
       
   286 		}
       
   287 	CleanupStack::PopAndDestroy(); //B64Cred
       
   288 	delete digest;
       
   289 	return statusCode;
       
   290 	}
       
   291 
       
   292 
       
   293 
       
   294 
       
   295