eapol/eapol_framework/eapol_symbian/am/type/tls_peap/symbian/plugin/src/EapTlsPeapCertFetcher.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 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 the License "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:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // This is enumeration of EAPOL source code.
       
    21 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    22 	#undef EAP_FILE_NUMBER_ENUM
       
    23 	#define EAP_FILE_NUMBER_ENUM 414 
       
    24 	#undef EAP_FILE_NUMBER_DATE 
       
    25 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    26 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    27 
       
    28 
       
    29 // INCLUDE FILES
       
    30 
       
    31 #include "EapTlsPeapCertFetcher.h"
       
    32 #include <EapTlsPeapUiCertificates.h>
       
    33 #include "eap_am_trace_symbian.h"
       
    34 
       
    35 #include <x509cert.h>
       
    36 #include <X509CertNameParser.h>
       
    37 #include <x509certext.h>
       
    38 
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 CEapTlsPeapCertFetcher* CEapTlsPeapCertFetcher::NewL(CEapTlsPeapUiCertificates* const aParent)
       
    43 {
       
    44 	CEapTlsPeapCertFetcher* self = new(ELeave) CEapTlsPeapCertFetcher(aParent);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop();
       
    48 	return self;
       
    49 }
       
    50 
       
    51 //--------------------------------------------------
       
    52 
       
    53 // DON'T USE THIS FUNCTION. THIS IS ONLY FOR EapTlsPeapUtils. 	
       
    54 CEapTlsPeapCertFetcher* CEapTlsPeapCertFetcher::NewL()
       
    55 {
       
    56 	CEapTlsPeapCertFetcher* self = new(ELeave) CEapTlsPeapCertFetcher();
       
    57 	CleanupStack::PushL(self);
       
    58 
       
    59 	/************* THIS PART MAY NOT BE NEEDED AT ALL. NOT A GOOD IDEA TO INSTALL SCHEDULER HERE ****************/
       
    60 	// Check if we are in a scheduler already.
       
    61 	CActiveScheduler* scheduler = NULL;
       
    62 	scheduler = CActiveScheduler::Current();
       
    63 	
       
    64 	// There may not be a default scheduler if called from EapTlsPeapUtils for the first time.
       
    65 	// Hence need to add one. Otherwise no need to install another.
       
    66 	if( scheduler == NULL )
       
    67 	{
       
    68 	
       
    69 		EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::NewL -No default scheduler!!\n") ) );
       
    70 	
       
    71 		scheduler = new (ELeave) CActiveScheduler();
       
    72 		CActiveScheduler::Install(scheduler);		
       
    73 	}
       
    74 	/*****************************/
       
    75 	
       
    76 	self->ConstructL();
       
    77 	CleanupStack::Pop();	
       
    78 	
       
    79 	return self;
       
    80 }
       
    81 
       
    82 //--------------------------------------------------
       
    83 
       
    84 CEapTlsPeapCertFetcher::CEapTlsPeapCertFetcher(CEapTlsPeapUiCertificates* const aParent)
       
    85 : CActive(CActive::EPriorityStandard)
       
    86 , iParent(aParent)
       
    87 , iEncodedCertificate(0)
       
    88 , iCertPtr(0,0)
       
    89 {
       
    90 }
       
    91 
       
    92 //--------------------------------------------------
       
    93 
       
    94 // DON'T USE THIS FUNCTION. THIS IS ONLY FOR EapTlsPeapUtils. 	
       
    95 CEapTlsPeapCertFetcher::CEapTlsPeapCertFetcher()
       
    96 : CActive(CActive::EPriorityStandard)
       
    97 , iParent(NULL)
       
    98 , iEncodedCertificate(0)
       
    99 , iCertPtr(0,0)
       
   100 {
       
   101 }
       
   102 
       
   103 //--------------------------------------------------
       
   104 
       
   105 void CEapTlsPeapCertFetcher::ConstructL()
       
   106 {
       
   107 	User::LeaveIfError(iFs.Connect());
       
   108 
       
   109 	CActiveScheduler::Add(this);
       
   110 	
       
   111 	iEncodedCertificate = HBufC8::NewL(0);
       
   112 	iCertPtr.Set(iEncodedCertificate->Des());	
       
   113 
       
   114 }
       
   115 
       
   116 //--------------------------------------------------
       
   117 
       
   118 CEapTlsPeapCertFetcher::~CEapTlsPeapCertFetcher()
       
   119 {
       
   120 	// Delete iCertInfos
       
   121 	for (TInt i = 0; i < iCertInfos.Count(); i++)
       
   122 	{
       
   123 		iCertInfos[i]->Release();
       
   124 	}
       
   125 	iCertInfos.Reset();
       
   126 
       
   127 	iCACerts.Reset();
       
   128 
       
   129 	iUserCerts.Reset();
       
   130 		
       
   131 	delete iCertFilter;
       
   132 	
       
   133 	delete iCertStore;
       
   134 	
       
   135 	delete iEncodedCertificate;	
       
   136 	
       
   137 	iFs.Close();
       
   138 
       
   139 	if(IsActive())
       
   140 	{
       
   141 		Cancel();		
       
   142 	}
       
   143 }
       
   144 
       
   145 //--------------------------------------------------
       
   146 
       
   147 void CEapTlsPeapCertFetcher::GetCertificatesL()
       
   148 {	
       
   149 	iState = EGetCertificatesInitStore;
       
   150 	if (iCertStore == 0)
       
   151 	{
       
   152 		iCertStore = CUnifiedCertStore::NewL(iFs, false);
       
   153 		iCertStore->Initialize(iStatus);		
       
   154 	}
       
   155 	else
       
   156 	{
       
   157 		TRequestStatus* status = &iStatus;
       
   158 		User::RequestComplete(status, KErrNone);		
       
   159 	}
       
   160 	SetActive();
       
   161 }
       
   162 
       
   163 
       
   164 void CEapTlsPeapCertFetcher::DoCancel()
       
   165 {
       
   166 }
       
   167 
       
   168 //--------------------------------------------------
       
   169 
       
   170 void CEapTlsPeapCertFetcher::RunL()
       
   171 {	
       
   172 	EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL - iStatus.Int()=%d, iState=%d \n"),
       
   173 	iStatus.Int() , iState));
       
   174 	if( iState == EGetSymbianSubjectKeyId )
       
   175 	{
       
   176 		// Only for GetSymbianSubjectKeyIdL.
       
   177 		iWait.AsyncStop(); // This is needed to continue the execution after Wait.Start()
       
   178 		return; // No need to proceed further.
       
   179 	}
       
   180 	
       
   181 	if( iState == EGetCertificatesRetrieveCert)
       
   182 	{
       
   183 		// This is executed when certificate details are being retrieved.
       
   184 		iWait.AsyncStop(); // This is needed to continue the execution after Wait.Start()
       
   185 		return; // No need to proceed further.
       
   186 	}
       
   187 	
       
   188 	int i;
       
   189 	TInt err(KErrNone);
       
   190 		
       
   191 	// This causes panic if leaves
       
   192 	if (iStatus.Int() != KErrNone)
       
   193 	{
       
   194 RDebug::Print(_L("CEapTlsPeapCertFetcher::RunL() -- don't leave..."));
       
   195 	}
       
   196 	
       
   197 	switch (iState)
       
   198 	{
       
   199 	case EGetCertificatesInitStore:
       
   200 		{
       
   201 			// Delete iCertInfos
       
   202 			for (TInt i = 0; i < iCertInfos.Count(); i++)
       
   203 			{
       
   204 				iCertInfos[i]->Release();
       
   205 			}
       
   206 			iCertInfos.Reset();
       
   207 		
       
   208 			delete iCertFilter;
       
   209 			iCertFilter = 0;
       
   210 		
       
   211 			TRAP(err, iCertFilter = CCertAttributeFilter::NewL());
       
   212 			if (err != KErrNone)
       
   213 			{
       
   214 				// Complete with empty lists
       
   215 				TInt err(KErrNone);
       
   216 				TRAP(err, iParent->CompleteReadCertificatesL(iUserCerts, iCACerts));
       
   217 				break;
       
   218 			}
       
   219 			iCertFilter->SetFormat(EX509Certificate);
       
   220 
       
   221 			iState = EGetCertificatesGetCertList;
       
   222 			iCertStore->List(
       
   223 				iCertInfos,
       
   224 				*iCertFilter, 
       
   225 				iStatus);
       
   226 			SetActive();		
       
   227 		}
       
   228 		break;
       
   229 
       
   230 	case EGetCertificatesGetCertList:
       
   231 		{			
       
   232 			EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL - EGetCertificatesGetCertList - Symbian cert store found %d certs in device\n"),
       
   233 			iCertInfos.Count()));
       
   234 		
       
   235 			if(0 == iCertInfos.Count())
       
   236 			{
       
   237 				EAP_TRACE_DEBUG_SYMBIAN((_L("ERROR : CEapTlsPeapCertFetcher::RunL - SERIOUS PROBLEM - Symbian cert store couldn't find any certs in device\n")));				
       
   238 			}
       
   239 		
       
   240 			for (i = 0; i < iCertInfos.Count(); i++)
       
   241 			{				
       
   242 			    CCTCertInfo* CertInfo;
       
   243 				CertInfo = iCertInfos[i];
       
   244 				iEncodedCertificate->Des().SetLength(0);
       
   245 				
       
   246 				TRAPD(err, iEncodedCertificate = iEncodedCertificate->ReAllocL(iCertInfos[i]->Size()));
       
   247 				if (err != KErrNone)
       
   248 				{
       
   249 					EAP_TRACE_DEBUG_SYMBIAN((_L("\nCEapTlsPeapCertFetcher::RunL() -  EGetCertificatesGetCertList - leave from iEncodedCertificate->ReAllocL Error:%d\n"), err ) );
       
   250 				}		
       
   251 				iCertPtr.Set(iEncodedCertificate->Des());
       
   252 
       
   253 				EAP_TRACE_DEBUG_SYMBIAN((_L("\nCEapTlsPeapCertFetcher::RunL() - EGetCertificatesGetCertList - Retreiving cert %d\n"), i ) );
       
   254 				
       
   255 			    iCertStore->Retrieve( *CertInfo, iCertPtr, iStatus);
       
   256 			    
       
   257 			    iState = EGetCertificatesRetrieveCert;
       
   258 
       
   259 			    SetActive();
       
   260 			    iWait.Start();
       
   261 			 	
       
   262 				EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL() - iWait.Start() returned, iStatus.Int()=%d \n"),iStatus.Int() ) );
       
   263 
       
   264 			 	CCertificate* cert = NULL;
       
   265 
       
   266 			    if ( iStatus.Int() == KErrNone )
       
   267 		        {
       
   268 			        switch ( CertInfo->CertificateFormat() )
       
   269 		            {
       
   270 		            case EX509Certificate:
       
   271 		                {
       
   272 		                TRAPD(err, cert = CX509Certificate::NewL( iCertPtr ));
       
   273 			            if (err != KErrNone)
       
   274 			               	EAP_TRACE_DEBUG_SYMBIAN((_L("\nCEapTlsPeapCertFetcher::RunL() - EGetCertificatesGetCertList - leave from CX509Certificate::NewL Label:%S Error:%d\n"),&(CertInfo->Label()), err ) );
       
   275 			            break;
       
   276 		                }
       
   277 		            default:
       
   278 		                {
       
   279 		                	// Only  X509 type of certificates are supported at the moment.
       
   280 		                	// This won't be happening ever since we have used a filter while getting the certificate list.
       
   281 							EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL() - Unsupported Certificate - Not X509\n") ) );
       
   282 							
       
   283 			                break;
       
   284 		                }
       
   285 		            }
       
   286 		        }
       
   287 		        else
       
   288 		        {
       
   289 					EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL() - Error from Certificate retrieve, iStatus.Int()=%d\n"), iStatus.Int() ) );
       
   290 		        }
       
   291 
       
   292 				if( cert == NULL )
       
   293 				{
       
   294 					// Some problem above. Skip the below and go for the next certificate.
       
   295 					continue;
       
   296 				}							
       
   297 			
       
   298                 HBufC* pri = NULL;
       
   299                 HBufC* sec = NULL;
       
   300 			
       
   301 				CleanupStack::PushL( cert );
       
   302 				
       
   303                 X509CertNameParser::PrimaryAndSecondaryNameL( *((CX509Certificate*)cert), pri, sec, CertInfo->Label());
       
   304 	
       
   305 				CleanupStack::PopAndDestroy(); // cert		
       
   306 
       
   307 				EAP_TRACE_DEBUG_SYMBIAN((_L("CEapTlsPeapCertFetcher::RunL() - Label=%S, Pri name=%S,Length=%d, Sec name=%S,Length=%d\n"),
       
   308 					 &(CertInfo->Label()), pri, pri->Length(), sec, sec->Length() ) );
       
   309 
       
   310 				EAP_TRACE_DATA_DEBUG_SYMBIAN( ( "CEapTlsPeapCertFetcher::RunL() - Sub Key Id:", (CertInfo->SubjectKeyId().Ptr()), 
       
   311 						(CertInfo->SubjectKeyId().Size()) ) );
       
   312 
       
   313 				SCertEntry certEntry;
       
   314 							
       
   315 				certEntry.iLabel.Copy(iCertInfos[i]->Label());
       
   316 				certEntry.iSubjectKeyId.Copy(iCertInfos[i]->SubjectKeyId());
       
   317 				
       
   318 				// Copy the new fields. Primary and secondary name.				
       
   319 				certEntry.iPrimaryName.Copy( pri->Des().Left(KMaxNameLength ) );
       
   320 				certEntry.iSecondaryName.Copy( sec->Des().Left(KMaxNameLength ) );
       
   321 				
       
   322 				delete pri;
       
   323 				delete sec;
       
   324 				
       
   325 				if (iCertInfos[i]->CertificateOwnerType() == ECACertificate)
       
   326 				{
       
   327 					iCACerts.Append(certEntry);	
       
   328 				}
       
   329 				else if (iCertInfos[i]->CertificateOwnerType() == EUserCertificate)
       
   330 				{
       
   331 					iUserCerts.Append(certEntry);
       
   332 				}				
       
   333 			}
       
   334 			delete iCertFilter;
       
   335 			iCertFilter = 0;
       
   336 
       
   337 			// Delete iCertInfos
       
   338 			for (TInt i = 0; i < iCertInfos.Count(); i++)
       
   339 			{
       
   340 				iCertInfos[i]->Release();
       
   341 			}
       
   342 			iCertInfos.Reset();
       
   343 			TRAP(err, iParent->CompleteReadCertificatesL(iUserCerts, iCACerts));
       
   344 			// Ignore error on purpose.			
       
   345 		}
       
   346 		break;
       
   347 	
       
   348 	default:
       
   349 		break;
       
   350 	}
       
   351 	return;
       
   352 }
       
   353 
       
   354 // End of file