sl@0: /* sl@0: * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "keytool_utils.h" sl@0: #include "keytool_view_imp.h" sl@0: #include "keytool_commands.h" sl@0: #include "certtool_controller.h" sl@0: #include "keytoolfileview.h" sl@0: sl@0: sl@0: // Boiler plate sl@0: _LIT(KShortName, "Symbian OS CertTool"); sl@0: _LIT(KName, "Symbian OS CertStore Manipulation Tool"); sl@0: _LIT(KCopyright, "Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved."); sl@0: sl@0: _LIT(KNewLine, "\n"); sl@0: sl@0: _LIT(KDone, "Press any key to continue... \n"); sl@0: sl@0: // CertTool command line parameters sl@0: sl@0: _LIT(KList, "-list"); sl@0: _LIT(KListShort, "-l"); sl@0: sl@0: _LIT(KListStores, "-liststores"); sl@0: _LIT(KListStoresShort, "-ls"); sl@0: sl@0: _LIT(KImport, "-import"); sl@0: _LIT(KImportShort, "-i"); sl@0: sl@0: _LIT(KPrivate, "-private"); sl@0: sl@0: sl@0: _LIT(KSetApps, "-setapps"); sl@0: _LIT(KSetAppsShort, "-s"); sl@0: sl@0: _LIT(KAddApps, "-addapps"); sl@0: _LIT(KAddAppsShort, "-a"); sl@0: sl@0: _LIT(KApps, "-apps"); sl@0: sl@0: _LIT(KRemoveApps, "-removeapps"); sl@0: sl@0: _LIT(KRemove, "-remove"); sl@0: _LIT(KRemoveShort, "-r"); sl@0: sl@0: // remove private key also while removing the certificate sl@0: // which is applicable iff the key is imported using the certool -private option sl@0: sl@0: _LIT(KRemoveKeyAlso, "-rka"); sl@0: sl@0: _LIT(KStore, "-store"); sl@0: sl@0: _LIT(KHelp, "-help"); sl@0: _LIT(KHelpShort, "-h"); sl@0: sl@0: // Command parameters sl@0: _LIT(KLabel, "-label"); sl@0: sl@0: _LIT(KDetails, "-details"); sl@0: _LIT(KDetailsShort, "-d"); sl@0: sl@0: _LIT(KOwnerType, "-owner"); sl@0: _LIT(KOwnerTypeShort, "-o"); sl@0: sl@0: _LIT(KPageWise, "-page"); sl@0: _LIT(KPageWiseShort, "-p"); sl@0: sl@0: _LIT(KUids, "-uids"); sl@0: sl@0: _LIT(KDeletable, "-deletable"); sl@0: _LIT(KDeletableShort, "-del"); sl@0: sl@0: const TInt KMaxArgs = 10; sl@0: sl@0: /** sl@0: * Certtool can operate in the following modes. sl@0: **/ sl@0: enum OperationMode { sl@0: Interactive, sl@0: NonInteractive sl@0: }; sl@0: sl@0: sl@0: /** sl@0: * Displays tool name and copy-right informations. sl@0: */ sl@0: LOCAL_D void BoilerPlateL(CConsoleBase* console) sl@0: { sl@0: console->Printf(KNewLine); sl@0: console->Printf(KName); sl@0: console->Printf(KNewLine); sl@0: console->Printf(KCopyright); sl@0: console->Printf(KNewLine); sl@0: console->Printf(KNewLine); sl@0: } sl@0: sl@0: LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount) sl@0: { sl@0: if ((aCmdNum != -1) && (aCommand[0] == '-')) sl@0: { sl@0: aCmdNum = CertToolDefController::KUsageCommand; sl@0: aCmdCount = KMaxArgs; sl@0: return 1; sl@0: } sl@0: if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KListCommand; sl@0: } sl@0: else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KListStoresCommand; sl@0: } sl@0: else if (aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KImportCommand; sl@0: } sl@0: else if (aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KRemoveCommand; sl@0: } sl@0: else if (aCommand.CompareF(KSetApps) == 0 || aCommand.Compare(KSetAppsShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KSetAppsCommand; sl@0: } sl@0: else if (aCommand.CompareF(KAddApps) == 0 || aCommand.Compare(KAddAppsShort) == 0) sl@0: { sl@0: aCmdNum = CertToolDefController::KAddAppsCommand; sl@0: } sl@0: else if (aCommand.CompareF(KRemoveApps) == 0 ) sl@0: { sl@0: aCmdNum = CertToolDefController::KRemoveAppsCommand; sl@0: } sl@0: else sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /** sl@0: * Returns the mode in which the tool would operate. If the command is invoked sl@0: * with 2 parameters(certool inputFile outputFile), the tool works in non-interactive sl@0: * mode else the interactive mode is chosen. sl@0: */ sl@0: sl@0: LOCAL_D OperationMode ModeOfOperationL(const CCommandLineArguments& aCmdArgs, RFs& aFs, RFile& aFile) sl@0: { sl@0: OperationMode mode = Interactive; sl@0: if (KeyToolUtils::DoesFileExistsL(aFs,aCmdArgs.Arg(1))) sl@0: { sl@0: mode = NonInteractive; sl@0: TInt error = aFile.Open(aFs, aCmdArgs.Arg(1), EFileRead|EFileShareAny); sl@0: aFile.Close(); sl@0: sl@0: TInt error1 = aFile.Replace(aFs, aCmdArgs.Arg(2), EFileWrite|EFileShareExclusive); sl@0: // If the input file doesn't exist or not able to create outputfile sl@0: // switch to Interactive mode sl@0: if (error != KErrNone || error1 != KErrNone) sl@0: { sl@0: CleanupStack::PopAndDestroy(&aFile); sl@0: mode = Interactive; sl@0: } sl@0: sl@0: } sl@0: return mode; sl@0: } sl@0: sl@0: /** sl@0: * The main parsing logic. Same for interactive and non-interactive modes. sl@0: */ sl@0: LOCAL_D void ParseAndHandleCommandL(CArrayFixFlat& aArgs, CCertToolController& aController) sl@0: { sl@0: CKeyToolParameters* params = CKeyToolParameters::NewLC(); sl@0: sl@0: TInt command = -1; sl@0: TInt i = -1; sl@0: sl@0: TInt argsCount = aArgs.Count(); sl@0: while (i < (argsCount-1)) sl@0: { sl@0: i++; sl@0: if ((aArgs.At(i).CompareF(KDetails)==0)|| (aArgs.At(i).Compare(KDetailsShort)==0)) sl@0: { sl@0: params->iIsDetailed = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: if (aArgs.At(i).CompareF(KPageWise)==0 || (aArgs.At(i).Compare(KPageWiseShort)==0)) sl@0: { sl@0: i++; sl@0: params->iPageWise = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: if (aArgs.At(i).Compare(KRemoveKeyAlso)==0) sl@0: { sl@0: params->iRemoveKey = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: if (aArgs.At(i).CompareF(KApps)==0) sl@0: { sl@0: i++; sl@0: RArray apps; sl@0: TInt k = 0; sl@0: for (k = i; k < argsCount; k++) sl@0: { sl@0: if (aArgs.At(k).Find(_L("-")) == KErrNotFound) sl@0: { sl@0: TUint uid; sl@0: if (aArgs.At(k).CompareF(KSWInstall)==0) sl@0: { sl@0: uid = swinstalluid; sl@0: } sl@0: else sl@0: { sl@0: if (aArgs.At(k).CompareF(KSWInstallOCSP)==0) sl@0: { sl@0: uid = swinstallocspuid; sl@0: } sl@0: else sl@0: { sl@0: if (aArgs.At(k).CompareF(KMidletInstall)==0) sl@0: { sl@0: uid = midletinstalluid; sl@0: } sl@0: else sl@0: { sl@0: if (aArgs.At(k).CompareF(KTls)==0) sl@0: { sl@0: uid = tlsuid; sl@0: } sl@0: else sl@0: { sl@0: // no more valid apps, break cycle sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: apps.Append(TUid::Uid(uid)); sl@0: } sl@0: else sl@0: { sl@0: // We parsed all UIDs, break the cycle and go on! sl@0: break; sl@0: } sl@0: } sl@0: i = k-1; sl@0: params->iUIDs = apps; // We pass on ownership sl@0: params->iIsDetailed = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: if (aArgs.At(i).CompareF(KUids)==0) sl@0: { sl@0: i++; sl@0: RArray uids; sl@0: TInt k = 0; sl@0: for (k = i; k < argsCount; k++) sl@0: { sl@0: if (aArgs.At(k).Left(2) == _L("0x")) sl@0: { sl@0: TLex lex(aArgs.At(k).Mid(2)); sl@0: TUint uid =0; sl@0: TInt err = lex.Val(uid, EHex); sl@0: if (err == KErrNone) sl@0: { sl@0: params->iUIDs.Append(TUid::Uid(uid)); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // We parsed all UIDs, break the cycle and go on! sl@0: break; sl@0: } sl@0: } sl@0: i = k-1; sl@0: params->iIsDetailed = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: TDesC& cmd = aArgs.At(i); sl@0: if (cmd.CompareF(KLabel) == 0 || sl@0: cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 || sl@0: cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 || sl@0: cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 || sl@0: cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0) sl@0: { sl@0: i++; sl@0: if (i >= argsCount || aArgs.At(i)[0] == '-') sl@0: { sl@0: i = argsCount; sl@0: command = CertToolDefController::KUsageCommand; sl@0: } sl@0: else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0) sl@0: { sl@0: params->iDefault = aArgs.At(i).AllocL(); sl@0: i = argsCount; sl@0: } sl@0: else if (cmd.CompareF(KLabel) == 0) sl@0: { sl@0: params->iLabel = aArgs.At(i).AllocL(); sl@0: } sl@0: else if (cmd.CompareF(KPrivate) == 0) sl@0: { sl@0: params->iPrivate = aArgs.At(i).AllocL(); sl@0: } sl@0: else if (cmd.CompareF(KStore) == 0) sl@0: { sl@0: TLex parser(aArgs.At(i)); sl@0: TInt err = parser.Val(params->iCertstoreIndex); sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0) sl@0: { sl@0: params->iIsDetailed = ETrue; sl@0: params->iOwnerType = aArgs.At(i).AllocL(); sl@0: } sl@0: else if (cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0) sl@0: { sl@0: params->iIsDetailed = ETrue; sl@0: params->iIsDeletable = aArgs.At(i).AllocL(); sl@0: } sl@0: continue; sl@0: } sl@0: sl@0: if (VerifyCommand(aArgs.At(i), command, i)) sl@0: { sl@0: continue; sl@0: } sl@0: sl@0: sl@0: if (i!=0) sl@0: { sl@0: if (aArgs.At(i)[0] == '-') sl@0: { sl@0: i = argsCount; sl@0: command = CertToolDefController::KUsageCommand; sl@0: continue; sl@0: } sl@0: delete params->iDefault; sl@0: params->iDefault = NULL; sl@0: params->iDefault = aArgs.At(i).AllocL(); sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: } sl@0: sl@0: sl@0: if (command != -1) sl@0: { sl@0: TRAP_IGNORE(aController.HandleCommandL(command, params)); sl@0: } sl@0: else sl@0: { sl@0: aController.HandleCommandL(CertToolDefController::KUsageCommand, params); sl@0: } sl@0: CleanupStack::PopAndDestroy(params); sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Parsing the command for non-interactive mode. sl@0: */ sl@0: sl@0: LOCAL_D void ParseCommandInNonInteractiveModeL(RFile& aFile, const CCommandLineArguments& aCmdArgs) sl@0: { sl@0: sl@0: KeyToolUtils::SetFile(&aFile); sl@0: sl@0: CKeytoolFileView* view(0); sl@0: view = CKeytoolFileView::NewLC(aCmdArgs.Arg(1)); sl@0: TInt cmdCount = view->SplitFileInputToArrayL(); sl@0: sl@0: //For every command, parse and handle. sl@0: for (TInt j = 0; j < cmdCount; j++) sl@0: { sl@0: CCertToolController* controller = CCertToolController::NewLC(*view); sl@0: sl@0: CArrayFixFlat* args = view->ReadArrayArgumentsLC(j); sl@0: ParseAndHandleCommandL(*args, *controller); sl@0: sl@0: CleanupStack::PopAndDestroy(2, controller); sl@0: } sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(view); sl@0: sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Parsing the command for interactive mode. sl@0: */ sl@0: sl@0: sl@0: LOCAL_D void ParseCommandInInteractiveModeL(CConsoleBase& aConsole, const CCommandLineArguments& aCmdArgs) sl@0: { sl@0: CArrayFixFlat* args = new (ELeave) CArrayFixFlat (10); sl@0: CleanupStack::PushL(args); sl@0: CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(aConsole); sl@0: CCertToolController* controller = CCertToolController::NewLC(*view); sl@0: sl@0: TInt cmdArgsCount = aCmdArgs.Count(); sl@0: sl@0: KeyToolUtils::SetConsole(&aConsole); sl@0: BoilerPlateL(&aConsole); sl@0: sl@0: for (TInt i = 0; i < cmdArgsCount; i++) sl@0: { sl@0: args->AppendL(aCmdArgs.Arg(i)); sl@0: } sl@0: sl@0: //Interactive mode can handle only one command at a time. sl@0: ParseAndHandleCommandL(*args, *controller); sl@0: sl@0: // We are done! sl@0: aConsole.Printf(KNewLine); sl@0: aConsole.Printf(KDone); sl@0: aConsole.Getch(); sl@0: sl@0: CleanupStack::PopAndDestroy(3, args); // controller, view, args sl@0: sl@0: } sl@0: sl@0: /** sl@0: * Parses the command line and given control to the handler to deal with the request. sl@0: */ sl@0: LOCAL_D void DoMainL() sl@0: { sl@0: sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: RFile file; sl@0: sl@0: CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen)); sl@0: CleanupStack::PushL(console); sl@0: CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC(); sl@0: TInt cmdArgsCount = cmdArgs->Count(); sl@0: sl@0: sl@0: OperationMode currentMode = Interactive; //Interactive by default. sl@0: sl@0: // Determine the mode of operation as either interactive or non-interactive. sl@0: if (cmdArgsCount == 3) sl@0: { sl@0: currentMode = ModeOfOperationL(*cmdArgs, fs, file); sl@0: } sl@0: sl@0: switch(currentMode) sl@0: { sl@0: case Interactive: sl@0: ParseCommandInInteractiveModeL(*console, *cmdArgs); sl@0: break; sl@0: sl@0: case NonInteractive: sl@0: //file refers to the output file name. sl@0: CleanupClosePushL(file); sl@0: ParseCommandInNonInteractiveModeL(file, *cmdArgs); sl@0: CleanupStack::PopAndDestroy(&file); sl@0: break; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, &fs); sl@0: } sl@0: sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() // main function called by E32 sl@0: { sl@0: __UHEAP_MARK; sl@0: CTrapCleanup* cleanup=CTrapCleanup::New(); sl@0: sl@0: TRAP_IGNORE(DoMainL()); sl@0: sl@0: delete cleanup; sl@0: __UHEAP_MARKEND; sl@0: return 0; sl@0: } sl@0: