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