cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool.cpp
changeset 0 2c201484c85f
child 6 50f2ff6984be
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32cons.h>
       
    17 #include <bacline.h>
       
    18 
       
    19 #include "keytool_utils.h"
       
    20 #include "keytool_defs.h"
       
    21 #include "keytool_controller.h"
       
    22 #include "keytool_view_imp.h"
       
    23 #include "keytool_commands.h"
       
    24 #include "keytoolfileview.h"
       
    25 
       
    26 
       
    27 // Boiler plate 
       
    28 _LIT(KShortName, "Symbian OS KeyTool");
       
    29 _LIT(KName, "Symbian OS KeyStore Manipulation Tool");
       
    30 _LIT(KCopyright, "Copyright (c) 2004 - 2007 Symbian Software Ltd.  All rights reserved.");
       
    31 
       
    32 _LIT(KNewLine, "\n");
       
    33 
       
    34 _LIT(KDone, "Press any key to continue... \n");
       
    35 
       
    36 // Keytool command line parameters
       
    37 _LIT(KList, "-list");
       
    38 _LIT(KListShort, "-l");
       
    39 
       
    40 _LIT(KListStores, "-liststores");
       
    41 _LIT(KListStoresShort, "-ls");
       
    42 
       
    43 _LIT(KImport, "-import");
       
    44 _LIT(KImportShort, "-i");
       
    45 
       
    46 
       
    47 _LIT(KRemove, "-remove");
       
    48 _LIT(KRemoveShort, "-r");
       
    49 
       
    50 _LIT(KHelp, "-help");
       
    51 _LIT(KHelpShort, "-h");
       
    52 
       
    53 _LIT(KSetUser, "-setuser");
       
    54 _LIT(KSetUserShort, "-s");
       
    55 
       
    56 _LIT(KSetAllUsers, "-setallusers");
       
    57 _LIT(KSetAllUsersShort, "-a");
       
    58 
       
    59 _LIT(KSetManager, "-setmanager");
       
    60 _LIT(KSetManagerShort, "-m");
       
    61 
       
    62 _LIT(KRemoveUser, "-removeuser");
       
    63 _LIT(KRemoveUserShort, "-ru");
       
    64 
       
    65 // Command parameters
       
    66 _LIT(KLabel, "-label");
       
    67 _LIT(KKeyUsage, "-usage");
       
    68 _LIT(KAccess, "-access");
       
    69 
       
    70 _LIT(KStore, "-store");
       
    71 
       
    72 //_LIT(KExpiry, "-expiry"); // Functionality not present at the moment...
       
    73 _LIT(KDetails, "-details");
       
    74 _LIT(KDetailsShort, "-d");
       
    75 
       
    76 _LIT(KPageWise, "-page");
       
    77 _LIT(KPageWiseShort, "-p");
       
    78 
       
    79 
       
    80 const TInt KMaxArgs = 10;
       
    81 
       
    82 /**
       
    83  * Displays tool name and copyright informations.
       
    84  */
       
    85 LOCAL_D void BoilerPlateL(CConsoleBase* console) 
       
    86 	{
       
    87 	console->Printf(KNewLine);
       
    88 	console->Printf(KName);
       
    89 	console->Printf(KNewLine);	
       
    90 	console->Printf(KCopyright);
       
    91 	console->Printf(KNewLine);
       
    92 	console->Printf(KNewLine);	
       
    93 	}
       
    94 
       
    95 
       
    96 LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
       
    97 	{
       
    98 	if ((aCmdNum != -1) && (aCommand[0] == '-'))
       
    99 		{
       
   100 		aCmdNum = KeyToolDefController::KUsageCommand;
       
   101 		aCmdCount = KMaxArgs;
       
   102 		return 1;
       
   103 		}
       
   104 	if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
       
   105 		{
       
   106 		aCmdNum = KeyToolDefController::KListCommand;
       
   107 		}
       
   108 	else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
       
   109 		{
       
   110 		aCmdNum = KeyToolDefController::KListStoresCommand;
       
   111 		}
       
   112 	else if	(aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
       
   113 		{
       
   114 		aCmdNum = KeyToolDefController::KImportCommand;
       
   115 		}
       
   116 	else if	(aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
       
   117 		{
       
   118 		aCmdNum = KeyToolDefController::KRemoveCommand;
       
   119 		}
       
   120 	else if	(aCommand.CompareF(KSetUser) == 0 || aCommand.Compare(KSetUserShort) == 0)
       
   121 		{
       
   122 		aCmdNum = KeyToolDefController::KSetUserCommand;
       
   123 		}
       
   124 	else if	(aCommand.CompareF(KSetManager) == 0 || aCommand.Compare(KSetManagerShort) == 0)
       
   125 		{
       
   126 		aCmdNum = KeyToolDefController::KSetManagerCommand;
       
   127 		}
       
   128 	else if	(aCommand.CompareF(KRemoveUser) == 0 || aCommand.Compare(KRemoveUserShort) == 0)
       
   129 		{
       
   130 		aCmdNum = KeyToolDefController::KRemoveUserCommand;
       
   131 		}
       
   132 	else if	(aCommand.CompareF(KSetAllUsers) == 0 || aCommand.Compare(KSetAllUsersShort) == 0)
       
   133 		{
       
   134 		aCmdNum = KeyToolDefController::KSetAllUsersCommand;
       
   135 		}
       
   136 	else	
       
   137 		{
       
   138 		return 0;
       
   139 		}
       
   140 
       
   141 	return 1;
       
   142 	}
       
   143 	
       
   144 	
       
   145 /**
       
   146  * Parses the command line and given control to the handler to deal with the request.
       
   147  */
       
   148 LOCAL_D void DoMainL() 
       
   149 	{
       
   150 	TBool interactiveMode = ETrue;
       
   151 	
       
   152 	RFs fs;
       
   153 	User::LeaveIfError(fs.Connect());
       
   154 	CleanupClosePushL(fs);
       
   155 	
       
   156 	CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
       
   157 	CleanupStack::PushL(console);
       
   158 	CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
       
   159 	TInt cmdArgsCount = cmdArgs->Count();
       
   160 	
       
   161 	RFile file;
       
   162 	// command: keytool inputfile outputfile
       
   163 	if (cmdArgsCount == 3)
       
   164 		{
       
   165 		interactiveMode = EFalse;
       
   166 		TInt error = file.Open(fs, cmdArgs->Arg(1), EFileRead|EFileShareAny);
       
   167 		file.Close();
       
   168 		TInt error1 = file.Replace(fs, cmdArgs->Arg(2), EFileWrite|EFileShareExclusive);
       
   169 	    CleanupClosePushL(file);
       
   170 	    // If the input file doesn't exist or not able to create outputfile
       
   171 		// switch to Interactive mode
       
   172 		if (error != KErrNone || error1 != KErrNone)
       
   173 			{
       
   174 			CleanupStack::PopAndDestroy(&file);
       
   175 			interactiveMode = ETrue;
       
   176 			}	
       
   177 		}
       
   178 		
       
   179 	CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(*console);
       
   180 	CKeyToolController* controller = CKeyToolController::NewLC(*view);
       
   181 	CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
       
   182 	CleanupStack::PushL(args);
       
   183 	CKeytoolFileView* view1 = NULL;
       
   184 
       
   185 	TInt cmdCount = 0;
       
   186 	if (interactiveMode)
       
   187 		{
       
   188 		KeyToolUtils::SetConsole(console);
       
   189 		BoilerPlateL(console);
       
   190 		for (TInt i = 0; i < cmdArgsCount; i++)
       
   191 			{
       
   192 			args->AppendL(cmdArgs->Arg(i));
       
   193 			}
       
   194 		// In Interactive mode by default we can execute 1 command only.
       
   195 		cmdCount = 1;
       
   196 		}
       
   197 	else
       
   198 		{
       
   199 		CleanupStack::PopAndDestroy(3, view); // args, controller, view
       
   200 		KeyToolUtils::SetFile(&file);
       
   201 		view1 = CKeytoolFileView::NewLC(cmdArgs->Arg(1));
       
   202 		cmdCount = view1->SplitFileInputToArrayL();
       
   203 		}	
       
   204 		
       
   205 	for (TInt j = 0; j < cmdCount; j++)
       
   206 		{
       
   207 		if (!interactiveMode)
       
   208 			{
       
   209 			controller = CKeyToolController::NewLC(*view1);
       
   210 			args = view1->ReadArrayArgumentsLC(j);
       
   211 			}
       
   212 			
       
   213 		CKeyToolParameters* params = CKeyToolParameters::NewLC();
       
   214 		
       
   215 		TInt command = -1; 
       
   216 		TInt i = -1;
       
   217 
       
   218 		TInt argsCount = args->Count();
       
   219 		while (i < (argsCount-1))
       
   220 		 	{
       
   221 			i++;
       
   222 
       
   223 			if ((args->At(i).CompareF(KDetails)==0)|| (args->At(i).Compare(KDetailsShort)==0))
       
   224 				{
       
   225 				params->iIsDetailed = ETrue;
       
   226 				continue;
       
   227 				}
       
   228 			
       
   229 			if (args->At(i).CompareF(KPageWise)==0 || (args->At(i).Compare(KPageWiseShort)==0))
       
   230 				{
       
   231 				params->iPageWise = ETrue;
       
   232 				continue;
       
   233 				}			
       
   234 			
       
   235 			TDesC& commd = args->At(i);
       
   236 			if (VerifyCommand(commd, command, i))
       
   237 				{
       
   238 				if (commd.CompareF(KSetAllUsers) == 0 || commd.Compare(KSetAllUsersShort) == 0 ||
       
   239 				    commd.CompareF(KRemoveUser) == 0 || commd.Compare(KRemoveUserShort) == 0 ||
       
   240 				    commd.CompareF(KSetManager) == 0 || commd.Compare(KSetManagerShort) == 0 )
       
   241 					{
       
   242 					i++;
       
   243 					if (args->At(i)[0] == '-' ||  args->At(i).Left(2) == _L("0x"))
       
   244 						{
       
   245 						i = argsCount;
       
   246 						command = KeyToolDefController::KUsageCommand;
       
   247 						}
       
   248 					else
       
   249 						{
       
   250 						i--;
       
   251 						}
       
   252 					}
       
   253 				if (commd.CompareF(KSetAllUsers) == 0 || commd.Compare(KSetAllUsersShort) == 0)
       
   254 					{
       
   255 					params->iPolicy = CKeyToolParameters::ESetAllUsersPolicy;
       
   256 					params->iIsDetailed = ETrue;
       
   257 					}
       
   258 				if (commd.CompareF(KRemoveUser) == 0 || commd.Compare(KRemoveUserShort) == 0)
       
   259 					{
       
   260 					params->iPolicy = CKeyToolParameters::ERemoveUserPolicy;
       
   261 					params->iIsDetailed = ETrue;
       
   262 					}
       
   263 				if (commd.CompareF(KSetManager) == 0 || commd.Compare(KSetManagerShort) == 0)
       
   264 					{
       
   265 					params->iPolicy = CKeyToolParameters::ESetManagerPolicy;
       
   266 					params->iIsDetailed = ETrue;
       
   267 					}
       
   268 				if (commd.CompareF(KSetUser) == 0 || commd.Compare(KSetUserShort) == 0)
       
   269 					{
       
   270 					params->iPolicy = CKeyToolParameters::ESetUserPolicy;
       
   271 					i++;
       
   272 					if (i >= argsCount || args->At(i)[0] == '-' ||  args->At(i).Left(2) != _L("0x"))
       
   273 						{
       
   274 						i = argsCount;
       
   275 						command = KeyToolDefController::KUsageCommand;
       
   276 						}
       
   277 					else
       
   278 						{
       
   279 						TUid app;
       
   280 						TLex lex(args->At(i).Mid(2));		
       
   281 						TUint uid =0;
       
   282 						TInt err = lex.Val(uid, EHex);
       
   283 						if (err == KErrNone)
       
   284 							{
       
   285 							app = TUid::Uid(uid);						
       
   286 							}			 
       
   287 						params->iUIDs.Append(app); 
       
   288 						params->iIsDetailed = ETrue;
       
   289 						}
       
   290 					}
       
   291 				continue;
       
   292 				}
       
   293 
       
   294 			TDesC& cmd = args->At(i);
       
   295 				if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 ||
       
   296 				    cmd.CompareF(KLabel) == 0 || cmd.CompareF(KKeyUsage) == 0 ||
       
   297 				    cmd.CompareF(KStore) == 0 || cmd.CompareF(KAccess) == 0)
       
   298 				{
       
   299 				i++;
       
   300 				if (i >= argsCount || args->At(i)[0] == '-')
       
   301 					{
       
   302 					i = argsCount;
       
   303 					command = KeyToolDefController::KUsageCommand;
       
   304 					}
       
   305 				else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
       
   306 					{
       
   307 					params->iDefault = args->At(i).AllocL();
       
   308 					i = argsCount;	
       
   309 					}
       
   310 				else if (cmd.CompareF(KLabel) == 0)
       
   311 					{
       
   312 					params->iLabel = args->At(i).AllocL();
       
   313 					}
       
   314 				else if (cmd.CompareF(KKeyUsage) == 0)
       
   315 					{
       
   316 					params->iUsage = KeyToolUtils::ParseKeyUsage(args->At(i));
       
   317 					}
       
   318 				else if (cmd.CompareF(KStore) == 0)
       
   319 					{
       
   320 					TLex parser(args->At(i));
       
   321 					TInt err = parser.Val(params->iKeystoreIndex);
       
   322 					params->iIsDetailed = ETrue;
       
   323 					}
       
   324 				else if (cmd.CompareF(KAccess) == 0)
       
   325 					{
       
   326 					params->iAccess = KeyToolUtils::ParseKeyAccess(args->At(i));
       
   327 					}
       
   328 				else
       
   329 					{
       
   330 					// no action required	
       
   331 					}
       
   332 				continue;
       
   333 				}
       
   334 				
       
   335 			if (i!=0) 
       
   336 				{
       
   337 				if (args->At(i)[0] == '-')
       
   338 					{
       
   339 					i = argsCount;
       
   340 					command = KeyToolDefController::KUsageCommand;
       
   341 					continue;
       
   342 					}
       
   343 				delete params->iDefault;
       
   344 				params->iDefault = NULL;
       
   345 				params->iDefault = args->At(i).AllocL();			
       
   346 				}
       
   347 		 	} 
       
   348 		 
       
   349 		 
       
   350 		if (command != -1)
       
   351 			{
       
   352 			TRAP_IGNORE(controller->HandleCommandL(command, params));
       
   353 			}
       
   354 		else 
       
   355 			{
       
   356 			controller->HandleCommandL(KeyToolDefController::KUsageCommand, params);		
       
   357 			}
       
   358 		
       
   359 		CleanupStack::PopAndDestroy(3, controller); // params, args, controller
       
   360 		}
       
   361 	if (interactiveMode)
       
   362 		{
       
   363 		CleanupStack::PopAndDestroy(view);
       
   364 		// We are done!
       
   365 		console->Printf(KNewLine);
       
   366 		console->Printf(KDone);
       
   367 		console->Getch();	
       
   368 		}
       
   369 	else
       
   370 		{
       
   371 		CleanupStack::PopAndDestroy(2, &file); //view1 and file
       
   372 		}
       
   373 	CleanupStack::PopAndDestroy(3, &fs); //cmdArgs, console, fs	
       
   374 	}
       
   375 	
       
   376 
       
   377 GLDEF_C TInt E32Main()         // main function called by E32
       
   378    	{
       
   379 	__UHEAP_MARK;
       
   380 	CTrapCleanup* cleanup=CTrapCleanup::New(); 
       
   381 	
       
   382 	TRAP_IGNORE(DoMainL());
       
   383 	
       
   384 	delete cleanup; 
       
   385 	__UHEAP_MARKEND;
       
   386 	return 0; 
       
   387    	}