os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <e32cons.h>
    20 #include <bacline.h>
    21 
    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"
    27 
    28 
    29 // Boiler plate 
    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.");
    33 
    34 _LIT(KNewLine, "\n");
    35 
    36 _LIT(KDone, "Press any key to continue... \n");
    37 
    38 // CertTool command line parameters
    39 
    40 _LIT(KList, "-list");
    41 _LIT(KListShort, "-l");
    42 
    43 _LIT(KListStores, "-liststores");
    44 _LIT(KListStoresShort, "-ls");
    45 
    46 _LIT(KImport, "-import");
    47 _LIT(KImportShort, "-i");
    48 
    49 _LIT(KPrivate, "-private");
    50 
    51 
    52 _LIT(KSetApps, "-setapps");
    53 _LIT(KSetAppsShort, "-s");
    54 
    55 _LIT(KAddApps, "-addapps");
    56 _LIT(KAddAppsShort, "-a");
    57 
    58 _LIT(KApps, "-apps");
    59 
    60 _LIT(KRemoveApps, "-removeapps");
    61 
    62 _LIT(KRemove, "-remove");
    63 _LIT(KRemoveShort, "-r");
    64 
    65 // remove private key also while removing the certificate
    66 // which is applicable iff the key is imported using the certool -private option
    67 
    68 _LIT(KRemoveKeyAlso, "-rka");
    69 
    70 _LIT(KStore, "-store");
    71 
    72 _LIT(KHelp, "-help");
    73 _LIT(KHelpShort, "-h");
    74 
    75 // Command parameters
    76 _LIT(KLabel, "-label");
    77 
    78 _LIT(KDetails, "-details");
    79 _LIT(KDetailsShort, "-d");
    80 
    81 _LIT(KOwnerType, "-owner");
    82 _LIT(KOwnerTypeShort, "-o");
    83 
    84 _LIT(KPageWise, "-page");
    85 _LIT(KPageWiseShort, "-p");
    86 
    87 _LIT(KUids, "-uids");
    88 
    89 _LIT(KDeletable, "-deletable");
    90 _LIT(KDeletableShort, "-del");
    91 
    92 const TInt KMaxArgs = 10;
    93 
    94 /**
    95  * Certtool can operate in the following modes.
    96 **/
    97 enum OperationMode {
    98 Interactive,
    99 NonInteractive
   100 };
   101 
   102 
   103 /**
   104  * Displays tool name and copy-right informations.
   105  */
   106 LOCAL_D void BoilerPlateL(CConsoleBase* console) 
   107 	{
   108 	console->Printf(KNewLine);
   109 	console->Printf(KName);
   110 	console->Printf(KNewLine);	
   111 	console->Printf(KCopyright);
   112 	console->Printf(KNewLine);
   113 	console->Printf(KNewLine);	
   114 	}
   115 
   116 LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount)
   117 	{
   118 	if ((aCmdNum != -1) && (aCommand[0] == '-'))
   119 		{
   120 		aCmdNum = CertToolDefController::KUsageCommand;
   121 		aCmdCount = KMaxArgs;
   122 		return 1;
   123 		}
   124 	if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0)
   125 		{
   126 		aCmdNum = CertToolDefController::KListCommand;
   127 		}
   128 	else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0)
   129 		{
   130 		aCmdNum = CertToolDefController::KListStoresCommand;
   131 		}
   132 	else if	(aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0)
   133 		{
   134 		aCmdNum = CertToolDefController::KImportCommand;
   135 		}
   136 	else if	(aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0)
   137 		{
   138 		aCmdNum = CertToolDefController::KRemoveCommand;
   139 		}
   140 	else if	(aCommand.CompareF(KSetApps) == 0 || aCommand.Compare(KSetAppsShort) == 0)
   141 		{
   142 		aCmdNum = CertToolDefController::KSetAppsCommand;
   143 		}
   144 	else if	(aCommand.CompareF(KAddApps) == 0 || aCommand.Compare(KAddAppsShort) == 0)
   145 		{
   146 		aCmdNum = CertToolDefController::KAddAppsCommand;
   147 		}
   148 	else if	(aCommand.CompareF(KRemoveApps) == 0 )
   149 		{
   150 		aCmdNum = CertToolDefController::KRemoveAppsCommand;
   151 		}
   152 	else	
   153 		{
   154 		return 0;
   155 		}
   156 
   157 	return 1;
   158 	}
   159 	
   160 /**
   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.
   164  */
   165 
   166 LOCAL_D OperationMode ModeOfOperationL(const CCommandLineArguments& aCmdArgs, RFs& aFs, RFile& aFile)
   167     {
   168     OperationMode mode = Interactive;
   169     if (KeyToolUtils::DoesFileExistsL(aFs,aCmdArgs.Arg(1)))
   170         {
   171 		mode = NonInteractive;
   172         TInt error = aFile.Open(aFs, aCmdArgs.Arg(1), EFileRead|EFileShareAny);
   173         aFile.Close();
   174         
   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)
   179             {
   180             CleanupStack::PopAndDestroy(&aFile);
   181             mode = Interactive;
   182             }   
   183                     
   184          }
   185     return mode;
   186     }
   187 
   188 /**
   189  * The main parsing logic. Same for interactive and non-interactive modes.
   190  */
   191 LOCAL_D void ParseAndHandleCommandL(CArrayFixFlat<TPtrC>& aArgs, CCertToolController& aController)
   192     {
   193     CKeyToolParameters* params = CKeyToolParameters::NewLC();
   194             
   195     TInt command = -1; 
   196     TInt i = -1;
   197            
   198     TInt argsCount = aArgs.Count();
   199     while (i < (argsCount-1))
   200         {
   201         i++;
   202         if ((aArgs.At(i).CompareF(KDetails)==0)|| (aArgs.At(i).Compare(KDetailsShort)==0))
   203                     {
   204                     params->iIsDetailed = ETrue;
   205                     continue;
   206                     }
   207         
   208                 if (aArgs.At(i).CompareF(KPageWise)==0 || (aArgs.At(i).Compare(KPageWiseShort)==0))
   209                     {
   210                     i++;
   211                     params->iPageWise = ETrue;
   212                     continue;
   213                     }
   214 
   215                 if (aArgs.At(i).Compare(KRemoveKeyAlso)==0)
   216                     {
   217                     params->iRemoveKey = ETrue;
   218                     continue;
   219                     }
   220 
   221                 if (aArgs.At(i).CompareF(KApps)==0)
   222                     {
   223                     i++;
   224                     RArray<TUid> apps;
   225                     TInt k = 0;
   226                     for (k = i; k < argsCount; k++)
   227                         {               
   228                         if (aArgs.At(k).Find(_L("-")) == KErrNotFound)
   229                             {
   230                             TUint uid;
   231                             if (aArgs.At(k).CompareF(KSWInstall)==0)
   232                                 {
   233                                 uid = swinstalluid;
   234                                 }
   235                             else
   236                                 {
   237                                 if (aArgs.At(k).CompareF(KSWInstallOCSP)==0)
   238                                     {
   239                                     uid = swinstallocspuid;
   240                                     }
   241                                 else
   242                                     {
   243                                     if (aArgs.At(k).CompareF(KMidletInstall)==0)
   244                                         {
   245                                         uid = midletinstalluid;
   246                                         }
   247                                     else
   248                                         {
   249                                         if (aArgs.At(k).CompareF(KTls)==0)
   250                                             {
   251                                             uid = tlsuid;
   252                                             }
   253                                         else
   254                                             {
   255                                             // no more valid apps, break cycle
   256                                             break;
   257                                             }
   258                                         }
   259                                     }
   260                                 }
   261                             apps.Append(TUid::Uid(uid));                        
   262                             }
   263                         else 
   264                             {
   265                             // We parsed all UIDs, break the cycle and go on!
   266                             break;
   267                             }
   268                         }
   269                     i = k-1;
   270                     params->iUIDs = apps; // We pass on ownership
   271                     params->iIsDetailed = ETrue;
   272                     continue;           
   273                     }
   274             
   275                 if (aArgs.At(i).CompareF(KUids)==0)
   276                     {
   277                     i++;
   278                     RArray<TUid> uids;
   279                     TInt k = 0;
   280                     for (k = i; k < argsCount; k++)
   281                         {               
   282                         if (aArgs.At(k).Left(2) == _L("0x"))
   283                             {
   284                             TLex lex(aArgs.At(k).Mid(2));       
   285                             TUint uid =0;
   286                             TInt err = lex.Val(uid, EHex);
   287                             if (err == KErrNone)
   288                                 {
   289                                 params->iUIDs.Append(TUid::Uid(uid));                       
   290                                 }            
   291                             }
   292                         else 
   293                             {
   294                             // We parsed all UIDs, break the cycle and go on!
   295                             break;
   296                             }
   297                         }
   298                     i = k-1;
   299                     params->iIsDetailed = ETrue;
   300                     continue;           
   301                     }
   302                 
   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)
   309                     {
   310                     i++;
   311                     if (i >= argsCount || aArgs.At(i)[0] == '-')
   312                         {
   313                         i = argsCount;
   314                         command = CertToolDefController::KUsageCommand;
   315                         }
   316                     else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
   317                         {
   318                         params->iDefault = aArgs.At(i).AllocL();
   319                         i = argsCount;  
   320                         }
   321                     else if (cmd.CompareF(KLabel) == 0)
   322                         {
   323                         params->iLabel = aArgs.At(i).AllocL();
   324                         }
   325                     else if (cmd.CompareF(KPrivate) == 0)
   326                         {
   327                         params->iPrivate = aArgs.At(i).AllocL();
   328                         }
   329                     else if (cmd.CompareF(KStore) == 0)
   330                         {
   331                         TLex parser(aArgs.At(i));
   332                         TInt err = parser.Val(params->iCertstoreIndex);
   333                         params->iIsDetailed = ETrue;
   334                         }
   335                     else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
   336                         {
   337                         params->iIsDetailed = ETrue;
   338                         params->iOwnerType = aArgs.At(i).AllocL();
   339                         }
   340                     else if (cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
   341                         {
   342                         params->iIsDetailed = ETrue;
   343                         params->iIsDeletable = aArgs.At(i).AllocL();
   344                         }
   345                     continue;
   346                     }
   347         
   348                 if (VerifyCommand(aArgs.At(i), command, i))
   349                     {
   350                     continue;
   351                     }
   352 
   353                     
   354                 if (i!=0) 
   355                     {
   356                     if (aArgs.At(i)[0] == '-')
   357                         {
   358                         i = argsCount;
   359                         command = CertToolDefController::KUsageCommand;
   360                         continue;
   361                         }
   362                     delete params->iDefault;
   363                     params->iDefault = NULL;
   364                     params->iDefault = aArgs.At(i).AllocL();
   365                     params->iIsDetailed = ETrue;            
   366                     }
   367                 } 
   368              
   369              
   370             if (command != -1)
   371                 {
   372                 TRAP_IGNORE(aController.HandleCommandL(command, params));
   373                 }
   374             else 
   375                 {
   376                 aController.HandleCommandL(CertToolDefController::KUsageCommand, params);       
   377                 }
   378             CleanupStack::PopAndDestroy(params);
   379             }
   380 
   381 
   382 /**
   383  * Parsing the command for non-interactive mode.
   384  */
   385 
   386 LOCAL_D void ParseCommandInNonInteractiveModeL(RFile& aFile, const CCommandLineArguments& aCmdArgs)
   387     {
   388   
   389     KeyToolUtils::SetFile(&aFile);
   390     
   391     CKeytoolFileView* view(0); 
   392     view = CKeytoolFileView::NewLC(aCmdArgs.Arg(1));
   393     TInt cmdCount = view->SplitFileInputToArrayL();
   394     
   395     //For every command, parse and handle.
   396     for (TInt j = 0; j < cmdCount; j++)
   397         {
   398         CCertToolController* controller = CCertToolController::NewLC(*view);
   399         
   400         CArrayFixFlat<TPtrC>* args = view->ReadArrayArgumentsLC(j);
   401         ParseAndHandleCommandL(*args, *controller);
   402         
   403         CleanupStack::PopAndDestroy(2, controller);
   404         }
   405         
   406     
   407     CleanupStack::PopAndDestroy(view);
   408     
   409     }
   410 
   411 
   412 /**
   413  * Parsing the command for interactive mode.
   414  */
   415 
   416 
   417 LOCAL_D void ParseCommandInInteractiveModeL(CConsoleBase& aConsole, const CCommandLineArguments& aCmdArgs)
   418     {
   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);
   423 
   424     TInt cmdArgsCount = aCmdArgs.Count();
   425     
   426     KeyToolUtils::SetConsole(&aConsole);
   427     BoilerPlateL(&aConsole);
   428     
   429     for (TInt i = 0; i < cmdArgsCount; i++)
   430         {
   431         args->AppendL(aCmdArgs.Arg(i));
   432         }
   433     
   434     //Interactive mode can handle only one command at a time.
   435     ParseAndHandleCommandL(*args, *controller); 
   436     
   437     // We are done!
   438     aConsole.Printf(KNewLine);
   439     aConsole.Printf(KDone);
   440     aConsole.Getch(); 
   441     
   442     CleanupStack::PopAndDestroy(3, args); // controller, view, args
   443     
   444     }
   445 
   446 /**
   447  * Parses the command line and given control to the handler to deal with the request.
   448  */
   449 LOCAL_D void DoMainL() 
   450 	{
   451 
   452 	RFs fs;
   453 	User::LeaveIfError(fs.Connect());
   454 	CleanupClosePushL(fs);
   455 	RFile file;
   456 	
   457 	CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
   458 	CleanupStack::PushL(console);
   459 	CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
   460 	TInt cmdArgsCount = cmdArgs->Count();
   461 	
   462 	
   463 	OperationMode currentMode = Interactive; //Interactive by default.
   464 	
   465 	// Determine the mode of operation as either interactive or non-interactive.
   466 	if (cmdArgsCount == 3)
   467 		{
   468 		currentMode = ModeOfOperationL(*cmdArgs, fs, file);
   469 		}
   470 		
   471 	switch(currentMode)
   472 	    {
   473 	    case Interactive:
   474 	        ParseCommandInInteractiveModeL(*console, *cmdArgs);
   475 	        break;
   476 	        
   477 	    case NonInteractive:
   478 	        //file refers to the output file name.
   479 	        CleanupClosePushL(file);
   480 	        ParseCommandInNonInteractiveModeL(file, *cmdArgs);
   481 	        CleanupStack::PopAndDestroy(&file);
   482 	        break;
   483 	    }
   484 
   485 	CleanupStack::PopAndDestroy(3, &fs);
   486 	}
   487 
   488 	
   489 
   490 GLDEF_C TInt E32Main()         // main function called by E32
   491    	{
   492 	__UHEAP_MARK;
   493 	CTrapCleanup* cleanup=CTrapCleanup::New(); 
   494 	
   495 	TRAP_IGNORE(DoMainL());
   496 	
   497 	delete cleanup; 
   498 	__UHEAP_MARKEND;
   499 	return 0; 
   500    	}
   501