secureswitools/swianalysistoolkit/source/dumpswicertstoretool/swicertstoremetadata.cpp
changeset 0 ba25891c3a9e
child 25 7333d7932ef7
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 "swicertstoremetadata.h"
       
    21 #include "datalocator.h"
       
    22 
       
    23 using namespace std;
       
    24 const int KMandatory = 0x00;
       
    25 const int KSystemUpgrade = 0x01;
       
    26 
       
    27 SwiCertStoreMetaData ::SwiCertStoreMetaData (Stream& aStream, StoreFile& aStore)
       
    28 	:iStream(aStream), 
       
    29 	iStore(aStore)
       
    30 	{
       
    31 	}
       
    32 
       
    33 void SwiCertStoreMetaData ::Read(const Options& aOptions)
       
    34 	{
       
    35 	iCount++;
       
    36 	DataLocator dataLoc(iStream, iStore.File());
       
    37 	StreamReader reader(dataLoc.GetStream());
       
    38 	reader.Initialise();
       
    39 	int certCount = reader.ReadInt32();
       
    40 	for (int j = 0; j < certCount; ++j)
       
    41 		{
       
    42 		iAppId.clear();
       
    43 		reader.ReadInt8();	
       
    44 		int certSize = reader.ReadInt32(); // certificate size
       
    45 
       
    46 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
    47 			{
       
    48 			if(!certSize)
       
    49 				{
       
    50 				cout << "warning : " << " Empty Certificate " << endl; 
       
    51 				}
       
    52 			}
       
    53 
       
    54 		bool isWide;
       
    55 		int certLabelLen = reader.GetDescriptorInfo(isWide, true);	// certificate label length
       
    56 		char* buf = reader.ReadDescriptor(); 		// certificate label
       
    57 		string certName((const char*)buf , certLabelLen);
       
    58 		iCertName = certName;
       
    59 		iCertTag = iCertName;
       
    60 		delete [] buf;
       
    61 		iCertName.append(".der");
       
    62 			
       
    63 		unsigned int certId = reader.ReadInt32(); // certificate ID
       
    64 		unsigned char certOwnerType = reader.ReadInt8(); // certificate owner type(should be only CA certificate)
       
    65 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
    66 			{
       
    67 			if(certOwnerType)							//for CA certificate certOwnerType is 0.
       
    68 				{
       
    69 				cout << "warning : " << "Not a CA certificate " << endl;
       
    70 				}
       
    71 			}
       
    72 		
       
    73 		buf = reader.ReadDescriptor(); // subject key ID
       
    74 		delete [] buf;
       
    75 		
       
    76 		buf = reader.ReadDescriptor(); // issuer key ID
       
    77 		delete [] buf;
       
    78 		
       
    79 		int apps = reader.ReadInt32(); // number of applications
       
    80 	
       
    81 		for (int i = 0; i < apps; ++i)
       
    82 			{
       
    83 			unsigned int app = reader.ReadInt32(); // application UID
       
    84 			iAppId.push_back(app);
       
    85 			}
       
    86 		
       
    87 		unsigned char trustStatus = reader.ReadInt8(); // trusted flag
       
    88 		if(!aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile())
       
    89 			{
       
    90 			if(!trustStatus)
       
    91 				{
       
    92 				cout << "warning : " << iCertTag << ": Untrusted  certificate " <<endl;
       
    93 				}
       
    94 			}
       
    95 			
       
    96 		int streamId = reader.ReadInt32(); // data stream ID
       
    97 		
       
    98 		int capsLength = reader.GetDescriptorInfo(isWide, true);
       
    99 
       
   100 		buf = reader.ReadDescriptor(); // capabilities buffer
       
   101 		iFileWrite = "\0";
       
   102 		if( aOptions.CreateDetailCCIFile() )
       
   103 			{
       
   104 			iCertCapabilities = ExtractDetailCapabilities(buf,capsLength);
       
   105 			}
       
   106 		else
       
   107 			{
       
   108 			iCertCapabilities = ExtractCapabilities(buf,capsLength);
       
   109 			}
       
   110 		
       
   111 		delete [] buf;
       
   112 		
       
   113 		// This value contains both mandatory and system upgrade values
       
   114 		iValue = reader.ReadInt8();
       
   115 		bool isDisplay = ExtractCertificate(iCertName , aOptions , streamId , iValue);
       
   116 		if( !aOptions.CreateCCIFile() && !aOptions.CreateDetailCCIFile() && isDisplay )
       
   117 			{
       
   118 			Display(aOptions);
       
   119 			}
       
   120 		}
       
   121 	}
       
   122 
       
   123 const string SwiCertStoreMetaData ::ExtractCapabilities(char* aCapability, int aCapsLength)
       
   124    	{
       
   125    	string capability;
       
   126   	int cap = 0;
       
   127    	int unknownIndex = 1;
       
   128   	char CapsString[10];
       
   129    	for(int j=0; j<aCapsLength; j++)
       
   130    		{
       
   131    		if(*aCapability & 0x01)
       
   132    			{
       
   133    			if(j < KNumberOfCaps)
       
   134    				{
       
   135    				capability.append("capability = ");
       
   136    				capability.append(CapabilityList[j]);
       
   137    				capability.append("\n"); 
       
   138    				*aCapability = *aCapability >> 1;
       
   139    				}
       
   140    			else
       
   141    				{
       
   142    				capability.append("capability= ");
       
   143    				capability.append("Unknown"+unknownIndex);
       
   144    				unknownIndex++;
       
   145    				}
       
   146   			cap += pow(2,j);
       
   147    			}
       
   148    		else
       
   149    			{
       
   150    			*aCapability = *aCapability >> 1;
       
   151    			}
       
   152    
       
   153    		if((j+1)%(sizeof(aCapability)*2) == 0)
       
   154    			{
       
   155    			aCapability ++;
       
   156    			}
       
   157    		}
       
   158   	sprintf(CapsString,"%d",cap);
       
   159   	iFileWrite.append(CapsString);
       
   160   	iFileWrite.append("\n");	
       
   161 
       
   162    	return capability;
       
   163    	}
       
   164 
       
   165 
       
   166 const string SwiCertStoreMetaData ::ExtractDetailCapabilities(char* aCapability, int aCapsLength)
       
   167 	{
       
   168 	string capability;
       
   169 	int unknownIndex = 1;
       
   170 	
       
   171 	for(int j=0; j<aCapsLength; j++)
       
   172 		{
       
   173 		if(*aCapability & 0x01)
       
   174 			{
       
   175 			if(j < KNumberOfCaps)
       
   176 				{
       
   177 				capability.append("capability = ");
       
   178 				capability.append(CapabilityList[j]);
       
   179 				capability.append("\n"); 
       
   180 				*aCapability = *aCapability >> 1;
       
   181 				}
       
   182 			else
       
   183 				{
       
   184 				capability.append("capability= ");
       
   185 				capability.append("Unknown"+unknownIndex);
       
   186 				unknownIndex++;
       
   187 				}
       
   188 			}
       
   189 		else
       
   190 			{
       
   191 			*aCapability = *aCapability >> 1;
       
   192 			}
       
   193 
       
   194 		if((j+1)%(sizeof(aCapability)*2) == 0)
       
   195 			{
       
   196 			aCapability ++;
       
   197 			}
       
   198 		}
       
   199 	
       
   200 	iFileWrite.append(capability);
       
   201 	iFileWrite.append("\n");
       
   202 	return capability;
       
   203 	}
       
   204 
       
   205 void SwiCertStoreMetaData::Display(const Options& aOptions)
       
   206 	{
       
   207 	string iniFileWrite,appID;
       
   208 	for (vector<unsigned int>::iterator it = iAppId.begin(); it!= iAppId.end();++it)
       
   209 		{
       
   210 		if (*it == ESwInstallUid)
       
   211 			{
       
   212 			appID.append("application = SWInstall\n");
       
   213 			}
       
   214 		if (*it == ESwinstallOcspUid)
       
   215 			{
       
   216 			appID.append("application = SWInstallOCSP\n");
       
   217 			}
       
   218 		}
       
   219 
       
   220 	int value = (int)iValue;
       
   221 	int isMandatory = !!( value & (1 << KMandatory)); 
       
   222 	int isSystemUpgrade = !!(value & (1 << KSystemUpgrade));
       
   223 
       
   224 	char buf[2],buf1[2];
       
   225 	sprintf(buf,"%d",isMandatory);
       
   226 	sprintf(buf1,"%d",isSystemUpgrade);
       
   227 	string mandatory = 	buf;
       
   228 	string systemUpgrade = buf1;
       
   229 	
       
   230 	iniFileWrite = "\n["+iCertTag+"]\n\nfile = "+ iCertName+"\n"+iCertCapabilities+appID+"Mandatory = "+mandatory+"\n"+"SystemUpgrade = "+systemUpgrade+"\n";
       
   231 	cout << iniFileWrite << endl;
       
   232 	ofstream iniFile("swicertstore.txt",ios::app);
       
   233 	iniFile.write(iniFileWrite.c_str(),iniFileWrite.length());
       
   234 	iniFile.close();
       
   235 	}
       
   236 
       
   237 bool SwiCertStoreMetaData::ExtractCertificate(std::string& aCertLabel,const Options& aOptions,int aStreamId,unsigned char aMandatory)
       
   238 	{
       
   239 	Handle handle(aStreamId);
       
   240 	Stream stream = iStore.FindStream(handle);
       
   241 	Certificate cert(stream, iStore);
       
   242 	bool isDisplay = cert.Read(aCertLabel.c_str(),aOptions);
       
   243 	
       
   244 	const char* buf = cert.GetIssuerName().c_str();
       
   245 	char* issuername = strstr(buf ,"/CN");
       
   246 	if (issuername)
       
   247 		{
       
   248 		issuername += 4;
       
   249 		}
       
   250 	else
       
   251 		{
       
   252 		issuername = strstr (buf, "/");
       
   253 		if (issuername)
       
   254 			{
       
   255 			issuername += 3;
       
   256 			}
       
   257 		}
       
   258 	if (issuername)
       
   259 		{
       
   260 		char* endstr = strchr (issuername, '/');
       
   261 		if (endstr)
       
   262 			{
       
   263 			*endstr = 0;
       
   264 			}
       
   265 		}
       
   266 
       
   267 	if(isDisplay)
       
   268 		{
       
   269 		iIssuer.append(issuername);
       
   270 		iIssuer.append("\n");
       
   271 		iIssuer.append("----------------------");
       
   272 		iIssuer.append("\n");
       
   273 		iIssuer.append(iFileWrite);
       
   274 		iMandatoryCert.append(issuername);
       
   275 		iMandatoryCert.append(" = ");
       
   276 
       
   277 		char mandatoryStr[2];
       
   278 		sprintf(mandatoryStr,"%d",(int)aMandatory);
       
   279 		iMandatoryCert.append(mandatoryStr);
       
   280 		iMandatoryCert.append("\n");
       
   281 		}
       
   282 	return isDisplay;
       
   283 	}
       
   284 
       
   285 SwiCertStoreMetaData::~SwiCertStoreMetaData()
       
   286 	{
       
   287 	}
       
   288 
       
   289 int SwiCertStoreMetaData::iCount = 0;