Frames | No Frames |
1: /* AlgorithmParameters.java --- Algorithm Parameters Implementation 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.io.IOException; 44: import java.security.spec.AlgorithmParameterSpec; 45: import java.security.spec.InvalidParameterSpecException; 46: 47: /** 48: * <code>AlgorithmParameters</code> is an Algorithm Parameters class which 49: * provides an interface through which the user can manage the parameters of an 50: * Algorithm. 51: * 52: * @author Mark Benvenuto 53: * @since 1.2 54: * @see AlgorithmParameterSpec 55: * @see java.security.spec.DSAParameterSpec 56: * @see KeyPairGenerator 57: */ 58: public class AlgorithmParameters 59: { 60: /** Service name for algorithm parameters. */ 61: private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters"; 62: 63: private AlgorithmParametersSpi paramSpi; 64: private Provider provider; 65: private String algorithm; 66: 67: /** 68: * Constructs a new instance of <code>AlgorithmParameters</code>. 69: * 70: * @param paramSpi 71: * the engine to use. 72: * @param provider 73: * the provider to use. 74: * @param algorithm 75: * the algorithm to use. 76: */ 77: protected AlgorithmParameters(AlgorithmParametersSpi paramSpi, 78: Provider provider, String algorithm) 79: { 80: this.paramSpi = paramSpi; 81: this.provider = provider; 82: this.algorithm = algorithm; 83: } 84: 85: /** @return A string with the name of the algorithm used. */ 86: public final String getAlgorithm() 87: { 88: return algorithm; 89: } 90: 91: /** 92: * Returns a new instance of <code>AlgorithmParameters</code> representing 93: * the specified algorithm parameters. 94: * 95: * <p>The returned <code>AlgorithmParameters</code> must still be initialized 96: * with an <code>init()</code> method.</p> 97: * 98: * @param algorithm 99: * the algorithm to use. 100: * @return the new instance repesenting the desired algorithm. 101: * @throws NoSuchAlgorithmException 102: * if the algorithm is not implemented by any provider. 103: */ 104: public static AlgorithmParameters getInstance(String algorithm) 105: throws NoSuchAlgorithmException 106: { 107: Provider[] p = Security.getProviders(); 108: 109: for (int i = 0; i < p.length; i++) 110: try 111: { 112: return getInstance(algorithm, p[i]); 113: } 114: catch (NoSuchAlgorithmException e) 115: { 116: // Ignore this. 117: } 118: 119: throw new NoSuchAlgorithmException(algorithm); 120: } 121: 122: /** 123: * Returns a new instance of <code>AlgorithmParameters</code> representing 124: * the specified algorithm parameters from a named provider. 125: * 126: * <p>The returned <code>AlgorithmParameters</code> must still be intialized 127: * with an <code>init()</code> method.</p> 128: * 129: * @param algorithm 130: * the algorithm to use. 131: * @param provider 132: * the name of the {@link Provider} to use. 133: * @return the new instance repesenting the desired algorithm. 134: * @throws NoSuchAlgorithmException 135: * if the algorithm is not implemented by the named provider. 136: * @throws NoSuchProviderException 137: * if the named provider was not found. 138: * @throws IllegalArgumentException 139: * if <code>provider</code> is <code>null</code> or is an empty 140: * string. 141: */ 142: public static AlgorithmParameters getInstance(String algorithm, String provider) 143: throws NoSuchAlgorithmException, NoSuchProviderException 144: { 145: if (provider == null || provider.length() == 0) 146: throw new IllegalArgumentException("Illegal provider"); 147: 148: Provider p = Security.getProvider(provider); 149: if (p == null) 150: throw new NoSuchProviderException(provider); 151: 152: return getInstance(algorithm, p); 153: } 154: 155: /** 156: * Returns a new instance of <code>AlgorithmParameters</code> representing 157: * the specified algorithm parameters from the specified {@link Provider}. 158: * 159: * <p>The returned <code>AlgorithmParameters</code> must still be intialized 160: * with an <code>init()</code> method.</p> 161: * 162: * @param algorithm 163: * the algorithm to use. 164: * @param provider 165: * the {@link Provider} to use. 166: * @return the new instance repesenting the desired algorithm. 167: * @throws NoSuchAlgorithmException 168: * if the algorithm is not implemented by the {@link Provider}. 169: * @throws IllegalArgumentException 170: * if <code>provider</code> is <code>null</code>. 171: * @since 1.4 172: */ 173: public static AlgorithmParameters getInstance(String algorithm, 174: Provider provider) 175: throws NoSuchAlgorithmException 176: { 177: if (provider == null) 178: throw new IllegalArgumentException("Illegal provider"); 179: 180: try 181: { 182: return new AlgorithmParameters((AlgorithmParametersSpi) 183: Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider), 184: provider, algorithm); 185: } 186: catch (java.lang.reflect.InvocationTargetException ite) 187: { 188: throw new NoSuchAlgorithmException(algorithm); 189: } 190: catch (ClassCastException cce) 191: { 192: throw new NoSuchAlgorithmException(algorithm); 193: } 194: } 195: 196: /** @return the provider of this parameter object. */ 197: public final Provider getProvider() 198: { 199: return provider; 200: } 201: 202: /** 203: * Initializes the engine with the specified {@link AlgorithmParameterSpec}. 204: * 205: * @param paramSpec 206: * A {@link AlgorithmParameterSpec} to use. 207: * @throws InvalidParameterSpecException 208: * if <code>paramSpec</code> is invalid. 209: */ 210: public final void init(AlgorithmParameterSpec paramSpec) 211: throws InvalidParameterSpecException 212: { 213: paramSpi.engineInit(paramSpec); 214: } 215: 216: /** 217: * Initializes the engine with the specified parameters stored in the byte 218: * array and decodes them according to the ASN.1 specification. If the ASN.1 219: * specification exists then it succeeds otherwise an {@link IOException} is 220: * thrown. 221: * 222: * @param params 223: * the parameters to use. 224: * @throws IOException 225: * if a decoding error occurs. 226: */ 227: public final void init(byte[]params) throws IOException 228: { 229: paramSpi.engineInit(params); 230: } 231: 232: /** 233: * Initializes the engine with the specified parameters stored in the byte 234: * array and decodes them according to the specified decoding specification. 235: * If <code>format</code> is <code>null</code>, then this method decodes the 236: * byte array using the ASN.1 specification if it exists, otherwise it throws 237: * an {@link IOException}. 238: * 239: * @param params 240: * the parameters to use. 241: * @param format 242: * the name of decoding format to use. 243: * @throws IOException 244: * if a decoding error occurs. 245: */ 246: public final void init(byte[]params, String format) throws IOException 247: { 248: paramSpi.engineInit(params, format); 249: } 250: 251: /** 252: * Returns a new instance of <code>AlgorithmParameters</code> as a 253: * designated parameter specification {@link Class}. 254: * 255: * @param paramSpec 256: * the {@link Class} to use. 257: * @return the parameter specification. 258: * @throws InvalidParameterSpecException 259: * if <code>paramSpec</code> is invalid. 260: */ 261: public final AlgorithmParameterSpec getParameterSpec(Class paramSpec) 262: throws InvalidParameterSpecException 263: { 264: return paramSpi.engineGetParameterSpec(paramSpec); 265: } 266: 267: /** 268: * Returns the parameters in the default encoding format. The primary encoding 269: * format is ASN.1 if it exists for the specified type. 270: * 271: * @return byte array representing the parameters. 272: */ 273: public final byte[] getEncoded() throws IOException 274: { 275: return paramSpi.engineGetEncoded(); 276: } 277: 278: /** 279: * Returns the parameters in the specified encoding format. If 280: * <code>format</code> is <code>null</code> then the ASN.1 encoding 281: * format is used if it exists for the specified type. 282: * 283: * @param format 284: * the name of the encoding format to use. 285: * @return the parameters encoded using the specified encoding scheme. 286: * @throws IOException 287: * if an encoding exception occurs, or if this parameter object has 288: * not been initialized. 289: */ 290: public final byte[] getEncoded(String format) throws IOException 291: { 292: return paramSpi.engineGetEncoded(format); 293: } 294: 295: /** 296: * Returns a string representation of the encoded form. 297: * 298: * @return a string representation of the encoded form. 299: */ 300: public final String toString() 301: { 302: return paramSpi.engineToString(); 303: } 304: }