os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp
Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "keytool_utils.h"
23 #include "keytool_view_imp.h"
24 #include "keytool_commands.h"
25 #include "certtool_controller.h"
26 #include "keytoolfileview.h"
30 _LIT(KShortName, "Symbian OS CertTool");
31 _LIT(KName, "Symbian OS CertStore Manipulation Tool");
32 _LIT(KCopyright, "Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.");
36 _LIT(KDone, "Press any key to continue... \n");
38 // CertTool command line parameters
41 _LIT(KListShort, "-l");
43 _LIT(KListStores, "-liststores");
44 _LIT(KListStoresShort, "-ls");
46 _LIT(KImport, "-import");
47 _LIT(KImportShort, "-i");
49 _LIT(KPrivate, "-private");
52 _LIT(KSetApps, "-setapps");
53 _LIT(KSetAppsShort, "-s");
55 _LIT(KAddApps, "-addapps");
56 _LIT(KAddAppsShort, "-a");
60 _LIT(KRemoveApps, "-removeapps");
62 _LIT(KRemove, "-remove");
63 _LIT(KRemoveShort, "-r");
65 // remove private key also while removing the certificate
66 // which is applicable iff the key is imported using the certool -private option
68 _LIT(KRemoveKeyAlso, "-rka");
70 _LIT(KStore, "-store");
73 _LIT(KHelpShort, "-h");
76 _LIT(KLabel, "-label");
78 _LIT(KDetails, "-details");
79 _LIT(KDetailsShort, "-d");
81 _LIT(KOwnerType, "-owner");
82 _LIT(KOwnerTypeShort, "-o");
84 _LIT(KPageWise, "-page");
85 _LIT(KPageWiseShort, "-p");
89 _LIT(KDeletable, "-deletable");
90 _LIT(KDeletableShort, "-del");
92 const TInt KMaxArgs = 10;
95 * Certtool can operate in the following modes.
104 * Displays tool name and copy-right informations.
106 LOCAL_D void BoilerPlateL(CConsoleBase* console)
108 console->Printf(KNewLine);
109 console->Printf(KName);
110 console->Printf(KNewLine);
111 console->Printf(KCopyright);
112 console->Printf(KNewLine);
113 console->Printf(KNewLine);
116 LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
118 if ((aCmdNum != -1) && (aCommand[0] == '-'))
120 aCmdNum = CertToolDefController::KUsageCommand;
121 aCmdCount = KMaxArgs;
124 if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
126 aCmdNum = CertToolDefController::KListCommand;
128 else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
130 aCmdNum = CertToolDefController::KListStoresCommand;
132 else if (aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
134 aCmdNum = CertToolDefController::KImportCommand;
136 else if (aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
138 aCmdNum = CertToolDefController::KRemoveCommand;
140 else if (aCommand.CompareF(KSetApps) == 0 || aCommand.Compare(KSetAppsShort) == 0)
142 aCmdNum = CertToolDefController::KSetAppsCommand;
144 else if (aCommand.CompareF(KAddApps) == 0 || aCommand.Compare(KAddAppsShort) == 0)
146 aCmdNum = CertToolDefController::KAddAppsCommand;
148 else if (aCommand.CompareF(KRemoveApps) == 0 )
150 aCmdNum = CertToolDefController::KRemoveAppsCommand;
161 * Returns the mode in which the tool would operate. If the command is invoked
162 * with 2 parameters(certool inputFile outputFile), the tool works in non-interactive
163 * mode else the interactive mode is chosen.
166 LOCAL_D OperationMode ModeOfOperationL(const CCommandLineArguments& aCmdArgs, RFs& aFs, RFile& aFile)
168 OperationMode mode = Interactive;
169 if (KeyToolUtils::DoesFileExistsL(aFs,aCmdArgs.Arg(1)))
171 mode = NonInteractive;
172 TInt error = aFile.Open(aFs, aCmdArgs.Arg(1), EFileRead|EFileShareAny);
175 TInt error1 = aFile.Replace(aFs, aCmdArgs.Arg(2), EFileWrite|EFileShareExclusive);
176 // If the input file doesn't exist or not able to create outputfile
177 // switch to Interactive mode
178 if (error != KErrNone || error1 != KErrNone)
180 CleanupStack::PopAndDestroy(&aFile);
189 * The main parsing logic. Same for interactive and non-interactive modes.
191 LOCAL_D void ParseAndHandleCommandL(CArrayFixFlat<TPtrC>& aArgs, CCertToolController& aController)
193 CKeyToolParameters* params = CKeyToolParameters::NewLC();
198 TInt argsCount = aArgs.Count();
199 while (i < (argsCount-1))
202 if ((aArgs.At(i).CompareF(KDetails)==0)|| (aArgs.At(i).Compare(KDetailsShort)==0))
204 params->iIsDetailed = ETrue;
208 if (aArgs.At(i).CompareF(KPageWise)==0 || (aArgs.At(i).Compare(KPageWiseShort)==0))
211 params->iPageWise = ETrue;
215 if (aArgs.At(i).Compare(KRemoveKeyAlso)==0)
217 params->iRemoveKey = ETrue;
221 if (aArgs.At(i).CompareF(KApps)==0)
226 for (k = i; k < argsCount; k++)
228 if (aArgs.At(k).Find(_L("-")) == KErrNotFound)
231 if (aArgs.At(k).CompareF(KSWInstall)==0)
237 if (aArgs.At(k).CompareF(KSWInstallOCSP)==0)
239 uid = swinstallocspuid;
243 if (aArgs.At(k).CompareF(KMidletInstall)==0)
245 uid = midletinstalluid;
249 if (aArgs.At(k).CompareF(KTls)==0)
255 // no more valid apps, break cycle
261 apps.Append(TUid::Uid(uid));
265 // We parsed all UIDs, break the cycle and go on!
270 params->iUIDs = apps; // We pass on ownership
271 params->iIsDetailed = ETrue;
275 if (aArgs.At(i).CompareF(KUids)==0)
280 for (k = i; k < argsCount; k++)
282 if (aArgs.At(k).Left(2) == _L("0x"))
284 TLex lex(aArgs.At(k).Mid(2));
286 TInt err = lex.Val(uid, EHex);
289 params->iUIDs.Append(TUid::Uid(uid));
294 // We parsed all UIDs, break the cycle and go on!
299 params->iIsDetailed = ETrue;
303 TDesC& cmd = aArgs.At(i);
304 if (cmd.CompareF(KLabel) == 0 ||
305 cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 ||
306 cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 ||
307 cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 ||
308 cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
311 if (i >= argsCount || aArgs.At(i)[0] == '-')
314 command = CertToolDefController::KUsageCommand;
316 else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
318 params->iDefault = aArgs.At(i).AllocL();
321 else if (cmd.CompareF(KLabel) == 0)
323 params->iLabel = aArgs.At(i).AllocL();
325 else if (cmd.CompareF(KPrivate) == 0)
327 params->iPrivate = aArgs.At(i).AllocL();
329 else if (cmd.CompareF(KStore) == 0)
331 TLex parser(aArgs.At(i));
332 TInt err = parser.Val(params->iCertstoreIndex);
333 params->iIsDetailed = ETrue;
335 else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
337 params->iIsDetailed = ETrue;
338 params->iOwnerType = aArgs.At(i).AllocL();
340 else if (cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
342 params->iIsDetailed = ETrue;
343 params->iIsDeletable = aArgs.At(i).AllocL();
348 if (VerifyCommand(aArgs.At(i), command, i))
356 if (aArgs.At(i)[0] == '-')
359 command = CertToolDefController::KUsageCommand;
362 delete params->iDefault;
363 params->iDefault = NULL;
364 params->iDefault = aArgs.At(i).AllocL();
365 params->iIsDetailed = ETrue;
372 TRAP_IGNORE(aController.HandleCommandL(command, params));
376 aController.HandleCommandL(CertToolDefController::KUsageCommand, params);
378 CleanupStack::PopAndDestroy(params);
383 * Parsing the command for non-interactive mode.
386 LOCAL_D void ParseCommandInNonInteractiveModeL(RFile& aFile, const CCommandLineArguments& aCmdArgs)
389 KeyToolUtils::SetFile(&aFile);
391 CKeytoolFileView* view(0);
392 view = CKeytoolFileView::NewLC(aCmdArgs.Arg(1));
393 TInt cmdCount = view->SplitFileInputToArrayL();
395 //For every command, parse and handle.
396 for (TInt j = 0; j < cmdCount; j++)
398 CCertToolController* controller = CCertToolController::NewLC(*view);
400 CArrayFixFlat<TPtrC>* args = view->ReadArrayArgumentsLC(j);
401 ParseAndHandleCommandL(*args, *controller);
403 CleanupStack::PopAndDestroy(2, controller);
407 CleanupStack::PopAndDestroy(view);
413 * Parsing the command for interactive mode.
417 LOCAL_D void ParseCommandInInteractiveModeL(CConsoleBase& aConsole, const CCommandLineArguments& aCmdArgs)
419 CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
420 CleanupStack::PushL(args);
421 CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(aConsole);
422 CCertToolController* controller = CCertToolController::NewLC(*view);
424 TInt cmdArgsCount = aCmdArgs.Count();
426 KeyToolUtils::SetConsole(&aConsole);
427 BoilerPlateL(&aConsole);
429 for (TInt i = 0; i < cmdArgsCount; i++)
431 args->AppendL(aCmdArgs.Arg(i));
434 //Interactive mode can handle only one command at a time.
435 ParseAndHandleCommandL(*args, *controller);
438 aConsole.Printf(KNewLine);
439 aConsole.Printf(KDone);
442 CleanupStack::PopAndDestroy(3, args); // controller, view, args
447 * Parses the command line and given control to the handler to deal with the request.
449 LOCAL_D void DoMainL()
453 User::LeaveIfError(fs.Connect());
454 CleanupClosePushL(fs);
457 CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
458 CleanupStack::PushL(console);
459 CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
460 TInt cmdArgsCount = cmdArgs->Count();
463 OperationMode currentMode = Interactive; //Interactive by default.
465 // Determine the mode of operation as either interactive or non-interactive.
466 if (cmdArgsCount == 3)
468 currentMode = ModeOfOperationL(*cmdArgs, fs, file);
474 ParseCommandInInteractiveModeL(*console, *cmdArgs);
478 //file refers to the output file name.
479 CleanupClosePushL(file);
480 ParseCommandInNonInteractiveModeL(file, *cmdArgs);
481 CleanupStack::PopAndDestroy(&file);
485 CleanupStack::PopAndDestroy(3, &fs);
490 GLDEF_C TInt E32Main() // main function called by E32
493 CTrapCleanup* cleanup=CTrapCleanup::New();
495 TRAP_IGNORE(DoMainL());