| 1 | /* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
|
|---|
| 2 | Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 | import java.security.spec.InvalidParameterSpecException;
|
|---|
| 41 | import java.security.spec.AlgorithmParameterSpec;
|
|---|
| 42 | import java.io.IOException;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | AlgorithmParameters is the Algorithm Parameters class which
|
|---|
| 46 | provides an interface through which to modify parameters for
|
|---|
| 47 | classes. This class is used to manage the algorithm parameters.
|
|---|
| 48 |
|
|---|
| 49 | @since JDK 1.2
|
|---|
| 50 | @author Mark Benvenuto
|
|---|
| 51 | */
|
|---|
| 52 | public class AlgorithmParameters
|
|---|
| 53 | {
|
|---|
| 54 | private AlgorithmParametersSpi paramSpi;
|
|---|
| 55 | private Provider provider;
|
|---|
| 56 | private String algorithm;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | Creates an instance of AlgorithmParameters
|
|---|
| 60 |
|
|---|
| 61 | @param paramSpi A parameters engine to use
|
|---|
| 62 | @param provider A provider to use
|
|---|
| 63 | @param algorithm The algorithm
|
|---|
| 64 | */
|
|---|
| 65 | protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
|
|---|
| 66 | Provider provider, String algorithm)
|
|---|
| 67 | {
|
|---|
| 68 | this.paramSpi = paramSpi;
|
|---|
| 69 | this.provider = provider;
|
|---|
| 70 | this.algorithm = algorithm;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | Returns the name of the algorithm used
|
|---|
| 75 |
|
|---|
| 76 | @return A string with the name of the algorithm
|
|---|
| 77 | */
|
|---|
| 78 | public final String getAlgorithm()
|
|---|
| 79 | {
|
|---|
| 80 | return algorithm;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | Gets an instance of the AlgorithmParameters class representing
|
|---|
| 85 | the specified algorithm parameters. If the algorithm is not
|
|---|
| 86 | found then, it throws NoSuchAlgorithmException.
|
|---|
| 87 |
|
|---|
| 88 | The returned AlgorithmParameters must still be intialized with
|
|---|
| 89 | init().
|
|---|
| 90 |
|
|---|
| 91 | @param algorithm the name of algorithm to choose
|
|---|
| 92 | @return a AlgorithmParameters repesenting the desired algorithm
|
|---|
| 93 |
|
|---|
| 94 | @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
|
|---|
| 95 | */
|
|---|
| 96 | public static AlgorithmParameters getInstance(String algorithm) throws
|
|---|
| 97 | NoSuchAlgorithmException
|
|---|
| 98 | {
|
|---|
| 99 | Provider[] p = Security.getProviders();
|
|---|
| 100 |
|
|---|
| 101 | for (int i = 0; i < p.length; i++)
|
|---|
| 102 | {
|
|---|
| 103 | String classname =
|
|---|
| 104 | p[i].getProperty("AlgorithmParameters." + algorithm);
|
|---|
| 105 | if (classname != null)
|
|---|
| 106 | return getInstance(classname, algorithm, p[i]);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | throw new NoSuchAlgorithmException(algorithm);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | Gets an instance of the AlgorithmParameters class representing
|
|---|
| 114 | the specified algorithm parameters from the specified provider.
|
|---|
| 115 | If the algorithm is not found then, it throws
|
|---|
| 116 | NoSuchAlgorithmException. If the provider is not found, then
|
|---|
| 117 | it throws NoSuchProviderException.
|
|---|
| 118 |
|
|---|
| 119 | The returned AlgorithmParameters must still be intialized with
|
|---|
| 120 | init().
|
|---|
| 121 |
|
|---|
| 122 | @param algorithm the name of algorithm to choose
|
|---|
| 123 | @param provider the name of the provider to find the algorithm in
|
|---|
| 124 | @return a AlgorithmParameters repesenting the desired algorithm
|
|---|
| 125 |
|
|---|
| 126 | @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
|
|---|
| 127 | @throws NoSuchProviderException if the provider is not found
|
|---|
| 128 | */
|
|---|
| 129 | public static AlgorithmParameters getInstance(String algorithm,
|
|---|
| 130 | String provider) throws
|
|---|
| 131 | NoSuchAlgorithmException, NoSuchProviderException
|
|---|
| 132 | {
|
|---|
| 133 | Provider p = Security.getProvider(provider);
|
|---|
| 134 | if (p == null)
|
|---|
| 135 | throw new NoSuchProviderException();
|
|---|
| 136 |
|
|---|
| 137 | return getInstance(p.getProperty("AlgorithmParameters." + algorithm),
|
|---|
| 138 | algorithm, p);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | private static AlgorithmParameters getInstance(String classname,
|
|---|
| 142 | String algorithm,
|
|---|
| 143 | Provider provider)
|
|---|
| 144 | throws NoSuchAlgorithmException
|
|---|
| 145 | {
|
|---|
| 146 |
|
|---|
| 147 | try
|
|---|
| 148 | {
|
|---|
| 149 | return new AlgorithmParameters((AlgorithmParametersSpi) Class.
|
|---|
| 150 | forName(classname).newInstance(),
|
|---|
| 151 | provider, algorithm);
|
|---|
| 152 | }
|
|---|
| 153 | catch (ClassNotFoundException cnfe)
|
|---|
| 154 | {
|
|---|
| 155 | throw new NoSuchAlgorithmException("Class not found");
|
|---|
| 156 | }
|
|---|
| 157 | catch (InstantiationException ie)
|
|---|
| 158 | {
|
|---|
| 159 | throw new NoSuchAlgorithmException("Class instantiation failed");
|
|---|
| 160 | }
|
|---|
| 161 | catch (IllegalAccessException iae)
|
|---|
| 162 | {
|
|---|
| 163 | throw new NoSuchAlgorithmException("Illegal Access");
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | Gets the provider that the class is from.
|
|---|
| 169 |
|
|---|
| 170 | @return the provider of this class
|
|---|
| 171 | */
|
|---|
| 172 | public final Provider getProvider()
|
|---|
| 173 | {
|
|---|
| 174 | return provider;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | Initializes the engine with the specified
|
|---|
| 179 | AlgorithmParameterSpec class.
|
|---|
| 180 |
|
|---|
| 181 | @param paramSpec A AlgorithmParameterSpec to initialize with
|
|---|
| 182 |
|
|---|
| 183 | @throws InvalidParameterSpecException For an inapporiate ParameterSpec class
|
|---|
| 184 | */
|
|---|
| 185 | public final void init(AlgorithmParameterSpec paramSpec) throws
|
|---|
| 186 | InvalidParameterSpecException
|
|---|
| 187 | {
|
|---|
| 188 | paramSpi.engineInit(paramSpec);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | Initializes the engine with the specified
|
|---|
| 193 | parameters stored in the byte array and decodes them
|
|---|
| 194 | according to the ASN.1 specification. If the ASN.1
|
|---|
| 195 | specification exists then it succeeds or else it throws
|
|---|
| 196 | IOException.
|
|---|
| 197 |
|
|---|
| 198 | @param params Parameters to initialize with
|
|---|
| 199 |
|
|---|
| 200 | @throws IOException Decoding Error
|
|---|
| 201 | */
|
|---|
| 202 | public final void init(byte[]params) throws IOException
|
|---|
| 203 | {
|
|---|
| 204 | paramSpi.engineInit(params);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | Initializes the engine with the specified
|
|---|
| 209 | parameters stored in the byte array and decodes them
|
|---|
| 210 | according to the specified decoding specification.
|
|---|
| 211 | If format is null, then it is decoded using the ASN.1
|
|---|
| 212 | specification if it exists or else it throws
|
|---|
| 213 | IOException.
|
|---|
| 214 |
|
|---|
| 215 | @param params Parameters to initialize with
|
|---|
| 216 | @param format Name of decoding format to use
|
|---|
| 217 |
|
|---|
| 218 | @throws IOException Decoding Error
|
|---|
| 219 | */
|
|---|
| 220 | public final void init(byte[]params, String format) throws IOException
|
|---|
| 221 | {
|
|---|
| 222 | paramSpi.engineInit(params, format);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | Returns a specification of this AlgorithmParameters object.
|
|---|
| 227 | paramSpec identifies the class to return the AlgortihmParameters
|
|---|
| 228 | in.
|
|---|
| 229 |
|
|---|
| 230 | @param paramSpec Class to return AlgorithmParameters in
|
|---|
| 231 |
|
|---|
| 232 | @return the parameter specification
|
|---|
| 233 |
|
|---|
| 234 | @throws InvalidParameterSpecException if the paramSpec is an invalid parameter class
|
|---|
| 235 | */
|
|---|
| 236 | public final AlgorithmParameterSpec getParameterSpec(Class paramSpec) throws
|
|---|
| 237 | InvalidParameterSpecException
|
|---|
| 238 | {
|
|---|
| 239 | return paramSpi.engineGetParameterSpec(paramSpec);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /**
|
|---|
| 243 | Returns the parameters in the default encoding format.
|
|---|
| 244 | The primary encoding format is ASN.1 format if it exists
|
|---|
| 245 | for the specified type.
|
|---|
| 246 |
|
|---|
| 247 | @return byte array representing the parameters
|
|---|
| 248 | */
|
|---|
| 249 | public final byte[] getEncoded() throws IOException
|
|---|
| 250 | {
|
|---|
| 251 | return paramSpi.engineGetEncoded();
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | Returns the parameters in the specified encoding format.
|
|---|
| 256 | If <code>format</code> is <code>null</code> then the
|
|---|
| 257 | primary encoding format is used, the ASN.1 format,
|
|---|
| 258 | if it exists for the specified type.
|
|---|
| 259 |
|
|---|
| 260 | @return byte array representing the parameters
|
|---|
| 261 | */
|
|---|
| 262 | public final byte[] getEncoded(String format) throws IOException
|
|---|
| 263 | {
|
|---|
| 264 | return paramSpi.engineGetEncoded(format);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | Returns a string representation of the encoding format
|
|---|
| 269 |
|
|---|
| 270 | @return a string containing the string representation
|
|---|
| 271 | */
|
|---|
| 272 | public final String toString()
|
|---|
| 273 | {
|
|---|
| 274 | return paramSpi.engineToString();
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|