001: package org.bouncycastle.jce;
002:
003: import org.bouncycastle.util.Strings;
004:
005: import java.security.BasicPermission;
006: import java.security.Permission;
007: import java.util.StringTokenizer;
008:
009: /**
010: * A permission class to define what can be done with the ConfigurableProvider interface.
011: * <p>
012: * Available permissions are "threadLocalEcImplicitlyCa" and "ecImplicitlyCa" which allow the setting
013: * of the thread local and global ecImplicitlyCa parameters respectively.
014: * </p>
015: * <p>
016: * Examples:
017: * <ul>
018: * <li>ProviderConfigurationPermission("BC"); // enable all permissions</li>
019: * <li>ProviderConfigurationPermission("BC", "threadLocalEcImplicitlyCa"); // enable thread local only</li>
020: * <li>ProviderConfigurationPermission("BC", "ecImplicitlyCa"); // enable global setting only</li>
021: * <li>ProviderConfigurationPermission("BC", "threadLocalEcImplicitlyCa, ecImplicitlyCa"); // enable both explicitly</li>
022: * </ul>
023: * <p>
024: * Note: permission checks are only enforced if a security manager is present.
025: * </p>
026: */
027: public class ProviderConfigurationPermission extends BasicPermission {
028: private static final int THREAD_LOCAL_EC_IMPLICITLY_CA = 0x01;
029:
030: private static final int EC_IMPLICITLY_CA = 0x02;
031: private static final int ALL = THREAD_LOCAL_EC_IMPLICITLY_CA
032: | EC_IMPLICITLY_CA;
033:
034: private static final String THREAD_LOCAL_EC_IMPLICITLY_CA_STR = "threadlocalecimplicitlyca";
035: private static final String EC_IMPLICITLY_CA_STR = "ecimplicitlyca";
036: private static final String ALL_STR = "all";
037:
038: private final String actions;
039: private final int permissionMask;
040:
041: public ProviderConfigurationPermission(String name) {
042: super (name);
043: this .actions = "all";
044: this .permissionMask = ALL;
045: }
046:
047: public ProviderConfigurationPermission(String name, String actions) {
048: super (name, actions);
049: this .actions = actions;
050: this .permissionMask = calculateMask(actions);
051: }
052:
053: private int calculateMask(String actions) {
054: StringTokenizer tok = new StringTokenizer(Strings
055: .toLowerCase(actions), " ,");
056: int mask = 0;
057:
058: while (tok.hasMoreTokens()) {
059: String s = tok.nextToken();
060:
061: if (s.equals(THREAD_LOCAL_EC_IMPLICITLY_CA_STR)) {
062: mask |= THREAD_LOCAL_EC_IMPLICITLY_CA;
063: } else if (s.equals(EC_IMPLICITLY_CA_STR)) {
064: mask |= EC_IMPLICITLY_CA;
065: } else if (s.equals(ALL_STR)) {
066: mask |= ALL;
067: }
068: }
069:
070: if (mask == 0) {
071: throw new IllegalArgumentException(
072: "unknown permissions passed to mask");
073: }
074:
075: return mask;
076: }
077:
078: public String getActions() {
079: return actions;
080: }
081:
082: public boolean implies(Permission permission) {
083: if (!(permission instanceof ProviderConfigurationPermission)) {
084: return false;
085: }
086:
087: if (!this .getName().equals(permission.getName())) {
088: return false;
089: }
090:
091: ProviderConfigurationPermission other = (ProviderConfigurationPermission) permission;
092:
093: return (this .permissionMask & other.permissionMask) == other.permissionMask;
094: }
095:
096: public boolean equals(Object obj) {
097: if (obj == this ) {
098: return true;
099: }
100:
101: if (obj instanceof ProviderConfigurationPermission) {
102: ProviderConfigurationPermission other = (ProviderConfigurationPermission) obj;
103:
104: return this .permissionMask == other.permissionMask
105: && this .getName().equals(other.getName());
106: }
107:
108: return false;
109: }
110:
111: public int hashCode() {
112: return this.getName().hashCode() + this.permissionMask;
113: }
114: }
|