1package org.bouncycastle.jcajce.provider.config; 2 3import java.security.BasicPermission; 4import java.security.Permission; 5import java.util.StringTokenizer; 6 7import org.bouncycastle.util.Strings; 8 9/** 10 * A permission class to define what can be done with the ConfigurableProvider interface. 11 * <p> 12 * Available permissions are "threadLocalEcImplicitlyCa" and "ecImplicitlyCa" which allow the setting 13 * of the thread local and global ecImplicitlyCa parameters respectively. 14 * </p> 15 * <p> 16 * Examples: 17 * <ul> 18 * <li>ProviderConfigurationPermission("BC"); // enable all permissions</li> 19 * <li>ProviderConfigurationPermission("BC", "threadLocalEcImplicitlyCa"); // enable thread local only</li> 20 * <li>ProviderConfigurationPermission("BC", "ecImplicitlyCa"); // enable global setting only</li> 21 * <li>ProviderConfigurationPermission("BC", "threadLocalEcImplicitlyCa, ecImplicitlyCa"); // enable both explicitly</li> 22 * </ul> 23 * <p> 24 * Note: permission checks are only enforced if a security manager is present. 25 * </p> 26 */ 27public class ProviderConfigurationPermission 28 extends BasicPermission 29{ 30 private static final int THREAD_LOCAL_EC_IMPLICITLY_CA = 0x01; 31 private static final int EC_IMPLICITLY_CA = 0x02; 32 private static final int THREAD_LOCAL_DH_DEFAULT_PARAMS = 0x04; 33 private static final int DH_DEFAULT_PARAMS = 0x08; 34 35 private static final int ALL = THREAD_LOCAL_EC_IMPLICITLY_CA | EC_IMPLICITLY_CA | THREAD_LOCAL_DH_DEFAULT_PARAMS | DH_DEFAULT_PARAMS; 36 37 private static final String THREAD_LOCAL_EC_IMPLICITLY_CA_STR = "threadlocalecimplicitlyca"; 38 private static final String EC_IMPLICITLY_CA_STR = "ecimplicitlyca"; 39 private static final String THREAD_LOCAL_DH_DEFAULT_PARAMS_STR = "threadlocaldhdefaultparams"; 40 private static final String DH_DEFAULT_PARAMS_STR = "dhdefaultparams"; 41 42 private static final String ALL_STR = "all"; 43 44 private final String actions; 45 private final int permissionMask; 46 47 public ProviderConfigurationPermission(String name) 48 { 49 super(name); 50 this.actions = "all"; 51 this.permissionMask = ALL; 52 } 53 54 public ProviderConfigurationPermission(String name, String actions) 55 { 56 super(name, actions); 57 this.actions = actions; 58 this.permissionMask = calculateMask(actions); 59 } 60 61 private int calculateMask( 62 String actions) 63 { 64 StringTokenizer tok = new StringTokenizer(Strings.toLowerCase(actions), " ,"); 65 int mask = 0; 66 67 while (tok.hasMoreTokens()) 68 { 69 String s = tok.nextToken(); 70 71 if (s.equals(THREAD_LOCAL_EC_IMPLICITLY_CA_STR)) 72 { 73 mask |= THREAD_LOCAL_EC_IMPLICITLY_CA; 74 } 75 else if (s.equals(EC_IMPLICITLY_CA_STR)) 76 { 77 mask |= EC_IMPLICITLY_CA; 78 } 79 else if (s.equals(THREAD_LOCAL_DH_DEFAULT_PARAMS_STR)) 80 { 81 mask |= THREAD_LOCAL_DH_DEFAULT_PARAMS; 82 } 83 else if (s.equals(DH_DEFAULT_PARAMS_STR)) 84 { 85 mask |= DH_DEFAULT_PARAMS; 86 } 87 else if (s.equals(ALL_STR)) 88 { 89 mask |= ALL; 90 } 91 } 92 93 if (mask == 0) 94 { 95 throw new IllegalArgumentException("unknown permissions passed to mask"); 96 } 97 98 return mask; 99 } 100 101 public String getActions() 102 { 103 return actions; 104 } 105 106 public boolean implies( 107 Permission permission) 108 { 109 if (!(permission instanceof ProviderConfigurationPermission)) 110 { 111 return false; 112 } 113 114 if (!this.getName().equals(permission.getName())) 115 { 116 return false; 117 } 118 119 ProviderConfigurationPermission other = (ProviderConfigurationPermission)permission; 120 121 return (this.permissionMask & other.permissionMask) == other.permissionMask; 122 } 123 124 public boolean equals( 125 Object obj) 126 { 127 if (obj == this) 128 { 129 return true; 130 } 131 132 if (obj instanceof ProviderConfigurationPermission) 133 { 134 ProviderConfigurationPermission other = (ProviderConfigurationPermission)obj; 135 136 return this.permissionMask == other.permissionMask && this.getName().equals(other.getName()); 137 } 138 139 return false; 140 } 141 142 public int hashCode() 143 { 144 return this.getName().hashCode() + this.permissionMask; 145 } 146} 147