os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool_addapps.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2004-2009 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: 
    15 *
    16 */
    17 
    18 
    19 #include "certtool_commands.h"
    20 #include "certtool_controller.h"
    21 #include "keytool_utils.h"
    22 
    23 /*static*/ CCertToolAddApps* CCertToolAddApps::NewLC(CCertToolController* aController)
    24 	{
    25 	CCertToolAddApps* self = new (ELeave) CCertToolAddApps(aController);
    26 	CleanupStack::PushL(self);
    27 	self->ConstructL();
    28 	return self;
    29 	}
    30 	
    31 /*static*/ CCertToolAddApps* CCertToolAddApps::NewL(CCertToolController* aController)
    32 	{
    33 	CCertToolAddApps* self = CCertToolAddApps::NewLC(aController);
    34 	CleanupStack::Pop(self);
    35 	return self;
    36 	}
    37 
    38 CCertToolAddApps::CCertToolAddApps(CCertToolController* aController) : CCertToolList(aController), iCertIndex (-1)
    39 	{
    40 	}
    41 
    42 	
    43 CCertToolAddApps::~CCertToolAddApps()
    44 	{
    45 	Cancel();
    46 	}
    47 
    48 void CCertToolAddApps::RunL()
    49 	{
    50 	if (iStatus.Int() != KErrNone)
    51 		{
    52 		// A problem occured. Handle gracefully.
    53 		User::Leave(iStatus.Int());
    54 		}	
    55 		
    56 	switch (iState)
    57 		{	 
    58 		case EListCerts:
    59 			{
    60 			if (iCertInfos.Count()==0)
    61 				{
    62 				iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_EMPTY);
    63 				CActiveScheduler::Stop();				
    64 				}
    65 			else
    66 				{ 
    67 				if (iParams->iDefault)    
    68 					{
    69 					// Look for a specific certificate					
    70 					for (TInt j = 0; j < iCertInfos.Count(); j++)
    71 						{
    72 						if (iCertInfos[j]->Label() == *iParams->iDefault) 
    73 							{
    74 							iCertIndex = j;
    75 							break;
    76 							}
    77 						}	
    78 						
    79 					if (iCertIndex != -1)
    80 						{
    81 						// Certificate found set app uids!
    82 						if (iParams->iCertstoreIndex == -1)
    83 							{
    84 							iParams->iCertstoreIndex = 0;
    85 							}
    86 						if (iParams->iCertstoreIndex >= iCertStore->CertStoreCount())
    87 							{
    88 							iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST);	
    89 							User::Leave(KErrArgument);			
    90 							}						
    91 						iCertStore->Applications(*iCertInfos[iCertIndex], iApps, iStatus);
    92 						iState = ESetApps;
    93 						SetActive();
    94 						}
    95 					else 
    96 						{
    97 						User::Leave(KErrNotFound);
    98 						}			
    99 					}
   100 				}
   101 			}
   102 			break;
   103 		case ESetApps:
   104 			{				
   105 			if (iParams->iCertstoreIndex == -1)
   106 				{
   107 				iParams->iCertstoreIndex = 0;
   108 				}
   109 			if (iParams->iCertstoreIndex >= iCertStore->CertStoreCount())
   110 				{
   111 				iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST);	
   112 				User::Leave(KErrArgument);			
   113 				}				
   114 			// Check for duplicates
   115 			for (TInt k = 0; k < iParams->iUIDs.Count(); k++)
   116 				{
   117 				if (iApps.Find(iParams->iUIDs[k]) != KErrNotFound)
   118 					{
   119 					TBuf<128> msg;
   120 					const TDesC& tmp = KeyToolUtils::Uid2Des(iParams->iUIDs[k]);
   121 					msg.Format(_L("The certificate is already trusted for application %S."), &tmp);
   122 					iController->DisplayErrorL(msg, iParams->iPageWise);
   123 					User::Leave(KErrArgument);
   124 					}
   125 				}
   126 
   127 			// add the apps to the array
   128 			for (TInt j = 0; j < iApps.Count(); j++)
   129 				{
   130 				iParams->iUIDs.Append(iApps[j]);
   131 				}
   132 			if(iCertInfos[iCertIndex]->CertificateOwnerType() == ECACertificate)							
   133 				{
   134 				iState = ESetTrust;
   135 				}
   136 			else
   137 				{
   138 				iState = EFinished;								
   139 				}	
   140 			iCertStore->SetApplicability(*iCertInfos[iCertIndex], iParams->iUIDs, iStatus);
   141 			SetActive();
   142 			}
   143 			break;
   144 		case ESetTrust:
   145 			{
   146 			iState = EFinished;
   147 			const TBool caCert = ETrue;
   148 			iCertStore->SetTrust(*iCertInfos[iCertIndex],caCert,iStatus);
   149 			SetActive();				
   150 			}
   151 			break;
   152 		case EFinished:
   153 			{
   154 			iCertInfos.Close(); 
   155 			iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_ADDAPPS_SUCCESS);				
   156 			CActiveScheduler::Stop();			
   157 			}
   158 			break;
   159 		default:
   160 			{
   161 			User::Panic(_L("ADDAPPS action: Illegal state."), 1);
   162 			}
   163 		}
   164 	}
   165 
   166 TInt CCertToolAddApps::RunError(TInt aError)
   167 	{
   168 	CActiveScheduler::Stop();
   169 
   170 	switch (aError)
   171 		{
   172 		case KErrNotFound :
   173 			{
   174 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTFOUND));
   175 			TRAP_IGNORE(iController->DisplayErrorL(_L("\n"), iParams->iPageWise));			
   176 			}
   177 			break;
   178 		default:
   179 			{
   180 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_ADDAPPS_FAIL));			
   181 			TRAP_IGNORE(iController->DisplayErrorL(_L("\n"), iParams->iPageWise));			
   182 			}
   183 		}	
   184 
   185 	return KErrNone;	
   186 	}