os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_setpolicy.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) 2007-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 <mctkeystore.h>
    20 
    21 #include "keytool_utils.h"
    22 #include "keytool_commands.h"
    23 #include "keytool_controller.h"
    24 
    25 #include <keytool.rsg>
    26 
    27 
    28 /*static*/ CKeytoolSetPolicy* CKeytoolSetPolicy::NewLC(CKeyToolController* aController)
    29 	{
    30 	CKeytoolSetPolicy* self = new (ELeave) CKeytoolSetPolicy(aController);
    31 	CleanupStack::PushL(self);
    32 	self->ConstructL();
    33 	return self;
    34 	}
    35 	
    36 /*static*/ CKeytoolSetPolicy* CKeytoolSetPolicy::NewL(CKeyToolController* aController)
    37 	{
    38 	CKeytoolSetPolicy* self = CKeytoolSetPolicy::NewLC(aController);
    39 	CleanupStack::Pop(self);
    40 	return self;
    41 	}
    42 
    43 CKeytoolSetPolicy::CKeytoolSetPolicy(CKeyToolController* aController) : CKeytoolList(aController)
    44 	{
    45 	}
    46 
    47 CKeytoolSetPolicy::~CKeytoolSetPolicy()
    48 	{
    49 	Cancel();
    50 	}
    51 
    52 void CKeytoolSetPolicy::RunL()
    53 	{
    54 	if (iStatus.Int() != KErrNone)
    55 		{
    56 		// A problem occured. Handle gracefully.
    57 		User::Leave(iStatus.Int());
    58 		}	
    59 		
    60 	switch (iState)
    61 		{	 
    62 		case EListKeys:
    63 			{
    64 			iKey = KeyToolUtils::findKey(iKeys, *iParams->iDefault);
    65 			if (iKey)
    66 				{
    67 				TSecureId secureId; // Application secure ID 
    68 				TSecurityPolicy securePolicy;
    69 				idx = 0;
    70 				if (iParams->iPolicy == CKeyToolParameters::ESetManagerPolicy)
    71 					{
    72 					securePolicy = ECapabilityWriteDeviceData;
    73 					iKeyStore->SetManagementPolicy(iKey->Handle(), securePolicy, iStatus);
    74 					}
    75 				else
    76 					{
    77 					if (iParams->iPolicy == CKeyToolParameters::ESetUserPolicy)
    78 						{
    79 						secureId = iParams->iUIDs[idx];
    80 						securePolicy = secureId;
    81 						}
    82 					else if (iParams->iPolicy == CKeyToolParameters::ESetAllUsersPolicy)
    83 						{
    84 						securePolicy = TSecurityPolicy::EAlwaysPass;
    85 						}
    86 					else if (iParams->iPolicy == CKeyToolParameters::ERemoveUserPolicy)
    87 						{
    88 						iParams->iUIDs.Close();
    89 						iParams->iUIDs.Append(TUid::Uid(KUidSecurityKeytool));
    90 						secureId = iParams->iUIDs[idx];
    91 						securePolicy = secureId;
    92 						}
    93 					else
    94 						{
    95 						User::Leave(KErrNotFound); //should not come here
    96 						}
    97 					iKeyStore->SetUsePolicy(iKey->Handle(), securePolicy, iStatus);
    98 					}
    99 				iState = EFinished;
   100 				SetActive();
   101 				}
   102 			else
   103 				{
   104 				User::Leave(KErrNotFound);
   105 				}
   106 			}
   107 			break;
   108 		case EFinished:
   109 			{
   110 			if (iParams->iPolicy == CKeyToolParameters::ESetManagerPolicy)
   111 				{
   112 				iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SMANAGERSUCCESS);
   113 				}
   114 			else if (iParams->iPolicy == CKeyToolParameters::ESetUserPolicy)
   115 				{
   116 				iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SUSERSUCCESS);
   117 				}
   118 			else if (iParams->iPolicy == CKeyToolParameters::ESetAllUsersPolicy)
   119 				{
   120 				iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SALLUSERSSUCCESS);
   121 				}
   122 			else 
   123 				{
   124 				iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_RUSERSUCCESS);
   125 				}
   126 			CActiveScheduler::Stop();			
   127 			}
   128 			break;
   129 		default:
   130 			{
   131 			User::Panic(_L("SETUSERS action: Illegal state."), 1);			
   132 			}
   133 		}		
   134 	}
   135 
   136 TInt CKeytoolSetPolicy::RunError(TInt aError)
   137 	{
   138 	CActiveScheduler::Stop();
   139 
   140 	switch (aError)
   141 		{
   142 		case KErrNotFound :
   143 			{
   144 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_NOTFOUND));
   145 			}
   146 			break;
   147 		case KErrAccessDenied :
   148 			{			
   149 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_ACCESS));
   150 			}
   151 			break;
   152 		default:
   153 			{
   154 			if (iParams->iPolicy == CKeyToolParameters::ESetManagerPolicy)
   155 				{
   156 				TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SMANAGERFAIL, aError));
   157 				}
   158 			else if (iParams->iPolicy == CKeyToolParameters::ESetUserPolicy)
   159 				{
   160 				TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SUSERFAIL, aError));
   161 				}
   162 			else if (iParams->iPolicy == CKeyToolParameters::ESetAllUsersPolicy)
   163 				{
   164 				TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SALLUSERSFAIL, aError));
   165 				}
   166 			else 
   167 				{
   168 				TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_RUSERFAIL, aError));
   169 				}
   170 			}
   171 		}	
   172 
   173 	return KErrNone;
   174 	}
   175