1package org.bouncycastle.jce.provider; 2 3import java.io.ByteArrayInputStream; 4import java.io.IOException; 5import java.io.InputStream; 6import java.security.Permission; 7 8import org.bouncycastle.jce.ProviderConfigurationPermission; 9import org.bouncycastle.jce.interfaces.ConfigurableProvider; 10import org.bouncycastle.jce.provider.asymmetric.ec.EC5Util; 11import org.bouncycastle.jce.spec.ECParameterSpec; 12 13public class ProviderUtil 14{ 15 private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory(); 16 17 private static Permission BC_EC_LOCAL_PERMISSION = new ProviderConfigurationPermission( 18 BouncyCastleProvider.PROVIDER_NAME, ConfigurableProvider.THREAD_LOCAL_EC_IMPLICITLY_CA); 19 private static Permission BC_EC_PERMISSION = new ProviderConfigurationPermission( 20 BouncyCastleProvider.PROVIDER_NAME, ConfigurableProvider.EC_IMPLICITLY_CA); 21 22 private static ThreadLocal threadSpec = new ThreadLocal(); 23 private static volatile ECParameterSpec ecImplicitCaParams; 24 25 static void setParameter(String parameterName, Object parameter) 26 { 27 SecurityManager securityManager = System.getSecurityManager(); 28 29 if (parameterName.equals(ConfigurableProvider.THREAD_LOCAL_EC_IMPLICITLY_CA)) 30 { 31 ECParameterSpec curveSpec; 32 33 if (securityManager != null) 34 { 35 securityManager.checkPermission(BC_EC_LOCAL_PERMISSION); 36 } 37 38 if (parameter instanceof ECParameterSpec || parameter == null) 39 { 40 curveSpec = (ECParameterSpec)parameter; 41 } 42 else // assume java.security.spec 43 { 44 curveSpec = EC5Util.convertSpec((java.security.spec.ECParameterSpec)parameter, false); 45 } 46 47 if (curveSpec == null) 48 { 49 threadSpec.remove(); 50 } 51 else 52 { 53 threadSpec.set(curveSpec); 54 } 55 } 56 else if (parameterName.equals(ConfigurableProvider.EC_IMPLICITLY_CA)) 57 { 58 if (securityManager != null) 59 { 60 securityManager.checkPermission(BC_EC_PERMISSION); 61 } 62 63 if (parameter instanceof ECParameterSpec || parameter == null) 64 { 65 ecImplicitCaParams = (ECParameterSpec)parameter; 66 } 67 else // assume java.security.spec 68 { 69 ecImplicitCaParams = EC5Util.convertSpec((java.security.spec.ECParameterSpec)parameter, false); 70 } 71 } 72 } 73 74 public static ECParameterSpec getEcImplicitlyCa() 75 { 76 ECParameterSpec spec = (ECParameterSpec)threadSpec.get(); 77 78 if (spec != null) 79 { 80 return spec; 81 } 82 83 return ecImplicitCaParams; 84 } 85 86 static int getReadLimit(InputStream in) 87 throws IOException 88 { 89 if (in instanceof ByteArrayInputStream) 90 { 91 return in.available(); 92 } 93 94 if (MAX_MEMORY > Integer.MAX_VALUE) 95 { 96 return Integer.MAX_VALUE; 97 } 98 99 return (int)MAX_MEMORY; 100 } 101} 102