ssl/libcrypto/src/crypto/certretriever/certretriever.cpp
changeset 0 e4d67989cc36
child 52 bf6a71c50e42
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     3 
       
     4 Redistribution and use in source and binary forms, with or without 
       
     5 modification, are permitted provided that the following conditions are met:
       
     6 
       
     7 * Redistributions of source code must retain the above copyright notice, this 
       
     8   list of conditions and the following disclaimer.
       
     9 * Redistributions in binary form must reproduce the above copyright notice, 
       
    10   this list of conditions and the following disclaimer in the documentation 
       
    11   and/or other materials provided with the distribution.
       
    12 * Neither the name of Nokia Corporation nor the names of its contributors 
       
    13   may be used to endorse or promote products derived from this software 
       
    14   without specific prior written permission.
       
    15 
       
    16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
       
    17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
       
    18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
       
    19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
       
    20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
       
    21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
       
    22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
       
    23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
       
    24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
       
    25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    26 
       
    27 Description:  Contains implementation for x509_add_symbian_cert - to use certificates installed in Symbian with OpenSSL code.
       
    28 */
       
    29 
       
    30  
       
    31 #include "certretriever.h"
       
    32 #include "createx509.h"
       
    33 
       
    34 #include <securitydefsconst.h>
       
    35 
       
    36 #ifdef __cplusplus
       
    37 extern "C" 
       
    38 {
       
    39 #endif
       
    40 int X509_add_symbian_certs(X509_STORE * store)
       
    41 {
       
    42 	CActiveScheduler* activeScheduler;
       
    43 	CActiveScheduler* CurrentActiveScheduler = CActiveScheduler::Current();
       
    44 	if(CurrentActiveScheduler == NULL)
       
    45 	{
       
    46 		activeScheduler = new (ELeave) CActiveScheduler;
       
    47 		CleanupStack::PushL(activeScheduler);
       
    48 		
       
    49 		CActiveScheduler::Install(activeScheduler);
       
    50 	}
       
    51 	
       
    52 		
       
    53 	TRequestStatus status;
       
    54 	CCertRetriever* certRetriever;
       
    55 	TRAPD(error, certRetriever = CCertRetriever::NewL(store, status, CActiveScheduler::Current()));
       
    56 	
       
    57 	// create CActiveSchedulerWait
       
    58 	if (CurrentActiveScheduler)
       
    59 	{
       
    60 		certRetriever->activeSchedulerwait = new (ELeave) CActiveSchedulerWait; 
       
    61 		certRetriever->OwnScheduler = EFalse;	
       
    62 	}
       
    63 	
       
    64 	
       
    65 	if(error != KErrNone)
       
    66 	{
       
    67 		CleanupStack::PopAndDestroy();	// activeScheduler
       
    68 		return 0;
       
    69 	}
       
    70 	
       
    71 	CleanupStack::PushL(certRetriever);	
       
    72 	
       
    73 	TRAP(error,certRetriever->RetriveCertificateL());
       
    74 	if(error != KErrNone)
       
    75 	{
       
    76 		if(CurrentActiveScheduler == NULL)
       
    77 		{
       
    78 			CleanupStack::PopAndDestroy(2);	// activeScheduler, certRetriever
       
    79 		}
       
    80 		else
       
    81 			CleanupStack::PopAndDestroy();	// certRetriever
       
    82 		return 0;
       
    83 	}
       
    84 		
       
    85 				
       
    86 	if(CurrentActiveScheduler == NULL)
       
    87 	{
       
    88 		activeScheduler->Start();
       
    89 		 CleanupStack::PopAndDestroy(2); // If you destroy the object it will not be there
       
    90 										   // in the iActiveQ, and hence stray signal.
       
    91 		
       
    92 		//CleanupStack::Pop(2);	   // So just pop it. // activeScheduler, certRetriever						   
       
    93 	}
       
    94 	else
       
    95 	{
       
    96 	//	CurrentActiveScheduler->Start();// If you are using CActiveScheduler::Current();
       
    97 										  // Why u want to start it again?	
       
    98         
       
    99         // CleanupStack::PopAndDestroy(); // If you destroy the object it will not be there
       
   100 										  // in the iActiveQ, and hence stray signal.	
       
   101 										  									   
       
   102 		// should wait here untill it finish loading certificates, ths API is synchronous
       
   103 		certRetriever->activeSchedulerwait->Start();
       
   104 		   
       
   105 		CleanupStack::Pop(); 		  // So just pop it. // certRetriever
       
   106 	}
       
   107 	
       
   108 	
       
   109 		
       
   110 	if(status == KErrNone)
       
   111 		return 1;
       
   112 	else
       
   113 		return 0;
       
   114 }
       
   115 #ifdef __cplusplus
       
   116 }
       
   117 #endif
       
   118 
       
   119 CCertRetriever::CCertRetriever(X509_STORE* aStore, 
       
   120 				TRequestStatus& aStatus,
       
   121 				const CActiveScheduler* aActiveScheduler)
       
   122 			: CActive(CActive::EPriorityHigh), 
       
   123 			  iStore(aStore),
       
   124 			  iFinStatus ( aStatus ),
       
   125 			  iActiveScheduler(aActiveScheduler),
       
   126 			  iCertPtr(0,0)			  
       
   127 			  
       
   128 	{
       
   129 	
       
   130 	OwnScheduler = ETrue;
       
   131 	if(iActiveScheduler)
       
   132 		iActiveScheduler->Add(this);
       
   133 	}
       
   134 
       
   135 CCertRetriever::~CCertRetriever()
       
   136 	{
       
   137 		Cancel();
       
   138  		delete iBuf;
       
   139  		delete iCertFilter;
       
   140  		
       
   141  	 	iCerts.Close();	
       
   142  		delete iCertStore;
       
   143  		iFs.Close();
       
   144  		if(!OwnScheduler)
       
   145  		  delete activeSchedulerwait;
       
   146  	}
       
   147 	
       
   148 CCertRetriever* CCertRetriever::NewLC(X509_STORE* aStore,
       
   149 						TRequestStatus& aStatus, 
       
   150 						const CActiveScheduler* aActiveScheduler)
       
   151 	{
       
   152 	CCertRetriever* self = new (ELeave) CCertRetriever(aStore, aStatus, aActiveScheduler);
       
   153 	CleanupStack::PushL(self);
       
   154 	TRAPD(err,self->ConstructL());
       
   155 	if(err != KErrNotFound)
       
   156 		return self;
       
   157 	
       
   158 	CleanupStack::PopAndDestroy(self);	
       
   159 	return NULL;
       
   160 	}
       
   161 
       
   162 CCertRetriever* CCertRetriever::NewL(X509_STORE* aStore,
       
   163 						TRequestStatus& aStatus, 
       
   164 						const CActiveScheduler* aActiveScheduler)
       
   165 	{
       
   166 	CCertRetriever* self = CCertRetriever::NewLC(aStore, aStatus, aActiveScheduler);
       
   167 	if(self)
       
   168 		CleanupStack::Pop();
       
   169 	return self;
       
   170 	}
       
   171 
       
   172 void CCertRetriever::ConstructL()
       
   173 	{
       
   174 		TInt err = iFs.Connect();
       
   175 		if(err != KErrNone)
       
   176 			User::Leave(err);
       
   177 		
       
   178  		iState = EInitializeCertStore;
       
   179  		iBuf = HBufC8::NewL(KMaxCertLength);
       
   180 	}
       
   181 
       
   182 
       
   183 void CCertRetriever::RunL()
       
   184 	{
       
   185 	// 1. All certificates retrieved.
       
   186 	// 2. yes. check iActiveScheduler. if null then call User::RequestComplete(iStatus) else iActiveScheduler->Stop();
       
   187 	User::LeaveIfError(iStatus.Int());
       
   188 		
       
   189 		switch(iState)
       
   190 		{
       
   191 		case EInitializeCertStore:
       
   192 					OpenUnifiedCertStoreL();		
       
   193 					break;
       
   194 		
       
   195 		case EListCerts:
       
   196 					ListCertsL();	
       
   197 					break;
       
   198 		case EAppendCerts:
       
   199 					if (!iCerts.Count()) // no certificate in store.
       
   200 					{
       
   201 						iState = ENoCerts;												
       
   202 					}
       
   203 					else
       
   204 					{
       
   205 						AppendCerts();
       
   206 						break;	
       
   207 					}			
       
   208 		       		
       
   209 		case EDone:
       
   210 					if (iState != ENoCerts)
       
   211 					{
       
   212 						ProcessCerts();	//Process the last certificate
       
   213 						iCertCount = 0;	
       
   214 					}					
       
   215 		case ENoCerts:
       
   216 								
       
   217 					if(iActiveScheduler)
       
   218 					{
       
   219 						if(OwnScheduler)
       
   220 							iActiveScheduler->Stop();
       
   221 						else
       
   222 							activeSchedulerwait->AsyncStop();
       
   223 						
       
   224 						iFinStatus = iStatus;
       
   225 					}
       
   226 					else
       
   227 					{
       
   228 						TRequestStatus *s = &iFinStatus;
       
   229 						User::RequestComplete(s, KErrNone);	
       
   230 					}
       
   231 					
       
   232 					
       
   233 					break;
       
   234 		
       
   235 		default:
       
   236 				User::Leave(KErrNotFound);
       
   237 				break;
       
   238 		}
       
   239 	}
       
   240 
       
   241 
       
   242 void CCertRetriever::DoCancel()
       
   243 	{
       
   244 	}
       
   245 
       
   246 TInt CCertRetriever::RunError(TInt aError)
       
   247 	{
       
   248 		//Can do some error handling here
       
   249 		if(iActiveScheduler)
       
   250 		{
       
   251 			iActiveScheduler->Stop();
       
   252 			iFinStatus = iStatus;
       
   253 		}
       
   254 		else
       
   255 		{
       
   256 			TRequestStatus *s = &iFinStatus;
       
   257 			User::RequestComplete(s, aError);	
       
   258 		}
       
   259 		
       
   260 		return KErrNone; 
       
   261 	}
       
   262 
       
   263 void CCertRetriever::RetriveCertificateL()
       
   264 	{
       
   265 		OpenUnifiedCertStoreL();
       
   266 	}
       
   267 	
       
   268 	
       
   269 void CCertRetriever::OpenUnifiedCertStoreL()
       
   270     {
       
   271     
       
   272     	iState = EListCerts;
       
   273     	delete iCertStore;
       
   274     	iCertStore = NULL;
       
   275     	iCertStore = CUnifiedCertStore::NewL(iFs, EFalse);
       
   276     	iCertStore->Initialize(iStatus);
       
   277     	SetActive();
       
   278     }
       
   279     
       
   280 void CCertRetriever::ListCertsL()
       
   281 {
       
   282     // Create filter object
       
   283    
       
   284    delete iCertFilter;
       
   285    iCertFilter = NULL;
       
   286    iCertFilter = CCertAttributeFilter::NewL();    
       
   287    iCertFilter->SetFormat(EX509Certificate);
       
   288    iCertFilter->SetOwnerType(ECACertificate);
       
   289    iCertFilter->SetUid(KTlsApplicabilityUid);
       
   290 
       
   291     iStatus = KRequestPending;
       
   292     SetActive();
       
   293     iCertStore->List(iCerts, *iCertFilter, iStatus);
       
   294     iState = EAppendCerts;
       
   295 }
       
   296 
       
   297 void CCertRetriever::AppendCerts()
       
   298 {
       
   299 	if(iCertCount>0)	
       
   300 		ProcessCerts();
       
   301 	
       
   302 	CCTCertInfo *cert = iCerts[iCertCount];
       
   303 		
       
   304 	SetActive();			
       
   305 	iStatus == KRequestPending;
       
   306 		
       
   307 	iCertPtr.Set( iBuf->Des() );
       
   308     iCertStore->Retrieve((*cert),iCertPtr,iStatus);
       
   309 	
       
   310 	iCertCount++;
       
   311 	
       
   312 		if(iCertCount == iCerts.Count())
       
   313 			iState = EDone;	
       
   314 }
       
   315 
       
   316 
       
   317 void CCertRetriever::ProcessCerts()
       
   318 {
       
   319 	CX509Certificate *X509Cert;
       
   320 	TRAPD(error, X509Cert = CX509Certificate::NewL( iCertPtr ));
       
   321 	if(error !=KErrNone)
       
   322 		return;
       
   323 		
       
   324 	CleanupStack::PushL(X509Cert);
       
   325 
       
   326 	X509* x509 = CX509_Initializer::CreateX509(X509Cert);
       
   327 
       
   328 	if(x509)
       
   329 	{
       
   330 		X509_STORE_add_cert(iStore,x509);
       
   331 		X509_free(x509);
       
   332 	}
       
   333 	
       
   334 	CleanupStack::PopAndDestroy(); //X509Cert
       
   335 	
       
   336 }