os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,501 @@
1.4 +/*
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <e32cons.h>
1.23 +#include <bacline.h>
1.24 +
1.25 +#include "keytool_utils.h"
1.26 +#include "keytool_view_imp.h"
1.27 +#include "keytool_commands.h"
1.28 +#include "certtool_controller.h"
1.29 +#include "keytoolfileview.h"
1.30 +
1.31 +
1.32 +// Boiler plate
1.33 +_LIT(KShortName, "Symbian OS CertTool");
1.34 +_LIT(KName, "Symbian OS CertStore Manipulation Tool");
1.35 +_LIT(KCopyright, "Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.");
1.36 +
1.37 +_LIT(KNewLine, "\n");
1.38 +
1.39 +_LIT(KDone, "Press any key to continue... \n");
1.40 +
1.41 +// CertTool command line parameters
1.42 +
1.43 +_LIT(KList, "-list");
1.44 +_LIT(KListShort, "-l");
1.45 +
1.46 +_LIT(KListStores, "-liststores");
1.47 +_LIT(KListStoresShort, "-ls");
1.48 +
1.49 +_LIT(KImport, "-import");
1.50 +_LIT(KImportShort, "-i");
1.51 +
1.52 +_LIT(KPrivate, "-private");
1.53 +
1.54 +
1.55 +_LIT(KSetApps, "-setapps");
1.56 +_LIT(KSetAppsShort, "-s");
1.57 +
1.58 +_LIT(KAddApps, "-addapps");
1.59 +_LIT(KAddAppsShort, "-a");
1.60 +
1.61 +_LIT(KApps, "-apps");
1.62 +
1.63 +_LIT(KRemoveApps, "-removeapps");
1.64 +
1.65 +_LIT(KRemove, "-remove");
1.66 +_LIT(KRemoveShort, "-r");
1.67 +
1.68 +// remove private key also while removing the certificate
1.69 +// which is applicable iff the key is imported using the certool -private option
1.70 +
1.71 +_LIT(KRemoveKeyAlso, "-rka");
1.72 +
1.73 +_LIT(KStore, "-store");
1.74 +
1.75 +_LIT(KHelp, "-help");
1.76 +_LIT(KHelpShort, "-h");
1.77 +
1.78 +// Command parameters
1.79 +_LIT(KLabel, "-label");
1.80 +
1.81 +_LIT(KDetails, "-details");
1.82 +_LIT(KDetailsShort, "-d");
1.83 +
1.84 +_LIT(KOwnerType, "-owner");
1.85 +_LIT(KOwnerTypeShort, "-o");
1.86 +
1.87 +_LIT(KPageWise, "-page");
1.88 +_LIT(KPageWiseShort, "-p");
1.89 +
1.90 +_LIT(KUids, "-uids");
1.91 +
1.92 +_LIT(KDeletable, "-deletable");
1.93 +_LIT(KDeletableShort, "-del");
1.94 +
1.95 +const TInt KMaxArgs = 10;
1.96 +
1.97 +/**
1.98 + * Certtool can operate in the following modes.
1.99 +**/
1.100 +enum OperationMode {
1.101 +Interactive,
1.102 +NonInteractive
1.103 +};
1.104 +
1.105 +
1.106 +/**
1.107 + * Displays tool name and copy-right informations.
1.108 + */
1.109 +LOCAL_D void BoilerPlateL(CConsoleBase* console)
1.110 + {
1.111 + console->Printf(KNewLine);
1.112 + console->Printf(KName);
1.113 + console->Printf(KNewLine);
1.114 + console->Printf(KCopyright);
1.115 + console->Printf(KNewLine);
1.116 + console->Printf(KNewLine);
1.117 + }
1.118 +
1.119 +LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
1.120 + {
1.121 + if ((aCmdNum != -1) && (aCommand[0] == '-'))
1.122 + {
1.123 + aCmdNum = CertToolDefController::KUsageCommand;
1.124 + aCmdCount = KMaxArgs;
1.125 + return 1;
1.126 + }
1.127 + if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
1.128 + {
1.129 + aCmdNum = CertToolDefController::KListCommand;
1.130 + }
1.131 + else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
1.132 + {
1.133 + aCmdNum = CertToolDefController::KListStoresCommand;
1.134 + }
1.135 + else if (aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
1.136 + {
1.137 + aCmdNum = CertToolDefController::KImportCommand;
1.138 + }
1.139 + else if (aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
1.140 + {
1.141 + aCmdNum = CertToolDefController::KRemoveCommand;
1.142 + }
1.143 + else if (aCommand.CompareF(KSetApps) == 0 || aCommand.Compare(KSetAppsShort) == 0)
1.144 + {
1.145 + aCmdNum = CertToolDefController::KSetAppsCommand;
1.146 + }
1.147 + else if (aCommand.CompareF(KAddApps) == 0 || aCommand.Compare(KAddAppsShort) == 0)
1.148 + {
1.149 + aCmdNum = CertToolDefController::KAddAppsCommand;
1.150 + }
1.151 + else if (aCommand.CompareF(KRemoveApps) == 0 )
1.152 + {
1.153 + aCmdNum = CertToolDefController::KRemoveAppsCommand;
1.154 + }
1.155 + else
1.156 + {
1.157 + return 0;
1.158 + }
1.159 +
1.160 + return 1;
1.161 + }
1.162 +
1.163 +/**
1.164 + * Returns the mode in which the tool would operate. If the command is invoked
1.165 + * with 2 parameters(certool inputFile outputFile), the tool works in non-interactive
1.166 + * mode else the interactive mode is chosen.
1.167 + */
1.168 +
1.169 +LOCAL_D OperationMode ModeOfOperationL(const CCommandLineArguments& aCmdArgs, RFs& aFs, RFile& aFile)
1.170 + {
1.171 + OperationMode mode = Interactive;
1.172 + if (KeyToolUtils::DoesFileExistsL(aFs,aCmdArgs.Arg(1)))
1.173 + {
1.174 + mode = NonInteractive;
1.175 + TInt error = aFile.Open(aFs, aCmdArgs.Arg(1), EFileRead|EFileShareAny);
1.176 + aFile.Close();
1.177 +
1.178 + TInt error1 = aFile.Replace(aFs, aCmdArgs.Arg(2), EFileWrite|EFileShareExclusive);
1.179 + // If the input file doesn't exist or not able to create outputfile
1.180 + // switch to Interactive mode
1.181 + if (error != KErrNone || error1 != KErrNone)
1.182 + {
1.183 + CleanupStack::PopAndDestroy(&aFile);
1.184 + mode = Interactive;
1.185 + }
1.186 +
1.187 + }
1.188 + return mode;
1.189 + }
1.190 +
1.191 +/**
1.192 + * The main parsing logic. Same for interactive and non-interactive modes.
1.193 + */
1.194 +LOCAL_D void ParseAndHandleCommandL(CArrayFixFlat<TPtrC>& aArgs, CCertToolController& aController)
1.195 + {
1.196 + CKeyToolParameters* params = CKeyToolParameters::NewLC();
1.197 +
1.198 + TInt command = -1;
1.199 + TInt i = -1;
1.200 +
1.201 + TInt argsCount = aArgs.Count();
1.202 + while (i < (argsCount-1))
1.203 + {
1.204 + i++;
1.205 + if ((aArgs.At(i).CompareF(KDetails)==0)|| (aArgs.At(i).Compare(KDetailsShort)==0))
1.206 + {
1.207 + params->iIsDetailed = ETrue;
1.208 + continue;
1.209 + }
1.210 +
1.211 + if (aArgs.At(i).CompareF(KPageWise)==0 || (aArgs.At(i).Compare(KPageWiseShort)==0))
1.212 + {
1.213 + i++;
1.214 + params->iPageWise = ETrue;
1.215 + continue;
1.216 + }
1.217 +
1.218 + if (aArgs.At(i).Compare(KRemoveKeyAlso)==0)
1.219 + {
1.220 + params->iRemoveKey = ETrue;
1.221 + continue;
1.222 + }
1.223 +
1.224 + if (aArgs.At(i).CompareF(KApps)==0)
1.225 + {
1.226 + i++;
1.227 + RArray<TUid> apps;
1.228 + TInt k = 0;
1.229 + for (k = i; k < argsCount; k++)
1.230 + {
1.231 + if (aArgs.At(k).Find(_L("-")) == KErrNotFound)
1.232 + {
1.233 + TUint uid;
1.234 + if (aArgs.At(k).CompareF(KSWInstall)==0)
1.235 + {
1.236 + uid = swinstalluid;
1.237 + }
1.238 + else
1.239 + {
1.240 + if (aArgs.At(k).CompareF(KSWInstallOCSP)==0)
1.241 + {
1.242 + uid = swinstallocspuid;
1.243 + }
1.244 + else
1.245 + {
1.246 + if (aArgs.At(k).CompareF(KMidletInstall)==0)
1.247 + {
1.248 + uid = midletinstalluid;
1.249 + }
1.250 + else
1.251 + {
1.252 + if (aArgs.At(k).CompareF(KTls)==0)
1.253 + {
1.254 + uid = tlsuid;
1.255 + }
1.256 + else
1.257 + {
1.258 + // no more valid apps, break cycle
1.259 + break;
1.260 + }
1.261 + }
1.262 + }
1.263 + }
1.264 + apps.Append(TUid::Uid(uid));
1.265 + }
1.266 + else
1.267 + {
1.268 + // We parsed all UIDs, break the cycle and go on!
1.269 + break;
1.270 + }
1.271 + }
1.272 + i = k-1;
1.273 + params->iUIDs = apps; // We pass on ownership
1.274 + params->iIsDetailed = ETrue;
1.275 + continue;
1.276 + }
1.277 +
1.278 + if (aArgs.At(i).CompareF(KUids)==0)
1.279 + {
1.280 + i++;
1.281 + RArray<TUid> uids;
1.282 + TInt k = 0;
1.283 + for (k = i; k < argsCount; k++)
1.284 + {
1.285 + if (aArgs.At(k).Left(2) == _L("0x"))
1.286 + {
1.287 + TLex lex(aArgs.At(k).Mid(2));
1.288 + TUint uid =0;
1.289 + TInt err = lex.Val(uid, EHex);
1.290 + if (err == KErrNone)
1.291 + {
1.292 + params->iUIDs.Append(TUid::Uid(uid));
1.293 + }
1.294 + }
1.295 + else
1.296 + {
1.297 + // We parsed all UIDs, break the cycle and go on!
1.298 + break;
1.299 + }
1.300 + }
1.301 + i = k-1;
1.302 + params->iIsDetailed = ETrue;
1.303 + continue;
1.304 + }
1.305 +
1.306 + TDesC& cmd = aArgs.At(i);
1.307 + if (cmd.CompareF(KLabel) == 0 ||
1.308 + cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 ||
1.309 + cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 ||
1.310 + cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 ||
1.311 + cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
1.312 + {
1.313 + i++;
1.314 + if (i >= argsCount || aArgs.At(i)[0] == '-')
1.315 + {
1.316 + i = argsCount;
1.317 + command = CertToolDefController::KUsageCommand;
1.318 + }
1.319 + else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
1.320 + {
1.321 + params->iDefault = aArgs.At(i).AllocL();
1.322 + i = argsCount;
1.323 + }
1.324 + else if (cmd.CompareF(KLabel) == 0)
1.325 + {
1.326 + params->iLabel = aArgs.At(i).AllocL();
1.327 + }
1.328 + else if (cmd.CompareF(KPrivate) == 0)
1.329 + {
1.330 + params->iPrivate = aArgs.At(i).AllocL();
1.331 + }
1.332 + else if (cmd.CompareF(KStore) == 0)
1.333 + {
1.334 + TLex parser(aArgs.At(i));
1.335 + TInt err = parser.Val(params->iCertstoreIndex);
1.336 + params->iIsDetailed = ETrue;
1.337 + }
1.338 + else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
1.339 + {
1.340 + params->iIsDetailed = ETrue;
1.341 + params->iOwnerType = aArgs.At(i).AllocL();
1.342 + }
1.343 + else if (cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
1.344 + {
1.345 + params->iIsDetailed = ETrue;
1.346 + params->iIsDeletable = aArgs.At(i).AllocL();
1.347 + }
1.348 + continue;
1.349 + }
1.350 +
1.351 + if (VerifyCommand(aArgs.At(i), command, i))
1.352 + {
1.353 + continue;
1.354 + }
1.355 +
1.356 +
1.357 + if (i!=0)
1.358 + {
1.359 + if (aArgs.At(i)[0] == '-')
1.360 + {
1.361 + i = argsCount;
1.362 + command = CertToolDefController::KUsageCommand;
1.363 + continue;
1.364 + }
1.365 + delete params->iDefault;
1.366 + params->iDefault = NULL;
1.367 + params->iDefault = aArgs.At(i).AllocL();
1.368 + params->iIsDetailed = ETrue;
1.369 + }
1.370 + }
1.371 +
1.372 +
1.373 + if (command != -1)
1.374 + {
1.375 + TRAP_IGNORE(aController.HandleCommandL(command, params));
1.376 + }
1.377 + else
1.378 + {
1.379 + aController.HandleCommandL(CertToolDefController::KUsageCommand, params);
1.380 + }
1.381 + CleanupStack::PopAndDestroy(params);
1.382 + }
1.383 +
1.384 +
1.385 +/**
1.386 + * Parsing the command for non-interactive mode.
1.387 + */
1.388 +
1.389 +LOCAL_D void ParseCommandInNonInteractiveModeL(RFile& aFile, const CCommandLineArguments& aCmdArgs)
1.390 + {
1.391 +
1.392 + KeyToolUtils::SetFile(&aFile);
1.393 +
1.394 + CKeytoolFileView* view(0);
1.395 + view = CKeytoolFileView::NewLC(aCmdArgs.Arg(1));
1.396 + TInt cmdCount = view->SplitFileInputToArrayL();
1.397 +
1.398 + //For every command, parse and handle.
1.399 + for (TInt j = 0; j < cmdCount; j++)
1.400 + {
1.401 + CCertToolController* controller = CCertToolController::NewLC(*view);
1.402 +
1.403 + CArrayFixFlat<TPtrC>* args = view->ReadArrayArgumentsLC(j);
1.404 + ParseAndHandleCommandL(*args, *controller);
1.405 +
1.406 + CleanupStack::PopAndDestroy(2, controller);
1.407 + }
1.408 +
1.409 +
1.410 + CleanupStack::PopAndDestroy(view);
1.411 +
1.412 + }
1.413 +
1.414 +
1.415 +/**
1.416 + * Parsing the command for interactive mode.
1.417 + */
1.418 +
1.419 +
1.420 +LOCAL_D void ParseCommandInInteractiveModeL(CConsoleBase& aConsole, const CCommandLineArguments& aCmdArgs)
1.421 + {
1.422 + CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
1.423 + CleanupStack::PushL(args);
1.424 + CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(aConsole);
1.425 + CCertToolController* controller = CCertToolController::NewLC(*view);
1.426 +
1.427 + TInt cmdArgsCount = aCmdArgs.Count();
1.428 +
1.429 + KeyToolUtils::SetConsole(&aConsole);
1.430 + BoilerPlateL(&aConsole);
1.431 +
1.432 + for (TInt i = 0; i < cmdArgsCount; i++)
1.433 + {
1.434 + args->AppendL(aCmdArgs.Arg(i));
1.435 + }
1.436 +
1.437 + //Interactive mode can handle only one command at a time.
1.438 + ParseAndHandleCommandL(*args, *controller);
1.439 +
1.440 + // We are done!
1.441 + aConsole.Printf(KNewLine);
1.442 + aConsole.Printf(KDone);
1.443 + aConsole.Getch();
1.444 +
1.445 + CleanupStack::PopAndDestroy(3, args); // controller, view, args
1.446 +
1.447 + }
1.448 +
1.449 +/**
1.450 + * Parses the command line and given control to the handler to deal with the request.
1.451 + */
1.452 +LOCAL_D void DoMainL()
1.453 + {
1.454 +
1.455 + RFs fs;
1.456 + User::LeaveIfError(fs.Connect());
1.457 + CleanupClosePushL(fs);
1.458 + RFile file;
1.459 +
1.460 + CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
1.461 + CleanupStack::PushL(console);
1.462 + CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
1.463 + TInt cmdArgsCount = cmdArgs->Count();
1.464 +
1.465 +
1.466 + OperationMode currentMode = Interactive; //Interactive by default.
1.467 +
1.468 + // Determine the mode of operation as either interactive or non-interactive.
1.469 + if (cmdArgsCount == 3)
1.470 + {
1.471 + currentMode = ModeOfOperationL(*cmdArgs, fs, file);
1.472 + }
1.473 +
1.474 + switch(currentMode)
1.475 + {
1.476 + case Interactive:
1.477 + ParseCommandInInteractiveModeL(*console, *cmdArgs);
1.478 + break;
1.479 +
1.480 + case NonInteractive:
1.481 + //file refers to the output file name.
1.482 + CleanupClosePushL(file);
1.483 + ParseCommandInNonInteractiveModeL(file, *cmdArgs);
1.484 + CleanupStack::PopAndDestroy(&file);
1.485 + break;
1.486 + }
1.487 +
1.488 + CleanupStack::PopAndDestroy(3, &fs);
1.489 + }
1.490 +
1.491 +
1.492 +
1.493 +GLDEF_C TInt E32Main() // main function called by E32
1.494 + {
1.495 + __UHEAP_MARK;
1.496 + CTrapCleanup* cleanup=CTrapCleanup::New();
1.497 +
1.498 + TRAP_IGNORE(DoMainL());
1.499 +
1.500 + delete cleanup;
1.501 + __UHEAP_MARKEND;
1.502 + return 0;
1.503 + }
1.504 +