installationservices/swi/source/sisfile/siscontroller.cpp
changeset 0 ba25891c3a9e
child 25 98b66e4fb0be
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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 * Definition of the Swi::Sis::CController
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file siscontroller.cpp
       
    22 */
       
    23 
       
    24 #include "siscontroller.h"
       
    25 #include "sisinfo.h"
       
    26 #include "sissupportedlanguages.h"
       
    27 #include "sissupportedoptions.h"
       
    28 #include "sisproperties.h"
       
    29 #include "sislogo.h"
       
    30 #include "sisinstallblock.h"
       
    31 #include "sisproperties.h"
       
    32 #include "sisprerequisites.h"
       
    33 #include "swi/sistruststatus.h"
       
    34 #include "certchainconstraints.h"
       
    35 #include "sisptrprovider.h"
       
    36 #include "log.h"
       
    37 
       
    38 using namespace Swi::Sis;
       
    39 
       
    40 
       
    41 EXPORT_C /*static*/ CController* CController::NewLC(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    42 	{
       
    43 	CController* self = new(ELeave) CController();
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 EXPORT_C /*static*/ CController* CController::NewL(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    50 	{
       
    51 	CController* self = NewLC(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 	
       
    56 EXPORT_C /*static*/ CController* CController::NewLC(MSisDataProvider& aDataProvider, TReadTypeBehaviour aTypeReadBehaviour)
       
    57 	{
       
    58 	CController* self = new(ELeave) CController();
       
    59 	CleanupStack::PushL(self);
       
    60 	TInt64 bytesRead=0;
       
    61 	self->ConstructL(aDataProvider, bytesRead, aTypeReadBehaviour);
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 EXPORT_C /*static*/ CController* CController::NewL(MSisDataProvider& aDataProvider, TReadTypeBehaviour aTypeReadBehaviour)
       
    66 	{
       
    67 	CController* self = NewLC(aDataProvider, aTypeReadBehaviour);
       
    68 	CleanupStack::Pop(self);
       
    69 	return self;
       
    70 	}
       
    71 
       
    72 CController::CController() : iTrust(ESisPackageUnsignedOrSelfSigned)
       
    73 	{
       
    74 	}
       
    75 	
       
    76 EXPORT_C CController* CController::NewLC(TPtrProvider& aDataProvider, TReadTypeBehaviour aTypeReadBehaviour)
       
    77 	{
       
    78 	CController* self = new(ELeave) CController();
       
    79 	CleanupStack::PushL(self);
       
    80 	TInt64 bytesRead(0);
       
    81 	self->ConstructL(aDataProvider, bytesRead, aTypeReadBehaviour);
       
    82 	return self;
       
    83 	}
       
    84 	
       
    85 EXPORT_C CController* CController::NewL(TPtrProvider& aDataProvider, TReadTypeBehaviour aTypeReadBehaviour)
       
    86 	{
       
    87 	CController* self = NewLC(aDataProvider, aTypeReadBehaviour);
       
    88 	CleanupStack::Pop(self);
       
    89 	return self;
       
    90 	}
       
    91 	
       
    92 EXPORT_C CController* CController::NewLC(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    93 	{
       
    94 	CController* self = new(ELeave) CController();
       
    95 	CleanupStack::PushL(self);
       
    96 	self->ConstructL(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    97 	return self;
       
    98 	}
       
    99 	
       
   100 EXPORT_C CController* CController::NewL(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
   101 	{
       
   102 	CController* self = NewLC(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
   103 	CleanupStack::Pop(self);
       
   104 	return self;
       
   105 	}
       
   106 
       
   107 EXPORT_C CController::~CController()
       
   108 	{
       
   109 	delete iInfo;
       
   110 	delete iInstallBlock;
       
   111 	delete iLanguages;
       
   112 	delete iSupportedOptions;
       
   113 	delete iProperties;
       
   114 	delete iLogo;
       
   115 	delete iPrerequisites;
       
   116 	delete iDataIndex;
       
   117 	delete iTrustStatus;
       
   118 	delete iCertChainConstraints;
       
   119     
       
   120     iCertChainIndices.Close();
       
   121     iSignatureCertificateChains.ResetAndDestroy(); 
       
   122 	}
       
   123 
       
   124 void CController::ConstructL(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
   125 	{
       
   126 	iDataOffset=aBytesRead;
       
   127 	
       
   128 	CField::ConstructL(aDataProvider, EFieldTypeController, aBytesRead, aTypeReadBehaviour);
       
   129 
       
   130  	iInfo=CInfo::NewL(aDataProvider, aBytesRead);
       
   131  	
       
   132 	iSupportedOptions=CSupportedOptions::NewL(aDataProvider, aBytesRead);
       
   133 
       
   134 	iLanguages=CSupportedLanguages::NewL(aDataProvider, aBytesRead);
       
   135 
       
   136 	iPrerequisites=CPrerequisites::NewL(aDataProvider, aBytesRead);
       
   137 
       
   138 	iProperties=CProperties::NewL(aDataProvider, aBytesRead);
       
   139 
       
   140 	TFieldType fieldType;
       
   141 	CField::ReadEnumL<TFieldType,TUint32>(aDataProvider, fieldType, aBytesRead);
       
   142 	
       
   143 	if (fieldType == EFieldTypeLogo)
       
   144 		{
       
   145 		// Logo present
       
   146 		iLogo=CLogo::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   147 		iInstallBlock=CInstallBlock::NewL(aDataProvider, aBytesRead);
       
   148 		}
       
   149 	else if (fieldType == EFieldTypeInstallBlock)
       
   150 		{
       
   151 		// No logo present
       
   152 		iInstallBlock=CInstallBlock::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   153 		}
       
   154 	else
       
   155 		{
       
   156 		User::Leave(KErrSISUnexpectedFieldType);
       
   157 		}
       
   158 	
       
   159 	ReadMemberCollectionL(aDataProvider, iSignatureCertificateChains, EFieldTypeSignatureCertificateChain, EFieldTypeDataIndex, aBytesRead);
       
   160 	
       
   161 	iDataIndex=CDataIndex::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   162 	
       
   163 	CField::SkipUnknownFieldsL(aDataProvider, aBytesRead - iDataOffset, aBytesRead, aTypeReadBehaviour);
       
   164 	CField::EnsureAlignedL(aDataProvider, aBytesRead - iDataOffset, aBytesRead, aTypeReadBehaviour);
       
   165 	
       
   166 	iTrustStatus = new (ELeave)Swi::TSisTrustStatus();
       
   167 
       
   168 	// Create unconstrained cert chain constraints object; this may be replaced.
       
   169 	iCertChainConstraints = CCertChainConstraints::NewL();
       
   170 	
       
   171 	iRemoveWithLastDependent = 0;
       
   172 	
       
   173 	#ifdef SYMBIAN_UNIVERSAL_INSTALL_FRAMEWORK
       
   174 	iUserGrantableCapabilities.SetEmpty();
       
   175 	#endif
       
   176 	}
       
   177 	
       
   178 void CController::ConstructL(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
   179 	{
       
   180 	iDataOffset=aBytesRead;
       
   181 	
       
   182 	CField::ConstructL(aDataProvider, EFieldTypeController, aBytesRead, aTypeReadBehaviour);
       
   183 
       
   184  	iInfo=CInfo::NewL(aDataProvider, aBytesRead);
       
   185  	
       
   186 	iSupportedOptions=CSupportedOptions::NewL(aDataProvider, aBytesRead);
       
   187 
       
   188 	iLanguages=CSupportedLanguages::NewL(aDataProvider, aBytesRead);
       
   189 
       
   190 	iPrerequisites=CPrerequisites::NewL(aDataProvider, aBytesRead);
       
   191 
       
   192 	iProperties=CProperties::NewL(aDataProvider, aBytesRead);
       
   193 
       
   194 	TFieldType fieldType;
       
   195 	CField::ReadEnumL<TFieldType,TUint32>(aDataProvider, fieldType, aBytesRead);
       
   196 	
       
   197 	if (fieldType == EFieldTypeLogo)
       
   198 		{
       
   199 		// Logo present
       
   200 		iLogo=CLogo::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   201 		iInstallBlock=CInstallBlock::NewL(aDataProvider, aBytesRead);
       
   202 		}
       
   203 	else if (fieldType == EFieldTypeInstallBlock)
       
   204 		{
       
   205 		// No logo present
       
   206 		iInstallBlock=CInstallBlock::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   207 		}
       
   208 	else
       
   209 		{
       
   210 		User::Leave(KErrSISUnexpectedFieldType);
       
   211 		}
       
   212 	
       
   213 	ReadMemberCollectionL(aDataProvider, iSignatureCertificateChains, EFieldTypeSignatureCertificateChain, EFieldTypeDataIndex, aBytesRead);
       
   214 	
       
   215 	iDataIndex=CDataIndex::NewL(aDataProvider, aBytesRead, EAssumeType);
       
   216 	
       
   217 	CField::SkipUnknownFieldsL(aDataProvider, aBytesRead - iDataOffset, aBytesRead, aTypeReadBehaviour);
       
   218 	CField::EnsureAlignedL(aDataProvider, aBytesRead - iDataOffset, aBytesRead, aTypeReadBehaviour);
       
   219 	
       
   220 	iTrustStatus = new (ELeave)Swi::TSisTrustStatus();
       
   221 
       
   222 	// Create unconstrained cert chain constraints object; this may be replaced.
       
   223 	iCertChainConstraints = CCertChainConstraints::NewL();
       
   224 	
       
   225 	iRemoveWithLastDependent = 0;
       
   226 	
       
   227 	#ifdef SYMBIAN_UNIVERSAL_INSTALL_FRAMEWORK
       
   228 	iUserGrantableCapabilities.SetEmpty();
       
   229 	#endif
       
   230 	}	
       
   231 
       
   232 EXPORT_C void CController::AddChainIndex(TInt aIndex) const
       
   233     {
       
   234 	iCertChainIndices.Append(aIndex);
       
   235     }
       
   236 
       
   237 EXPORT_C void CController::ClearCertChainIndices() const
       
   238     {
       
   239 	iCertChainIndices.Reset();
       
   240     }
       
   241 
       
   242 EXPORT_C void CController::SetCertChainConstraints(CCertChainConstraints* aConstraints)
       
   243  	{
       
   244  	// Delete existing cert chain constraints object.
       
   245  	delete iCertChainConstraints;
       
   246  	iCertChainConstraints=aConstraints;
       
   247  	}
       
   248  	
       
   249 EXPORT_C TInt CController::RemoveWithLastDependent() const
       
   250 	{
       
   251 	return iRemoveWithLastDependent;
       
   252 	}
       
   253 	 	
       
   254 EXPORT_C void CController::SetRemoveWithLastDependent()
       
   255 	{
       
   256 	iRemoveWithLastDependent = 1;
       
   257 	}
       
   258 
       
   259 
       
   260 EXPORT_C CMessageDigest* CController::GenerateControllerHashLC(const TDesC8& aController)
       
   261 	{
       
   262 	TInt64 infoLength = iInfo->Length() + iInfo->HeaderSize() + iInfo->PaddingSize();
       
   263 	TInt64 installBLength = iInstallBlock->Length() + iInstallBlock->HeaderSize() + iInstallBlock->PaddingSize();	
       
   264 	TInt64 supportLLength = iLanguages->Length() + iLanguages->HeaderSize() + iLanguages->PaddingSize();
       
   265 	TInt64 supportOLength = iSupportedOptions->Length() + iSupportedOptions->HeaderSize() + iSupportedOptions->PaddingSize();
       
   266 	TInt64 propertiesLength = iProperties->Length() + iProperties->HeaderSize() + iProperties->PaddingSize();
       
   267 	TInt64 prerequisitesLength = iPrerequisites->Length() + iPrerequisites->HeaderSize() + iPrerequisites->PaddingSize();
       
   268 	TInt64 logoLength = 0;
       
   269 	
       
   270 	if (iLogo)
       
   271 		{
       
   272 		logoLength = iLogo->Length() + iLogo->HeaderSize() + iLogo->PaddingSize();
       
   273 		}
       
   274 
       
   275    	TInt64 controllerLength = 0;
       
   276 	controllerLength = infoLength + installBLength + supportLLength + supportOLength + propertiesLength + prerequisitesLength + logoLength;
       
   277 
       
   278 	// Length octets are either 4 or 8 bytes but since controllers are loaded into
       
   279 	// bytes should ALWAYS be sufficient.
       
   280 	TInt offset = 4;
       
   281 	
       
   282 	// Should really modify the length and not the offset skipping the length octets. 
       
   283 	// This makes the controller hash independent of the signature chains.
       
   284 	//
       
   285 	// Unfortunately, this is fixed and changing it potentially breaks compatibility
       
   286 	// E.g. If the firmware is upgraded without reformatting the system drive then SWI 
       
   287 	// may think that an application on removable media has not been installed.
       
   288 	controllerLength += (Length() > 0x80000000 ? 8 : 4);
       
   289 		
       
   290 	DEBUG_PRINTF4(_L8("GenerateControllerHashLC aController.Length()=%d, offset=%d, controllerLength=%d"), 
       
   291 		aController.Length(), offset, controllerLength);
       
   292 	
       
   293 	if (offset + controllerLength >= aController.Length()) 
       
   294 		{
       
   295 		// Mid will panic if the lengths are outside the buffer.
       
   296 		User::Leave(KErrCorrupt);
       
   297 		}
       
   298 	TPtrC8 ptr;
       
   299 	ptr.Set(aController.Mid(offset, controllerLength));
       
   300 
       
   301 	CMessageDigest* digest = CMessageDigestFactory::NewDigestLC(CMessageDigest::ESHA1);
       
   302 	digest->Update(ptr);
       
   303 
       
   304 	return digest;
       
   305 	}