javacommons/security/javasrc/com/nokia/mj/impl/security/midp/storage/SecurityStorage.java
changeset 69 773449708c84
parent 61 bf7ee68962da
child 66 2455ef1f5bbc
child 79 2f468c1958d0
equal deleted inserted replaced
61:bf7ee68962da 69:773449708c84
    99     public static final int AUTHENTICATION_DOMAIN_NAME_QUERY = 1;
    99     public static final int AUTHENTICATION_DOMAIN_NAME_QUERY = 1;
   100     public static final int AUTHENTICATION_DOMAIN_CATEGORY_QUERY = 2;
   100     public static final int AUTHENTICATION_DOMAIN_CATEGORY_QUERY = 2;
   101     public static final int AUTHENTICATION_ROOT_HASH_QUERY = 4;
   101     public static final int AUTHENTICATION_ROOT_HASH_QUERY = 4;
   102     public static final int AUTHENTICATION_JAR_HASH_QUERY = 8;
   102     public static final int AUTHENTICATION_JAR_HASH_QUERY = 8;
   103     public static final int AUTHENTICATION_VALID_CERTS_QUERY = 16;
   103     public static final int AUTHENTICATION_VALID_CERTS_QUERY = 16;
   104 
       
   105     private static final int NOT_FOUND = -2;
       
   106     private static final int REMOVED = -1;
       
   107 
   104 
   108     /**
   105     /**
   109      * Constructor
   106      * Constructor
   110      */
   107      */
   111     public SecurityStorage()
   108     public SecurityStorage()
   433                 && grantedPermissions.size() > 0
   430                 && grantedPermissions.size() > 0
   434                 && appUID != null)
   431                 && appUID != null)
   435         {
   432         {
   436             if (oldAppUID != null)
   433             if (oldAppUID != null)
   437             {
   434             {
   438                 updateGrantedPermissions(
   435                 // remove granted permissions
   439                     appUID,
   436                 removeGrantedPermissions(appUID);
   440                     oldAppUID,
   437                 
   441                     grantedPermissions);
   438                 // write granted permissions
       
   439                 writeGrantedPermissions(appUID, null, grantedPermissions);
   442                 return;
   440                 return;
   443             }
   441             }
   444             // put all the function group names into a vector and use it
   442             // put all the function group names into a vector and use it
   445             // to check if details of a certain function group have already
   443             // to check if details of a certain function group have already
   446             // been writen into storage (it's faster than querying the
   444             // been writen into storage (it's faster than querying the
  1244         }
  1242         }
  1245         catch (StorageException e)
  1243         catch (StorageException e)
  1246             {/* move on with defaults */}
  1244             {/* move on with defaults */}
  1247     }
  1245     }
  1248 
  1246 
  1249 
       
  1250     private void updateGrantedPermissions(Uid newAppUID, Uid oldAppUID, Vector grantedPermissions)
       
  1251     {
       
  1252         // the vector containing the newGrantedPermissions
       
  1253         Vector newGrantedPermissions = new Vector();
       
  1254 
       
  1255         // get the old permissions & settings
       
  1256         Vector oldPermissions = readGrantedPermissions(oldAppUID);
       
  1257 
       
  1258         // filter out the the brand new permissions
       
  1259         // (permissions which are not found among the old permissions)
       
  1260         if (oldPermissions != null)
       
  1261         {
       
  1262             int index=0;
       
  1263             while (index < grantedPermissions.size())
       
  1264             {
       
  1265                 // instead of calling Vector.removeElement(p) we will do the
       
  1266                 // remove manually, since the search is to be based on
       
  1267                 // the permission without the settings
       
  1268                 PolicyBasedPermission p = (PolicyBasedPermission)
       
  1269                                           grantedPermissions.elementAt(index);
       
  1270                 int status = removeElement(oldPermissions, p);
       
  1271                 switch (status)
       
  1272                 {
       
  1273                 case NOT_FOUND:
       
  1274                     index++;
       
  1275                     break;
       
  1276                 case REMOVED:
       
  1277                     grantedPermissions.removeElementAt(index);
       
  1278                     break;
       
  1279                 default:
       
  1280                     // different settings
       
  1281                     UserSecuritySettings oldSettings
       
  1282                     = ((PolicyBasedPermission)oldPermissions
       
  1283                        .elementAt(status)).getUserSecuritySettings();
       
  1284                     UserSecuritySettings newSettings
       
  1285                     = p.getUserSecuritySettings();
       
  1286                     if (oldSettings != null
       
  1287                             && newSettings != null)
       
  1288                     {
       
  1289                         boolean activeSettings = false;
       
  1290                         if (oldSettings.isActive() 
       
  1291                             || newSettings.isActive())
       
  1292                         {
       
  1293                             activeSettings = true;
       
  1294                         }
       
  1295                         newGrantedPermissions.addElement(
       
  1296                             new PolicyBasedPermissionImpl(
       
  1297                                 p.getName(),
       
  1298                                 p.getTarget(),
       
  1299                                 p.getActionList(),
       
  1300                                 p.getType(),
       
  1301                                 new UserSecuritySettingsImpl(
       
  1302                                     newSettings.getName(),
       
  1303                                     oldSettings.getCurrentInteractionMode(),
       
  1304                                     newSettings.getAllowedInteractionModes(),
       
  1305                                     oldSettings.getBlanketPrompt(),
       
  1306                                     activeSettings)));
       
  1307                     }
       
  1308                     else
       
  1309                     {
       
  1310                         newGrantedPermissions.addElement(p);
       
  1311                     }
       
  1312                     grantedPermissions.removeElementAt(index);
       
  1313                     break;
       
  1314                 }
       
  1315             }
       
  1316         }
       
  1317         // write what's left from the granted permissions
       
  1318         writeGrantedPermissions(newAppUID, null, grantedPermissions, true /* preserveSettings */);
       
  1319         for (int i=0; i<newGrantedPermissions.size(); i++)
       
  1320         {
       
  1321             grantedPermissions.addElement(newGrantedPermissions.elementAt(i));
       
  1322         }
       
  1323 
       
  1324         // remove what's left from the old permissions
       
  1325         if (oldPermissions != null)
       
  1326         {
       
  1327             for (int i=0; i<oldPermissions.size(); i++)
       
  1328             {
       
  1329                 PolicyBasedPermission p = (PolicyBasedPermission)
       
  1330                                           oldPermissions.elementAt(i);
       
  1331                 StorageEntry removePermissionQuery = new StorageEntry();
       
  1332                 removePermissionQuery.addAttribute(new StorageAttribute(
       
  1333                                                        StorageAttribute.ID,
       
  1334                                                        oldAppUID.getStringValue()));
       
  1335                 removePermissionQuery.addAttribute(new StorageAttribute(
       
  1336                                                        StorageNames.CLASS,
       
  1337                                                        p.getName()));
       
  1338                 if (p.getTarget() != null
       
  1339                         && p.getTarget().length() > 0)
       
  1340                 {
       
  1341                     removePermissionQuery.addAttribute(new StorageAttribute(
       
  1342                                                            StorageNames.NAME,
       
  1343                                                            p.getTarget()));
       
  1344                 }
       
  1345                 if (p.getActionList() != null
       
  1346                         && p.getActionList().length() > 0)
       
  1347                 {
       
  1348                     removePermissionQuery.addAttribute(new StorageAttribute(
       
  1349                                                            StorageNames.ACTION,
       
  1350                                                            p.getActionList()));
       
  1351                 }
       
  1352                 doStorageRemove(StorageNames.MIDP_PERMISSIONS_TABLE,
       
  1353                                 removePermissionQuery);
       
  1354                 // remove the setting also if not used by some other permission
       
  1355                 UserSecuritySettings settings =
       
  1356                     p.getUserSecuritySettings();
       
  1357                 if (settings != null)
       
  1358                 {
       
  1359                     StorageEntry permissionsQuery = new StorageEntry();
       
  1360                     permissionsQuery.addAttribute(new StorageAttribute(
       
  1361                                                       StorageAttribute.ID,
       
  1362                                                       newAppUID.getStringValue()));
       
  1363                     permissionsQuery.addAttribute(new StorageAttribute(
       
  1364                                                       StorageNames.FUNCTION_GROUP,
       
  1365                                                       settings.getName()));
       
  1366                     StorageEntry[] permissions = doStorageSearch(
       
  1367                                                      StorageNames.MIDP_PERMISSIONS_TABLE, permissionsQuery);
       
  1368                     if (permissions == null || (permissions != null
       
  1369                                                 && permissions.length == 0))
       
  1370                     {
       
  1371                         // remove the orphaned settings from settings table
       
  1372                         StorageEntry removeSettingsQuery = new StorageEntry();
       
  1373                         removeSettingsQuery.addAttribute(new StorageAttribute(
       
  1374                                                              StorageAttribute.ID,
       
  1375                                                              newAppUID.getStringValue()));
       
  1376                         removeSettingsQuery.addAttribute(new StorageAttribute(
       
  1377                                                              StorageNames.FUNCTION_GROUP,
       
  1378                                                              settings.getName()));
       
  1379                         doStorageRemove(StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
  1380                                         removeSettingsQuery);
       
  1381                     }
       
  1382                 }
       
  1383             }
       
  1384         }
       
  1385         // write the new permissions
       
  1386         writeGrantedPermissions(newAppUID, null, newGrantedPermissions, true /* preserveSettings */);
       
  1387     }
       
  1388 
       
  1389     private AuthenticationStorageData readAuthenticationStorageData(
  1247     private AuthenticationStorageData readAuthenticationStorageData(
  1390         Uid appUID, String aAppName, String aAppVersion,
  1248         Uid appUID, String aAppName, String aAppVersion,
  1391         String aAppVendor, int readFilter)
  1249         String aAppVendor, int readFilter)
  1392     {
  1250     {
  1393         if (appUID == null && (aAppName == null
  1251         if (appUID == null && (aAppName == null
  1836                 return attrValue = tmp;
  1694                 return attrValue = tmp;
  1837             }
  1695             }
  1838         }
  1696         }
  1839         return attrValue;
  1697         return attrValue;
  1840     }
  1698     }
  1841 
       
  1842     private int removeElement(Vector elements, PolicyBasedPermission element)
       
  1843     {
       
  1844         PolicyBasedPermissionImpl p1 = new PolicyBasedPermissionImpl(
       
  1845             element.getName(),
       
  1846             element.getTarget(),
       
  1847             element.getActionList(),
       
  1848             null);
       
  1849         for (int i=0; i<elements.size(); i++)
       
  1850         {
       
  1851             PolicyBasedPermission tmp = (PolicyBasedPermission)elements
       
  1852                                         .elementAt(i);
       
  1853             PolicyBasedPermissionImpl p2 = new PolicyBasedPermissionImpl(
       
  1854                 tmp.getName(),
       
  1855                 tmp.getTarget(),
       
  1856                 tmp.getActionList(),
       
  1857                 null);
       
  1858             if (p1.equals(p2))
       
  1859             {
       
  1860                 UserSecuritySettings s1 = element.getUserSecuritySettings();
       
  1861                 UserSecuritySettings s2 = tmp.getUserSecuritySettings();
       
  1862                 if ((s1 == null && s2 == null)
       
  1863                         || (s1 != null
       
  1864                             && s2 != null
       
  1865                             && s1.equals(s2)))
       
  1866                 {
       
  1867                     // identical permissions
       
  1868                     elements.removeElementAt(i);
       
  1869                     return REMOVED;
       
  1870                 }
       
  1871                 return i;
       
  1872             }
       
  1873         }
       
  1874         return NOT_FOUND;
       
  1875     }
       
  1876     
  1699     
  1877     private String encodeFunctionGroup(int type, String name)
  1700     private String encodeFunctionGroup(int type, String name)
  1878     {
  1701     {
  1879         return (type + ";" + name);
  1702         return (type + ";" + name);
  1880     }
  1703     }