| 1 |
package org.globus.gsi.gssapi; |
| 2 |
|
| 3 |
import java.util.Map; |
| 4 |
import java.util.Hashtable; |
| 5 |
|
| 6 |
import java.security.KeyPair; |
| 7 |
import java.security.KeyPairGenerator; |
| 8 |
import java.security.NoSuchAlgorithmException; |
| 9 |
import java.security.NoSuchProviderException; |
| 10 |
|
| 11 |
import org.apache.commons.logging.Log; |
| 12 |
import org.apache.commons.logging.LogFactory; |
| 13 |
import org.globus.common.CoGProperties; |
| 14 |
|
| 15 |
/** |
| 16 |
* Simple cache for key pairs. The cache is used to avoid excessive |
| 17 |
* CPU consumption from key pair generation. In particular for |
| 18 |
* purposes of delegation, reusing a key pair is safe. |
| 19 |
* |
| 20 |
* @author Gerd Behrmann ([email protected]) |
| 21 |
*/ |
| 22 |
public class KeyPairCache { |
| 23 |
|
| 24 |
static class KeyPairCacheEntry { |
| 25 |
private long created_at; |
| 26 |
private KeyPair keys; |
| 27 |
|
| 28 |
public KeyPairCacheEntry(KeyPair keys, long created_at) { |
| 29 |
this.keys = keys; |
| 30 |
this.created_at = created_at; |
| 31 |
} |
| 32 |
|
| 33 |
public long getCreatedAt() { |
| 34 |
return created_at; |
| 35 |
} |
| 36 |
|
| 37 |
public KeyPair getKeyPair() { |
| 38 |
return keys; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private static Log logger = |
| 43 |
LogFactory.getLog(GlobusGSSContextImpl.class.getName()); |
| 44 |
|
| 45 |
public static final String DEFAULT_ALGORITHM = "RSA"; |
| 46 |
public static final String DEFAULT_PROVIDER = "BC"; |
| 47 |
|
| 48 |
private final String algorithm; |
| 49 |
private final String provider; |
| 50 |
private final long lifetime; |
| 51 |
private static KeyPairCache keyPairCache; |
| 52 |
|
| 53 |
/** |
| 54 |
* Hash table of cache entries. The use of <code>Hashtable</code> |
| 55 |
* is significant, since we rely on access to the table being |
| 56 |
* synchronized. |
| 57 |
*/ |
| 58 |
private final Map entries = new Hashtable(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates a KeyPairCache object for the specified algorithm, as |
| 62 |
* supplied from the specified provider. |
| 63 |
* |
| 64 |
* @param algorithm the standard string name of the algorithm. See |
| 65 |
* Appendix A in the Java Cryptography Architecture API |
| 66 |
* Specification & Reference for information about standard |
| 67 |
* algorithm names. |
| 68 |
* @param provider the string name of the provider. |
| 69 |
* @param lifetime the lifetime of the cache in milliseconds. |
| 70 |
*/ |
| 71 |
private KeyPairCache(String algorithm, String provider, long lifetime) { |
| 72 |
this.algorithm = algorithm; |
| 73 |
this.provider = provider; |
| 74 |
this.lifetime = lifetime; |
| 75 |
} |
| 76 |
|
| 77 |
public static synchronized KeyPairCache getKeyPairCache() { |
| 78 |
|
| 79 |
if (keyPairCache == null) { |
| 80 |
keyPairCache = new KeyPairCache(DEFAULT_ALGORITHM, DEFAULT_PROVIDER, |
| 81 |
CoGProperties.getDefault(). |
| 82 |
getDelegationKeyCacheLifetime()); |
| 83 |
} |
| 84 |
return keyPairCache; |
| 85 |
} |
| 86 |
|
| 87 |
public static synchronized KeyPairCache getKeyPairCache(String algorithm, |
| 88 |
String provider, |
| 89 |
long lifetime) { |
| 90 |
|
| 91 |
if (keyPairCache == null) { |
| 92 |
keyPairCache = new KeyPairCache(algorithm, provider, lifetime); |
| 93 |
} |
| 94 |
|
| 95 |
return keyPairCache; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns a key pair of size <code>bits</code>. The same key pair |
| 100 |
* may be returned several times within a period of the cache |
| 101 |
* lifetime. |
| 102 |
* |
| 103 |
* If lifetime was set to zero or less than zero, no keys are cached. |
| 104 |
* |
| 105 |
* @param bits the keysize. This is an algorithm-specific metric, |
| 106 |
* such as modulus length, specified in number of bits. |
| 107 |
* @throws NoSuchAlgorithmException if the algorithm is not |
| 108 |
* available in the environment. |
| 109 |
* @throws NoSuchProviderException if the provider is not |
| 110 |
* available in the environment. |
| 111 |
*/ |
| 112 |
public KeyPair getKeyPair(int bits) |
| 113 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
| 114 |
|
| 115 |
if (this.lifetime < 1) { |
| 116 |
|
| 117 |
logger.debug("Cache lifetime is less than 1, generating new " + |
| 118 |
"keypair each time"); |
| 119 |
KeyPairGenerator generator = |
| 120 |
KeyPairGenerator.getInstance(this.algorithm, this.provider); |
| 121 |
generator.initialize(bits); |
| 122 |
return generator.generateKeyPair(); |
| 123 |
} |
| 124 |
|
| 125 |
long st = System.currentTimeMillis(); |
| 126 |
Integer keysize = new Integer(bits); |
| 127 |
KeyPairCacheEntry entry = (KeyPairCacheEntry)entries.get(keysize); |
| 128 |
if (entry == null || st - entry.getCreatedAt() >= lifetime) { |
| 129 |
logger.debug("Creating " + bits + " bits keypair"); |
| 130 |
|
| 131 |
KeyPairGenerator generator = |
| 132 |
KeyPairGenerator.getInstance(algorithm, provider); |
| 133 |
generator.initialize(bits); |
| 134 |
logger.debug("Time to generate key pair: " + |
| 135 |
(System.currentTimeMillis() - st)); |
| 136 |
|
| 137 |
entry = new KeyPairCacheEntry(generator.generateKeyPair(), st); |
| 138 |
entries.put(keysize, entry); |
| 139 |
} |
| 140 |
return entry.getKeyPair(); |
| 141 |
} |
| 142 |
} |