secureswitools/swianalysistoolkit/source/dumpswicertstoretool/dumpswicertstoretool.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2005-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 
       
    20 #include "dumpswicertstoretool.h"
       
    21 #include "options.h"
       
    22 #include "datalocator.h"
       
    23 #include "osinterface.h"
       
    24 
       
    25 #ifdef _MSC_VER
       
    26 #include <new>
       
    27 #include <new.h>
       
    28 #endif /* _MSC_VER */
       
    29 
       
    30 #ifdef _MSC_VER
       
    31 // House-keeping to allow ::new to throw rather than return NULL in MSVC++
       
    32 int throwingHandler(size_t)
       
    33 	{
       
    34 	throw std::bad_alloc();
       
    35 	return 0;
       
    36 	}
       
    37 #endif /* _MSC_VER */
       
    38 
       
    39 // entry point
       
    40 int main(int argc,char *argv[])
       
    41 	{
       
    42 #ifdef _MSC_VER
       
    43 	_set_new_handler (throwingHandler);	// Make ::new throw rather than return NULL
       
    44 #endif /* _MSC_VER */
       
    45 
       
    46 	Options* options = 0;
       
    47 	//if no arguments are specified,display help msg and exit.
       
    48 	if(argc < 2)
       
    49 		{
       
    50 		Options::DisplayUsage();
       
    51 		return 0;
       
    52 		}
       
    53 	try
       
    54 		{
       
    55 		options = new Options(argc, argv) ;
       
    56 		//Required in order to delete the der files which are present in the current directory.
       
    57 		string curDirectory = ".";
       
    58 		string extension = "der";
       
    59 		OSInterface::DeleteFilesFromDirectory(curDirectory , extension);
       
    60 		Tool.Run(*options);
       
    61 		}
       
    62 
       
    63 	catch (Exceptions aErr)
       
    64 		{
       
    65 		Options::DisplayError (aErr);
       
    66 		delete options;
       
    67 		return aErr;
       
    68 		}
       
    69 
       
    70 	catch (bad_alloc&)
       
    71 		{
       
    72 		cout << " Error Allocating Memory " << endl ;
       
    73 		}
       
    74 
       
    75 	delete options;
       
    76 	return 0;
       
    77 	}
       
    78 
       
    79 // Class DumpSWICertstoreTool
       
    80 class DumpSWICertstoreTool Tool;
       
    81 
       
    82 DumpSWICertstoreTool::DumpSWICertstoreTool()
       
    83 	:iTocRevision (0),
       
    84 	iErrors (0)
       
    85 	{
       
    86 	}
       
    87 
       
    88 void DumpSWICertstoreTool::Run(const Options& aOptions)
       
    89 	{
       
    90 	if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
    91 		{
       
    92 		ofstream iniFile("swicertstore.txt",ios::out);
       
    93 		iniFile.close();
       
    94 		}
       
    95 
       
    96 	//if both ROM and C:\Based certstore are specified.
       
    97 	if((aOptions.CBasedWritableCertstore()) && (aOptions.ROMCertstore()))
       
    98 		{
       
    99 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
   100 			{
       
   101 			cout << "Merged ROM Based and C:BASED Writable Certstore " << endl << endl;
       
   102 			}
       
   103 
       
   104 		//calls C:\Based certstore first
       
   105 		bool isCBasedValid = InitializeCBasedCertstore(aOptions);
       
   106 		if(!isCBasedValid)
       
   107 			{
       
   108 			cout << "C: Based Writable Certstore is not a Permanent File Store " << endl << endl;
       
   109 			}
       
   110 		
       
   111 		//ROM Based certstore called next.
       
   112 		bool isROMValid = InitializeROMCerstore(aOptions);
       
   113 		if(!isROMValid)
       
   114 			{
       
   115 			cout << (aOptions.ROMCertstoreFileName()).c_str() << " : Not a Permanent File Store " <<endl <<endl;
       
   116 			}
       
   117 		}
       
   118 
       
   119 	//when only ROM Based dat file specified.
       
   120 	else if (!(aOptions.CBasedWritableCertstore()) && (aOptions.ROMCertstore()))
       
   121 		{
       
   122 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
   123 			{
       
   124 			cout <<"ROM Based SwiCertStore :" << endl << endl;
       
   125 			}
       
   126 		bool isROMValid = InitializeROMCerstore(aOptions);
       
   127 		if(!isROMValid)
       
   128 			{
       
   129 			throw ENotAPermanentFileStore;;
       
   130 			}
       
   131 		}
       
   132 
       
   133 	//when only C:\Based Certstore is specified
       
   134 	else if((aOptions.CBasedWritableCertstore()) && !(aOptions.ROMCertstore()))
       
   135 		{
       
   136 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
   137 			{
       
   138 			cout << " C:\\ Based Writable SwiCertstore :" << endl << endl;
       
   139 			}
       
   140 		bool isCBasedValid = InitializeCBasedCertstore(aOptions);
       
   141 		if(!isCBasedValid)
       
   142 			{
       
   143 			throw ENotAPermanentFileStore;
       
   144 			}
       
   145 		}
       
   146 
       
   147 	if(aOptions.CreateCCIFile() || aOptions.CreateDetailCCIFile())
       
   148 		{
       
   149 		cout << "[CAPABILITIES]" <<endl;
       
   150 		cout << iIssuer <<endl;
       
   151 		cout << "[MANDATORY]" <<endl;
       
   152 		cout << iMandatory <<endl;
       
   153 		}
       
   154 	}
       
   155 
       
   156 bool DumpSWICertstoreTool::InitializeCBasedCertstore(const Options& aOptions)
       
   157 	{
       
   158 	StoreFile* store = 0;
       
   159 	int size = aOptions.GetDirectoryFiles().size();
       
   160 	if(size == 0)
       
   161 		{
       
   162 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
   163 			{
       
   164 			cout << "No files in the specified Writable Certstore" << endl << endl;
       
   165 			}
       
   166 		}
       
   167 
       
   168 	for(int i = size-1; i >= 0; i--)
       
   169 		{
       
   170 		string fileName = aOptions.GetDirectoryFiles().at(i);
       
   171 		store = new StoreFile();
       
   172 		bool isFileValid = store->CheckFileValidity(fileName.c_str(),aOptions);
       
   173 		//supposing the file is corrupt,the previous file is considered,else there will be only 1 file in the directory.
       
   174 		if(isFileValid)
       
   175 			{
       
   176 			Initialize(aOptions,*store);
       
   177 			delete store;
       
   178 			return true;
       
   179 			}
       
   180 		delete store;
       
   181 		}
       
   182 	
       
   183 	return false;
       
   184 	}
       
   185 
       
   186 bool DumpSWICertstoreTool::InitializeROMCerstore(const Options& aOptions)
       
   187 	{
       
   188 	StoreFile* store = 0;
       
   189 	store = new StoreFile();
       
   190 	bool isFileValid = store->CheckFileValidity((aOptions.ROMCertstoreFileName()).c_str(),aOptions);
       
   191 	if(isFileValid)
       
   192 		{
       
   193 		Initialize(aOptions,*store);
       
   194 		delete store;
       
   195 		return true;
       
   196 		}
       
   197 
       
   198 	delete store;
       
   199 	return false;
       
   200 	}
       
   201 
       
   202 void DumpSWICertstoreTool::Initialize(const Options& aOptions , StoreFile& aStore)
       
   203 	{
       
   204 	Stream root = aStore.RootStream();
       
   205 	DataLocator dataLoc(root, aStore.File());
       
   206 	StreamReader reader(dataLoc.GetStream());
       
   207 	reader.Initialise();
       
   208 	Handle metadataHandle(reader.ReadInt32());
       
   209 
       
   210 	Stream metadataStream = aStore.FindStream(metadataHandle);
       
   211 	SwiCertStoreMetaData  metadata(metadataStream, aStore);
       
   212 	metadata.Read(aOptions);
       
   213 	iIssuer.append(metadata.GetIssuer());
       
   214 	iMandatory.append(metadata.GetMandatoryStatusOfAllCertstoreCerts());
       
   215 	}
       
   216 
       
   217 ostream& DumpSWICertstoreTool::Error()
       
   218 	{
       
   219 	++Tool.iErrors;
       
   220 	return cerr << "error: ";
       
   221 	}
       
   222 
       
   223 ostream& DumpSWICertstoreTool::Warning()
       
   224 	{
       
   225 	++Tool.iErrors;
       
   226 	return cerr << "warning: ";
       
   227 	}
       
   228 
       
   229 DumpSWICertstoreTool::~DumpSWICertstoreTool()
       
   230 	{
       
   231 	}
       
   232 
       
   233 
       
   234 
       
   235 
       
   236 
       
   237