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