1 7 8 package javax.crypto; 9 10 import java.security.*; 11 import java.net.*; 12 import java.util.*; 13 import java.util.jar.*; 14 15 30 31 final class JceSecurityManager extends SecurityManager { 32 33 private static final String permsFile = "cryptoPerms"; 34 private static final CryptoPermissions defaultPolicy; 35 private static final CryptoPermissions exemptPolicy; 36 private static final CryptoAllPermission allPerm; 37 private static final Vector TrustedCallersCache = new Vector(2); 38 private static final Map exemptCache = new HashMap(); 39 40 static final JceSecurityManager INSTANCE; 42 43 static { 44 defaultPolicy = JceSecurity.getDefaultPolicy(); 45 exemptPolicy = JceSecurity.getExemptPolicy(); 46 allPerm = CryptoAllPermission.INSTANCE; 47 INSTANCE = (JceSecurityManager) 48 AccessController.doPrivileged(new PrivilegedAction() { 49 public Object run() { 50 return new JceSecurityManager(); 51 } 52 }); 53 } 54 55 private JceSecurityManager() { 56 } 58 59 63 CryptoPermission getCryptoPermission(String alg) { 64 65 CryptoPermission defaultPerm = getDefaultPermission(alg); 69 if (defaultPerm == CryptoAllPermission.INSTANCE) { 70 return defaultPerm; 71 } 72 73 Class[] context = getClassContext(); 79 URL callerCodeBase = null; 80 int i; 81 for (i=0; i<context.length; i++) { 82 Class cls = context[i]; 83 callerCodeBase = JceSecurity.getCodeBase(cls); 84 if (callerCodeBase != null) { 85 break; 86 } else { 87 if (cls.getName().startsWith("javax.crypto.")) { 88 continue; 90 } 91 return defaultPerm; 93 } 94 } 95 96 if (i == context.length) { 97 return defaultPerm; 98 } 99 100 CryptoPermissions appPerms; 101 synchronized (this.getClass()) { 102 if (exemptCache.containsKey(callerCodeBase)) { 103 appPerms = (CryptoPermissions)exemptCache.get(callerCodeBase); 104 } else { 105 appPerms = getAppPermissions(callerCodeBase); 106 exemptCache.put(callerCodeBase, appPerms); 107 } 108 } 109 110 if (appPerms == null) { 111 return defaultPerm; 112 } 113 114 if (appPerms.implies(allPerm)) { 116 return allPerm; 117 } 118 119 PermissionCollection appPc = appPerms.getPermissionCollection(alg); 124 if (appPc == null) { 125 return defaultPerm; 126 } 127 Enumeration enum_ = appPc.elements(); 128 while (enum_.hasMoreElements()) { 129 CryptoPermission cp = (CryptoPermission)enum_.nextElement(); 130 if (cp.getExemptionMechanism() == null) { 131 return cp; 132 } 133 } 134 135 PermissionCollection exemptPc = 139 exemptPolicy.getPermissionCollection(alg); 140 if (exemptPc == null) { 141 return defaultPerm; 142 } 143 144 enum_ = exemptPc.elements(); 152 while (enum_.hasMoreElements()) { 153 CryptoPermission cp = (CryptoPermission)enum_.nextElement(); 154 try { 155 ExemptionMechanism.getInstance(cp.getExemptionMechanism()); 156 if (cp.getAlgorithm().equals( 157 CryptoPermission.ALG_NAME_WILDCARD)) { 158 CryptoPermission newCp; 159 if (cp.getCheckParam()) { 160 newCp = new CryptoPermission( 161 alg, cp.getMaxKeySize(), 162 cp.getAlgorithmParameterSpec(), 163 cp.getExemptionMechanism()); 164 } else { 165 newCp = new CryptoPermission( 166 alg, cp.getMaxKeySize(), 167 cp.getExemptionMechanism()); 168 } 169 if (appPerms.implies(newCp)) { 170 return newCp; 171 } 172 } 173 174 if (appPerms.implies(cp)) { 175 return cp; 176 } 177 } catch (Exception e) { 178 continue; 179 } 180 } 181 return defaultPerm; 182 } 183 184 private static CryptoPermissions getAppPermissions(URL callerCodeBase) { 185 JarFile jf; 187 try { 188 jf = JceSecurity.verifyExemptJar(callerCodeBase); 189 } catch (Exception e) { 190 return null; 191 } 192 JarEntry je = jf.getJarEntry(permsFile); 193 if (je == null) { 194 return null; 196 } 197 198 202 CryptoPermissions appPerms = new CryptoPermissions(); 203 try { 204 appPerms.load(jf.getInputStream(je)); 205 } catch (Exception e) { 206 return null; 207 } 208 209 jf = null; 210 211 return appPerms; 212 } 213 214 217 private CryptoPermission getDefaultPermission(String alg) { 218 Enumeration enum_ = 219 defaultPolicy.getPermissionCollection(alg).elements(); 220 return (CryptoPermission)enum_.nextElement(); 221 } 222 223 boolean isCallerTrusted() { 225 Class[] context = getClassContext(); 227 URL callerCodeBase = null; 228 int i; 229 for (i=0; i<context.length; i++) { 230 callerCodeBase = JceSecurity.getCodeBase(context[i]); 231 if (callerCodeBase != null) { 232 break; 233 } 234 } 235 if (i == context.length) { 237 return true; 238 } 239 if (TrustedCallersCache.contains(context[i])) { 241 return true; 242 } 243 try { 245 JceSecurity.verifyProviderJar(callerCodeBase); 246 } catch (Exception e2) { 247 return false; 248 } 249 TrustedCallersCache.addElement(context[i]); 250 return true; 251 } 252 } 253 | Popular Tags |