cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.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_view_imp.h"
       
    21 #include "keytool_commands.h"
       
    22 #include "certtool_controller.h"
       
    23 #include "keytoolfileview.h"
       
    24 
       
    25 
       
    26 // Boiler plate 
       
    27 _LIT(KShortName, "Symbian OS CertTool");
       
    28 _LIT(KName, "Symbian OS CertStore Manipulation Tool");
       
    29 _LIT(KCopyright, "Copyright (c) 2004-2007 Symbian Software Ltd.  All rights reserved.");
       
    30 
       
    31 _LIT(KNewLine, "\n");
       
    32 
       
    33 _LIT(KDone, "Press any key to continue... \n");
       
    34 
       
    35 // CertTool command line parameters
       
    36 _LIT(KList, "-list");
       
    37 _LIT(KListShort, "-l");
       
    38 
       
    39 _LIT(KListStores, "-liststores");
       
    40 _LIT(KListStoresShort, "-ls");
       
    41 
       
    42 _LIT(KImport, "-import");
       
    43 _LIT(KImportShort, "-i");
       
    44 
       
    45 _LIT(KPrivate, "-private");
       
    46 
       
    47 
       
    48 _LIT(KSetApps, "-setapps");
       
    49 _LIT(KSetAppsShort, "-s");
       
    50 
       
    51 _LIT(KAddApps, "-addapps");
       
    52 _LIT(KAddAppsShort, "-a");
       
    53 
       
    54 _LIT(KApps, "-apps");
       
    55 
       
    56 _LIT(KRemoveApps, "-removeapps");
       
    57 
       
    58 _LIT(KRemove, "-remove");
       
    59 _LIT(KRemoveShort, "-r");
       
    60 
       
    61 // remove private key also while removing the certificate
       
    62 // which is applicable iff the key is imported using the certool -private option
       
    63 
       
    64 _LIT(KRemoveKeyAlso, "-rka");
       
    65 
       
    66 _LIT(KStore, "-store");
       
    67 
       
    68 _LIT(KHelp, "-help");
       
    69 _LIT(KHelpShort, "-h");
       
    70 
       
    71 // Command parameters
       
    72 _LIT(KLabel, "-label");
       
    73 
       
    74 _LIT(KDetails, "-details");
       
    75 _LIT(KDetailsShort, "-d");
       
    76 
       
    77 _LIT(KOwnerType, "-owner");
       
    78 _LIT(KOwnerTypeShort, "-o");
       
    79 
       
    80 _LIT(KPageWise, "-page");
       
    81 _LIT(KPageWiseShort, "-p");
       
    82 
       
    83 _LIT(KUids, "-uids");
       
    84 
       
    85 const TInt KMaxArgs = 10;
       
    86 
       
    87 /**
       
    88  * Displays tool name and copyright informations.
       
    89  */
       
    90 LOCAL_D void BoilerPlateL(CConsoleBase* console) 
       
    91 	{
       
    92 	console->Printf(KNewLine);
       
    93 	console->Printf(KName);
       
    94 	console->Printf(KNewLine);	
       
    95 	console->Printf(KCopyright);
       
    96 	console->Printf(KNewLine);
       
    97 	console->Printf(KNewLine);	
       
    98 	}
       
    99 
       
   100 LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
       
   101 	{
       
   102 	if ((aCmdNum != -1) && (aCommand[0] == '-'))
       
   103 		{
       
   104 		aCmdNum = CertToolDefController::KUsageCommand;
       
   105 		aCmdCount = KMaxArgs;
       
   106 		return 1;
       
   107 		}
       
   108 	if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
       
   109 		{
       
   110 		aCmdNum = CertToolDefController::KListCommand;
       
   111 		}
       
   112 	else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
       
   113 		{
       
   114 		aCmdNum = CertToolDefController::KListStoresCommand;
       
   115 		}
       
   116 	else if	(aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
       
   117 		{
       
   118 		aCmdNum = CertToolDefController::KImportCommand;
       
   119 		}
       
   120 	else if	(aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
       
   121 		{
       
   122 		aCmdNum = CertToolDefController::KRemoveCommand;
       
   123 		}
       
   124 	else if	(aCommand.CompareF(KSetApps) == 0 || aCommand.Compare(KSetAppsShort) == 0)
       
   125 		{
       
   126 		aCmdNum = CertToolDefController::KSetAppsCommand;
       
   127 		}
       
   128 	else if	(aCommand.CompareF(KAddApps) == 0 || aCommand.Compare(KAddAppsShort) == 0)
       
   129 		{
       
   130 		aCmdNum = CertToolDefController::KAddAppsCommand;
       
   131 		}
       
   132 	else if	(aCommand.CompareF(KRemoveApps) == 0 )
       
   133 		{
       
   134 		aCmdNum = CertToolDefController::KRemoveAppsCommand;
       
   135 		}
       
   136 	else	
       
   137 		{
       
   138 		return 0;
       
   139 		}
       
   140 
       
   141 	return 1;
       
   142 	}
       
   143 	
       
   144 /**
       
   145  * Parses the command line and given control to the handler to deal with the request.
       
   146  */
       
   147 LOCAL_D void DoMainL() 
       
   148 	{
       
   149 	TBool interactiveMode = ETrue;
       
   150 	
       
   151 	RFs fs;
       
   152 	User::LeaveIfError(fs.Connect());
       
   153 	CleanupClosePushL(fs);
       
   154 	
       
   155 	CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
       
   156 	CleanupStack::PushL(console);
       
   157 	CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
       
   158 	TInt cmdArgsCount = cmdArgs->Count();
       
   159 	
       
   160 	RFile file;
       
   161 	// command: certtool inputfile outputfile
       
   162 	if (cmdArgsCount == 3)
       
   163 		{
       
   164 		if (KeyToolUtils::DoesFileExistsL(fs,cmdArgs->Arg(1)))
       
   165 		  	{
       
   166 		  	interactiveMode = EFalse;
       
   167 		  	TInt error = file.Open(fs, cmdArgs->Arg(1), EFileRead|EFileShareAny);
       
   168 		  	file.Close();
       
   169 		  	TInt error1 = file.Replace(fs, cmdArgs->Arg(2), EFileWrite|EFileShareExclusive);
       
   170 		  	CleanupClosePushL(file);
       
   171 		  	// If the input file doesn't exist or not able to create outputfile
       
   172 		  	// switch to Interactive mode
       
   173 		  	if (error != KErrNone || error1 != KErrNone)
       
   174 		   		{
       
   175 			   	CleanupStack::PopAndDestroy(&file);
       
   176 			   	interactiveMode = ETrue;
       
   177 			  	}	
       
   178 				
       
   179 			}
       
   180 		}
       
   181 		
       
   182 	CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(*console);
       
   183 	CCertToolController* controller = CCertToolController::NewLC(*view);
       
   184 	CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
       
   185 	CleanupStack::PushL(args);
       
   186 	CKeytoolFileView* view1 = NULL;
       
   187 
       
   188 	TInt cmdCount = 0;
       
   189 	if (interactiveMode)
       
   190 		{
       
   191 		KeyToolUtils::SetConsole(console);
       
   192 		BoilerPlateL(console);
       
   193 		for (TInt i = 0; i < cmdArgsCount; i++)
       
   194 			{
       
   195 			args->AppendL(cmdArgs->Arg(i));
       
   196 			}
       
   197 		// In Interactive mode by default we can execute 1 command only.
       
   198 		cmdCount = 1;
       
   199 		}
       
   200 	else
       
   201 		{
       
   202 		CleanupStack::PopAndDestroy(3, view); // args, controller, view
       
   203 		KeyToolUtils::SetFile(&file);
       
   204 		view1 = CKeytoolFileView::NewLC(cmdArgs->Arg(1));
       
   205 		cmdCount = view1->SplitFileInputToArrayL();
       
   206 		}	
       
   207 	
       
   208 	for (TInt j = 0; j < cmdCount; j++)
       
   209 		{
       
   210 		if (!interactiveMode)
       
   211 			{
       
   212 			controller = CCertToolController::NewLC(*view1);
       
   213 			args = view1->ReadArrayArgumentsLC(j);
       
   214 			}
       
   215 
       
   216 		CKeyToolParameters* params = CKeyToolParameters::NewLC();
       
   217 		
       
   218 		TInt command = -1; 
       
   219 		TInt i = -1;
       
   220 		
       
   221 		TInt argsCount = args->Count();
       
   222 		while (i < (argsCount-1))
       
   223 		 	{
       
   224 		 	i++;
       
   225 			if ((args->At(i).CompareF(KDetails)==0)|| (args->At(i).Compare(KDetailsShort)==0))
       
   226 				{
       
   227 				params->iIsDetailed = ETrue;
       
   228 				continue;
       
   229 				}
       
   230 	
       
   231 			if (args->At(i).CompareF(KPageWise)==0 || (args->At(i).Compare(KPageWiseShort)==0))
       
   232 				{
       
   233 				i++;
       
   234 				params->iPageWise = ETrue;
       
   235 				continue;
       
   236 				}
       
   237 
       
   238 			if (args->At(i).Compare(KRemoveKeyAlso)==0)
       
   239 				{
       
   240 				params->iRemoveKey = ETrue;
       
   241 				continue;
       
   242 				}
       
   243 
       
   244 			if (args->At(i).CompareF(KApps)==0)
       
   245 				{
       
   246 				i++;
       
   247 				RArray<TUid> apps;
       
   248 				TInt k = 0;
       
   249 				for (k = i; k < argsCount; k++)
       
   250 					{				
       
   251 					if (args->At(k).Find(_L("-")) == KErrNotFound)
       
   252 						{
       
   253 						TUint uid;
       
   254 						if (args->At(k).CompareF(KSWInstall)==0)
       
   255 							{
       
   256 							uid = swinstalluid;
       
   257 							}
       
   258 						else
       
   259 							{
       
   260 							if (args->At(k).CompareF(KSWInstallOCSP)==0)
       
   261 								{
       
   262 								uid = swinstallocspuid;
       
   263 								}
       
   264 							else
       
   265 								{
       
   266 								if (args->At(k).CompareF(KMidletInstall)==0)
       
   267 									{
       
   268 									uid = midletinstalluid;
       
   269 									}
       
   270 								else
       
   271 									{
       
   272 									if (args->At(k).CompareF(KTls)==0)
       
   273 										{
       
   274 										uid = tlsuid;
       
   275 										}
       
   276 									else
       
   277 										{
       
   278 										// no more valid apps, break cycle
       
   279 										break;
       
   280 										}
       
   281 									}
       
   282 								}
       
   283 							}
       
   284 						apps.Append(TUid::Uid(uid));						
       
   285 						}
       
   286 					else 
       
   287 						{
       
   288 						// We parsed all UIDs, break the cycle and go on!
       
   289 						break;
       
   290 						}
       
   291 					}
       
   292 				i = k-1;
       
   293 				params->iUIDs = apps; // We pass on ownership
       
   294 				params->iIsDetailed = ETrue;
       
   295 				continue;			
       
   296 				}
       
   297 		
       
   298 			if (args->At(i).CompareF(KUids)==0)
       
   299 				{
       
   300 				i++;
       
   301 				RArray<TUid> uids;
       
   302 				TInt k = 0;
       
   303 				for (k = i; k < argsCount; k++)
       
   304 					{				
       
   305 					if (args->At(k).Left(2) == _L("0x"))
       
   306 						{
       
   307 						TLex lex(args->At(k).Mid(2));		
       
   308 						TUint uid =0;
       
   309 						TInt err = lex.Val(uid, EHex);
       
   310 						if (err == KErrNone)
       
   311 							{
       
   312 							params->iUIDs.Append(TUid::Uid(uid));						
       
   313 							}			 
       
   314 						}
       
   315 					else 
       
   316 						{
       
   317 						// We parsed all UIDs, break the cycle and go on!
       
   318 						break;
       
   319 						}
       
   320 					}
       
   321 				i = k-1;
       
   322 				params->iIsDetailed = ETrue;
       
   323 				continue;			
       
   324 				}
       
   325 			
       
   326 			TDesC& cmd = args->At(i);
       
   327 			if (cmd.CompareF(KLabel) == 0 || 
       
   328 			    cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 ||
       
   329 			    cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 ||
       
   330 			    cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 )
       
   331 				{
       
   332 				i++;
       
   333 				if (i >= argsCount || args->At(i)[0] == '-')
       
   334 					{
       
   335 					i = argsCount;
       
   336 					command = CertToolDefController::KUsageCommand;
       
   337 					}
       
   338 				else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
       
   339 					{
       
   340 					params->iDefault = args->At(i).AllocL();
       
   341 					i = argsCount;	
       
   342 					}
       
   343 				else if (cmd.CompareF(KLabel) == 0)
       
   344 					{
       
   345 					params->iLabel = args->At(i).AllocL();
       
   346 					}
       
   347 				else if (cmd.CompareF(KPrivate) == 0)
       
   348 					{
       
   349 					params->iPrivate = args->At(i).AllocL();
       
   350 					}
       
   351 				else if (cmd.CompareF(KStore) == 0)
       
   352 					{
       
   353 					TLex parser(args->At(i));
       
   354 					TInt err = parser.Val(params->iCertstoreIndex);
       
   355 					params->iIsDetailed = ETrue;
       
   356 					}
       
   357 				else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
       
   358 					{
       
   359 					params->iIsDetailed = ETrue;
       
   360 					params->iOwnerType = args->At(i).AllocL();
       
   361 					}
       
   362 				else
       
   363 					{
       
   364 					// no action required	
       
   365 					}
       
   366 				continue;
       
   367 				}
       
   368 	
       
   369 			if (VerifyCommand(args->At(i), command, i))
       
   370 				{
       
   371 				continue;
       
   372 				}
       
   373 
       
   374 				
       
   375 			if (i!=0) 
       
   376 				{
       
   377 				if (args->At(i)[0] == '-')
       
   378 					{
       
   379 					i = argsCount;
       
   380 					command = CertToolDefController::KUsageCommand;
       
   381 					continue;
       
   382 					}
       
   383 				delete params->iDefault;
       
   384 				params->iDefault = NULL;
       
   385 				params->iDefault = args->At(i).AllocL();
       
   386 				params->iIsDetailed = ETrue;			
       
   387 				}
       
   388 		 	} 
       
   389 		 
       
   390 		 
       
   391 		if (command != -1)
       
   392 			{
       
   393 			TRAP_IGNORE(controller->HandleCommandL(command, params));
       
   394 			}
       
   395 		else 
       
   396 			{
       
   397 			controller->HandleCommandL(CertToolDefController::KUsageCommand, params);		
       
   398 			}
       
   399 		CleanupStack::PopAndDestroy(3, controller); // params, args, controller
       
   400 		}
       
   401 	if (interactiveMode)
       
   402 		{
       
   403 		CleanupStack::PopAndDestroy(view);
       
   404 		// We are done!
       
   405 		console->Printf(KNewLine);
       
   406 		console->Printf(KDone);
       
   407 		console->Getch();	
       
   408 		}
       
   409 	else
       
   410 		{
       
   411 		CleanupStack::PopAndDestroy(2, &file); //view1 and file
       
   412 		}
       
   413 	CleanupStack::PopAndDestroy(3, &fs); //cmdArgs, console, fs
       
   414 	}
       
   415 	
       
   416 
       
   417 GLDEF_C TInt E32Main()         // main function called by E32
       
   418    	{
       
   419 	__UHEAP_MARK;
       
   420 	CTrapCleanup* cleanup=CTrapCleanup::New(); 
       
   421 	
       
   422 	TRAP_IGNORE(DoMainL());
       
   423 	
       
   424 	delete cleanup; 
       
   425 	__UHEAP_MARKEND;
       
   426 	return 0; 
       
   427    	}