0
|
1 |
/*
|
|
2 |
* Copyright (c) 2000 - 2006 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
1
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
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 |
package com.nokia.epdt.plugins.s60.securityconf;
|
|
20 |
|
|
21 |
/* java.util */
|
|
22 |
import java.util.List;
|
|
23 |
import java.util.ArrayList;
|
|
24 |
|
|
25 |
/* java.io */
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.io.InputStream;
|
|
28 |
import java.io.BufferedReader;
|
|
29 |
import java.io.InputStreamReader;
|
|
30 |
import java.io.ObjectInputStream;
|
|
31 |
import java.io.ObjectOutputStream;
|
|
32 |
import java.io.OutputStream;
|
|
33 |
import java.io.PrintWriter;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* MIDP security configuration model.
|
|
37 |
*/
|
|
38 |
class SecConfModel implements SecurityConstants {
|
|
39 |
|
|
40 |
// Emulater/real life security mode
|
|
41 |
private boolean iEmulatedSecurityMode = false;
|
|
42 |
private String iSelectedDomainId;
|
|
43 |
private ProtectionDomain iUserDefinedDomain;
|
|
44 |
private List iProtectionDomains = new ArrayList();
|
|
45 |
private List iFunctionGroups = new ArrayList();
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Creates MIDP security configuration model.
|
|
49 |
* @throws IOException if it fails to load default MIDP security
|
|
50 |
* configuration from the resources.
|
|
51 |
*/
|
|
52 |
SecConfModel() throws IOException {
|
|
53 |
InputStream in = openPolicyFileResource();
|
|
54 |
try {
|
|
55 |
PolicyFileParser.parse(in, new InitialScan());
|
|
56 |
} finally {
|
|
57 |
in.close();
|
|
58 |
}
|
|
59 |
|
|
60 |
// Create user defined domain
|
|
61 |
iUserDefinedDomain = new ProtectionDomain(USER_DEFINED_DOMAIN);
|
|
62 |
iProtectionDomains.add(iUserDefinedDomain);
|
|
63 |
int n = iFunctionGroups.size();
|
|
64 |
for (int i=0; i<n; i++) {
|
|
65 |
String domain = (String)iFunctionGroups.get(i);
|
|
66 |
iUserDefinedDomain.setPermission(domain,Permission.Allowed);
|
|
67 |
}
|
|
68 |
|
|
69 |
// Select first domain by default
|
|
70 |
iSelectedDomainId = ((ProtectionDomain)
|
|
71 |
iProtectionDomains.get(0)).getIdentifier();
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Gets the protection domains known to the model, including the
|
|
76 |
* "User defined" pseudo-domain.
|
|
77 |
*
|
|
78 |
* @param aIncludeHidden <code>true</code> to include hidden domains
|
|
79 |
* @return array of protection domains
|
|
80 |
*/
|
|
81 |
ProtectionDomain [] getProtectionDomains(boolean aIncludeHidden) {
|
|
82 |
List domains = iProtectionDomains;
|
|
83 |
if (!aIncludeHidden) {
|
|
84 |
// remove hidden domains from the list
|
|
85 |
domains = new ArrayList(iProtectionDomains);
|
|
86 |
for (int i=domains.size()-1; i>=0; i--) {
|
|
87 |
ProtectionDomain domain = (ProtectionDomain)domains.get(i);
|
1
|
88 |
|
|
89 |
if( i ==5 || i == 6)
|
|
90 |
domains.remove(i);
|
0
|
91 |
if (domain.isHidden()) domains.remove(i);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
return (ProtectionDomain[]) domains.toArray(
|
|
95 |
new ProtectionDomain[domains.size()]);
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Gets currently selected protection domain.
|
|
100 |
* @return the currently selected protection domain, <code>null</code>
|
|
101 |
* if none.
|
|
102 |
*/
|
|
103 |
ProtectionDomain getSelectedDomain() {
|
|
104 |
return findProtectionDomain(iSelectedDomainId);
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Selects a protection domain
|
|
109 |
* @param aDomain the domain to select
|
|
110 |
*/
|
|
111 |
void setSelectedDomain(ProtectionDomain aDomain) {
|
|
112 |
if (aDomain == null) {
|
|
113 |
iSelectedDomainId = null;
|
|
114 |
} else {
|
|
115 |
iSelectedDomainId = aDomain.getIdentifier();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Gets known function groups.
|
|
121 |
* @return array of function group names
|
|
122 |
*/
|
|
123 |
String[] getFunctionGroups() {
|
|
124 |
return (String[])iFunctionGroups.toArray(
|
|
125 |
new String[iFunctionGroups.size()]);
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Checks if we are in the "emulated security mode".
|
|
130 |
* @return <code>true</code> if we are enforcing the same permissions
|
|
131 |
* for all protection domains.
|
|
132 |
*/
|
|
133 |
boolean isEmulatedSecurityMode() {
|
|
134 |
return iEmulatedSecurityMode;
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Turns the "emulated security mode" on or off.
|
|
139 |
* @param aEmulatedSecurityMode <code>true</code> to enforce the same
|
|
140 |
* permissions for all protection domains, <code>false</code> to use
|
|
141 |
* default JVM secrity settings.
|
|
142 |
*/
|
|
143 |
void setEmulatedSecurityMode(boolean aEmulatedSecurityMode) {
|
|
144 |
iEmulatedSecurityMode = aEmulatedSecurityMode;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Opens the policy file resource.
|
|
149 |
* @return the input stream for the policy file resource.
|
|
150 |
* Never returns null.
|
|
151 |
* @throws IOException if resource is not found
|
|
152 |
*/
|
|
153 |
private InputStream openPolicyFileResource() throws IOException {
|
|
154 |
InputStream in = getClass().getResourceAsStream(POLICY_FILE_NAME);
|
|
155 |
if (in != null) return in;
|
|
156 |
throw new IOException(POLICY_FILE_NAME);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Finds ProtectionDomain object for the specified identifier.
|
|
161 |
* @param aId the domain identifier
|
|
162 |
* @return ProtectionDomain object if found, null otherwise.
|
|
163 |
*/
|
|
164 |
private ProtectionDomain findProtectionDomain(String aId) {
|
|
165 |
int n = iProtectionDomains.size();
|
|
166 |
for (int i=0; i<n; i++) {
|
|
167 |
ProtectionDomain d = (ProtectionDomain)iProtectionDomains.get(i);
|
|
168 |
if (d.getIdentifier().equals(aId)) {
|
|
169 |
return d;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
return null;
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Writes policy file to the output stream
|
|
177 |
* @param aStream the stream to write to
|
|
178 |
* @throws IOException if an I/O error occurs
|
|
179 |
*/
|
|
180 |
void writePolicyFile(OutputStream aStream) throws IOException {
|
|
181 |
InputStream in = openPolicyFileResource();
|
|
182 |
try {
|
|
183 |
PrintWriter out = new PrintWriter(aStream, false);
|
|
184 |
try {
|
|
185 |
if (!iEmulatedSecurityMode) {
|
|
186 |
// Just copy the original file
|
|
187 |
BufferedReader reader = new BufferedReader(
|
|
188 |
new InputStreamReader(in));
|
|
189 |
String line = null;
|
|
190 |
while ((line = reader.readLine()) != null) {
|
|
191 |
out.println(line);
|
|
192 |
}
|
|
193 |
} else {
|
|
194 |
// Save modified version of the original file
|
|
195 |
out.println("# This is a generated file.");
|
|
196 |
out.println();
|
|
197 |
PolicyFileParser.parse(in, new PolicyFileWriter(out));
|
|
198 |
}
|
|
199 |
} finally {
|
|
200 |
out.flush();
|
|
201 |
}
|
|
202 |
} finally {
|
|
203 |
in.close();
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
/**
|
|
208 |
* Saves the state of the model to the output stream
|
|
209 |
* @param aStream the stream to write to
|
|
210 |
* @throws IOException if an I/O error occurs
|
|
211 |
*/
|
|
212 |
void save(OutputStream aStream) throws IOException {
|
|
213 |
ObjectOutputStream out = new ObjectOutputStream(aStream);
|
|
214 |
out.writeBoolean(iEmulatedSecurityMode);
|
|
215 |
out.writeObject(iSelectedDomainId);
|
|
216 |
out.writeObject(iUserDefinedDomain);
|
|
217 |
}
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Loads the state of the model from the input stream
|
|
221 |
* @param aStream the stream to read from
|
|
222 |
* @throws IOException if an I/O error occurs
|
|
223 |
* @throws ClassNotFoundException if deserialization error occurs
|
|
224 |
*/
|
|
225 |
void load(InputStream aStream) throws IOException, ClassNotFoundException {
|
|
226 |
ObjectInputStream in = new ObjectInputStream(aStream);
|
|
227 |
boolean emulatedSecurityMode = in.readBoolean();
|
|
228 |
String selectedDomainId = (String)in.readObject();
|
|
229 |
ProtectionDomain userDomain = (ProtectionDomain)in.readObject();
|
|
230 |
|
|
231 |
// Verify and store deserialized values
|
|
232 |
iEmulatedSecurityMode = emulatedSecurityMode;
|
|
233 |
if (findProtectionDomain(selectedDomainId) != null) {
|
|
234 |
iSelectedDomainId = selectedDomainId;
|
|
235 |
}
|
|
236 |
if (userDomain.isUserDefined()) {
|
|
237 |
// User defined domain is always the last one
|
|
238 |
iProtectionDomains.set(iProtectionDomains.size()-1,userDomain);
|
|
239 |
iUserDefinedDomain = userDomain;
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* This handler scans the policy file to collect information about
|
|
245 |
* domains, function groups and bindings.
|
|
246 |
*/
|
|
247 |
private class InitialScan implements PolicyFileParser.Handler {
|
|
248 |
private String iProtectionDomain;
|
|
249 |
private String iFunctionGroup;
|
|
250 |
public void handleFormatDecl(String aFormatVersion) {}
|
|
251 |
public void handleEmptyLine() {}
|
|
252 |
public void handleDecl(String aLine) {}
|
|
253 |
public void handleProtectionDomainDecl(String aDomain) {
|
|
254 |
ProtectionDomain domain = findProtectionDomain(aDomain);
|
|
255 |
if (domain == null) {
|
|
256 |
domain = new ProtectionDomain(aDomain);
|
|
257 |
iProtectionDomains.add(domain);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
public void handleFunctionGroupDecl(String aGroupName) {
|
|
261 |
iFunctionGroups.add(aGroupName);
|
|
262 |
}
|
|
263 |
public void handleDomainBindingsDecl(String aProtectionDomain) {
|
|
264 |
iProtectionDomain = aProtectionDomain;
|
|
265 |
}
|
|
266 |
public void handleFunctionGroupBindingDecl(String aFunctionGroup) {
|
|
267 |
iFunctionGroup = aFunctionGroup;
|
|
268 |
}
|
|
269 |
public void handlePermission(Permission aPermission) {
|
|
270 |
ProtectionDomain domain = findProtectionDomain(iProtectionDomain);
|
|
271 |
if (domain != null && iFunctionGroup != null) {
|
|
272 |
if (WILDCARD.equals(iFunctionGroup)) {
|
|
273 |
int n = iFunctionGroups.size();
|
|
274 |
for (int i=0; i<n; i++) {
|
|
275 |
domain.setPermission((String)iFunctionGroups.get(i),
|
|
276 |
aPermission);
|
|
277 |
}
|
|
278 |
} else {
|
|
279 |
domain.setPermission(iFunctionGroup, aPermission);
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
public void handleEndFunctionGroupBinding() {
|
|
285 |
iFunctionGroup = null;
|
|
286 |
}
|
|
287 |
public void handleEndDomainBindings() {
|
|
288 |
iProtectionDomain = null;
|
|
289 |
}
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* This handler writes a modified version of the policy file.
|
|
294 |
* Must only be used in the "emulated" security mode.
|
|
295 |
*/
|
|
296 |
private class PolicyFileWriter implements PolicyFileParser.Handler {
|
|
297 |
private PrintWriter iWriter;
|
|
298 |
private ProtectionDomain iEmulatedDomain;
|
|
299 |
private String iProtectionDomain;
|
|
300 |
|
|
301 |
PolicyFileWriter(PrintWriter aWriter) {
|
|
302 |
if (!isEmulatedSecurityMode()) throw new IllegalStateException();
|
|
303 |
iWriter = aWriter;
|
|
304 |
iEmulatedDomain = getSelectedDomain();
|
|
305 |
}
|
|
306 |
private void printDecl(String aKeyword, String aValue) {
|
|
307 |
iWriter.print('\t');
|
|
308 |
iWriter.print(aKeyword);
|
|
309 |
iWriter.print(' ');
|
|
310 |
// For some reason, strings enclosed in square brackets (such as
|
|
311 |
// [UNTRUSTED] or [*]) are not quoted. That may have some special
|
|
312 |
// meaning to the phone software.
|
|
313 |
int len = aValue.length();
|
|
314 |
if (len >= 2 &&
|
|
315 |
aValue.charAt(0) == '[' &&
|
|
316 |
aValue.charAt(len-1) == ']') {
|
|
317 |
iWriter.println(aValue);
|
|
318 |
} else {
|
|
319 |
iWriter.print('"');
|
|
320 |
iWriter.print(aValue);
|
|
321 |
iWriter.println('"');
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
// PolicyFileParser.Handler interface
|
|
326 |
public void handleFormatDecl(String aFormatVersion) {
|
|
327 |
iWriter.print(FORMAT_VERSION_KEYWORD);
|
|
328 |
iWriter.print(" ");
|
|
329 |
iWriter.println(aFormatVersion);
|
|
330 |
}
|
|
331 |
public void handleEmptyLine() {
|
|
332 |
if (iProtectionDomain == null) {
|
|
333 |
iWriter.println();
|
|
334 |
}
|
|
335 |
}
|
|
336 |
public void handleDecl(String aLine) {
|
|
337 |
if (iProtectionDomain == null) {
|
|
338 |
iWriter.println(aLine);
|
|
339 |
}
|
|
340 |
}
|
|
341 |
public void handleProtectionDomainDecl(String aProtectionDomain) {
|
|
342 |
printDecl(DOMAIN_KEYWORD, aProtectionDomain);
|
|
343 |
}
|
|
344 |
public void handleFunctionGroupDecl(String aGroupName) {
|
|
345 |
iWriter.print('\t');
|
|
346 |
iWriter.print(FUNCTION_GROUP_KEYWORD);
|
|
347 |
iWriter.print(" \"");
|
|
348 |
iWriter.print(aGroupName);
|
|
349 |
iWriter.println('"');
|
|
350 |
}
|
|
351 |
public void handleDomainBindingsDecl(String aProtectionDomain) {
|
|
352 |
printDecl(DOMAIN_BINDINGS_KEYWORD, aProtectionDomain);
|
|
353 |
iWriter.println();
|
|
354 |
iProtectionDomain = aProtectionDomain;
|
|
355 |
}
|
|
356 |
public void handleFunctionGroupBindingDecl(String aFunctionGroup) {}
|
|
357 |
public void handlePermission(Permission aPermission) {}
|
|
358 |
public void handleEndFunctionGroupBinding() {}
|
|
359 |
public void handleEndDomainBindings() {
|
|
360 |
int n = iFunctionGroups.size();
|
|
361 |
for (int i=0; i<n; i++) {
|
|
362 |
String group = (String)iFunctionGroups.get(i);
|
|
363 |
Permission p = iEmulatedDomain.getPermission(group);
|
|
364 |
if (!p.isDenied()) {
|
|
365 |
iWriter.print('\t');
|
|
366 |
printDecl(FUNCTION_GROUP_BINDING_KEYWORD, group);
|
|
367 |
iWriter.println();
|
|
368 |
p.print(iWriter);
|
|
369 |
iWriter.print("\t\t");
|
|
370 |
iWriter.println(END_FUNCTION_GROUP_BINDING_KEYWORD);
|
|
371 |
iWriter.println();
|
|
372 |
}
|
|
373 |
}
|
|
374 |
iWriter.print('\t');
|
|
375 |
iWriter.println(END_DOMAIN_BINDINGS_KEYWORD);
|
|
376 |
iProtectionDomain = null;
|
|
377 |
}
|
|
378 |
}
|
|
379 |
}
|