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