Frames | No Frames |
1: /* KeyFactory.java --- Key Factory Class 2: Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.security; 40: 41: import gnu.java.security.Engine; 42: 43: import java.security.spec.InvalidKeySpecException; 44: import java.security.spec.KeySpec; 45: 46: /** 47: * Key factories are used to convert keys (opaque cryptographic keys of type 48: * {@link Key}) into key specifications (transparent representations of the 49: * underlying key material). 50: * 51: * <p>Key factories are bi-directional. They allow a key class to be converted 52: * into a key specification (key material) and back again. For example DSA 53: * public keys can be specified as <code>DSAPublicKeySpec</code> or 54: * <code>X509EncodedKeySpec</code>. A key factory translates these key 55: * specifications.</p> 56: * 57: * @since 1.2 58: * @see Key 59: * @see KeySpec 60: * @see java.security.spec.DSAPublicKeySpec 61: * @see java.security.spec.X509EncodedKeySpec 62: @author Mark Benvenuto 63: */ 64: public class KeyFactory 65: { 66: /** The service name for key factories. */ 67: private static final String KEY_FACTORY = "KeyFactory"; 68: 69: private KeyFactorySpi keyFacSpi; 70: private Provider provider; 71: private String algorithm; 72: 73: /** 74: * Constructs a new instance of <code>KeyFactory</code> with the specified 75: * parameters. 76: * 77: * @param keyFacSpi 78: * the key factory to use. 79: * @param provider 80: * the provider to use. 81: * @param algorithm 82: * the name of the key algorithm to use. 83: */ 84: protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, 85: String algorithm) 86: { 87: this.keyFacSpi = keyFacSpi; 88: this.provider = provider; 89: this.algorithm = algorithm; 90: } 91: 92: /** 93: * Returns a new instance of <code>KeyFactory</code> representing the 94: * specified key factory. 95: * 96: * @param algorithm 97: * the name of algorithm to use. 98: * @return a new instance repesenting the desired algorithm. 99: * @throws NoSuchAlgorithmException 100: * if the algorithm is not implemented by any provider. 101: */ 102: public static KeyFactory getInstance(String algorithm) 103: throws NoSuchAlgorithmException 104: { 105: Provider[] p = Security.getProviders(); 106: for (int i = 0; i < p.length; i++) 107: try 108: { 109: return getInstance(algorithm, p[i]); 110: } 111: catch (NoSuchAlgorithmException e) 112: { 113: // Ignore. 114: } 115: 116: throw new NoSuchAlgorithmException(algorithm); 117: } 118: 119: /** 120: * Returns a new instance of <code>KeyFactory</code> representing the 121: * specified key factory from the specified provider. 122: * 123: * @param algorithm 124: * the name of algorithm to use. 125: * @param provider 126: * the name of the provider to use. 127: * @return a new instance repesenting the desired algorithm. 128: * @throws IllegalArgumentException 129: * if <code>provider</code> is <code>null</code> or is an empty 130: * string. 131: * @throws NoSuchAlgorithmException 132: * if the algorithm is not implemented by the named provider. 133: * @throws NoSuchProviderException 134: * if the named provider was not found. 135: */ 136: public static KeyFactory getInstance(String algorithm, String provider) 137: throws NoSuchAlgorithmException, NoSuchProviderException 138: { 139: if (provider == null || provider.length() == 0) 140: throw new IllegalArgumentException("Illegal provider"); 141: 142: Provider p = Security.getProvider(provider); 143: if (p == null) 144: throw new NoSuchProviderException(provider); 145: 146: return getInstance(algorithm, p); 147: } 148: 149: /** 150: * Returns a new instance of <code>KeyFactory</code> representing the 151: * specified key factory from the designated {@link Provider}. 152: * 153: * @param algorithm 154: * the name of algorithm to use. 155: * @param provider 156: * the {@link Provider} to use. 157: * @return a new instance repesenting the desired algorithm. 158: * @throws IllegalArgumentException 159: * if <code>provider</code> is <code>null</code>. 160: * @throws NoSuchAlgorithmException 161: * if the algorithm is not implemented by {@link Provider}. 162: * @since 1.4 163: * @see Provider 164: */ 165: public static KeyFactory getInstance(String algorithm, Provider provider) 166: throws NoSuchAlgorithmException 167: { 168: if (provider == null) 169: throw new IllegalArgumentException("Illegal provider"); 170: 171: try 172: { 173: return new KeyFactory((KeyFactorySpi) 174: Engine.getInstance(KEY_FACTORY, algorithm, provider), 175: provider, algorithm); 176: } 177: catch (java.lang.reflect.InvocationTargetException ite) 178: { 179: throw new NoSuchAlgorithmException(algorithm); 180: } 181: catch (ClassCastException cce) 182: { 183: throw new NoSuchAlgorithmException(algorithm); 184: } 185: } 186: 187: /** 188: * Returns the {@link Provider} of this instance. 189: * 190: * @return the {@link Provider} of this instance. 191: */ 192: public final Provider getProvider() 193: { 194: return provider; 195: } 196: 197: /** 198: * Returns the name of the algorithm used. 199: * 200: * @return the name of the algorithm used. 201: */ 202: public final String getAlgorithm() 203: { 204: return algorithm; 205: } 206: 207: /** 208: * Generates a public key from the provided key specification. 209: * 210: * @param keySpec 211: * the key specification. 212: * @return the public key. 213: * @throws InvalidKeySpecException 214: * if the key specification is invalid. 215: */ 216: public final PublicKey generatePublic(KeySpec keySpec) 217: throws InvalidKeySpecException 218: { 219: return keyFacSpi.engineGeneratePublic(keySpec); 220: } 221: 222: /** 223: * Generates a private key from the provided key specification. 224: * 225: * @param keySpec 226: * the key specification. 227: * @return the private key. 228: * @throws InvalidKeySpecException 229: * if the key specification is invalid. 230: */ 231: public final PrivateKey generatePrivate(KeySpec keySpec) 232: throws InvalidKeySpecException 233: { 234: return keyFacSpi.engineGeneratePrivate(keySpec); 235: } 236: 237: /** 238: * Returns a key specification for the given key. <code>keySpec</code> 239: * identifies the specification class to return the key material in. 240: * 241: * @param key 242: * the key to use. 243: * @param keySpec 244: * the specification class to use. 245: * @return the key specification in an instance of the requested specification 246: * class. 247: * @throws InvalidKeySpecException 248: * the requested key specification is inappropriate for this key or 249: * the key is unrecognized. 250: */ 251: public final KeySpec getKeySpec(Key key, Class keySpec) 252: throws InvalidKeySpecException 253: { 254: return keyFacSpi.engineGetKeySpec(key, keySpec); 255: } 256: 257: /** 258: * Translates the key from an unknown or untrusted provider into a key from 259: * this key factory. 260: * 261: * @param key 262: * the key to translate from. 263: * @return the translated key. 264: * @throws InvalidKeyException 265: * if the key cannot be processed by this key factory. 266: */ 267: public final Key translateKey(Key key) throws InvalidKeyException 268: { 269: return keyFacSpi.engineTranslateKey(key); 270: } 271: }