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