os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_authenticationpolicy.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003-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 <unifiedkeystore.h>
    20 #include <e32base.h>
    21 #include "t_keystore_actions.h"
    22 #include "t_keystore_defs.h"
    23 #include "t_input.h"
    24 #include "t_output.h"
    25 
    26 const TInt KKeyStoreEmpty = -1199;
    27 _LIT(KEllipsis, "...");
    28 
    29 /////////////////////////////////////////////////////////////////////////////////
    30 // CAuthenticationPolicy
    31 /////////////////////////////////////////////////////////////////////////////////
    32 
    33 CTestAction* CAuthenticationPolicy::NewL(RFs& aFs, 
    34 							CConsoleBase& aConsole, 
    35 							Output& aOut,
    36 							const TTestActionSpec& aTestActionSpec)
    37 	{
    38 	CTestAction* self = CAuthenticationPolicy::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    39 	CleanupStack::Pop(self);
    40 	return self;
    41 	}
    42 
    43 CTestAction* CAuthenticationPolicy::NewLC(RFs& aFs,
    44 							CConsoleBase& aConsole, 
    45 							Output& aOut,
    46 							const TTestActionSpec& aTestActionSpec)
    47 	{
    48 	CAuthenticationPolicy* self = new (ELeave) CAuthenticationPolicy(aFs, aConsole, aOut);
    49 	CleanupStack::PushL(self);
    50 	self->ConstructL(aTestActionSpec);
    51 	return self;
    52 	}
    53 
    54 CAuthenticationPolicy::~CAuthenticationPolicy()
    55 	{
    56 	}
    57 
    58 CAuthenticationPolicy::CAuthenticationPolicy(RFs& aFs, CConsoleBase& aConsole, Output& aOut) :
    59 	CKeyStoreTestAction(aFs, aConsole, aOut), iState(EInit)
    60 	{
    61 	}
    62 
    63 void CAuthenticationPolicy::ConstructL(const TTestActionSpec& aTestActionSpec)
    64 	{
    65 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
    66           
    67 	iMode.Copy(Input::ParseElement(aTestActionSpec.iActionBody, KAuthModeStart, KAuthModeEnd));
    68 	
    69 	if(iMode.Compare(_L8("get")) == 0)
    70 		{
    71 		iExpectedExpression.Copy(Input::ParseElement(aTestActionSpec.iActionBody, KExpectedExpressionStart, KExpectedExpressionEnd));
    72 		iExpectedFreshness = Input::ParseIntElement(aTestActionSpec.iActionBody, KExpectedFreshnessStart, KExpectedFreshnessEnd);
    73 		}
    74 	
    75 	}
    76 
    77 void CAuthenticationPolicy::PerformAction(TRequestStatus& aStatus)
    78 	{
    79 	switch (iState)
    80 		{
    81 		case EInit:
    82 			{
    83 			CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    84 		
    85 			iFilter.iUsage = EPKCS15UsageAll;
    86 			keyStore->List(iKeys, iFilter, aStatus);
    87 							
    88 			iState = EListing;
    89 					
    90 			}
    91 			break;
    92 
    93 		case EListing:
    94 				{
    95 				switch(aStatus.Int())
    96 					{				
    97 					case KErrNone:
    98 						{
    99 						TCTTokenObjectHandle keyHandle;	
   100 						if (iKeys.Count() == 0)
   101 							{
   102 							iConsole.Printf(_L("keyStore empty!!"));
   103 							iState = EFinished;
   104 							TRequestStatus *status = &aStatus;
   105 							User::RequestComplete(status, KKeyStoreEmpty);			
   106 							break;
   107 							}
   108 					
   109 						// Select the key with the label we want!
   110 						for (TInt j = 0; j < iKeys.Count(); j++)
   111 							{
   112 							if (iKeys[j]->Label() == *iLabel) 
   113 								{
   114 								keyHandle = iKeys[j]->Handle();
   115 								break;
   116 								}
   117 							}
   118 						if(keyHandle.iObjectId == 0)
   119 							{
   120 							iConsole.Printf(_L("No keys with matching label!!"));
   121 							iState = EFinished;
   122 							TRequestStatus *status = &aStatus;
   123 							User::RequestComplete(status, KErrNotFound);			
   124 							break;
   125 							}
   126 						
   127 						CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   128 						if(iMode.Compare(_L8("set")) == 0)
   129 							{
   130 							keyStore->SetAuthenticationPolicy(keyHandle, *iAuthExpression, iFreshness, aStatus);
   131 							iState = EFinished;
   132 							}
   133 						else if(iMode.Compare(_L8("get")) == 0)
   134 							{
   135 							if(iAuthExpression)
   136 								{
   137 								delete iAuthExpression;
   138 								iAuthExpression = NULL;
   139 								}
   140 							iAuthExpression = HBufC::NewL(1024);
   141 							keyStore->GetAuthenticationPolicy(keyHandle, iAuthExpression, iFreshness, aStatus);
   142 							iState = EGetAuthenticationPolicy;
   143 							}
   144 						else
   145 							{
   146 							iConsole.Printf(_L("Invalid Mode"));
   147 							iState = EFinished;
   148 							TRequestStatus *status = &aStatus;
   149 							User::RequestComplete(status, aStatus.Int());
   150 							}
   151 						}
   152 						break;
   153 					default:
   154 						{
   155 						iConsole.Printf(_L("keyStore->ExportKey returned: %d\n"), aStatus.Int());
   156 						iState = EFinished;
   157 						TRequestStatus *status = &aStatus;
   158 						User::RequestComplete(status, aStatus.Int());
   159 						}
   160 						break;
   161 					} // switch
   162 				break;
   163 				}
   164 		case EGetAuthenticationPolicy:
   165 			switch(aStatus.Int())
   166 				{
   167 				case KErrNone:
   168 					{
   169 					TInt err = KErrNone;
   170 					HBufC* expectedExpr = HBufC::NewL(iExpectedExpression.Length());
   171 					expectedExpr->Des().Copy(iExpectedExpression);
   172 					if(iAuthExpression->Des().Compare(*expectedExpr) != 0 ||
   173 							iFreshness != iExpectedFreshness)
   174 						{
   175 						err = KErrArgument;
   176 						}
   177 					iConsole.Printf(_L("Authentication policy returned: %S, %d\n"), iAuthExpression, iFreshness);
   178 					delete expectedExpr;
   179 					iState = EFinished;
   180 					delete iAuthExpression;
   181 					iAuthExpression = NULL;
   182 					TRequestStatus *status = &aStatus;
   183 					User::RequestComplete(status, err);
   184 					break;
   185 					}
   186 				default:
   187 					{
   188 					iConsole.Printf(_L("Setting Authentication policy returned: %d\n"), aStatus.Int());
   189 					iState = EFinished;
   190 					TRequestStatus *status = &aStatus;
   191 					User::RequestComplete(status, aStatus.Int());
   192 					}
   193 					break;
   194 				}
   195 			break;
   196 		case EFinished:
   197 			{
   198 			TRequestStatus* status = &aStatus;
   199 			User::RequestComplete(status, aStatus.Int());
   200 			if (aStatus == iExpectedResult)
   201 				{
   202 				iResult = ETrue;
   203 				}
   204 			else
   205 				{
   206 				iResult = EFalse;
   207 				}
   208 		
   209 			iActionState = EPostrequisite;
   210 			}
   211 			break;
   212 			
   213 		default:
   214 			break;	//	Nothing to do, for the compiler
   215 		}
   216 	}
   217 
   218 void CAuthenticationPolicy::PerformCancel()
   219     {
   220     if (iState == EFinished)
   221         {
   222         CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   223         }
   224     }
   225 
   226 void CAuthenticationPolicy::Reset()
   227 	{
   228 	iState = EInit;
   229 	}
   230 
   231 void CAuthenticationPolicy::DoReportAction()
   232 {
   233 	if(iMode.Compare(_L8("set")) == 0)
   234 		{
   235 		_LIT(KSet, "Setting authentication policy...");
   236 		iOut.writeString(KSet);
   237 		}
   238     else if(iMode.Compare(_L8("get")) == 0)
   239 		{
   240 		_LIT(KGet, "Getting authentication policy...");
   241 		iOut.writeString(KGet);
   242 		}
   243 	
   244 	iOut.writeString(iLabel->Left(32));
   245 	if (iLabel->Length() > 32)
   246 		{
   247 		iOut.writeString(KEllipsis);
   248 		}
   249 	
   250 	iOut.writeNewLine();
   251 }
   252 
   253 
   254 void CAuthenticationPolicy::DoCheckResult(TInt aError)
   255 {
   256 	if (iFinished)
   257 	{
   258 		if (aError == KErrNone)
   259 		{
   260 			_LIT(KSuccessful, "Operation Successfull\n");
   261 			iConsole.Write(KSuccessful);
   262 			iOut.writeString(KSuccessful);
   263 			iOut.writeNewLine();
   264 			iOut.writeNewLine();
   265 		}
   266 		else
   267 		{
   268 			if (aError!=iExpectedResult)
   269 			{
   270 				_LIT(KFailed, "!!!Operation Failed!!!\n");
   271 				iConsole.Write(KFailed);
   272 				iOut.writeString(KFailed);
   273 			}
   274 			else
   275 			{
   276 				_LIT(KFailed, "Operation failed, but expected\n");
   277 				iConsole.Write(KFailed);
   278 				iOut.writeString(KFailed);
   279 			}
   280 
   281 			iOut.writeNewLine();
   282 			iOut.writeNewLine();
   283 		}
   284 	}
   285 }