sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Parsing of capability names sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include "t_input.h" sl@0: #include "t_output.h" sl@0: sl@0: // This file needs to be updated in three places whenever new capabilities are sl@0: // added - the list of capability names below and the methods sl@0: // Input::ParseCapabilityNameL and Output::writeCapabilityL. sl@0: sl@0: // Capability names sl@0: _LIT8(KCapabilityTCB, "TCB"); sl@0: _LIT8(KCapabilityCommDD, "CommDD"); sl@0: _LIT8(KCapabilityPowerMgmt, "PowerMgmt"); sl@0: _LIT8(KCapabilityMultimediaDD, "MultimediaDD"); sl@0: _LIT8(KCapabilityReadDeviceData, "ReadDeviceData"); sl@0: _LIT8(KCapabilityWriteDeviceData, "WriteDeviceData"); sl@0: _LIT8(KCapabilityDRM, "DRM"); sl@0: _LIT8(KCapabilityTrustedUI, "TrustedUI"); sl@0: _LIT8(KCapabilityProtServ, "ProtServ"); sl@0: _LIT8(KCapabilityDiskAdmin, "DiskAdmin"); sl@0: _LIT8(KCapabilityNetworkControl, "NetworkControl"); sl@0: _LIT8(KCapabilityAllFiles, "AllFiles"); sl@0: _LIT8(KCapabilitySwEvent, "SwEvent"); sl@0: _LIT8(KCapabilityNetworkServices, "NetworkServices"); sl@0: _LIT8(KCapabilityLocalServices, "LocalServices"); sl@0: _LIT8(KCapabilityReadUserData, "ReadUserData"); sl@0: _LIT8(KCapabilityWriteUserData, "WriteUserData"); sl@0: _LIT8(KCapabilityLocation, "Location"); sl@0: _LIT8(KCapabilitySurroundingsDD, "SurroundingsDD"); sl@0: _LIT8(KCapabilityUserEnvironment, "UserEnvironment"); sl@0: sl@0: // Script file tags sl@0: _LIT8(KCapabilityStart, ""); sl@0: _LIT8(KCapabilityEnd, ""); sl@0: _LIT8(KPolicyPass, ""); sl@0: _LIT8(KPolicyFail, ""); sl@0: _LIT8(KSecureIdStart, ""); sl@0: _LIT8(KSecureIdEnd, ""); sl@0: _LIT8(KVendorIdStart, ""); sl@0: _LIT8(KVendorIdEnd, ""); sl@0: sl@0: _LIT(KCommaSpace, ", "); sl@0: sl@0: EXPORT_C TCapability Input::ParseCapabilityNameL(const TDesC8& aName) sl@0: { sl@0: TCapability result = ECapability_None; sl@0: sl@0: if (aName == KCapabilityTCB) result = ECapabilityTCB; sl@0: else if (aName == KCapabilityCommDD) result = ECapabilityCommDD; sl@0: else if (aName == KCapabilityPowerMgmt) result = ECapabilityPowerMgmt; sl@0: else if (aName == KCapabilityMultimediaDD) result = ECapabilityMultimediaDD; sl@0: else if (aName == KCapabilityReadDeviceData) result = ECapabilityReadDeviceData; sl@0: else if (aName == KCapabilityWriteDeviceData) result = ECapabilityWriteDeviceData; sl@0: else if (aName == KCapabilityDRM) result = ECapabilityDRM; sl@0: else if (aName == KCapabilityTrustedUI) result = ECapabilityTrustedUI; sl@0: else if (aName == KCapabilityProtServ) result = ECapabilityProtServ; sl@0: else if (aName == KCapabilityDiskAdmin) result = ECapabilityDiskAdmin; sl@0: else if (aName == KCapabilityNetworkControl) result = ECapabilityNetworkControl; sl@0: else if (aName == KCapabilityAllFiles) result = ECapabilityAllFiles; sl@0: else if (aName == KCapabilitySwEvent) result = ECapabilitySwEvent; sl@0: else if (aName == KCapabilityNetworkServices) result = ECapabilityNetworkServices; sl@0: else if (aName == KCapabilityLocalServices) result = ECapabilityLocalServices; sl@0: else if (aName == KCapabilityReadUserData) result = ECapabilityReadUserData; sl@0: else if (aName == KCapabilityWriteUserData) result = ECapabilityWriteUserData; sl@0: else if (aName == KCapabilityLocation) result = ECapabilityLocation; sl@0: else if (aName == KCapabilitySurroundingsDD) result = ECapabilitySurroundingsDD; sl@0: else if (aName == KCapabilityUserEnvironment) result = ECapabilityUserEnvironment; sl@0: sl@0: if (result == ECapability_None) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: return result; sl@0: } sl@0: sl@0: EXPORT_C void Input::ParseCapabilitySetL(const TDesC8& aIn, TCapabilitySet& aOut) sl@0: { sl@0: aOut.SetEmpty(); sl@0: TInt pos = 0, err = KErrNone; sl@0: for (;;) sl@0: { sl@0: const TDesC8& match = Input::ParseElement(aIn, KCapabilityStart, KCapabilityEnd, pos, err); sl@0: if (err != KErrNone) sl@0: { sl@0: break; sl@0: } sl@0: aOut.AddCapability(ParseCapabilityNameL(match)); sl@0: } sl@0: if (err != KErrNone && err != KErrNotFound) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void Input::ParseSecurityPolicyL(const TDesC8& aIn, TSecurityPolicy& aOut) sl@0: { sl@0: TInt options = 0; sl@0: sl@0: TBool pass = aIn.Find(KPolicyPass) != KErrNotFound; // why didn't we use a proper xml parser? sl@0: if (pass) ++options; sl@0: sl@0: TBool fail = aIn.Find(KPolicyFail) != KErrNotFound; sl@0: if (fail) ++options; sl@0: sl@0: TUint secureId = ParseIntElement(aIn, KSecureIdStart, KSecureIdEnd); sl@0: if (secureId) ++options; sl@0: sl@0: TUint vendorId = ParseIntElement(aIn, KVendorIdStart, KVendorIdEnd); sl@0: if (vendorId) ++options; sl@0: sl@0: // Can only sepcify one of pass, fail, secureid, vendorid sl@0: if (options > 1) sl@0: User::Leave(KErrArgument); sl@0: sl@0: // Parse the capabilities sl@0: TCapabilitySet capSet; sl@0: ParseCapabilitySetL(aIn, capSet); sl@0: sl@0: // Determine maximum number of capabilities allowed sl@0: TInt maxCaps; sl@0: if (pass || fail) sl@0: { sl@0: maxCaps = 0; sl@0: } sl@0: else if (secureId || vendorId) sl@0: { sl@0: maxCaps = 3; sl@0: } sl@0: else sl@0: { sl@0: maxCaps = 7; sl@0: } sl@0: sl@0: TCapability caps[7]; sl@0: TInt count = 0; sl@0: sl@0: // Extract capabilities into array sl@0: TInt i; sl@0: for (i = 0 ; i < ECapability_Limit ; ++i) sl@0: { sl@0: TCapability c = static_cast(i); sl@0: if (capSet.HasCapability(c)) sl@0: { sl@0: // Check if more capabities are specified that allowed sl@0: if (count == maxCaps) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: caps[count++] = c; sl@0: } sl@0: } sl@0: sl@0: // Fill the rest of the array with ECapability_None sl@0: for (i = count ; i < maxCaps ; ++i) sl@0: { sl@0: caps[i] = ECapability_None; sl@0: } sl@0: sl@0: if (pass) sl@0: { sl@0: aOut = TSecurityPolicy(TSecurityPolicy::EAlwaysPass); sl@0: } sl@0: else if (fail) sl@0: { sl@0: aOut = TSecurityPolicy(TSecurityPolicy::EAlwaysFail); sl@0: } sl@0: else if (secureId) sl@0: { sl@0: aOut = TSecurityPolicy(TSecureId(secureId), caps[0], caps[1], caps[2]); sl@0: } sl@0: else if (vendorId) sl@0: { sl@0: aOut = TSecurityPolicy(TVendorId(vendorId), caps[0], caps[1], caps[2]); sl@0: } sl@0: else sl@0: { sl@0: aOut = TSecurityPolicy(caps[0], caps[1], caps[2], caps[3], sl@0: caps[4], caps[5], caps[6]); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void Output::writeCapabilityL(TCapability aCap) sl@0: { sl@0: switch (aCap) sl@0: { sl@0: case ECapabilityTCB: sl@0: writeString(KCapabilityTCB); sl@0: break; sl@0: sl@0: case ECapabilityCommDD: sl@0: writeString(KCapabilityCommDD); sl@0: break; sl@0: sl@0: case ECapabilityPowerMgmt: sl@0: writeString(KCapabilityPowerMgmt); sl@0: break; sl@0: sl@0: case ECapabilityMultimediaDD: sl@0: writeString(KCapabilityMultimediaDD); sl@0: break; sl@0: sl@0: case ECapabilityReadDeviceData: sl@0: writeString(KCapabilityReadDeviceData); sl@0: break; sl@0: sl@0: case ECapabilityWriteDeviceData: sl@0: writeString(KCapabilityWriteDeviceData); sl@0: break; sl@0: sl@0: case ECapabilityDRM: sl@0: writeString(KCapabilityDRM); sl@0: break; sl@0: sl@0: case ECapabilityTrustedUI: sl@0: writeString(KCapabilityTrustedUI); sl@0: break; sl@0: sl@0: case ECapabilityProtServ: sl@0: writeString(KCapabilityProtServ); sl@0: break; sl@0: sl@0: case ECapabilityDiskAdmin: sl@0: writeString(KCapabilityDiskAdmin); sl@0: break; sl@0: sl@0: case ECapabilityNetworkControl: sl@0: writeString(KCapabilityNetworkControl); sl@0: break; sl@0: sl@0: case ECapabilityAllFiles: sl@0: writeString(KCapabilityAllFiles); sl@0: break; sl@0: sl@0: case ECapabilitySwEvent: sl@0: writeString(KCapabilitySwEvent); sl@0: break; sl@0: sl@0: case ECapabilityNetworkServices: sl@0: writeString(KCapabilityNetworkServices); sl@0: break; sl@0: sl@0: case ECapabilityLocalServices: sl@0: writeString(KCapabilityLocalServices); sl@0: break; sl@0: sl@0: case ECapabilityReadUserData: sl@0: writeString(KCapabilityReadUserData); sl@0: break; sl@0: sl@0: case ECapabilityWriteUserData: sl@0: writeString(KCapabilityWriteUserData); sl@0: break; sl@0: sl@0: case ECapabilityLocation: sl@0: writeString(KCapabilityLocation); sl@0: break; sl@0: sl@0: case ECapabilitySurroundingsDD: sl@0: writeString(KCapabilitySurroundingsDD); sl@0: break; sl@0: sl@0: case ECapabilityUserEnvironment: sl@0: writeString(KCapabilityUserEnvironment); sl@0: break; sl@0: sl@0: default: sl@0: User::Invariant(); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void Output::writeCapabilitySetL(const TCapabilitySet& aCaps) sl@0: { sl@0: TBool first = ETrue; sl@0: for (TInt i = 0 ; i < ECapability_Limit ; ++i) sl@0: { sl@0: TCapability cap = static_cast(i); sl@0: if (aCaps.HasCapability(cap)) sl@0: { sl@0: if (!first) sl@0: { sl@0: writeString(KCommaSpace); sl@0: } sl@0: else sl@0: { sl@0: first = EFalse; sl@0: } sl@0: writeCapabilityL(cap); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * The real TSecurityPolicy class has no accessors, so to extract information sl@0: * from it we cast it to this class, which has exactly the same layout. sl@0: */ sl@0: class TPrintableSecurityPolicy sl@0: { sl@0: public: sl@0: void WriteL(Output& aOut) const; sl@0: sl@0: public: sl@0: enum TType sl@0: { sl@0: ETypeFail=0, // Always fail sl@0: ETypePass=1, // Always pass sl@0: ETypeC3=2, // Up to 3 capabilities sl@0: ETypeC7=3, // Up to 7 capabilities sl@0: ETypeS3=4, // SID + up to 3 capabilities sl@0: ETypeV3=5, // VID + up to 3 capabilities sl@0: sl@0: ETypeLimit sl@0: }; sl@0: sl@0: private: sl@0: TPrintableSecurityPolicy(); sl@0: TBool WriteCapsL(Output& aOut, TBool aFirst) const; sl@0: void WriteExtraCapsL(Output& aOut, TBool aFirst) const; sl@0: sl@0: private: sl@0: TUint8 iType; sl@0: TUint8 iCaps[3]; // missing capabilities are set to 0xff sl@0: union sl@0: { sl@0: TUint32 iSecureId; sl@0: TUint32 iVendorId; sl@0: TUint8 iExtraCaps[4]; // missing capabilities are set to 0xff sl@0: }; sl@0: }; sl@0: sl@0: // Check noone added another type to the enumeration sl@0: __ASSERT_COMPILE(((TInt)TPrintableSecurityPolicy::ETypeLimit) == ((TInt)TSecurityPolicy::ETypeLimit)); sl@0: sl@0: EXPORT_C void Output::writeSecurityPolicyL(const TSecurityPolicy& aPolicy) sl@0: { sl@0: const TPrintableSecurityPolicy* p = reinterpret_cast(&aPolicy); sl@0: p->WriteL(*this); sl@0: } sl@0: sl@0: void TPrintableSecurityPolicy::WriteL(Output& aOut) const sl@0: { sl@0: switch (iType) sl@0: { sl@0: case ETypeFail: sl@0: aOut.writeString(_L("AlwaysFail")); sl@0: break; sl@0: case ETypePass: sl@0: aOut.writeString(_L("AlwaysPass")); sl@0: break; sl@0: case ETypeC3: sl@0: WriteCapsL(aOut, ETrue); sl@0: break; sl@0: case ETypeC7: sl@0: { sl@0: TBool first = WriteCapsL(aOut, ETrue); sl@0: WriteExtraCapsL(aOut, first); sl@0: } sl@0: break; sl@0: case ETypeS3: sl@0: aOut.writeString(_L("SID 0x")); sl@0: aOut.writeHex(iSecureId); sl@0: WriteCapsL(aOut, EFalse); sl@0: break; sl@0: case ETypeV3: sl@0: aOut.writeString(_L("VID 0x")); sl@0: aOut.writeHex(iVendorId); sl@0: WriteCapsL(aOut, EFalse); sl@0: break; sl@0: default: sl@0: User::Invariant(); sl@0: } sl@0: } sl@0: sl@0: TBool TPrintableSecurityPolicy::WriteCapsL(Output& aOut, TBool aFirst) const sl@0: { sl@0: for (TInt i = 0 ; i < 3 ; ++i) sl@0: { sl@0: if (iCaps[i] != 0xff) sl@0: { sl@0: if (!aFirst) sl@0: { sl@0: aOut.writeString(KCommaSpace); sl@0: } sl@0: aFirst = EFalse; sl@0: aOut.writeCapabilityL((TCapability) iCaps[i]); sl@0: } sl@0: } sl@0: return aFirst; sl@0: } sl@0: sl@0: void TPrintableSecurityPolicy::WriteExtraCapsL(Output& aOut, TBool aFirst) const sl@0: { sl@0: for (TInt i = 0 ; i < 3 ; ++i) sl@0: { sl@0: if (iExtraCaps[i] != 0xff) sl@0: { sl@0: if (!aFirst) sl@0: { sl@0: aOut.writeString(KCommaSpace); sl@0: } sl@0: aFirst = EFalse; sl@0: aOut.writeCapabilityL((TCapability) iExtraCaps[i]); sl@0: } sl@0: } sl@0: }