AlgorithmParameters.java revision 897538a36c18f4db8f9f68ee566aec0bda842e9f
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.security;
19
20import java.io.IOException;
21import java.security.spec.AlgorithmParameterSpec;
22import java.security.spec.InvalidParameterSpecException;
23
24import org.apache.harmony.security.fortress.Engine;
25
26
27/**
28 * {@code AlgorithmParameters} is an engine class which provides algorithm
29 * parameters.
30 */
31public class AlgorithmParameters {
32    /**
33     * The service name.
34     */
35    private static final String SEVICE = "AlgorithmParameters";
36
37    /**
38     * Used to access common engine functionality.
39     */
40    private static Engine engine = new Engine(SEVICE);
41
42    /**
43     * The security provider.
44     */
45    private Provider provider;
46
47    /**
48     * The SPI implementation.
49     */
50    private AlgorithmParametersSpi spiImpl;
51
52    /**
53     * The security algorithm.
54     */
55    private String algorithm;
56
57    /**
58     * The initialization state.
59     */
60    private boolean initialized; // = false;
61
62    /**
63     * Constructs a new instance of {@code AlgorithmParameters} with the given
64     * arguments.
65     *
66     * @param algPramSpi
67     *            the concrete implementation.
68     * @param provider
69     *            the security provider.
70     * @param algorithm
71     *            the name of the algorithm.
72     */
73    protected AlgorithmParameters(AlgorithmParametersSpi algPramSpi,
74            Provider provider, String algorithm) {
75        this.provider = provider;
76        this.algorithm = algorithm;
77        this.spiImpl = algPramSpi;
78    }
79
80    /**
81     * Returns a new instance of {@code AlgorithmParameters} for the specified
82     * algorithm.
83     *
84     * @param algorithm
85     *            the name of the algorithm to use.
86     * @return a new instance of {@code AlgorithmParameters} for the specified
87     *         algorithm.
88     * @throws NoSuchAlgorithmException
89     *             if the specified algorithm is not available.
90     * @throws NullPointerException
91     *             if {@code algorithm} is {@code null}.
92     */
93    public static AlgorithmParameters getInstance(String algorithm)
94            throws NoSuchAlgorithmException {
95        if (algorithm == null) {
96            throw new NullPointerException();
97        }
98        synchronized (engine) {
99            engine.getInstance(algorithm, null);
100            return new AlgorithmParameters((AlgorithmParametersSpi) engine.spi,
101                    engine.provider, algorithm);
102        }
103    }
104
105    /**
106     * Returns a new instance of {@code AlgorithmParameters} from the specified
107     * provider for the specified algorithm.
108     *
109     * @param algorithm
110     *            the name of the algorithm to use.
111     * @param provider
112     *            name of the provider of the {@code AlgorithmParameters}.
113     * @return a new instance of {@code AlgorithmParameters} for the specified
114     *         algorithm.
115     * @throws NoSuchAlgorithmException
116     *             if the specified algorithm is not available.
117     * @throws NoSuchProviderException
118     *             if the specified provider is not available.
119     * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
120     * @throws NullPointerException
121     *             if {@code algorithm} is {@code null}.
122     */
123    public static AlgorithmParameters getInstance(String algorithm,
124            String provider) throws NoSuchAlgorithmException,
125            NoSuchProviderException {
126        if (provider == null || provider.isEmpty()) {
127            throw new IllegalArgumentException();
128        }
129        Provider p = Security.getProvider(provider);
130        if (p == null) {
131            throw new NoSuchProviderException(provider);
132        }
133        return getInstance(algorithm, p);
134    }
135
136    /**
137     * Returns a new instance of {@code AlgorithmParameters} from the specified
138     * provider for the specified algorithm.
139     *
140     * @param algorithm
141     *            the name of the algorithm to use.
142     * @param provider
143     *            the provider of the {@code AlgorithmParameters}.
144     * @return a new instance of {@code AlgorithmParameters} for the specified
145     *         algorithm.
146     * @throws NoSuchAlgorithmException
147     *             if the specified algorithm is not available.
148     * @throws NullPointerException
149     *             if {@code algorithm} is {@code null}.
150     * @throws IllegalArgumentException if {@code provider == null}
151     */
152    public static AlgorithmParameters getInstance(String algorithm,
153            Provider provider) throws NoSuchAlgorithmException {
154        if (provider == null) {
155            throw new IllegalArgumentException();
156        }
157        if (algorithm == null) {
158            throw new NullPointerException();
159        }
160        synchronized (engine) {
161            engine.getInstance(algorithm, provider, null);
162            return new AlgorithmParameters((AlgorithmParametersSpi) engine.spi,
163                    provider, algorithm);
164        }
165    }
166
167    /**
168     * Returns the provider associated with this {@code AlgorithmParameters}.
169     *
170     * @return the provider associated with this {@code AlgorithmParameters}.
171     */
172    public final Provider getProvider() {
173        return provider;
174    }
175
176    /**
177     * Returns the name of the algorithm.
178     *
179     * @return the name of the algorithm.
180     */
181    public final String getAlgorithm() {
182        return algorithm;
183    }
184
185    /**
186     * Initializes this {@code AlgorithmParameters} with the specified {@code
187     * AlgorithmParameterSpec}.
188     *
189     * @param paramSpec
190     *            the parameter specification.
191     * @throws InvalidParameterSpecException
192     *             if this {@code AlgorithmParameters} has already been
193     *             initialized or the given {@code paramSpec} is not appropriate
194     *             for initializing this {@code AlgorithmParameters}.
195     */
196    public final void init(AlgorithmParameterSpec paramSpec)
197            throws InvalidParameterSpecException {
198        if (initialized) {
199            throw new InvalidParameterSpecException("Parameter has already been initialized");
200        }
201        spiImpl.engineInit(paramSpec);
202        initialized = true;
203    }
204
205    /**
206     * Initializes this {@code AlgorithmParameters} with the specified {@code
207     * byte[]} using the default decoding format for parameters. The default
208     * encoding format is ASN.1.
209     *
210     * @param params
211     *            the encoded parameters.
212     * @throws IOException
213     *             if this {@code AlgorithmParameters} has already been
214     *             initialized, or the parameter could not be encoded.
215     */
216    public final void init(byte[] params) throws IOException {
217        if (initialized) {
218            throw new IOException("Parameter has already been initialized");
219        }
220        spiImpl.engineInit(params);
221        initialized = true;
222    }
223
224    /**
225     * Initializes this {@code AlgorithmParameters} with the specified {@code
226     * byte[]} using the specified decoding format.
227     *
228     * @param params
229     *            the encoded parameters.
230     * @param format
231     *            the name of the decoding format.
232     * @throws IOException
233     *             if this {@code AlgorithmParameters} has already been
234     *             initialized, or the parameter could not be encoded.
235     */
236    public final void init(byte[] params, String format) throws IOException {
237        if (initialized) {
238            throw new IOException("Parameter has already been initialized");
239        }
240        spiImpl.engineInit(params, format);
241        initialized = true;
242    }
243
244    /**
245     * Returns the {@code AlgorithmParameterSpec} for this {@code
246     * AlgorithmParameters}.
247     *
248     * @param paramSpec
249     *            the type of the parameter specification in which this
250     *            parameters should be converted.
251     * @return the {@code AlgorithmParameterSpec} for this {@code
252     *         AlgorithmParameters}.
253     * @throws InvalidParameterSpecException
254     *             if this {@code AlgorithmParameters} has already been
255     *             initialized, or if this parameters could not be converted to
256     *             the specified class.
257     */
258    public final <T extends AlgorithmParameterSpec> T getParameterSpec(Class<T> paramSpec)
259            throws InvalidParameterSpecException {
260        if (!initialized) {
261            throw new InvalidParameterSpecException("Parameter has not been initialized");
262        }
263        return spiImpl.engineGetParameterSpec(paramSpec);
264    }
265
266    /**
267     * Returns this {@code AlgorithmParameters} in their default encoding
268     * format. The default encoding format is ASN.1.
269     *
270     * @return the encoded parameters.
271     * @throws IOException
272     *             if this {@code AlgorithmParameters} has already been
273     *             initialized, or if this parameters could not be encoded.
274     */
275    public final byte[] getEncoded() throws IOException {
276        if (!initialized) {
277            throw new IOException("Parameter has not been initialized");
278        }
279        return spiImpl.engineGetEncoded();
280    }
281
282    /**
283     * Returns this {@code AlgorithmParameters} in the specified encoding
284     * format.
285     *
286     * @param format
287     *            the name of the encoding format.
288     * @return the encoded parameters.
289     * @throws IOException
290     *             if this {@code AlgorithmParameters} has already been
291     *             initialized, or if this parameters could not be encoded.
292     */
293    public final byte[] getEncoded(String format) throws IOException {
294        if (!initialized) {
295            throw new IOException("Parameter has not been initialized");
296        }
297        return spiImpl.engineGetEncoded(format);
298    }
299
300    /**
301     * Returns a string containing a concise, human-readable description of this
302     * {@code AlgorithmParameters}.
303     *
304     * @return a printable representation for this {@code AlgorithmParameters}.
305     */
306    @Override
307    public final String toString() {
308        if (!initialized) {
309            return null;
310        }
311        return spiImpl.engineToString();
312    }
313}
314