javacommons/security/javasrc/com/nokia/mj/impl/security/midp/storage/SecurityStorage.java
branchRCL_3
changeset 19 04becd199f91
child 25 9ac0a0a7da70
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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 "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 *
       
    16 */
       
    17 
       
    18 package com.nokia.mj.impl.security.midp.storage;
       
    19 
       
    20 import com.nokia.mj.impl.utils.Uid;
       
    21 import com.nokia.mj.impl.security.midp.common.AuthorizationRestriction;
       
    22 import com.nokia.mj.impl.security.midp.common.AuthorizationRestrictionAttribute;
       
    23 import com.nokia.mj.impl.security.midp.common.AuthenticationAttribute;
       
    24 import com.nokia.mj.impl.security.midp.common.PolicyBasedPermission;
       
    25 import com.nokia.mj.impl.security.midp.common.PolicyBasedPermissionImpl;
       
    26 import com.nokia.mj.impl.security.midp.common.UserSecuritySettings;
       
    27 import com.nokia.mj.impl.security.midp.common.UserSecuritySettingsImpl;
       
    28 import com.nokia.mj.impl.security.midp.common.SecurityAttributes;
       
    29 import com.nokia.mj.impl.security.midp.common.GeneralSecuritySettings;
       
    30 import com.nokia.mj.impl.storage.*;
       
    31 import com.nokia.mj.impl.utils.Logger;
       
    32 import com.nokia.mj.impl.utils.Tokenizer;
       
    33 import java.util.Vector;
       
    34 import java.util.Hashtable;
       
    35 
       
    36 /**
       
    37  * Class for handling the security related storage data. There are three types
       
    38  * of security data:
       
    39  *    - authentication data, which includes:
       
    40  *        - security domain
       
    41  *        - jar hash
       
    42  *        - root hash
       
    43  *        - succesfully validated certificates
       
    44  *    - authorization data, which includes the set of granted permissions
       
    45  *    - app access authorization data, which includes:
       
    46  *        - vendor name
       
    47  *        - domain name
       
    48  *        - signers list (the end-entity certificates from the succesfully
       
    49  *          validated certificate chains)
       
    50  */
       
    51 public final class SecurityStorage
       
    52 {
       
    53     /* Session to storage */
       
    54     private StorageSession session = null;
       
    55 
       
    56     // boolean indicating if the storage session needs to be destroyed.
       
    57     // This flag is modified if the storage session has been provided
       
    58     // to this class, so that the one providing the storage session
       
    59     // will destroy it
       
    60     private boolean destroyStorageSession = true;
       
    61 
       
    62     /*
       
    63      * The allowed modes info is stored as a 4-bit constant:
       
    64      *     X(oneshot)X(session)X(blanket)X(no)
       
    65      * e.g. 1011 (=11) means that oneshot, blanket and no are allowed.
       
    66      * The following constants are used to encode/decode the allowed modes
       
    67      * into/from a 4-bit number
       
    68      */
       
    69     private static final int NO_MASK = 1;
       
    70     private static final int BLANKET_MASK = 2;
       
    71     private static final int SESSION_MASK = 4;
       
    72     private static final int ONESHOT_MASK = 8;
       
    73 
       
    74     /*
       
    75      * The actual interaction with the storage session is kept in one place
       
    76      * (so that error handling is centralized, instead of being spread here
       
    77      * and there). These constants specify which storage operation to be
       
    78      * executed
       
    79      */
       
    80     private static final int STORAGE_OPEN = 1;
       
    81     private static final int STORAGE_CLOSE = 2;
       
    82     private static final int STORAGE_SEARCH = 3;
       
    83     private static final int STORAGE_WRITE = 4;
       
    84     private static final int STORAGE_UPDATE = 5;
       
    85     private static final int STORAGE_REMOVE = 6;
       
    86 
       
    87     /*
       
    88      * Filters for specifying which app access authorization data to be read
       
    89      * from storage
       
    90      */
       
    91     public static final int APP_ACCESS_AUTH_DOMAIN_QUERY = 1;
       
    92     public static final int APP_ACCESS_AUTH_VENDOR_QUERY = 2;
       
    93     public static final int APP_ACCESS_AUTH_SIGNERS_LIST_QUERY = 4;
       
    94 
       
    95     /*
       
    96      * Filters for specifying which authentication data to be read
       
    97      * from storage
       
    98      */
       
    99     public static final int AUTHENTICATION_DOMAIN_NAME_QUERY = 1;
       
   100     public static final int AUTHENTICATION_DOMAIN_CATEGORY_QUERY = 2;
       
   101     public static final int AUTHENTICATION_ROOT_HASH_QUERY = 4;
       
   102     public static final int AUTHENTICATION_JAR_HASH_QUERY = 8;
       
   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 
       
   108     /**
       
   109      * Constructor
       
   110      */
       
   111     public SecurityStorage()
       
   112     {
       
   113         doStorageOpen();
       
   114     }
       
   115 
       
   116     /**
       
   117      * Constructor
       
   118      */
       
   119     public SecurityStorage(StorageSession session)
       
   120     {
       
   121         if (session != null)
       
   122         {
       
   123             destroyStorageSession = false;
       
   124             this.session = session;
       
   125         }
       
   126         else
       
   127         {
       
   128             doStorageOpen();
       
   129         }
       
   130     }
       
   131 
       
   132     /**
       
   133      * Destroies the session to the storage
       
   134      */
       
   135     public void close()
       
   136     {
       
   137         doStorageClose();
       
   138     }
       
   139 
       
   140     /**
       
   141      * Retrieves the storage session
       
   142      */
       
   143     public StorageSession getStorageSession()
       
   144     {
       
   145         return session;
       
   146     }
       
   147 
       
   148     /**
       
   149      * Retrieves the authorization restrictions of a certain application
       
   150      *
       
   151      * @param appUID the UID of the application whose authorization
       
   152      *               restrictions are retrieved
       
   153      * @return       an array of all the authorization restrictions or null if
       
   154      *               no restrictions found
       
   155      */
       
   156     public AuthorizationRestriction[] readAuthorizationRestrictions(Uid appUID)
       
   157     {
       
   158         if (appUID == null)
       
   159         {
       
   160             return null;
       
   161         }
       
   162         Vector authRestrictions = new Vector();
       
   163         StorageEntry query = new StorageEntry();
       
   164         query.addAttribute(new StorageAttribute(
       
   165                                StorageAttribute.ID,
       
   166                                appUID.getStringValue()));
       
   167         int i=1;
       
   168         while (true)
       
   169         {
       
   170             String mainAttrName = AuthorizationRestrictionAttribute
       
   171                                   .MAIN_ATTRIBUTE_PREFIX  + i;
       
   172             StorageAttribute mainAttr = new StorageAttribute(
       
   173                 StorageNames.NAME, mainAttrName);
       
   174             query.addAttribute(mainAttr);
       
   175             query.addAttribute(new StorageAttribute(StorageNames.VALUE, ""));
       
   176             StorageEntry[] entries = doStorageSearch(
       
   177                                          StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, query);
       
   178             if (entries != null && entries.length > 0)
       
   179             {
       
   180                 String mainAttrValue = getStorageAttributeValue(entries[0],
       
   181                                        StorageNames.VALUE);
       
   182                 String secondAttrName = AuthorizationRestrictionAttribute
       
   183                                         .SECOND_ATTRIBUTE_PREFIX + i;
       
   184                 query.removeAttribute(mainAttr);
       
   185                 StorageAttribute secondAttr = new StorageAttribute(
       
   186                     StorageNames.NAME, secondAttrName);
       
   187                 query.addAttribute(secondAttr);
       
   188                 entries = doStorageSearch(
       
   189                               StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE,
       
   190                               query);
       
   191                 if (entries != null && entries.length > 0)
       
   192                 {
       
   193                     String secondAttrValue = getStorageAttributeValue(
       
   194                                                  entries[0], StorageNames.VALUE);
       
   195                     authRestrictions.addElement(
       
   196                         new AuthorizationRestrictionAttribute(
       
   197                             mainAttrName, mainAttrValue,
       
   198                             secondAttrName, secondAttrValue));
       
   199                 }
       
   200                 else
       
   201                 {
       
   202                     authRestrictions.addElement(
       
   203                         new AuthorizationRestrictionAttribute(
       
   204                             mainAttrName, mainAttrValue,
       
   205                             null, null));
       
   206                 }
       
   207                 query.removeAttribute(secondAttr);
       
   208             }
       
   209             else
       
   210             {
       
   211                 break;
       
   212             }
       
   213             i++;
       
   214         }
       
   215         if (authRestrictions.size() == 0)
       
   216         {
       
   217             return null;
       
   218         }
       
   219         AuthorizationRestriction[] authorizationRestrictions =
       
   220             new AuthorizationRestriction[authRestrictions.size()];
       
   221         authRestrictions.copyInto(authorizationRestrictions);
       
   222         return authorizationRestrictions;
       
   223     }
       
   224 
       
   225     public AppAccessAuthorizationStorageData readAppAccessAuthorizationStorageData(Uid appUID)
       
   226     {
       
   227         // read everything
       
   228         return readAppAccessAuthorizationStorageData(appUID,
       
   229                 APP_ACCESS_AUTH_DOMAIN_QUERY | APP_ACCESS_AUTH_VENDOR_QUERY | APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   230     }
       
   231 
       
   232     /**
       
   233      * Retrieves the app access authorization data of a certain application
       
   234      *
       
   235      * @param aAppName    the name of the application for which the
       
   236      *                    authentication data is queried
       
   237      * @param aAppVersion the version of the application for which the
       
   238      *                    authentication data is queried
       
   239      * @param aAppVendor  the vendor of the application for which the
       
   240      *                    authentication data is queried
       
   241      * @param readFilter  specifies which app access authorization data to be
       
   242      *                    read
       
   243      * @return            the app access authorization data associated with the
       
   244      *                    queried application
       
   245      */
       
   246     public AppAccessAuthorizationStorageData readAppAccessAuthorizationStorageData(
       
   247         String aAppName,
       
   248         String aAppVersion,
       
   249         String aAppVendor,
       
   250         int readFilter)
       
   251     {
       
   252         return readAppAccessAuthorizationStorageData(null /* appUID */,
       
   253                 aAppName,
       
   254                 aAppVersion,
       
   255                 aAppVendor,
       
   256                 readFilter);
       
   257     }
       
   258 
       
   259     /**
       
   260      * Retrieves the app access authorization data of a certain application
       
   261      *
       
   262      * @param appUID     the UID of the application whose app access
       
   263      *                   authorization
       
   264      *                   data is retrieved
       
   265      * @param readFilter specifies which app access authorization data to be
       
   266      *                   read
       
   267      * @return           the app access authorization data
       
   268      */
       
   269     public AppAccessAuthorizationStorageData readAppAccessAuthorizationStorageData(
       
   270         Uid appUID, int readFilter)
       
   271     {
       
   272         return readAppAccessAuthorizationStorageData(appUID,
       
   273                 null /* appName */,
       
   274                 null /* appVersion */,
       
   275                 null /* appvendor*/,
       
   276                 readFilter);
       
   277     }
       
   278 
       
   279     /**
       
   280      * Retrieves the permissions granted to a certain application.
       
   281      * The user permissions are retrieved first and then the rest
       
   282      * of the permissions
       
   283      *
       
   284      * @param appUID the UID of the application whose granted permissions
       
   285      *               are retrieved
       
   286      * @return      aAn array of the granted permissions
       
   287      */
       
   288     public Vector readGrantedPermissions(Uid appUID)
       
   289     {
       
   290         if (appUID == null)
       
   291         {
       
   292             return null;
       
   293         }
       
   294         StorageEntry query = new StorageEntry();
       
   295         query.addAttribute(new StorageAttribute(
       
   296                                StorageAttribute.ID,
       
   297                                appUID.getStringValue()));
       
   298         StorageEntry[] permEntries = doStorageSearch(
       
   299                                          StorageNames.MIDP_PERMISSIONS_TABLE, query);
       
   300         Vector permissions = new Vector();
       
   301         Vector nonUserPermissions = new Vector();
       
   302         // put all the function group names into a hashtable and use it
       
   303         // to check if details of a certain function group have already
       
   304         // been read from storage (it's faster than querying the
       
   305         // storage)
       
   306         Hashtable fGroups = new Hashtable();
       
   307         if (permEntries != null && permEntries.length > 0)
       
   308         {
       
   309             for (int i=0; i<permEntries.length; i++)
       
   310             {
       
   311                 String className = getStorageAttributeValue(permEntries[i],
       
   312                                    StorageNames.CLASS);
       
   313                 String target = getStorageAttributeValue(permEntries[i],
       
   314                                 StorageNames.NAME);
       
   315                 String actionList = getStorageAttributeValue(permEntries[i],
       
   316                                     StorageNames.ACTION);
       
   317                 String fgName = getStorageAttributeValue(permEntries[i],
       
   318                                 StorageNames.FUNCTION_GROUP);
       
   319                 if (fgName != null)
       
   320                 {
       
   321                     UserSecuritySettings cachedSettings =
       
   322                         (UserSecuritySettings)fGroups.get(fgName);
       
   323                     if (cachedSettings != null)
       
   324                     {
       
   325                         permissions.addElement(
       
   326                             new PolicyBasedPermissionImpl(
       
   327                                 className,
       
   328                                 target,
       
   329                                 actionList,
       
   330                                 cachedSettings));
       
   331                     }
       
   332                     else
       
   333                     {
       
   334                         // query the settings details
       
   335                         StorageEntry fgQuery = new StorageEntry();
       
   336                         fgQuery.addAttribute(new StorageAttribute(
       
   337                                                  StorageAttribute.ID,
       
   338                                                  appUID.getStringValue()));
       
   339                         fgQuery.addAttribute(new StorageAttribute(
       
   340                                                  StorageNames.FUNCTION_GROUP,
       
   341                                                  fgName));
       
   342                         fgQuery.addAttribute(new StorageAttribute(
       
   343                                                  StorageNames.ALLOWED_SETTINGS,
       
   344                                                  ""));
       
   345                         fgQuery.addAttribute(new StorageAttribute(
       
   346                                                  StorageNames.CURRENT_SETTING,
       
   347                                                  ""));
       
   348                         fgQuery.addAttribute(new StorageAttribute(
       
   349                                                  StorageNames.BLANKET_PROMPT,
       
   350                                                  ""));
       
   351                         StorageEntry[] fgEntries = doStorageSearch(
       
   352                                                        StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
   353                                                        fgQuery);
       
   354                         if (fgEntries != null && fgEntries.length > 0)
       
   355                         {
       
   356                             try
       
   357                             {
       
   358                                 int allowedSettings = Integer.valueOf(
       
   359                                                           getStorageAttributeValue(fgEntries[0],
       
   360                                                                                    StorageNames.ALLOWED_SETTINGS)).intValue();
       
   361                                 int currentSetting = Integer.valueOf(
       
   362                                                          getStorageAttributeValue(fgEntries[0],
       
   363                                                                                   StorageNames.CURRENT_SETTING)).intValue();
       
   364                                 String blanketPrompt = getStorageAttributeValue(fgEntries[0],
       
   365                                                        StorageNames.BLANKET_PROMPT);
       
   366                                 UserSecuritySettings settings = new UserSecuritySettingsImpl(
       
   367                                     fgName,
       
   368                                     currentSetting,
       
   369                                     decodeAllowedSettings(allowedSettings),
       
   370                                     ("1".equals(blanketPrompt) ? true : false));
       
   371                                 permissions.addElement(
       
   372                                     new PolicyBasedPermissionImpl(
       
   373                                         className,
       
   374                                         target,
       
   375                                         actionList,
       
   376                                         settings));
       
   377                                 fGroups.put(fgName, settings);
       
   378                             }
       
   379                             catch (NumberFormatException e)
       
   380                             {
       
   381                                 Logger.ELOG(Logger.EJavaSecurity, "Permission read failed", e);
       
   382                             }
       
   383                         }
       
   384                     }
       
   385                 }
       
   386                 else
       
   387                 {
       
   388                     // is not a user permission
       
   389                     nonUserPermissions.addElement(
       
   390                         new PolicyBasedPermissionImpl(
       
   391                             className,
       
   392                             target,
       
   393                             actionList,
       
   394                             null));
       
   395                 }
       
   396                 // cleanup the query, so it can be used at the next interation
       
   397                 query.removeAttribute(fgName);
       
   398             }
       
   399         }
       
   400         // add the non user permissions into the set of returned permissions
       
   401         for (int i=0; i<nonUserPermissions.size(); i++)
       
   402         {
       
   403             permissions.addElement(nonUserPermissions.elementAt(i));
       
   404         }
       
   405         if (permissions.size() == 0)
       
   406         {
       
   407             return null;
       
   408         }
       
   409         return permissions;
       
   410     }
       
   411 
       
   412     /**
       
   413      * Stores the set of permissions granted to a certain application.
       
   414      * If this is an update (oldAppUID parameter is not null), then the
       
   415      * settings of the old application are retained
       
   416      *
       
   417      * @param appUID               the UID of the application to whome the
       
   418      *                             permissions are granted
       
   419      * @param oldAppUID            the UID if the application being updated
       
   420      *                             (if applicable) or null otherwise
       
   421      * @param grantedPermissions   the set of granted permissions
       
   422      */
       
   423     public void writeGrantedPermissions(Uid appUID, Uid oldAppUID, Vector grantedPermissions, boolean preserveSettings)
       
   424     {
       
   425         if (grantedPermissions != null
       
   426                 && grantedPermissions.size() > 0
       
   427                 && appUID != null)
       
   428         {
       
   429             if (oldAppUID != null)
       
   430             {
       
   431                 updateGrantedPermissions(
       
   432                     appUID,
       
   433                     oldAppUID,
       
   434                     grantedPermissions);
       
   435                 return;
       
   436             }
       
   437             // put all the function group names into a vector and use it
       
   438             // to check if details of a certain function group have already
       
   439             // been writen into storage (it's faster than querying the
       
   440             // storage or overwriting the storage data)
       
   441             Vector fGroups = new Vector();
       
   442             for (int i=0; i<grantedPermissions.size(); i++)
       
   443             {
       
   444                 PolicyBasedPermission p = (PolicyBasedPermission)
       
   445                                           grantedPermissions.elementAt(i);
       
   446                 if (p == null)
       
   447                 {
       
   448                     // move to the next permission
       
   449                     continue;
       
   450                 }
       
   451                 StorageEntry permEntry = new StorageEntry();
       
   452                 permEntry.addAttribute(new StorageAttribute(
       
   453                                            StorageAttribute.ID,
       
   454                                            appUID.getStringValue()));
       
   455                 permEntry.addAttribute(new StorageAttribute(
       
   456                                            StorageNames.CLASS,
       
   457                                            p.getName()));
       
   458                 permEntry.addAttribute(new StorageAttribute(
       
   459                                            StorageNames.NAME,
       
   460                                            p.getTarget()));
       
   461                 if (p.getActionList() != null
       
   462                         && p.getActionList().length() > 0)
       
   463                 {
       
   464                     permEntry.addAttribute(new StorageAttribute(
       
   465                                                StorageNames.ACTION,
       
   466                                                p.getActionList()));
       
   467                 }
       
   468                 UserSecuritySettings settings =
       
   469                     p.getUserSecuritySettings();
       
   470                 if (settings != null)
       
   471                 {
       
   472                     StorageEntry fgEntry = new StorageEntry();
       
   473                     fgEntry.addAttribute(new StorageAttribute(
       
   474                                              StorageAttribute.ID,
       
   475                                              appUID.getStringValue()));
       
   476                     // update the MIDP_FUNC_GRP_SETTINGS_TABLE table as well
       
   477                     // (if not already done)
       
   478                     String fgName = settings.getName();
       
   479                     StorageAttribute fgAttribute = new StorageAttribute(
       
   480                         StorageNames.FUNCTION_GROUP,
       
   481                         fgName);
       
   482                     permEntry.addAttribute(fgAttribute);
       
   483                     // Do we need to check if the settings which already exist
       
   484                     // are equal to the ones which are to be inserted?
       
   485                     // This won't even happen, since all the settings are read
       
   486                     // from the security policy
       
   487                     // If we don't check, then the first settings are stored and are
       
   488                     // available for all the permissions
       
   489                     if (!fGroups.contains(fgName))
       
   490                     {
       
   491                         fGroups.addElement(fgName);
       
   492                         if (!preserveSettings
       
   493                                 || readUserSecuritySettings(appUID, fgName) == null)
       
   494                         {
       
   495                             fgEntry.addAttribute(new StorageAttribute(
       
   496                                                      StorageNames.FUNCTION_GROUP,
       
   497                                                      fgName));
       
   498                             fgEntry.addAttribute(new StorageAttribute(
       
   499                                                      StorageNames.ALLOWED_SETTINGS,
       
   500                                                      "" + encodeAllowedSettings(
       
   501                                                          settings.getAllowedInteractionModes())));
       
   502                             fgEntry.addAttribute(new StorageAttribute(
       
   503                                                      StorageNames.CURRENT_SETTING,
       
   504                                                      "" + settings.getCurrentInteractionMode()));
       
   505                             fgEntry.addAttribute(new StorageAttribute(
       
   506                                                      StorageNames.BLANKET_PROMPT,
       
   507                                                      settings.getBlanketPrompt() ? "1" : "0"));
       
   508                             doStorageWrite(
       
   509                                 StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
   510                                 fgEntry);
       
   511                         }
       
   512                     }
       
   513                 }
       
   514                 doStorageWrite(StorageNames.MIDP_PERMISSIONS_TABLE,
       
   515                                permEntry);
       
   516             }
       
   517         }
       
   518     }
       
   519 
       
   520     public void writeGrantedPermissions(Uid appUID, Uid oldAppUID, Vector grantedPermissions)
       
   521     {
       
   522         writeGrantedPermissions(appUID,oldAppUID,grantedPermissions, false /*preserveSettings*/);
       
   523     }
       
   524 
       
   525     /**
       
   526      * Removes from storage all the permissions granted to a certain
       
   527      * application
       
   528      *
       
   529      * @param appUID The UID of the application whose permissions are
       
   530      *               removed
       
   531      */
       
   532     public void removeGrantedPermissions(Uid appUid)
       
   533     {
       
   534         if (appUid != null)
       
   535         {
       
   536             // delete everything from MIDP_PERMISSIONS_TABLE
       
   537             doStorageRemove(StorageNames.MIDP_PERMISSIONS_TABLE, appUid);
       
   538             // delete everything from MIDP_FUNC_GRP_SETTINGS_TABLE
       
   539             doStorageRemove(StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE, appUid);
       
   540         }
       
   541     }
       
   542 
       
   543     /**
       
   544      * Reads the authentication data associated with certain application
       
   545      *
       
   546      * @param aAppName    the name of the application for which the
       
   547      *                    authentication data is queried
       
   548      * @param aAppVersion the version of the application for which the
       
   549      *                    authentication data is queried
       
   550      * @param aAppVendor  the vendor of the application for which the
       
   551      *                    authentication data is queried
       
   552      * @return            the authentication data associated with the
       
   553      *                    queried application
       
   554      */
       
   555     public AuthenticationStorageData readAuthenticationStorageData(
       
   556         String aAppName,
       
   557         String aAppVersion,
       
   558         String aAppVendor,
       
   559         int readFilter)
       
   560     {
       
   561         return readAuthenticationStorageData(null /* appUID */,
       
   562                                              aAppName,
       
   563                                              aAppVersion,
       
   564                                              aAppVendor,
       
   565                                              readFilter);
       
   566     }
       
   567 
       
   568     /**
       
   569      * Reads the authentication data
       
   570      *
       
   571      * @param appUID the UID of the application whose authentication data is
       
   572      *               retrieved
       
   573      * @return       the authentication data associated with the application
       
   574      *               identified by the appUID
       
   575      */
       
   576     public AuthenticationStorageData readAuthenticationStorageData(Uid appUID)
       
   577     {
       
   578         return readAuthenticationStorageData(appUID,
       
   579                                              null /* appName */,
       
   580                                              null /* appVersion */,
       
   581                                              null /* appvendor*/,
       
   582                                              AUTHENTICATION_DOMAIN_NAME_QUERY
       
   583                                              | AUTHENTICATION_DOMAIN_CATEGORY_QUERY
       
   584                                              | AUTHENTICATION_ROOT_HASH_QUERY
       
   585                                              | AUTHENTICATION_JAR_HASH_QUERY
       
   586                                              | AUTHENTICATION_VALID_CERTS_QUERY/* read everything */);
       
   587     }
       
   588 
       
   589     /**
       
   590      * Reads the protection domain associated with certain application
       
   591      *
       
   592      * @param appUID the UID of the application whose protection domain is
       
   593      *               retrieved
       
   594      * @return       the protection domain associated with the application
       
   595      *               identified by the appUID
       
   596      */
       
   597     public String readProtectionDomain(Uid appUID)
       
   598     {
       
   599         if (appUID == null)
       
   600         {
       
   601             return null;
       
   602         }
       
   603         StorageEntry query = new StorageEntry();
       
   604         AuthenticationStorageData authData = null;
       
   605         query.addAttribute(new StorageAttribute(
       
   606                                StorageAttribute.ID,
       
   607                                appUID.getStringValue()));
       
   608         query.addAttribute(new StorageAttribute(
       
   609                                StorageNames.SECURITY_DOMAIN,
       
   610                                ""));
       
   611         StorageEntry[] entries = doStorageSearch(
       
   612                                      StorageNames.MIDP_PACKAGE_TABLE, query);
       
   613         if (entries != null && entries.length > 0)
       
   614         {
       
   615             return getStorageAttributeValue(entries[0],
       
   616                                             StorageNames.SECURITY_DOMAIN);
       
   617         }
       
   618         return null;
       
   619     }
       
   620 
       
   621     /**
       
   622      * Reads the protection domain category associated with certain application
       
   623      *
       
   624      * @param appUID the UID of the application whose protection domain category
       
   625      *               is retrieved
       
   626      * @return       the protection domain category associated with the
       
   627      *               application identified by the appUID
       
   628      */
       
   629     public String readProtectionDomainCategory(Uid appUID)
       
   630     {
       
   631         if (appUID == null)
       
   632         {
       
   633             return null;
       
   634         }
       
   635         StorageEntry query = new StorageEntry();
       
   636         AuthenticationStorageData authData = null;
       
   637         query.addAttribute(new StorageAttribute(
       
   638                                StorageAttribute.ID,
       
   639                                appUID.getStringValue()));
       
   640         query.addAttribute(new StorageAttribute(
       
   641                                StorageNames.SECURITY_DOMAIN_CATEGORY,
       
   642                                ""));
       
   643         StorageEntry[] entries = doStorageSearch(
       
   644                                      StorageNames.MIDP_PACKAGE_TABLE, query);
       
   645         if (entries != null && entries.length > 0)
       
   646         {
       
   647             return getStorageAttributeValue(entries[0],
       
   648                                             StorageNames.SECURITY_DOMAIN_CATEGORY);
       
   649         }
       
   650         return null;
       
   651     }
       
   652 
       
   653     /**
       
   654      * Stores the authentication data
       
   655      *
       
   656      * @param appUID the UID of the application whose authentication data is
       
   657      *               stored
       
   658      * @param data   the authentication data to be stored
       
   659      */
       
   660     public void writeAuthenticationStorageData(Uid appUID, AuthenticationStorageData data)
       
   661     {
       
   662         writeAuthenticationStorageData(appUID, data,
       
   663                                        false /* this is not an update*/);
       
   664     }
       
   665 
       
   666     /**
       
   667      * Stores the authentication data
       
   668      *
       
   669      * @param appUID the UID of the application whose authentication data is
       
   670      *               stored
       
   671      * @param data   the authentication data to be stored
       
   672      * @param update boolean specifying if the DB operation should be an
       
   673      *               update or not
       
   674      */
       
   675     public void writeAuthenticationStorageData(Uid appUID, AuthenticationStorageData data, boolean update)
       
   676     {
       
   677         // protection domain is mandatory values,
       
   678         // no sense to try storing something useless
       
   679         if (data == null || data.getProtectionDomain() == null)
       
   680         {
       
   681             return;
       
   682         }
       
   683         StorageEntry entry = new StorageEntry();
       
   684         entry.addAttribute(new StorageAttribute(StorageAttribute.ID,
       
   685                                                 appUID.getStringValue()));
       
   686         entry.addAttribute(new StorageAttribute(
       
   687                                StorageNames.SECURITY_DOMAIN,
       
   688                                data.getProtectionDomain()));
       
   689         entry.addAttribute(new StorageAttribute(
       
   690                                StorageNames.SECURITY_DOMAIN_CATEGORY,
       
   691                                data.getProtectionDomainCategory()));
       
   692         if (data.getJarHashValue() != null
       
   693                 && data.getJarHashValue().length() > 0)
       
   694         {
       
   695             entry.addAttribute(new StorageAttribute(
       
   696                                    StorageNames.HASH,
       
   697                                    data.getJarHashValue()));
       
   698         }
       
   699         else
       
   700         {
       
   701             entry.addAttribute(new StorageAttribute(
       
   702                                    StorageNames.HASH,
       
   703                                    null, StorageAttribute.NULL_TYPE));
       
   704         }
       
   705         if (data.getRootHashValue() != null
       
   706                 && data.getRootHashValue().length() > 0)
       
   707         {
       
   708             entry.addAttribute(new StorageAttribute(
       
   709                                    StorageNames.CERT_HASH,
       
   710                                    data.getRootHashValue()));
       
   711         }
       
   712         String validCerts = encodeValidatedChainIndexes(
       
   713                                 data.getValidatedChainIndexes());
       
   714         if (validCerts != null && validCerts.length() > 0)
       
   715         {
       
   716             entry.addAttribute(new StorageAttribute(
       
   717                                    StorageNames.VALID_CERTS,
       
   718                                    validCerts));
       
   719         }
       
   720         int securityWarningsMode = data.getSecurityWarningsMode();
       
   721         if (securityWarningsMode == GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
   722                 || securityWarningsMode == GeneralSecuritySettings.USER_SECURITY_MODE)
       
   723         {
       
   724             entry.addAttribute(new StorageAttribute(
       
   725                                    StorageNames.SECURITY_WARNINGS,
       
   726                                    "" + securityWarningsMode));
       
   727         }
       
   728         if (update)
       
   729         {
       
   730             StorageEntry oldEntry = new StorageEntry();
       
   731             oldEntry.addAttribute(new StorageAttribute(StorageAttribute.ID,
       
   732                                   appUID.getStringValue()));
       
   733             doStorageUpdate(StorageNames.MIDP_PACKAGE_TABLE, entry, oldEntry);
       
   734         }
       
   735         else
       
   736         {
       
   737             doStorageWrite(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
   738         }
       
   739     }
       
   740 
       
   741     /**
       
   742      * Removes from storage all the authentication data related to a certain
       
   743      * application
       
   744      *
       
   745      * @param appUID The UID of the application whose authentication data is
       
   746      *               removed
       
   747      */
       
   748     public void removeAuthenticationStorageData(Uid appUid)
       
   749     {
       
   750         // delete from MIDP_PACKAGE_TABLE
       
   751         if (appUid != null)
       
   752         {
       
   753             doStorageRemove(StorageNames.MIDP_PACKAGE_TABLE, appUid);
       
   754         }
       
   755     }
       
   756 
       
   757     /**
       
   758      * Retrieves the flag indicating if the security prompt was shown in blanket
       
   759      * mode
       
   760      *
       
   761      * @param aAppUID       The UID of the application whose info about the
       
   762      *                      prompt is retrieved
       
   763      * @param aSettingsName The name of the settings whose info about the
       
   764      *                      prompt is retrieved
       
   765      * @return              A boolean indicating if the security prompt in
       
   766      *                      blanket mode was shown
       
   767      */
       
   768     public boolean readUserSecuritySettingsPromptFlag(Uid aAppUID, String aSettingsName)
       
   769     {
       
   770         StorageEntry query = new StorageEntry();
       
   771         UserSecuritySettings settings = null;
       
   772         query.addAttribute(new StorageAttribute(
       
   773                                StorageAttribute.ID,
       
   774                                aAppUID.getStringValue()));
       
   775         query.addAttribute(new StorageAttribute(
       
   776                                StorageNames.FUNCTION_GROUP,
       
   777                                aSettingsName));
       
   778         query.addAttribute(new StorageAttribute(
       
   779                                StorageNames.BLANKET_PROMPT,
       
   780                                ""));
       
   781         StorageEntry[] entries = doStorageSearch(
       
   782                                      StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
   783                                      query);
       
   784         if (entries != null && entries.length > 0)
       
   785         {
       
   786             String blanketPrompt = getStorageAttributeValue(entries[0],
       
   787                                    StorageNames.BLANKET_PROMPT);
       
   788             if (blanketPrompt!= null && blanketPrompt.equals("1"))
       
   789             {
       
   790                 return true;
       
   791             }
       
   792         }
       
   793         return false;
       
   794     }
       
   795 
       
   796     /**
       
   797      * Stores the flag indicating if the security prompt was shown in blanket
       
   798      * mode
       
   799      *
       
   800      * @param aAppUID       the UID of the application whose flag for blanket
       
   801      *                      prompt is stored
       
   802      * @param aSettingsName the name of the settings whose whose flag for
       
   803      *                      blanket prompt is stored
       
   804      * @param aPromptFlag   true is prompt in blanket mode was shown and false
       
   805      *                      otherwise
       
   806      */
       
   807     public void writeUserSecuritySettingsPromptFlag(Uid aAppUID, String aSettingsName, boolean aPromptFlag)
       
   808     {
       
   809         StorageEntry query = new StorageEntry();
       
   810         StorageEntry newEntry = new StorageEntry();
       
   811         UserSecuritySettings settings = null;
       
   812         query.addAttribute(new StorageAttribute(
       
   813                                StorageAttribute.ID,
       
   814                                aAppUID.getStringValue()));
       
   815         query.addAttribute(new StorageAttribute(
       
   816                                StorageNames.FUNCTION_GROUP,
       
   817                                aSettingsName));
       
   818         newEntry.addAttribute(new StorageAttribute(
       
   819                                   StorageNames.BLANKET_PROMPT,
       
   820                                   (aPromptFlag ? "1":"0")));
       
   821         doStorageUpdate(StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
   822                         newEntry, query);
       
   823     }
       
   824 
       
   825     /**
       
   826      * Retrieves certain settings associated to a certain application
       
   827      *
       
   828      * @param appUID       the UID of the application whose security settings
       
   829      *                     are retrieved
       
   830      * @param settingsName the name of the settings to be retrieved
       
   831      * @return             user security settings
       
   832      */
       
   833     public UserSecuritySettings readUserSecuritySettings(
       
   834         Uid msUID,
       
   835         String settingsName)
       
   836     {
       
   837         if (settingsName == null)
       
   838         {
       
   839             return null;
       
   840         }
       
   841         StorageEntry query = new StorageEntry();
       
   842         UserSecuritySettings settings = null;
       
   843         query.addAttribute(new StorageAttribute(
       
   844                                StorageAttribute.ID,
       
   845                                msUID.getStringValue()));
       
   846         query.addAttribute(new StorageAttribute(StorageNames.FUNCTION_GROUP,
       
   847                                                 settingsName));
       
   848         query.addAttribute(new StorageAttribute(
       
   849                                StorageNames.ALLOWED_SETTINGS,
       
   850                                ""));
       
   851         query.addAttribute(new StorageAttribute(
       
   852                                StorageNames.CURRENT_SETTING,
       
   853                                ""));
       
   854         query.addAttribute(new StorageAttribute(
       
   855                                StorageNames.BLANKET_PROMPT,
       
   856                                ""));
       
   857         StorageEntry[] entries = doStorageSearch(
       
   858                                      StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
   859                                      query);
       
   860         if (entries != null && entries.length > 0)
       
   861         {
       
   862             try
       
   863             {
       
   864                 int allowedSettings = Integer.valueOf(getStorageAttributeValue(
       
   865                                                           entries[0],StorageNames.ALLOWED_SETTINGS)).intValue();
       
   866                 int currentSetting = Integer.valueOf(getStorageAttributeValue(
       
   867                                                          entries[0],StorageNames.CURRENT_SETTING)).intValue();
       
   868                 String blanketPrompt = getStorageAttributeValue(entries[0],
       
   869                                        StorageNames.BLANKET_PROMPT);
       
   870                 settings = new UserSecuritySettingsImpl(
       
   871                     settingsName,
       
   872                     currentSetting,
       
   873                     decodeAllowedSettings(allowedSettings),
       
   874                     ("1".equals(blanketPrompt) ? true : false));
       
   875             }
       
   876             catch (NumberFormatException e)
       
   877             {
       
   878                 Logger.ELOG(Logger.EJavaSecurity, "SecuritySettings read failed", e);
       
   879             }
       
   880         }
       
   881         return settings;
       
   882     }
       
   883 
       
   884     /**
       
   885      * Retrieves ALL the settings associated to a certain application
       
   886      *
       
   887      * @param appUID the UID of the application whose security settings
       
   888      *               are retrieved
       
   889      * @return       an array of all the user security settings
       
   890      */
       
   891     public UserSecuritySettings[] readUserSecuritySettings(
       
   892         Uid msUID)
       
   893     {
       
   894 
       
   895         StorageEntry query = new StorageEntry();
       
   896         query.addAttribute(new StorageAttribute(
       
   897                                StorageAttribute.ID,
       
   898                                msUID.getStringValue()));
       
   899         query.addAttribute(new StorageAttribute(
       
   900                                StorageNames.FUNCTION_GROUP,
       
   901                                ""));
       
   902         StorageEntry[] entries = doStorageSearch(
       
   903                                      StorageNames.MIDP_PERMISSIONS_TABLE, query);
       
   904         Vector vSettings = new Vector();
       
   905         if (entries != null)
       
   906         {
       
   907             for (int i=0; i<entries.length; i++)
       
   908             {
       
   909                 UserSecuritySettings settings =
       
   910                     readUserSecuritySettings(msUID,getStorageAttributeValue(
       
   911                                                  entries[i],StorageNames.FUNCTION_GROUP));
       
   912                 if (settings != null)
       
   913                 {
       
   914                     vSettings.addElement(settings);
       
   915                 }
       
   916             }
       
   917         }
       
   918         if (vSettings.size() == 0)
       
   919         {
       
   920             return null;
       
   921         }
       
   922         UserSecuritySettings[] settings =
       
   923             new UserSecuritySettings[vSettings.size()];
       
   924         vSettings.copyInto(settings);
       
   925         return settings;
       
   926     }
       
   927 
       
   928     /**
       
   929      * Stores settings info associated to a certain application
       
   930      *
       
   931      * @param appUID                 the UID of the application whose security
       
   932      *                               settings info are stored
       
   933      * @param settingsName           the name of the settings to be modified
       
   934      * @param currentInteractionMode the new value of the settings
       
   935      */
       
   936     public void writeUserSecuritySettings(
       
   937         Uid msUID,
       
   938         String settingsName,
       
   939         int currentInteractionMode)
       
   940     {
       
   941         StorageEntry entry = new StorageEntry();
       
   942         entry.addAttribute(new StorageAttribute(
       
   943                                StorageAttribute.ID,
       
   944                                msUID.getStringValue()));
       
   945         entry.addAttribute(new StorageAttribute(
       
   946                                StorageNames.FUNCTION_GROUP,
       
   947                                settingsName));
       
   948         entry.addAttribute(new StorageAttribute(
       
   949                                StorageNames.CURRENT_SETTING,
       
   950                                "" + currentInteractionMode));
       
   951         updateFunctionGroupTable(msUID, entry);
       
   952     }
       
   953 
       
   954     /**
       
   955      * Stores settings info associated to a certain application
       
   956      *
       
   957      * @param appUID                 the UID of the application whose security
       
   958      *                               settings info are stored
       
   959      * @param settingsName           the name of the settings to be modified
       
   960      * @param currentInteractionMode the new value of the settings
       
   961      * @param blanketPromptShown     the new value of the settings
       
   962      */
       
   963     public void writeUserSecuritySettings(
       
   964         Uid msUID,
       
   965         String settingsName,
       
   966         int currentInteractionMode,
       
   967         boolean blanketPromptShown)
       
   968     {
       
   969         StorageEntry entry = new StorageEntry();
       
   970         entry.addAttribute(new StorageAttribute(
       
   971                                StorageAttribute.ID,
       
   972                                msUID.getStringValue()));
       
   973         entry.addAttribute(new StorageAttribute(
       
   974                                StorageNames.FUNCTION_GROUP,
       
   975                                settingsName));
       
   976         entry.addAttribute(new StorageAttribute(
       
   977                                StorageNames.CURRENT_SETTING,
       
   978                                "" + currentInteractionMode));
       
   979         entry.addAttribute(new StorageAttribute(
       
   980                                StorageNames.BLANKET_PROMPT,
       
   981                                (blanketPromptShown ? "1":"0")));
       
   982         updateFunctionGroupTable(msUID, entry);
       
   983     }
       
   984 
       
   985     /**
       
   986      * Sets all the settings associated to a certain application
       
   987      * to a certain value
       
   988      *
       
   989      * @param appUID                 the UID of the application whose security
       
   990      *                               settings are set
       
   991      * @param currentInteractionMode the new value of the settings
       
   992      * @param blanketPromptShown     the new value of the settings
       
   993      */
       
   994     public void writeUserSecuritySettings(
       
   995         Uid msUID,
       
   996         int currentInteractionMode,
       
   997         boolean blanketPromptShown)
       
   998     {
       
   999         // read all the settings
       
  1000         UserSecuritySettings[] settings = readUserSecuritySettings(msUID);
       
  1001         if (settings != null)
       
  1002         {
       
  1003             for (int i=0; i<settings.length; i++)
       
  1004             {
       
  1005                 // for each of the settings write the new value
       
  1006                 writeUserSecuritySettings(msUID,
       
  1007                                           settings[i].getName(),
       
  1008                                           currentInteractionMode,
       
  1009                                           blanketPromptShown);
       
  1010             }
       
  1011         }
       
  1012     }
       
  1013 
       
  1014     /**
       
  1015      * Reads the value of the suite version attribute
       
  1016      *
       
  1017      * @param appUID the UID of the application whose version is read
       
  1018      * @return       the value of the version attribute
       
  1019      */
       
  1020     public String readSuiteVersion(Uid appUID)
       
  1021     {
       
  1022         StorageEntry query = new StorageEntry();
       
  1023         query.addAttribute(new StorageAttribute(
       
  1024                                StorageAttribute.ID,
       
  1025                                appUID.getStringValue()));
       
  1026         StorageAttribute versionAttr = new StorageAttribute(
       
  1027             StorageNames.NAME, SecurityAttributes.MIDP_VERSION_ATTRIBUTE_NAME);
       
  1028         query.addAttribute(versionAttr);
       
  1029         query.addAttribute(new StorageAttribute(StorageNames.VALUE, ""));
       
  1030         StorageEntry[] entries = doStorageSearch(
       
  1031                                      StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, query);
       
  1032         if (entries != null && entries.length > 0)
       
  1033         {
       
  1034             return getStorageAttributeValue(entries[0],
       
  1035                                             StorageNames.VALUE);
       
  1036         }
       
  1037         return null;
       
  1038     }
       
  1039 
       
  1040     /**
       
  1041      * Reads the value of the MEDIA_ID attribute
       
  1042      *
       
  1043      * @param appUID the UID of the application whose MEDIA_ID is retrieved
       
  1044      * @return       the value of the MEDIA_ID attribute
       
  1045      */
       
  1046     public String readSuiteMediaId(Uid appUID)
       
  1047     {
       
  1048         StorageEntry query = new StorageEntry();
       
  1049         query.addAttribute(new StorageAttribute(
       
  1050                                StorageAttribute.ID,
       
  1051                                appUID.getStringValue()));
       
  1052         query.addAttribute(new StorageAttribute(
       
  1053                                StorageNames.MEDIA_ID,
       
  1054                                ""));
       
  1055         StorageEntry[] entries = doStorageSearch(
       
  1056                                      StorageNames.APPLICATION_PACKAGE_TABLE,
       
  1057                                      query);
       
  1058         if (entries != null && entries.length > 0)
       
  1059         {
       
  1060             return getStorageAttributeValue(entries[0],
       
  1061                                             StorageNames.MEDIA_ID);
       
  1062         }
       
  1063         return null;
       
  1064     }
       
  1065 
       
  1066     /**
       
  1067      * Reads the network restrictions
       
  1068      *
       
  1069      * @param  appUID the UID of the application whose network restrictions
       
  1070      *         are retrieved
       
  1071      * @return A string containing the network restrictions or null if
       
  1072      *         there are no network restrictions associated
       
  1073      */
       
  1074     public String readNetworkRestrictions(Uid appUID)
       
  1075     {
       
  1076         StorageEntry query = new StorageEntry();
       
  1077         query.addAttribute(new StorageAttribute(
       
  1078                                StorageAttribute.ID,
       
  1079                                appUID.getStringValue()));
       
  1080         query.addAttribute(new StorageAttribute(
       
  1081                                StorageNames.NAME,
       
  1082                                "MIDlet-Operator-Allowed"));
       
  1083         query.addAttribute(new StorageAttribute(
       
  1084                                StorageNames.VALUE,
       
  1085                                ""));
       
  1086         StorageEntry[] entries = doStorageSearch(
       
  1087                                      StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE,
       
  1088                                      query);
       
  1089         if (entries != null && entries.length > 0)
       
  1090         {
       
  1091             return getStorageAttributeValue(entries[0],
       
  1092                                             StorageNames.VALUE);
       
  1093         }
       
  1094         return null;
       
  1095     }
       
  1096 
       
  1097     /**
       
  1098      * Reads security warnings mode value
       
  1099      * @return One of the values
       
  1100      *         SECURITY_WARNINGS_USER_DEFINED_MODE
       
  1101      *         SECURITY_WARNINGS_DEFAULT_MODE
       
  1102      */
       
  1103     public int readSecurityWarningsMode(Uid appUid)
       
  1104     {
       
  1105         try
       
  1106         {
       
  1107             StorageEntry query = new StorageEntry();
       
  1108             query.addAttribute(new StorageAttribute(
       
  1109                                    StorageAttribute.ID,
       
  1110                                    appUid.getStringValue()));
       
  1111             query.addAttribute(new StorageAttribute(
       
  1112                                    StorageNames.SECURITY_WARNINGS,
       
  1113                                    ""));
       
  1114             StorageEntry[] entries = doStorageSearch(
       
  1115                                          StorageNames.MIDP_PACKAGE_TABLE,
       
  1116                                          query);
       
  1117             if (entries != null && entries.length > 0)
       
  1118             {
       
  1119                 try
       
  1120                 {
       
  1121                     int value = Integer.valueOf(getStorageAttributeValue(entries[0],
       
  1122                                                 StorageNames.SECURITY_WARNINGS)).intValue();
       
  1123                     if (value == GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1124                             || value == GeneralSecuritySettings.USER_SECURITY_MODE)
       
  1125                     {
       
  1126                         return value;
       
  1127                     }
       
  1128                 }
       
  1129                 catch (NumberFormatException e)
       
  1130                 {
       
  1131                     Logger.ELOG(Logger.EJavaSecurity, "SecurityWarnings read failed", e);
       
  1132                 }
       
  1133             }
       
  1134         }
       
  1135         catch (StorageException e)
       
  1136             {/* move on with defaults */}
       
  1137         return GeneralSecuritySettings.DEFAULT_SECURITY_MODE;
       
  1138     }
       
  1139 
       
  1140     /**
       
  1141      * Writes security warnings mode value
       
  1142      * @param aSecurityWarningsMode One of the values
       
  1143      *        SECURITY_WARNINGS_USER_DEFINED_MODE
       
  1144      *        SECURITY_WARNINGS_DEFAULT_MODE
       
  1145      */
       
  1146     public void writeSecurityWarningsMode(Uid appUid, int aSecurityWarningsMode)
       
  1147     {
       
  1148         try
       
  1149         {
       
  1150             switch (aSecurityWarningsMode)
       
  1151             {
       
  1152             case GeneralSecuritySettings.DEFAULT_SECURITY_MODE:
       
  1153             case GeneralSecuritySettings.USER_SECURITY_MODE:
       
  1154                 StorageEntry entry = new StorageEntry();
       
  1155                 entry.addAttribute(new StorageAttribute(
       
  1156                                        StorageNames.SECURITY_WARNINGS,
       
  1157                                        "" + aSecurityWarningsMode));
       
  1158                 StorageEntry oldEntry = new StorageEntry();
       
  1159                 oldEntry.addAttribute(new StorageAttribute(
       
  1160                                           StorageAttribute.ID,
       
  1161                                           appUid.getStringValue()));
       
  1162                 doStorageUpdate(StorageNames.MIDP_PACKAGE_TABLE,
       
  1163                                 entry,
       
  1164                                 oldEntry);
       
  1165                 break;
       
  1166             }
       
  1167         }
       
  1168         catch (StorageException e)
       
  1169             {/* move on with defaults */}
       
  1170     }
       
  1171 
       
  1172 
       
  1173     private void updateGrantedPermissions(Uid newAppUID, Uid oldAppUID, Vector grantedPermissions)
       
  1174     {
       
  1175         // the vector containing the newGrantedPermissions
       
  1176         Vector newGrantedPermissions = new Vector();
       
  1177 
       
  1178         // get the old permissions & settings
       
  1179         Vector oldPermissions = readGrantedPermissions(oldAppUID);
       
  1180 
       
  1181         // filter out the the brand new permissions
       
  1182         // (permissions which are not found among the old permissions)
       
  1183         if (oldPermissions != null)
       
  1184         {
       
  1185             int index=0;
       
  1186             while (index < grantedPermissions.size())
       
  1187             {
       
  1188                 // instead of calling Vector.removeElement(p) we will do the
       
  1189                 // remove manually, since the search is to be based on
       
  1190                 // the permission without the settings
       
  1191                 PolicyBasedPermission p = (PolicyBasedPermission)
       
  1192                                           grantedPermissions.elementAt(index);
       
  1193                 int status = removeElement(oldPermissions, p);
       
  1194                 switch (status)
       
  1195                 {
       
  1196                 case NOT_FOUND:
       
  1197                     index++;
       
  1198                     break;
       
  1199                 case REMOVED:
       
  1200                     grantedPermissions.removeElementAt(index);
       
  1201                     break;
       
  1202                 default:
       
  1203                     // different settings
       
  1204                     UserSecuritySettings oldSettings
       
  1205                     = ((PolicyBasedPermission)oldPermissions
       
  1206                        .elementAt(status)).getUserSecuritySettings();
       
  1207                     UserSecuritySettings newSettings
       
  1208                     = p.getUserSecuritySettings();
       
  1209                     if (oldSettings != null
       
  1210                             && newSettings != null)
       
  1211                     {
       
  1212                         newGrantedPermissions.addElement(
       
  1213                             new PolicyBasedPermissionImpl(
       
  1214                                 p.getName(),
       
  1215                                 p.getTarget(),
       
  1216                                 p.getActionList(),
       
  1217                                 new UserSecuritySettingsImpl(
       
  1218                                     newSettings.getName(),
       
  1219                                     oldSettings.getCurrentInteractionMode(),
       
  1220                                     newSettings.getAllowedInteractionModes(),
       
  1221                                     oldSettings.getBlanketPrompt())));
       
  1222                     }
       
  1223                     else
       
  1224                     {
       
  1225                         newGrantedPermissions.addElement(p);
       
  1226                     }
       
  1227                     grantedPermissions.removeElementAt(index);
       
  1228                     break;
       
  1229                 }
       
  1230             }
       
  1231         }
       
  1232         // write what's left from the granted permissions
       
  1233         writeGrantedPermissions(newAppUID, null, grantedPermissions, true /* preserveSettings */);
       
  1234         for (int i=0; i<newGrantedPermissions.size(); i++)
       
  1235         {
       
  1236             grantedPermissions.addElement(newGrantedPermissions.elementAt(i));
       
  1237         }
       
  1238 
       
  1239         // remove what's left from the old permissions
       
  1240         if (oldPermissions != null)
       
  1241         {
       
  1242             for (int i=0; i<oldPermissions.size(); i++)
       
  1243             {
       
  1244                 PolicyBasedPermission p = (PolicyBasedPermission)
       
  1245                                           oldPermissions.elementAt(i);
       
  1246                 StorageEntry removePermissionQuery = new StorageEntry();
       
  1247                 removePermissionQuery.addAttribute(new StorageAttribute(
       
  1248                                                        StorageAttribute.ID,
       
  1249                                                        oldAppUID.getStringValue()));
       
  1250                 removePermissionQuery.addAttribute(new StorageAttribute(
       
  1251                                                        StorageNames.CLASS,
       
  1252                                                        p.getName()));
       
  1253                 removePermissionQuery.addAttribute(new StorageAttribute(
       
  1254                                                        StorageNames.NAME,
       
  1255                                                        p.getTarget()));
       
  1256                 if (p.getActionList() != null
       
  1257                         && p.getActionList().length() > 0)
       
  1258                 {
       
  1259                     removePermissionQuery.addAttribute(new StorageAttribute(
       
  1260                                                            StorageNames.ACTION,
       
  1261                                                            p.getActionList()));
       
  1262                 }
       
  1263                 doStorageRemove(StorageNames.MIDP_PERMISSIONS_TABLE,
       
  1264                                 removePermissionQuery);
       
  1265                 // remove the setting also if not used by some other permission
       
  1266                 UserSecuritySettings settings =
       
  1267                     p.getUserSecuritySettings();
       
  1268                 if (settings != null)
       
  1269                 {
       
  1270                     StorageEntry permissionsQuery = new StorageEntry();
       
  1271                     permissionsQuery.addAttribute(new StorageAttribute(
       
  1272                                                       StorageAttribute.ID,
       
  1273                                                       newAppUID.getStringValue()));
       
  1274                     permissionsQuery.addAttribute(new StorageAttribute(
       
  1275                                                       StorageNames.FUNCTION_GROUP,
       
  1276                                                       settings.getName()));
       
  1277                     StorageEntry[] permissions = doStorageSearch(
       
  1278                                                      StorageNames.MIDP_PERMISSIONS_TABLE, permissionsQuery);
       
  1279                     if (permissions == null || (permissions != null
       
  1280                                                 && permissions.length == 0))
       
  1281                     {
       
  1282                         // remove the orphaned settings from settings table
       
  1283                         StorageEntry removeSettingsQuery = new StorageEntry();
       
  1284                         removeSettingsQuery.addAttribute(new StorageAttribute(
       
  1285                                                              StorageAttribute.ID,
       
  1286                                                              newAppUID.getStringValue()));
       
  1287                         removeSettingsQuery.addAttribute(new StorageAttribute(
       
  1288                                                              StorageNames.FUNCTION_GROUP,
       
  1289                                                              settings.getName()));
       
  1290                         doStorageRemove(StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
  1291                                         removeSettingsQuery);
       
  1292                     }
       
  1293                 }
       
  1294             }
       
  1295         }
       
  1296         // write the new permissions
       
  1297         writeGrantedPermissions(newAppUID, null, newGrantedPermissions, true /* preserveSettings */);
       
  1298     }
       
  1299 
       
  1300     private AuthenticationStorageData readAuthenticationStorageData(
       
  1301         Uid appUID, String aAppName, String aAppVersion,
       
  1302         String aAppVendor, int readFilter)
       
  1303     {
       
  1304         if (appUID == null && (aAppName == null
       
  1305                                || aAppVersion == null || aAppVendor == null))
       
  1306         {
       
  1307             return null;
       
  1308         }
       
  1309         // decode the filter
       
  1310         boolean readDomainName = ((readFilter & AUTHENTICATION_DOMAIN_NAME_QUERY) != 0);
       
  1311         boolean readDomainCategory = ((readFilter & AUTHENTICATION_DOMAIN_CATEGORY_QUERY) != 0);
       
  1312         boolean readRootHash = ((readFilter & AUTHENTICATION_ROOT_HASH_QUERY) != 0);
       
  1313         boolean readJarHash = ((readFilter & AUTHENTICATION_JAR_HASH_QUERY) != 0);
       
  1314         boolean readValidCerts = ((readFilter & AUTHENTICATION_VALID_CERTS_QUERY) != 0);
       
  1315         if (!readDomainName && !readDomainCategory
       
  1316                 && !readRootHash && !readJarHash && !readValidCerts)
       
  1317         {
       
  1318             // nothing has been requested
       
  1319             return null;
       
  1320         }
       
  1321         StorageAttribute ID = null;
       
  1322         if (appUID != null)
       
  1323         {
       
  1324             ID = new StorageAttribute(
       
  1325                 StorageAttribute.ID,
       
  1326                 appUID.getStringValue());
       
  1327         }
       
  1328         else
       
  1329         {
       
  1330             ID = readApplicationID(aAppName, aAppVersion, aAppVendor);
       
  1331             if (ID == null)
       
  1332             {
       
  1333                 return null;
       
  1334             }
       
  1335         }
       
  1336         StorageEntry query = new StorageEntry();
       
  1337         query.addAttribute(ID);
       
  1338         AuthenticationStorageData authData = null;
       
  1339         if (readDomainName)
       
  1340         {
       
  1341             query.addAttribute(new StorageAttribute(
       
  1342                                    StorageNames.SECURITY_DOMAIN,
       
  1343                                    ""));
       
  1344         }
       
  1345         if (readDomainCategory)
       
  1346         {
       
  1347             query.addAttribute(new StorageAttribute(
       
  1348                                    StorageNames.SECURITY_DOMAIN_CATEGORY,
       
  1349                                    ""));
       
  1350         }
       
  1351         if (readJarHash)
       
  1352         {
       
  1353             query.addAttribute(new StorageAttribute(
       
  1354                                    StorageNames.HASH,
       
  1355                                    ""));
       
  1356         }
       
  1357         if (readRootHash)
       
  1358         {
       
  1359             query.addAttribute(new StorageAttribute(
       
  1360                                    StorageNames.CERT_HASH,
       
  1361                                    ""));
       
  1362         }
       
  1363         if (readValidCerts)
       
  1364         {
       
  1365             query.addAttribute(new StorageAttribute(
       
  1366                                    StorageNames.VALID_CERTS,
       
  1367                                    ""));
       
  1368         }
       
  1369         StorageEntry[] entries = doStorageSearch(
       
  1370                                      StorageNames.MIDP_PACKAGE_TABLE, query);
       
  1371         if (entries != null && entries.length > 0)
       
  1372         {
       
  1373             try
       
  1374             {
       
  1375                 authData = new AuthenticationStorageData(
       
  1376                     getStorageAttributeValue(entries[0],
       
  1377                                              StorageNames.SECURITY_DOMAIN),
       
  1378                     getStorageAttributeValue(entries[0],
       
  1379                                              StorageNames.SECURITY_DOMAIN_CATEGORY),
       
  1380                     getStorageAttributeValue(entries[0],
       
  1381                                              StorageNames.HASH),
       
  1382                     getStorageAttributeValue(entries[0],
       
  1383                                              StorageNames.CERT_HASH),
       
  1384                     decodeValidatedChainIndexes(getStorageAttributeValue(
       
  1385                                                     entries[0],StorageNames.VALID_CERTS)));
       
  1386             }
       
  1387             catch (NumberFormatException e)
       
  1388             {
       
  1389                 Logger.ELOG(Logger.EJavaSecurity, "Authentication read failed", e);
       
  1390             }
       
  1391         }
       
  1392         return authData;
       
  1393     }
       
  1394 
       
  1395     private AppAccessAuthorizationStorageData readAppAccessAuthorizationStorageData(
       
  1396         Uid appUID, String aAppName, String aAppVersion,
       
  1397         String aAppVendor, int readFilter)
       
  1398     {
       
  1399         if (appUID == null && (aAppName == null
       
  1400                                || aAppVersion == null || aAppVendor == null))
       
  1401         {
       
  1402             return null;
       
  1403         }
       
  1404         // decode the filter
       
  1405         boolean readDomain = ((readFilter & APP_ACCESS_AUTH_DOMAIN_QUERY) != 0);
       
  1406         boolean readVendor = ((readFilter & APP_ACCESS_AUTH_VENDOR_QUERY) != 0);
       
  1407         boolean readSignersList = ((readFilter & APP_ACCESS_AUTH_SIGNERS_LIST_QUERY) != 0);
       
  1408         if (!readDomain && !readVendor && !readSignersList)
       
  1409         {
       
  1410             // nothing has been requested
       
  1411             return null;
       
  1412         }
       
  1413         StorageAttribute ID = null;
       
  1414         if (appUID != null)
       
  1415         {
       
  1416             ID = new StorageAttribute(
       
  1417                 StorageAttribute.ID,
       
  1418                 appUID.getStringValue());
       
  1419         }
       
  1420         else
       
  1421         {
       
  1422             ID = readApplicationID(aAppName, aAppVersion, aAppVendor);
       
  1423             if (ID == null)
       
  1424             {
       
  1425                 return null;
       
  1426             }
       
  1427         }
       
  1428         StorageEntry query = new StorageEntry();
       
  1429         query.addAttribute(ID);
       
  1430         StorageEntry[] entries = null;
       
  1431         String vendorName = null;
       
  1432         String domainName = null;
       
  1433         String[] signersList = null;
       
  1434         // read the vendor name
       
  1435         if (readVendor)
       
  1436         {
       
  1437             if (aAppVendor != null)
       
  1438             {
       
  1439                 vendorName = aAppVendor;
       
  1440             }
       
  1441             else
       
  1442             {
       
  1443                 query.addAttribute(new StorageAttribute(
       
  1444                                        StorageNames.VENDOR,
       
  1445                                        ""));
       
  1446                 entries = doStorageSearch(
       
  1447                               StorageNames. APPLICATION_PACKAGE_TABLE, query);
       
  1448                 if (entries != null && entries.length > 0)
       
  1449                 {
       
  1450                     vendorName = getStorageAttributeValue(entries[0],
       
  1451                                                           StorageNames.VENDOR);
       
  1452                 }
       
  1453             }
       
  1454         }
       
  1455         if (readDomain || readSignersList)
       
  1456         {
       
  1457             query.removeAll();
       
  1458             query.addAttribute(ID);
       
  1459             if (readDomain)
       
  1460             {
       
  1461                 query.addAttribute(new StorageAttribute(
       
  1462                                        StorageAttribute.SECURITY_DOMAIN,
       
  1463                                        ""));
       
  1464             }
       
  1465             if (readSignersList)
       
  1466             {
       
  1467                 query.addAttribute(new StorageAttribute(
       
  1468                                        StorageAttribute.VALID_CERTS,
       
  1469                                        ""));
       
  1470             }
       
  1471             entries = doStorageSearch(
       
  1472                           StorageNames.MIDP_PACKAGE_TABLE, query);
       
  1473             if (entries != null && entries.length > 0)
       
  1474             {
       
  1475                 domainName = getStorageAttributeValue(entries[0],
       
  1476                                                       StorageNames.SECURITY_DOMAIN);
       
  1477                 Vector validatedChainIndexes = decodeValidatedChainIndexes(
       
  1478                                                    getStorageAttributeValue(entries[0],
       
  1479                                                                             StorageNames.VALID_CERTS));
       
  1480                 if (validatedChainIndexes != null)
       
  1481                 {
       
  1482                     Vector vSignersList = new Vector();
       
  1483                     for (int i=0; i<validatedChainIndexes.size(); i++)
       
  1484                     {
       
  1485                         // retrieve the end entity cert from the succesfully
       
  1486                         // validated certs
       
  1487                         String attrName = AuthenticationAttribute
       
  1488                                           .MAIN_ATTRIBUTE_PREFIX
       
  1489                                           + ((Integer)validatedChainIndexes.elementAt(i))
       
  1490                                           .intValue() + "-1";
       
  1491                         query.removeAll();
       
  1492                         query.addAttribute(ID);
       
  1493                         query.addAttribute(new StorageAttribute(
       
  1494                                                StorageNames.NAME, attrName));
       
  1495                         query.addAttribute(new StorageAttribute(
       
  1496                                                StorageNames.VALUE, ""));
       
  1497                         entries = doStorageSearch(StorageNames
       
  1498                                                   .APPLICATION_PACKAGE_ATTRIBUTES_TABLE,query);
       
  1499                         if (entries != null && entries.length > 0)
       
  1500                         {
       
  1501                             vSignersList.addElement(getStorageAttributeValue(
       
  1502                                                         entries[0],StorageNames.VALUE));
       
  1503                         }
       
  1504                     }
       
  1505                     if (vSignersList.size() > 0)
       
  1506                     {
       
  1507                         signersList = new String[vSignersList.size()];
       
  1508                         vSignersList.copyInto(signersList);
       
  1509                     }
       
  1510                 }
       
  1511             }
       
  1512         }
       
  1513         if (domainName == null && vendorName == null && signersList == null)
       
  1514         {
       
  1515             return null;
       
  1516         }
       
  1517         return new AppAccessAuthorizationStorageData(
       
  1518                    vendorName, domainName, signersList);
       
  1519     }
       
  1520 
       
  1521     private StorageAttribute readApplicationID(String aAppName,
       
  1522             String aAppVersion,
       
  1523             String aAppVendor)
       
  1524     {
       
  1525         StorageEntry query = new StorageEntry();
       
  1526         StorageAttribute NAME = new StorageAttribute(
       
  1527             StorageAttribute.PACKAGE_NAME,
       
  1528             aAppName);
       
  1529         StorageAttribute VERSION = new StorageAttribute(
       
  1530             StorageAttribute.VERSION,
       
  1531             aAppVersion);
       
  1532         StorageAttribute VENDOR = new StorageAttribute(
       
  1533             StorageAttribute.VENDOR,
       
  1534             aAppVendor);
       
  1535         query.addAttribute(NAME);
       
  1536         query.addAttribute(VERSION);
       
  1537         query.addAttribute(VENDOR);
       
  1538         query.addAttribute(new StorageAttribute(
       
  1539                                StorageNames.ID,
       
  1540                                ""));
       
  1541         StorageEntry[] entries = doStorageSearch(
       
  1542                                      StorageNames. APPLICATION_PACKAGE_TABLE, query);
       
  1543         if (entries != null && entries.length > 0)
       
  1544         {
       
  1545             return entries[0].getAttribute(StorageNames.ID);
       
  1546         }
       
  1547         return null;
       
  1548     }
       
  1549 
       
  1550     private void updateFunctionGroupTable(Uid msUID, StorageEntry entry)
       
  1551     {
       
  1552         StorageEntry oldEntry = new StorageEntry();
       
  1553         oldEntry.addAttribute(new StorageAttribute(
       
  1554                                   StorageAttribute.ID,
       
  1555                                   msUID.getStringValue()));
       
  1556         oldEntry.addAttribute(new StorageAttribute(
       
  1557                                   StorageNames.FUNCTION_GROUP,
       
  1558                                   getStorageAttributeValue(entry,
       
  1559                                                            StorageNames.FUNCTION_GROUP)));
       
  1560         doStorageUpdate(StorageNames.MIDP_FUNC_GRP_SETTINGS_TABLE,
       
  1561                         entry,
       
  1562                         oldEntry);
       
  1563     }
       
  1564 
       
  1565     private void doStorageOpen()
       
  1566     {
       
  1567         doStorageOperation(STORAGE_OPEN, null, null, null, null);
       
  1568     }
       
  1569 
       
  1570     private void doStorageClose()
       
  1571     {
       
  1572         doStorageOperation(STORAGE_CLOSE, null, null, null, null);
       
  1573     }
       
  1574 
       
  1575     private StorageEntry[] doStorageSearch(String tableName, StorageEntry entry)
       
  1576     {
       
  1577         return doStorageOperation(STORAGE_SEARCH, tableName, entry, null, null);
       
  1578     }
       
  1579 
       
  1580     private void doStorageWrite(String tableName, StorageEntry entry)
       
  1581     {
       
  1582         doStorageOperation(STORAGE_WRITE, tableName, entry, null, null);
       
  1583     }
       
  1584 
       
  1585     private void doStorageUpdate(String tableName, StorageEntry newEntry, StorageEntry oldEntry)
       
  1586     {
       
  1587         doStorageOperation(STORAGE_UPDATE, tableName, newEntry, oldEntry, null);
       
  1588     }
       
  1589 
       
  1590     private void doStorageRemove(String tableName, StorageEntry removeEntry)
       
  1591     {
       
  1592         doStorageOperation(STORAGE_REMOVE, tableName, null, removeEntry, null);
       
  1593     }
       
  1594 
       
  1595     private void doStorageRemove(String tableName, Uid removeKey)
       
  1596     {
       
  1597         doStorageOperation(STORAGE_REMOVE, tableName, null, null, removeKey);
       
  1598     }
       
  1599 
       
  1600     // have all the session operations in one place, so that error handling is centralized
       
  1601     private StorageEntry[] doStorageOperation(int opID, String tableName, StorageEntry newEntry, StorageEntry oldEntry, Uid removeKey)
       
  1602     {
       
  1603         switch (opID)
       
  1604         {
       
  1605         case STORAGE_OPEN:
       
  1606             session = StorageFactory.createSession();
       
  1607             session.open();
       
  1608             break;
       
  1609         case STORAGE_CLOSE:
       
  1610             if (destroyStorageSession)
       
  1611             {
       
  1612                 session.close();
       
  1613                 session.destroySession();
       
  1614             }
       
  1615             break;
       
  1616         case STORAGE_SEARCH:
       
  1617             return session.search(tableName, newEntry);
       
  1618         case STORAGE_WRITE:
       
  1619             session.write(tableName, newEntry);
       
  1620             break;
       
  1621         case STORAGE_UPDATE:
       
  1622             session.update(tableName, newEntry, oldEntry);
       
  1623             break;
       
  1624         case STORAGE_REMOVE:
       
  1625             if (removeKey != null)
       
  1626             {
       
  1627                 session.remove(tableName, removeKey);
       
  1628             }
       
  1629             else
       
  1630             {
       
  1631                 session.remove(tableName, oldEntry);
       
  1632             }
       
  1633             break;
       
  1634         }
       
  1635         return null;
       
  1636     }
       
  1637 
       
  1638     private String encodeValidatedChainIndexes(int[] validatedChainIndexes)
       
  1639     {
       
  1640         String strValidatedChainIndexes = null;
       
  1641         if (validatedChainIndexes != null && validatedChainIndexes.length > 0)
       
  1642         {
       
  1643             strValidatedChainIndexes = "";
       
  1644             for (int i=0; i<validatedChainIndexes.length -1; i++)
       
  1645             {
       
  1646                 strValidatedChainIndexes = strValidatedChainIndexes
       
  1647                                            + validatedChainIndexes[i] + ",";
       
  1648             }
       
  1649             strValidatedChainIndexes = strValidatedChainIndexes
       
  1650                                        + validatedChainIndexes[validatedChainIndexes.length - 1];
       
  1651         }
       
  1652         return strValidatedChainIndexes;
       
  1653     }
       
  1654 
       
  1655     private Vector decodeValidatedChainIndexes(String strValidatedChainIndexes)
       
  1656     throws NumberFormatException
       
  1657     {
       
  1658         if (strValidatedChainIndexes == null
       
  1659                 || strValidatedChainIndexes.length() == 0)
       
  1660         {
       
  1661             return null;
       
  1662         }
       
  1663         String[] tokens = Tokenizer.split(strValidatedChainIndexes, ",");
       
  1664         Vector validatedChainIndexes = null;
       
  1665         if (tokens != null && tokens.length > 0)
       
  1666         {
       
  1667             validatedChainIndexes = new Vector();
       
  1668             for (int i=0; i<tokens.length; i++)
       
  1669             {
       
  1670                 validatedChainIndexes.addElement(Integer.valueOf(tokens[i]));
       
  1671             }
       
  1672         }
       
  1673         return validatedChainIndexes;
       
  1674     }
       
  1675 
       
  1676     private int[] decodeAllowedSettings(int allowedSettings)
       
  1677     {
       
  1678         Vector vAllowedSettings = new Vector();
       
  1679         int tmp = allowedSettings & BLANKET_MASK;
       
  1680         if (tmp > 0)
       
  1681         {
       
  1682             vAllowedSettings.addElement(
       
  1683                 new Integer(UserSecuritySettings.BLANKET_INTERACTION_MODE));
       
  1684         }
       
  1685         tmp = allowedSettings & SESSION_MASK;
       
  1686         if (tmp > 0)
       
  1687         {
       
  1688             vAllowedSettings.addElement(
       
  1689                 new Integer(UserSecuritySettings.SESSION_INTERACTION_MODE));
       
  1690         }
       
  1691         tmp = allowedSettings & ONESHOT_MASK;
       
  1692         if (tmp > 0)
       
  1693         {
       
  1694             vAllowedSettings.addElement(
       
  1695                 new Integer(UserSecuritySettings.ONESHOT_INTERACTION_MODE));
       
  1696         }
       
  1697         tmp = allowedSettings & NO_MASK;
       
  1698         if (tmp > 0)
       
  1699         {
       
  1700             vAllowedSettings.addElement(
       
  1701                 new Integer(UserSecuritySettings.NO_INTERACTION_MODE));
       
  1702         }
       
  1703         int[] settings = new int[vAllowedSettings.size()];
       
  1704         for (int i=0; i<vAllowedSettings.size(); i++)
       
  1705         {
       
  1706             settings[i] = ((Integer)vAllowedSettings.elementAt(i)).intValue();
       
  1707         }
       
  1708         return settings;
       
  1709     }
       
  1710 
       
  1711     private int encodeAllowedSettings(int[] allowedSettings)
       
  1712     {
       
  1713         int settings = 0;
       
  1714         if (allowedSettings != null)
       
  1715         {
       
  1716             for (int i=0; i<allowedSettings.length; i++)
       
  1717             {
       
  1718                 switch (allowedSettings[i])
       
  1719                 {
       
  1720                 case UserSecuritySettings.NO_INTERACTION_MODE:
       
  1721                     settings = settings | NO_MASK;
       
  1722                     break;
       
  1723                 case UserSecuritySettings.ONESHOT_INTERACTION_MODE:
       
  1724                     settings = settings | ONESHOT_MASK;
       
  1725                     break;
       
  1726                 case UserSecuritySettings.SESSION_INTERACTION_MODE:
       
  1727                     settings = settings | SESSION_MASK;
       
  1728                     break;
       
  1729                 case UserSecuritySettings.BLANKET_INTERACTION_MODE:
       
  1730                     settings = settings | BLANKET_MASK;
       
  1731                     break;
       
  1732                 }
       
  1733             }
       
  1734         }
       
  1735         return settings;
       
  1736     }
       
  1737 
       
  1738     private String getStorageAttributeValue(StorageEntry entry, String attrName)
       
  1739     {
       
  1740         String attrValue = null;
       
  1741         StorageAttribute attr = entry.getAttribute(attrName);
       
  1742         if (attr != null)
       
  1743         {
       
  1744             String tmp = attr.getValue();
       
  1745             if (tmp != null && tmp.length() > 0)
       
  1746             {
       
  1747                 return attrValue = tmp;
       
  1748             }
       
  1749         }
       
  1750         return attrValue;
       
  1751     }
       
  1752 
       
  1753     private int removeElement(Vector elements, PolicyBasedPermission element)
       
  1754     {
       
  1755         PolicyBasedPermissionImpl p1 = new PolicyBasedPermissionImpl(
       
  1756             element.getName(),
       
  1757             element.getTarget(),
       
  1758             element.getActionList(),
       
  1759             null);
       
  1760         for (int i=0; i<elements.size(); i++)
       
  1761         {
       
  1762             PolicyBasedPermission tmp = (PolicyBasedPermission)elements
       
  1763                                         .elementAt(i);
       
  1764             PolicyBasedPermissionImpl p2 = new PolicyBasedPermissionImpl(
       
  1765                 tmp.getName(),
       
  1766                 tmp.getTarget(),
       
  1767                 tmp.getActionList(),
       
  1768                 null);
       
  1769             if (p1.equals(p2))
       
  1770             {
       
  1771                 UserSecuritySettings s1 = element.getUserSecuritySettings();
       
  1772                 UserSecuritySettings s2 = tmp.getUserSecuritySettings();
       
  1773                 if ((s1 == null && s2 == null)
       
  1774                         || (s1 != null
       
  1775                             && s2 != null
       
  1776                             && s1.equals(s2)))
       
  1777                 {
       
  1778                     // identical permissions
       
  1779                     elements.removeElementAt(i);
       
  1780                     return REMOVED;
       
  1781                 }
       
  1782                 return i;
       
  1783             }
       
  1784         }
       
  1785         return NOT_FOUND;
       
  1786     }
       
  1787 }