networksecurity/tlsprovider/source/tlsprovider/Ctlsbrowsetoken.cpp
changeset 0 af10295192d8
child 67 bb2423252ea3
child 68 1697cc2ba93d
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-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 //
       
    15 
       
    16 #include "Ctlsbrowsetoken.h"
       
    17 #include "tlsprovider.h"
       
    18 
       
    19 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    20 #include <tlstypedef_internal.h>
       
    21 #endif
       
    22 
       
    23 
       
    24 //
       
    25 //  CTlsBrowseToken
       
    26 //
       
    27 
       
    28 
       
    29 CTlsBrowseToken* CTlsBrowseToken::NewL()
       
    30 	{ 
       
    31 	CTlsBrowseToken* aPtrClnt = new (ELeave)CTlsBrowseToken();
       
    32 	return(aPtrClnt);
       
    33 	}
       
    34 
       
    35 
       
    36 CTlsBrowseToken::CTlsBrowseToken():CActive(0)
       
    37 	{
       
    38 	if(CActiveScheduler::Current()) //Already installed?
       
    39 		{
       
    40 		CActiveScheduler::Add( this );
       
    41 		}
       
    42 	}
       
    43 
       
    44 void CTlsBrowseToken::StartBrowsingL(RArray<CTokenTypesAndTokens>& aListAllTokensAndTypes,
       
    45 									TRequestStatus& aStatus)
       
    46 	{
       
    47 	
       
    48 	
       
    49 	aStatus = KRequestPending;
       
    50 	User::LeaveIfError(iFs.Connect());
       
    51 	iOriginalRequestStatus = &aStatus;
       
    52 
       
    53 	iListAllTokensAndTypes = &aListAllTokensAndTypes;
       
    54 	TLSPROV_LOG(_L("Listing all types and tokens"))
       
    55 
       
    56 	//The following functions fills the tokentype info in iTokenTypeInfo array.
       
    57 	TLSPROV_LOG(_L("Browsing for Tokens..."))
       
    58 	ListTokenTypesL();		
       
    59 	}
       
    60 
       
    61 void CTlsBrowseToken::ListTokenTypesL()
       
    62 	{
       
    63 	iCurrentTokentype= 0;  
       
    64 		
       
    65 	//Filter defining the expected interface
       
    66 	RArray<TUid> Interface;
       
    67 	CleanupClosePushL(Interface);	
       
    68 	User::LeaveIfError(Interface.Append(TUid::Uid(KInterfaceTLSTokenProvider)));
       
    69 	User::LeaveIfError(Interface.Append(TUid::Uid(KInterfaceTLSSession)));
       
    70 	TCTFindTokenTypesByInterface filter(Interface.Array());
       
    71 			
       
    72 	//List all the tokentypes supported [eg. Software Token, WIM1, WIM2, etc]	
       
    73 	CCTTokenTypeInfo::ListL(iTokenTypeInfo, filter);
       
    74 	CleanupStack::Pop(&Interface);	// Interface
       
    75 	Interface.Close();
       
    76 
       
    77 	iTotalTokenTypeCount = iTokenTypeInfo.Count();
       
    78 	if(!iTotalTokenTypeCount)
       
    79 		{
       
    80 		//Not possible..Software token always present
       
    81 		User::Leave(KErrNoTokenTypes);
       
    82 		}	
       
    83 	BrowseTokenTypesL();
       
    84 	}
       
    85 
       
    86 TInt CTlsBrowseToken::TotalTypeCount()
       
    87 	{
       
    88 	return iTotalTokenTypeCount;
       
    89 	}
       
    90 
       
    91 /*
       
    92 The following function gets a handle to a new token type. If any of the interfaces in opening 
       
    93 a token fails and if no other tokens are present in the token type, this function will be then called 
       
    94 to browse for the next token type if any.
       
    95 */
       
    96 void CTlsBrowseToken::BrowseTokenTypesL()
       
    97 	{
       
    98 	//Now we try opening the token types here
       
    99 	TInt Error = KErrNone;
       
   100 	while( iCurrentTokentype  < iTotalTokenTypeCount)
       
   101 		{
       
   102 		if(iPtrTokenType)
       
   103 			iPtrTokenType->Release();
       
   104 	
       
   105 		TRAP(Error,(iPtrTokenType = MCTTokenType::NewL(*iTokenTypeInfo[iCurrentTokentype], iFs)));
       
   106 		
       
   107 		if(Error != KErrNone)
       
   108 			{
       
   109 			//This Token type couldnt be opened..try the next one if present
       
   110          iPtrTokenType = NULL;
       
   111 			if((iCurrentTokentype + 1) <= iTotalTokenTypeCount)
       
   112 				{
       
   113 				(iCurrentTokentype++);
       
   114 				continue;
       
   115 				}
       
   116 			else
       
   117 				User::Leave(Error); 
       
   118 			}
       
   119 		else
       
   120 			{
       
   121 			//List all those tokens in that type
       
   122 			iCurrentState = EGetTokenList;
       
   123 			iStatus = KRequestPending;
       
   124 			iTokenList.Close();
       
   125 			iPtrTokenType->List(iTokenList,iStatus);
       
   126 			SetActive();
       
   127 			return;
       
   128 			}		
       
   129 		}
       
   130 
       
   131 	//Will only come here if every token has been browsed
       
   132 	iCurrentTokentype= 0;
       
   133 	User::RequestComplete(iOriginalRequestStatus, Error); 
       
   134 	}
       
   135 
       
   136 
       
   137 void CTlsBrowseToken::OnEGetTokenListL()
       
   138 	{
       
   139 
       
   140 	TLSPROV_LOG(_L("Obtaining the list of Tokens in a type"))
       
   141 				
       
   142 	if(!iStatus.Int()) //Token list for a particular Token type obtained successfully
       
   143 		{
       
   144 		iTotalTokensinType = iTokenList.Count();
       
   145 		iCurrentToken = 0;
       
   146 		if(iTotalTokensinType)
       
   147 			{
       
   148 			iStatus = KRequestPending;
       
   149 			iCurrentState = EOpenToken;	
       
   150 			iPtrTokenType->OpenToken((*iTokenList[0]), iTokenHandle, iStatus);
       
   151 			SetActive();
       
   152 			return;							
       
   153 			}
       
   154 		}	
       
   155 	//Try for any other TokenTypes? If not present BrowseTokenTypesL() will complete
       
   156 	iCurrentTokentype++;
       
   157 	BrowseTokenTypesL();									
       
   158 	}
       
   159 
       
   160 void CTlsBrowseToken::OnEOpenTokenL()
       
   161 	{		
       
   162 	if(!iStatus.Int())
       
   163 		{		
       
   164 		iStatus = KRequestPending;
       
   165 		iCurrentState = EGetProviderInterface;
       
   166 		iTokenHandle->GetInterface(UidProv,iTokenInterface,iStatus);
       
   167 		SetActive();
       
   168 		return;
       
   169 		}		
       
   170 	
       
   171 	//This token failed, try another one
       
   172 	iCurrentTokentype++;
       
   173 	BrowseTokenTypesL();
       
   174 	}
       
   175 
       
   176 void CTlsBrowseToken::OnEGetProviderInterfaceL()
       
   177 	{
       
   178 	if(!iStatus.Int()) 
       
   179 		{
       
   180 		TLSPROV_LOG(_L("Provider Interface for this token obtained successfully"))
       
   181 				
       
   182       CTokenTypesAndTokens* tempObj = new CTokenTypesAndTokens;
       
   183       if ( !tempObj || iListAllTokensAndTypes->Append(*tempObj) != KErrNone )
       
   184          {
       
   185    		delete tempObj;
       
   186          iTokenHandle->Release();
       
   187          iTokenHandle = NULL;
       
   188          User::Leave( KErrNoMemory );
       
   189          }
       
   190   		delete tempObj;
       
   191       CTokenTypesAndTokens& Tokens = (*iListAllTokensAndTypes)[iListAllTokensAndTypes->Count() - 1];
       
   192       Tokens.iTokenInfo = new CTokenInfo;
       
   193       if ( !Tokens.iTokenInfo  )
       
   194          {
       
   195          iTokenHandle->Release();
       
   196          iTokenHandle = NULL;
       
   197          User::Leave( KErrNoMemory );
       
   198          }
       
   199 
       
   200       iTokenHandle = NULL; //owned by iProviderInterface
       
   201 		Tokens.iProviderInterface = static_cast<MTLSTokenProvider*>(iTokenInterface);
       
   202       iTokenInterface = NULL;
       
   203    
       
   204 					
       
   205 		iCurrentState = EGetCiphers;
       
   206 		iStatus = KRequestPending;
       
   207 		
       
   208 		Tokens.iProviderInterface->CryptoCapabilities(
       
   209 			Tokens.iTokenInfo->iSupportedProtocols,
       
   210 			Tokens.iTokenInfo->aKeyExchAlgs,
       
   211 			Tokens.iTokenInfo->aSignatureExchAlgs,
       
   212 			iStatus);		
       
   213 		SetActive();
       
   214 		return;
       
   215 
       
   216 		}
       
   217 	
       
   218 	iCurrentTokentype++;
       
   219 	BrowseTokenTypesL();
       
   220 	}
       
   221 
       
   222 
       
   223 void CTlsBrowseToken::RunL()
       
   224 	{
       
   225 	if(iStatus.Int() == KErrNoMemory)
       
   226 		{
       
   227 		User::RequestComplete(iOriginalRequestStatus, iStatus.Int()); 
       
   228 		iCurrentState = ENullState;
       
   229 		return;
       
   230 		}
       
   231 
       
   232 	switch(iCurrentState)
       
   233 		{
       
   234 	case EGetTokenList:
       
   235 		OnEGetTokenListL();
       
   236 		break;
       
   237 	case EOpenToken:
       
   238 		OnEOpenTokenL();
       
   239 		break;
       
   240 	case EGetProviderInterface:
       
   241 		{
       
   242 		OnEGetProviderInterfaceL();
       
   243 		}
       
   244 		break;
       
   245 	case EGetCiphers:
       
   246 		{		
       
   247 
       
   248       CTokenTypesAndTokens& Tokens = (*iListAllTokensAndTypes)[iListAllTokensAndTypes->Count() - 1];
       
   249 		Tokens.iTotalTokenCount = iTotalTokensinType;
       
   250 
       
   251 		const TUid KSoftwareTokenType = {0x101FE20D};
       
   252 		TLSPROV_LOG2(_L("loaded ECOM TLS tokentype plugin with uid = %08x"), ((Tokens.iProviderInterface->Token()).TokenType()).Type())
       
   253 		if(((Tokens.iProviderInterface->Token()).TokenType()).Type() == KSoftwareTokenType)
       
   254 			Tokens.iSoftwareToken = ETrue;
       
   255 		else
       
   256 			Tokens.iSoftwareToken = EFalse;
       
   257 
       
   258 		
       
   259 		iCurrentTokentype++;
       
   260 		BrowseTokenTypesL();
       
   261 		}
       
   262 		break;
       
   263 	case EGetSessionInterface:
       
   264 		{
       
   265 		TLSPROV_LOG(_L("Session Interface for this token obtained successfully"))
       
   266 		*iSessionInterface = static_cast<MTLSSession*>(iTokenInterface);
       
   267       iTokenInterface = NULL;
       
   268 		User::RequestComplete(iOriginalRequestStatus, iStatus.Int()); 
       
   269 		}
       
   270 		break;
       
   271 	default:
       
   272 		break;
       
   273 		}
       
   274 
       
   275 	}
       
   276 
       
   277 
       
   278 void CTlsBrowseToken::GetSessionInterface(MTLSTokenProvider* aProviderInterface,
       
   279 										  MTLSSession*& aSessionInterface,
       
   280 										  TRequestStatus& aStatus)
       
   281 	{
       
   282 	aStatus = KRequestPending;	
       
   283 	iSessionInterface = &aSessionInterface;
       
   284 	iCurrentState = EGetSessionInterface;
       
   285 	iOriginalRequestStatus = &aStatus;
       
   286 	iTokenInterface = 0;
       
   287 	iStatus = KRequestPending;
       
   288 	iTokenProvider = aProviderInterface;
       
   289 	(aProviderInterface->Token()).GetInterface(UidSess,iTokenInterface,iStatus);
       
   290 	SetActive();
       
   291 	return;
       
   292 	}
       
   293 
       
   294 TInt CTlsBrowseToken::RunError(TInt aError)
       
   295 	{
       
   296 	User::RequestComplete(iOriginalRequestStatus, aError); 
       
   297 	return KErrNone;
       
   298 	}
       
   299 
       
   300 void CTlsBrowseToken::CancelRequest()
       
   301 	{
       
   302 	Cancel();
       
   303 	return;
       
   304 	}
       
   305 void CTlsBrowseToken::DoCancel()
       
   306 	{
       
   307 	switch (iCurrentState)
       
   308 		{
       
   309 	case EGetTokenList:
       
   310 		iPtrTokenType->CancelList();
       
   311 		break;
       
   312 		
       
   313 	case EOpenToken:
       
   314 		iPtrTokenType->CancelOpenToken();
       
   315 		break;
       
   316 		
       
   317 	case EGetProviderInterface:
       
   318 		iTokenHandle->CancelGetInterface();
       
   319 		break;
       
   320 	
       
   321 	case EGetSessionInterface:
       
   322 		(iTokenProvider->Token()).CancelGetInterface();
       
   323 		break;		
       
   324 
       
   325 	case EGetCiphers:
       
   326 		MTLSTokenProvider* provider = static_cast<MTLSTokenProvider*>(iTokenInterface);
       
   327 		provider->CancelCryptoCapabilities();
       
   328 		break;
       
   329 
       
   330 		}
       
   331 	iCurrentState=ENullState;
       
   332 	User::RequestComplete(iOriginalRequestStatus, KErrCancel);
       
   333 	return;
       
   334 	}
       
   335 
       
   336 CTlsBrowseToken::~CTlsBrowseToken()
       
   337 	{
       
   338 	if(iPtrTokenType)
       
   339 		{
       
   340 		iPtrTokenType->Release();
       
   341 		}
       
   342 		
       
   343 	iTokenTypeInfo.Close();
       
   344 	iTokenList.Close();
       
   345 	
       
   346 	if ( iTokenInterface )
       
   347 		{
       
   348 		iTokenInterface->Release();
       
   349 		}
       
   350 		
       
   351 	if(iTokenHandle)
       
   352 		{
       
   353 		iTokenHandle->Release();
       
   354 		}
       
   355 		
       
   356 	iFs.Close();
       
   357 	
       
   358 	return;
       
   359 	}