os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool.cpp
First public contribution.
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.
21 #include <keytool.rsg>
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"
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.");
38 _LIT(KDone, "Press any key to continue... \n");
40 // Keytool command line parameters
42 _LIT(KListShort, "-l");
44 _LIT(KListStores, "-liststores");
45 _LIT(KListStoresShort, "-ls");
47 _LIT(KImport, "-import");
48 _LIT(KImportShort, "-i");
51 _LIT(KRemove, "-remove");
52 _LIT(KRemoveShort, "-r");
55 _LIT(KHelpShort, "-h");
57 _LIT(KSetUser, "-setuser");
58 _LIT(KSetUserShort, "-s");
60 _LIT(KSetAllUsers, "-setallusers");
61 _LIT(KSetAllUsersShort, "-a");
63 _LIT(KSetManager, "-setmanager");
64 _LIT(KSetManagerShort, "-m");
66 _LIT(KRemoveUser, "-removeuser");
67 _LIT(KRemoveUserShort, "-ru");
70 _LIT(KLabel, "-label");
71 _LIT(KKeyUsage, "-usage");
72 _LIT(KAccess, "-access");
74 _LIT(KStore, "-store");
76 //_LIT(KExpiry, "-expiry"); // Functionality not present at the moment...
77 _LIT(KDetails, "-details");
78 _LIT(KDetailsShort, "-d");
80 _LIT(KPageWise, "-page");
81 _LIT(KPageWiseShort, "-p");
84 _LIT(KMigrateStore, "-migratestore");
85 _LIT(KNewFileName, "-new");
86 _LIT(KAuthExpression, "-expr");
87 _LIT(KFreshness, "-freshness");
90 const TInt KMaxArgs = 10;
93 * Displays tool name and copyright informations.
95 LOCAL_D void BoilerPlateL(CConsoleBase* console)
97 console->Printf(KNewLine);
98 console->Printf(KName);
99 console->Printf(KNewLine);
100 console->Printf(KCopyright);
101 console->Printf(KNewLine);
102 console->Printf(KNewLine);
106 LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
108 if ((aCmdNum != -1) && (aCommand[0] == '-'))
110 aCmdNum = KeyToolDefController::KUsageCommand;
111 aCmdCount = KMaxArgs;
114 if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
116 aCmdNum = KeyToolDefController::KListCommand;
118 else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
120 aCmdNum = KeyToolDefController::KListStoresCommand;
122 else if (aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
124 aCmdNum = KeyToolDefController::KImportCommand;
126 else if (aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
128 aCmdNum = KeyToolDefController::KRemoveCommand;
130 else if (aCommand.CompareF(KSetUser) == 0 || aCommand.Compare(KSetUserShort) == 0)
132 aCmdNum = KeyToolDefController::KSetUserCommand;
134 else if (aCommand.CompareF(KSetManager) == 0 || aCommand.Compare(KSetManagerShort) == 0)
136 aCmdNum = KeyToolDefController::KSetManagerCommand;
138 else if (aCommand.CompareF(KRemoveUser) == 0 || aCommand.Compare(KRemoveUserShort) == 0)
140 aCmdNum = KeyToolDefController::KRemoveUserCommand;
142 else if (aCommand.CompareF(KSetAllUsers) == 0 || aCommand.Compare(KSetAllUsersShort) == 0)
144 aCmdNum = KeyToolDefController::KSetAllUsersCommand;
148 else if ( aCommand.CompareF(KMigrateStore) == 0 )
150 aCmdNum = KeyToolDefController::KMigrateStore;
164 * Parses the command line and given control to the handler to deal with the request.
166 LOCAL_D void DoMainL()
168 TBool interactiveMode = ETrue;
171 User::LeaveIfError(fs.Connect());
172 CleanupClosePushL(fs);
174 CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
175 CleanupStack::PushL(console);
176 CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
177 TInt cmdArgsCount = cmdArgs->Count();
180 // command: keytool inputfile outputfile
181 if (cmdArgsCount == 3)
183 if (KeyToolUtils::DoesFileExistsL(fs,cmdArgs->Arg(1)))
185 interactiveMode = EFalse;
186 TInt error = file.Open(fs, cmdArgs->Arg(1), EFileRead|EFileShareAny);
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)
195 CleanupStack::PopAndDestroy(&file);
196 interactiveMode = ETrue;
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;
210 KeyToolUtils::SetConsole(console);
211 BoilerPlateL(console);
212 for (TInt i = 0; i < cmdArgsCount; i++)
214 args->AppendL(cmdArgs->Arg(i));
216 // In Interactive mode by default we can execute 1 command only.
221 CleanupStack::PopAndDestroy(3, view); // args, controller, view
222 KeyToolUtils::SetFile(&file);
223 view1 = CKeytoolFileView::NewLC(cmdArgs->Arg(1));
224 cmdCount = view1->SplitFileInputToArrayL();
227 for (TInt j = 0; j < cmdCount; j++)
229 if (!interactiveMode)
231 controller = CKeyToolController::NewLC(*view1);
232 args = view1->ReadArrayArgumentsLC(j);
235 CKeyToolParameters* params = CKeyToolParameters::NewLC();
240 TInt argsCount = args->Count();
241 while (i < (argsCount-1))
245 if ((args->At(i).CompareF(KDetails)==0)|| (args->At(i).Compare(KDetailsShort)==0))
247 params->iIsDetailed = ETrue;
251 if (args->At(i).CompareF(KPageWise)==0 || (args->At(i).Compare(KPageWiseShort)==0))
253 params->iPageWise = ETrue;
257 TDesC& commd = args->At(i);
258 if (VerifyCommand(commd, command, i))
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 )
265 if (args->At(i)[0] == '-' || args->At(i).Left(2) == _L("0x"))
268 command = KeyToolDefController::KUsageCommand;
275 if (commd.CompareF(KSetAllUsers) == 0 || commd.Compare(KSetAllUsersShort) == 0)
277 params->iPolicy = CKeyToolParameters::ESetAllUsersPolicy;
278 params->iIsDetailed = ETrue;
280 if (commd.CompareF(KRemoveUser) == 0 || commd.Compare(KRemoveUserShort) == 0)
282 params->iPolicy = CKeyToolParameters::ERemoveUserPolicy;
283 params->iIsDetailed = ETrue;
285 if (commd.CompareF(KSetManager) == 0 || commd.Compare(KSetManagerShort) == 0)
287 params->iPolicy = CKeyToolParameters::ESetManagerPolicy;
288 params->iIsDetailed = ETrue;
290 if (commd.CompareF(KSetUser) == 0 || commd.Compare(KSetUserShort) == 0)
292 params->iPolicy = CKeyToolParameters::ESetUserPolicy;
294 if (i >= argsCount || args->At(i)[0] == '-' || args->At(i).Left(2) != _L("0x"))
297 command = KeyToolDefController::KUsageCommand;
302 TLex lex(args->At(i).Mid(2));
304 TInt err = lex.Val(uid, EHex);
307 app = TUid::Uid(uid);
309 params->iUIDs.Append(app);
310 params->iIsDetailed = ETrue;
315 if(commd.CompareF(KMigrateStore) == 0 )
317 int mandatoryParams = 2;
318 if(argsCount-1 >= ++i)
320 params->iOldKeyFile = args->At(i).AllocL();
324 controller->DisplayLocalisedMsgL(R_KEYTOOL_USAGE_OLDKEY_ABSENT);
327 if(argsCount-1 >= ++i)
329 params->iPassphrase = args->At(i).AllocL();
333 controller->DisplayLocalisedMsgL(R_KEYTOOL_USAGE_PASSPHRASE_ABSENT);
337 if( mandatoryParams != 2 )
339 command = KeyToolDefController::KUsageCommand;
343 TInt count = args->Count();
347 TDesC& commd = args->At(i);
348 if(commd.CompareF(KNewFileName) == 0 && argsCount-1 >= ++i)
350 delete params->iNewKeyFile;
351 params->iNewKeyFile=NULL;
352 params->iNewKeyFile = args->At(i++).AllocL();
354 else if(commd.CompareF(KAuthExpression) == 0 && argsCount-1 >= ++i )
356 params->iAuthExpression = args->At(i++).AllocL();
358 else if(commd.CompareF(KFreshness) == 0 && argsCount-1 >= ++i )
360 TLex lex(args->At(i++));
361 TInt err = lex.Val(params->iFreshness);
364 } // if(mandatoryParams == 0)
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)
377 if (i >= argsCount || args->At(i)[0] == '-')
380 command = KeyToolDefController::KUsageCommand;
382 else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
384 params->iDefault = args->At(i).AllocL();
387 else if (cmd.CompareF(KLabel) == 0)
389 params->iLabel = args->At(i).AllocL();
391 else if (cmd.CompareF(KKeyUsage) == 0)
393 params->iUsage = KeyToolUtils::ParseKeyUsage(args->At(i));
395 else if (cmd.CompareF(KStore) == 0)
397 TLex parser(args->At(i));
398 TInt err = parser.Val(params->iKeystoreIndex);
399 params->iIsDetailed = ETrue;
401 else if (cmd.CompareF(KAccess) == 0)
403 params->iAccess = KeyToolUtils::ParseKeyAccess(args->At(i));
407 // no action required
414 if (args->At(i)[0] == '-')
417 command = KeyToolDefController::KUsageCommand;
420 delete params->iDefault;
421 params->iDefault = NULL;
422 params->iDefault = args->At(i).AllocL();
429 TRAP_IGNORE(controller->HandleCommandL(command, params));
433 controller->HandleCommandL(KeyToolDefController::KUsageCommand, params);
436 CleanupStack::PopAndDestroy(3, controller); // params, args, controller
440 CleanupStack::PopAndDestroy(view);
442 console->Printf(KNewLine);
443 console->Printf(KDone);
448 CleanupStack::PopAndDestroy(2, &file); //view1 and file
450 CleanupStack::PopAndDestroy(3, &fs); //cmdArgs, console, fs
454 GLDEF_C TInt E32Main() // main function called by E32
457 CTrapCleanup* cleanup=CTrapCleanup::New();
459 TRAP_IGNORE(DoMainL());