os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/t_capabilities.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 * Parsing of capability names
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22 */
    23 
    24 #include "t_input.h"
    25 #include "t_output.h"
    26 
    27 // This file needs to be updated in three places whenever new capabilities are
    28 // added - the list of capability names below and the methods
    29 // Input::ParseCapabilityNameL and Output::writeCapabilityL.
    30 
    31 // Capability names
    32 _LIT8(KCapabilityTCB,  				"TCB");
    33 _LIT8(KCapabilityCommDD,  			"CommDD");
    34 _LIT8(KCapabilityPowerMgmt,  		"PowerMgmt");
    35 _LIT8(KCapabilityMultimediaDD,  	"MultimediaDD");
    36 _LIT8(KCapabilityReadDeviceData,  	"ReadDeviceData");
    37 _LIT8(KCapabilityWriteDeviceData,  	"WriteDeviceData");
    38 _LIT8(KCapabilityDRM,  				"DRM");
    39 _LIT8(KCapabilityTrustedUI,  		"TrustedUI");
    40 _LIT8(KCapabilityProtServ,  		"ProtServ");
    41 _LIT8(KCapabilityDiskAdmin,  		"DiskAdmin");
    42 _LIT8(KCapabilityNetworkControl,  	"NetworkControl");
    43 _LIT8(KCapabilityAllFiles,  		"AllFiles");
    44 _LIT8(KCapabilitySwEvent,  			"SwEvent");
    45 _LIT8(KCapabilityNetworkServices,  	"NetworkServices");
    46 _LIT8(KCapabilityLocalServices,  	"LocalServices");
    47 _LIT8(KCapabilityReadUserData,  	"ReadUserData");
    48 _LIT8(KCapabilityWriteUserData,  	"WriteUserData");
    49 _LIT8(KCapabilityLocation,  		"Location");
    50 _LIT8(KCapabilitySurroundingsDD,  	"SurroundingsDD");
    51 _LIT8(KCapabilityUserEnvironment,  	"UserEnvironment");
    52 
    53 // Script file tags
    54 _LIT8(KCapabilityStart, "<capability>");
    55 _LIT8(KCapabilityEnd, "</capability>");
    56 _LIT8(KPolicyPass, "<pass/>");
    57 _LIT8(KPolicyFail, "<fail/>");
    58 _LIT8(KSecureIdStart, "<secureid>");
    59 _LIT8(KSecureIdEnd, "</secureid>");
    60 _LIT8(KVendorIdStart, "<vendorid>");
    61 _LIT8(KVendorIdEnd, "</vendorid>");
    62 
    63 _LIT(KCommaSpace, ", ");
    64 
    65 EXPORT_C TCapability Input::ParseCapabilityNameL(const TDesC8& aName)
    66 	{
    67 	TCapability result = ECapability_None;
    68 
    69 	if (aName == KCapabilityTCB) 					result = ECapabilityTCB;
    70 	else if (aName == KCapabilityCommDD) 			result = ECapabilityCommDD;
    71 	else if (aName == KCapabilityPowerMgmt) 		result = ECapabilityPowerMgmt;
    72 	else if (aName == KCapabilityMultimediaDD) 		result = ECapabilityMultimediaDD;
    73 	else if (aName == KCapabilityReadDeviceData) 	result = ECapabilityReadDeviceData;
    74 	else if (aName == KCapabilityWriteDeviceData) 	result = ECapabilityWriteDeviceData;
    75 	else if (aName == KCapabilityDRM) 				result = ECapabilityDRM;
    76 	else if (aName == KCapabilityTrustedUI) 		result = ECapabilityTrustedUI;
    77 	else if (aName == KCapabilityProtServ) 			result = ECapabilityProtServ;
    78 	else if (aName == KCapabilityDiskAdmin) 		result = ECapabilityDiskAdmin;
    79 	else if (aName == KCapabilityNetworkControl) 	result = ECapabilityNetworkControl;
    80 	else if (aName == KCapabilityAllFiles) 			result = ECapabilityAllFiles;
    81 	else if (aName == KCapabilitySwEvent) 			result = ECapabilitySwEvent;
    82 	else if (aName == KCapabilityNetworkServices) 	result = ECapabilityNetworkServices;
    83 	else if (aName == KCapabilityLocalServices) 	result = ECapabilityLocalServices;
    84 	else if (aName == KCapabilityReadUserData) 		result = ECapabilityReadUserData;
    85 	else if (aName == KCapabilityWriteUserData) 	result = ECapabilityWriteUserData;
    86 	else if (aName == KCapabilityLocation) 			result = ECapabilityLocation;
    87 	else if (aName == KCapabilitySurroundingsDD) 	result = ECapabilitySurroundingsDD;
    88 	else if (aName == KCapabilityUserEnvironment) 	result = ECapabilityUserEnvironment;
    89 
    90 	if (result == ECapability_None)
    91 		{
    92 		User::Leave(KErrArgument);
    93 		}
    94 	
    95 	return result;
    96 	}
    97 
    98 EXPORT_C void Input::ParseCapabilitySetL(const TDesC8& aIn, TCapabilitySet& aOut)
    99 	{
   100 	aOut.SetEmpty();
   101 	TInt pos = 0, err = KErrNone;
   102 	for (;;)
   103 		{
   104 		const TDesC8& match = Input::ParseElement(aIn, KCapabilityStart, KCapabilityEnd, pos, err);
   105 		if (err != KErrNone)
   106 			{
   107 			break;
   108 			}
   109 		aOut.AddCapability(ParseCapabilityNameL(match));
   110 		}
   111 	if (err != KErrNone && err != KErrNotFound)
   112 		{
   113 		User::Leave(err);
   114 		}
   115 	}
   116 
   117 EXPORT_C void Input::ParseSecurityPolicyL(const TDesC8& aIn, TSecurityPolicy& aOut)
   118 	{
   119 	TInt options = 0;
   120 
   121 	TBool pass = aIn.Find(KPolicyPass) != KErrNotFound; // why didn't we use a proper xml parser?
   122 	if (pass) ++options;
   123 
   124 	TBool fail = aIn.Find(KPolicyFail) != KErrNotFound;
   125 	if (fail) ++options;
   126 
   127 	TUint secureId = ParseIntElement(aIn, KSecureIdStart, KSecureIdEnd);
   128 	if (secureId) ++options;
   129 
   130 	TUint vendorId = ParseIntElement(aIn, KVendorIdStart, KVendorIdEnd);
   131 	if (vendorId) ++options;
   132 
   133 	// Can only sepcify one of pass, fail, secureid, vendorid
   134 	if (options > 1)
   135 		User::Leave(KErrArgument);
   136 
   137 	// Parse the capabilities
   138 	TCapabilitySet capSet;
   139 	ParseCapabilitySetL(aIn, capSet);
   140 
   141 	// Determine maximum number of capabilities allowed
   142 	TInt maxCaps;
   143 	if (pass || fail)
   144 		{
   145 		maxCaps = 0;
   146 		}
   147 	else if (secureId || vendorId)
   148 		{
   149 		maxCaps = 3;
   150 		}
   151 	else
   152 		{
   153 		maxCaps = 7;
   154 		}
   155 	
   156 	TCapability caps[7];
   157 	TInt count = 0;
   158 
   159 	// Extract capabilities into array
   160 	TInt i;
   161 	for (i = 0 ; i < ECapability_Limit ; ++i)
   162 		{
   163 		TCapability c = static_cast<TCapability>(i);
   164 		if (capSet.HasCapability(c))
   165 			{
   166 			// Check if more capabities are specified that allowed
   167 			if (count == maxCaps)
   168 				{
   169 				User::Leave(KErrArgument);
   170 				}
   171 			caps[count++] = c;
   172 			}
   173 		}
   174 	
   175 	// Fill the rest of the array with ECapability_None
   176 	for (i = count ; i < maxCaps ; ++i)
   177 		{
   178 		caps[i] = ECapability_None;
   179 		}
   180 
   181 	if (pass)
   182 		{
   183 		aOut = TSecurityPolicy(TSecurityPolicy::EAlwaysPass);
   184 		}
   185 	else if (fail)
   186 		{
   187 		aOut = TSecurityPolicy(TSecurityPolicy::EAlwaysFail);
   188 		}
   189 	else if (secureId)
   190 		{
   191 		aOut = TSecurityPolicy(TSecureId(secureId), caps[0], caps[1], caps[2]);
   192 		}
   193 	else if (vendorId)
   194 		{
   195 		aOut = TSecurityPolicy(TVendorId(vendorId), caps[0], caps[1], caps[2]);
   196 		}			
   197 	else
   198 		{
   199 		aOut = TSecurityPolicy(caps[0], caps[1], caps[2], caps[3],
   200 							   caps[4], caps[5], caps[6]);
   201 		}
   202 	}
   203 
   204 EXPORT_C void Output::writeCapabilityL(TCapability aCap)
   205 	{
   206 	switch (aCap)
   207 		{
   208 		case ECapabilityTCB:
   209 			writeString(KCapabilityTCB);
   210 			break;
   211 
   212 		case ECapabilityCommDD:
   213 			writeString(KCapabilityCommDD);
   214 			break;
   215 
   216 		case ECapabilityPowerMgmt:
   217 			writeString(KCapabilityPowerMgmt);
   218 			break;
   219 
   220 		case ECapabilityMultimediaDD:
   221 			writeString(KCapabilityMultimediaDD);
   222 			break;
   223 
   224 		case ECapabilityReadDeviceData:
   225 			writeString(KCapabilityReadDeviceData);
   226 			break;
   227 
   228 		case ECapabilityWriteDeviceData:
   229 			writeString(KCapabilityWriteDeviceData);
   230 			break;
   231 
   232 		case ECapabilityDRM:
   233 			writeString(KCapabilityDRM);
   234 			break;
   235 
   236 		case ECapabilityTrustedUI:
   237 			writeString(KCapabilityTrustedUI);
   238 			break;
   239 
   240 		case ECapabilityProtServ:
   241 			writeString(KCapabilityProtServ);
   242 			break;
   243 
   244 		case ECapabilityDiskAdmin:
   245 			writeString(KCapabilityDiskAdmin);
   246 			break;
   247 
   248 		case ECapabilityNetworkControl:
   249 			writeString(KCapabilityNetworkControl);
   250 			break;
   251 
   252 		case ECapabilityAllFiles:
   253 			writeString(KCapabilityAllFiles);
   254 			break;
   255 
   256 		case ECapabilitySwEvent:
   257 			writeString(KCapabilitySwEvent);
   258 			break;
   259 
   260 		case ECapabilityNetworkServices:
   261 			writeString(KCapabilityNetworkServices);
   262 			break;
   263 
   264 		case ECapabilityLocalServices:
   265 			writeString(KCapabilityLocalServices);
   266 			break;
   267 
   268 		case ECapabilityReadUserData:
   269 			writeString(KCapabilityReadUserData);
   270 			break;
   271 
   272 		case ECapabilityWriteUserData:
   273 			writeString(KCapabilityWriteUserData);
   274 			break;
   275 
   276 		case ECapabilityLocation:
   277 			writeString(KCapabilityLocation);
   278 			break;
   279 
   280 		case ECapabilitySurroundingsDD:
   281 			writeString(KCapabilitySurroundingsDD);
   282 			break;
   283 
   284 		case ECapabilityUserEnvironment:
   285 			writeString(KCapabilityUserEnvironment);
   286 			break;
   287 
   288 		default:
   289 			User::Invariant();
   290 		}
   291 	}
   292 
   293 EXPORT_C void Output::writeCapabilitySetL(const TCapabilitySet& aCaps)
   294 	{
   295 	TBool first = ETrue;
   296 	for (TInt i = 0 ; i < ECapability_Limit ; ++i)
   297 		{
   298 		TCapability cap = static_cast<TCapability>(i);
   299 		if (aCaps.HasCapability(cap))
   300 			{
   301 			if (!first)
   302 				{
   303 				writeString(KCommaSpace);
   304 				}
   305 			else
   306 				{
   307 				first = EFalse;
   308 				}
   309 			writeCapabilityL(cap);
   310 			}
   311 		}
   312 	}
   313 
   314 /**
   315  * The real TSecurityPolicy class has no accessors, so to extract information
   316  * from it we cast it to this class, which has exactly the same layout.
   317  */
   318 class TPrintableSecurityPolicy
   319 	{
   320 public:
   321 	void WriteL(Output& aOut) const;
   322 	
   323 public:
   324 	enum TType
   325 		{
   326 		ETypeFail=0,	// Always fail
   327 		ETypePass=1,	// Always pass
   328 		ETypeC3=2,		// Up to 3 capabilities
   329 		ETypeC7=3,		// Up to 7 capabilities
   330 		ETypeS3=4,		// SID + up to 3 capabilities
   331 		ETypeV3=5,		// VID + up to 3 capabilities
   332 
   333 		ETypeLimit
   334 		};
   335 
   336 private:
   337 	TPrintableSecurityPolicy();
   338 	TBool WriteCapsL(Output& aOut, TBool aFirst) const;
   339 	void WriteExtraCapsL(Output& aOut, TBool aFirst) const;
   340 	
   341 private:
   342 	TUint8 iType;
   343 	TUint8 iCaps[3];				// missing capabilities are set to 0xff
   344 	union
   345 		{
   346 		TUint32 iSecureId;
   347 		TUint32 iVendorId;
   348 		TUint8 iExtraCaps[4];		// missing capabilities are set to 0xff
   349 		};
   350 	};
   351 
   352 // Check noone added another type to the enumeration
   353 __ASSERT_COMPILE(((TInt)TPrintableSecurityPolicy::ETypeLimit) == ((TInt)TSecurityPolicy::ETypeLimit));
   354 
   355 EXPORT_C void Output::writeSecurityPolicyL(const TSecurityPolicy& aPolicy)
   356 	{
   357 	const TPrintableSecurityPolicy* p = reinterpret_cast<const TPrintableSecurityPolicy*>(&aPolicy);
   358 	p->WriteL(*this);
   359 	}
   360 
   361 void TPrintableSecurityPolicy::WriteL(Output& aOut) const
   362 	{
   363 	switch (iType)
   364 		{
   365 		case ETypeFail:
   366 			aOut.writeString(_L("AlwaysFail"));
   367 			break;
   368 		case ETypePass:
   369 			aOut.writeString(_L("AlwaysPass"));
   370 			break;
   371 		case ETypeC3:
   372 			WriteCapsL(aOut, ETrue);
   373 			break;
   374 		case ETypeC7:
   375 			{
   376 			TBool first = WriteCapsL(aOut, ETrue);
   377 			WriteExtraCapsL(aOut, first);
   378 			}
   379 			break;
   380 		case ETypeS3:
   381 			aOut.writeString(_L("SID 0x"));
   382 			aOut.writeHex(iSecureId);
   383 			WriteCapsL(aOut, EFalse);
   384 			break;
   385 		case ETypeV3:
   386 			aOut.writeString(_L("VID 0x"));
   387 			aOut.writeHex(iVendorId);
   388 			WriteCapsL(aOut, EFalse);
   389 			break;
   390 		default:
   391 			User::Invariant();
   392 		}
   393 	}
   394 
   395 TBool TPrintableSecurityPolicy::WriteCapsL(Output& aOut, TBool aFirst) const
   396 	{
   397 	for (TInt i = 0 ; i < 3 ; ++i)
   398 		{
   399 		if (iCaps[i] != 0xff)
   400 			{
   401 			if (!aFirst)
   402 				{
   403 				aOut.writeString(KCommaSpace);
   404 				}
   405 			aFirst = EFalse;
   406 			aOut.writeCapabilityL((TCapability) iCaps[i]);
   407 			}
   408 		}
   409 	return aFirst;
   410 	}
   411 
   412 void TPrintableSecurityPolicy::WriteExtraCapsL(Output& aOut, TBool aFirst) const
   413 	{
   414 	for (TInt i = 0 ; i < 3 ; ++i)
   415 		{
   416 		if (iExtraCaps[i] != 0xff)
   417 			{
   418 			if (!aFirst)
   419 				{
   420 				aOut.writeString(KCommaSpace);
   421 				}
   422 			aFirst = EFalse;
   423 			aOut.writeCapabilityL((TCapability) iExtraCaps[i]);
   424 			}
   425 		}
   426 	}