|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.math.BigInteger; |
| 5 | +import java.security.AccessControlException; |
| 6 | +import java.security.AccessController; |
| 7 | +import java.security.PrivilegedAction; |
| 8 | +import java.security.Security; |
| 9 | +import java.util.HashMap; |
5 | 10 | import java.util.Map; |
6 | 11 | import java.util.WeakHashMap; |
7 | 12 |
|
|
17 | 22 | import org.bouncycastle.asn1.x9.X9ObjectIdentifiers; |
18 | 23 | import org.bouncycastle.crypto.CryptoServicesRegistrar; |
19 | 24 | import org.bouncycastle.math.Primes; |
20 | | -import org.bouncycastle.util.Properties; |
| 25 | +import org.bouncycastle.util.Strings; |
21 | 26 |
|
| 27 | +/** |
| 28 | + * A checker for vetting subject public keys based on the direct checking of the ASN.1 |
| 29 | + */ |
22 | 30 | public class SubjectPublicKeyInfoChecker |
23 | 31 | { |
24 | 32 | private static final Cache validatedQs = new Cache(); |
@@ -173,6 +181,29 @@ else if (bits >= 512) |
173 | 181 | } |
174 | 182 | } |
175 | 183 |
|
| 184 | + /** |
| 185 | + * Enable the specified override property for the current thread only. |
| 186 | + * |
| 187 | + * @param propertyName the property name for the override. |
| 188 | + * @param enable true if the override should be enabled, false if it should be disabled. |
| 189 | + * @return true if the override was already set true, false otherwise. |
| 190 | + */ |
| 191 | + public static boolean setThreadOverride(String propertyName, boolean enable) |
| 192 | + { |
| 193 | + return Properties.setThreadOverride(propertyName, enable); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Remove any value for the specified override property for the current thread only. |
| 198 | + * |
| 199 | + * @param propertyName the property name for the override. |
| 200 | + * @return true if the override was already set true in thread local, false otherwise. |
| 201 | + */ |
| 202 | + public static boolean removeThreadOverride(String propertyName) |
| 203 | + { |
| 204 | + return Properties.removeThreadOverride(propertyName); |
| 205 | + } |
| 206 | + |
176 | 207 | private static class Cache |
177 | 208 | { |
178 | 209 | private final Map<BigInteger, Boolean> values = new WeakHashMap<BigInteger, Boolean>(); |
@@ -206,4 +237,140 @@ public synchronized void clear() |
206 | 237 | } |
207 | 238 | } |
208 | 239 | } |
| 240 | + |
| 241 | + private static class Properties |
| 242 | + { |
| 243 | + private Properties() |
| 244 | + { |
| 245 | + } |
| 246 | + |
| 247 | + private static final ThreadLocal threadProperties = new ThreadLocal(); |
| 248 | + |
| 249 | + /** |
| 250 | + * Return whether a particular override has been set to true. |
| 251 | + * |
| 252 | + * @param propertyName the property name for the override. |
| 253 | + * @return true if the property is set to "true", false otherwise. |
| 254 | + */ |
| 255 | + static boolean isOverrideSet(String propertyName) |
| 256 | + { |
| 257 | + try |
| 258 | + { |
| 259 | + return isSetTrue(getPropertyValue(propertyName)); |
| 260 | + } |
| 261 | + catch (AccessControlException e) |
| 262 | + { |
| 263 | + return false; |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + static boolean setThreadOverride(String propertyName, boolean enable) |
| 268 | + { |
| 269 | + boolean isSet = isOverrideSet(propertyName); |
| 270 | + |
| 271 | + Map localProps = (Map)threadProperties.get(); |
| 272 | + if (localProps == null) |
| 273 | + { |
| 274 | + localProps = new HashMap(); |
| 275 | + |
| 276 | + threadProperties.set(localProps); |
| 277 | + } |
| 278 | + |
| 279 | + localProps.put(propertyName, enable ? "true" : "false"); |
| 280 | + |
| 281 | + return isSet; |
| 282 | + } |
| 283 | + |
| 284 | + static boolean removeThreadOverride(String propertyName) |
| 285 | + { |
| 286 | + Map localProps = (Map)threadProperties.get(); |
| 287 | + if (localProps != null) |
| 288 | + { |
| 289 | + String p = (String)localProps.remove(propertyName); |
| 290 | + if (p != null) |
| 291 | + { |
| 292 | + if (localProps.isEmpty()) |
| 293 | + { |
| 294 | + threadProperties.remove(); |
| 295 | + } |
| 296 | + |
| 297 | + return "true".equals(Strings.toLowerCase(p)); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + return false; |
| 302 | + } |
| 303 | + |
| 304 | + /** |
| 305 | + * Return propertyName as an integer, defaultValue used if not defined. |
| 306 | + * |
| 307 | + * @param propertyName name of property. |
| 308 | + * @param defaultValue integer to return if property not defined. |
| 309 | + * @return value of property, or default if not found, as an int. |
| 310 | + */ |
| 311 | + static int asInteger(String propertyName, int defaultValue) |
| 312 | + { |
| 313 | + String p = getPropertyValue(propertyName); |
| 314 | + |
| 315 | + if (p != null) |
| 316 | + { |
| 317 | + return Integer.parseInt(p); |
| 318 | + } |
| 319 | + |
| 320 | + return defaultValue; |
| 321 | + } |
| 322 | + |
| 323 | + /** |
| 324 | + * Return the String value of the property propertyName. Property valuation |
| 325 | + * starts with java.security, then thread local, then system properties. |
| 326 | + * |
| 327 | + * @param propertyName name of property. |
| 328 | + * @return value of property as a String, null if not defined. |
| 329 | + */ |
| 330 | + static String getPropertyValue(final String propertyName) |
| 331 | + { |
| 332 | + String val = (String)AccessController.doPrivileged(new PrivilegedAction() |
| 333 | + { |
| 334 | + public Object run() |
| 335 | + { |
| 336 | + return Security.getProperty(propertyName); |
| 337 | + } |
| 338 | + }); |
| 339 | + if (val != null) |
| 340 | + { |
| 341 | + return val; |
| 342 | + } |
| 343 | + |
| 344 | + Map localProps = (Map)threadProperties.get(); |
| 345 | + if (localProps != null) |
| 346 | + { |
| 347 | + String p = (String)localProps.get(propertyName); |
| 348 | + if (p != null) |
| 349 | + { |
| 350 | + return p; |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + return (String)AccessController.doPrivileged(new PrivilegedAction() |
| 355 | + { |
| 356 | + public Object run() |
| 357 | + { |
| 358 | + return System.getProperty(propertyName); |
| 359 | + } |
| 360 | + }); |
| 361 | + } |
| 362 | + |
| 363 | + private static boolean isSetTrue(String p) |
| 364 | + { |
| 365 | + if (p == null || p.length() != 4) |
| 366 | + { |
| 367 | + return false; |
| 368 | + } |
| 369 | + |
| 370 | + return (p.charAt(0) == 't' || p.charAt(0) == 'T') |
| 371 | + && (p.charAt(1) == 'r' || p.charAt(1) == 'R') |
| 372 | + && (p.charAt(2) == 'u' || p.charAt(2) == 'U') |
| 373 | + && (p.charAt(3) == 'e' || p.charAt(3) == 'E'); |
| 374 | + } |
| 375 | + } |
209 | 376 | } |
0 commit comments