1 /*
2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.security;
27 
28 import java.io.*;
29 import java.security.spec.AlgorithmParameterSpec;
30 import java.security.spec.InvalidParameterSpecException;
31 
32 /**
33  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
34  * for the {@code AlgorithmParameters} class, which is used to manage
35  * algorithm parameters.
36  *
37  * <p> All the abstract methods in this class must be implemented by each
38  * cryptographic service provider who wishes to supply parameter management
39  * for a particular algorithm.
40  *
41  * @author Jan Luehe
42  *
43  *
44  * @see AlgorithmParameters
45  * @see java.security.spec.AlgorithmParameterSpec
46  * @see java.security.spec.DSAParameterSpec
47  *
48  * @since 1.2
49  */
50 
51 public abstract class AlgorithmParametersSpi {
52 
53     /**
54      * Constructor for subclasses to call.
55      */
AlgorithmParametersSpi()56     public AlgorithmParametersSpi() {}
57 
58     /**
59      * Initializes this parameters object using the parameters
60      * specified in {@code paramSpec}.
61      *
62      * @param paramSpec the parameter specification.
63      *
64      * @throws    InvalidParameterSpecException if the given parameter
65      * specification is inappropriate for the initialization of this parameter
66      * object.
67      */
engineInit(AlgorithmParameterSpec paramSpec)68     protected abstract void engineInit(AlgorithmParameterSpec paramSpec)
69         throws InvalidParameterSpecException;
70 
71     /**
72      * Imports the specified parameters and decodes them
73      * according to the primary decoding format for parameters.
74      * The primary decoding format for parameters is ASN.1, if an ASN.1
75      * specification for this type of parameters exists.
76      *
77      * @param params the encoded parameters.
78      *
79      * @throws    IOException on decoding errors
80      */
engineInit(byte[] params)81     protected abstract void engineInit(byte[] params)
82         throws IOException;
83 
84     /**
85      * Imports the parameters from {@code params} and
86      * decodes them according to the specified decoding format.
87      * If {@code format} is null, the
88      * primary decoding format for parameters is used. The primary decoding
89      * format is ASN.1, if an ASN.1 specification for these parameters
90      * exists.
91      *
92      * @param params the encoded parameters.
93      *
94      * @param format the name of the decoding format.
95      *
96      * @throws    IOException on decoding errors
97      */
engineInit(byte[] params, String format)98     protected abstract void engineInit(byte[] params, String format)
99         throws IOException;
100 
101     /**
102      * Returns a (transparent) specification of this parameters
103      * object.
104      * {@code paramSpec} identifies the specification class in which
105      * the parameters should be returned. It could, for example, be
106      * {@code DSAParameterSpec.class}, to indicate that the
107      * parameters should be returned in an instance of the
108      * {@code DSAParameterSpec} class.
109      *
110      * @param <T> the type of the parameter specification to be returned
111      *
112      * @param paramSpec the specification class in which
113      * the parameters should be returned.
114      *
115      * @return the parameter specification.
116      *
117      * @throws    InvalidParameterSpecException if the requested parameter
118      * specification is inappropriate for this parameter object.
119      */
120     protected abstract
121         <T extends AlgorithmParameterSpec>
engineGetParameterSpec(Class<T> paramSpec)122         T engineGetParameterSpec(Class<T> paramSpec)
123         throws InvalidParameterSpecException;
124 
125     /**
126      * Returns the parameters in their primary encoding format.
127      * The primary encoding format for parameters is ASN.1, if an ASN.1
128      * specification for this type of parameters exists.
129      *
130      * @return the parameters encoded using their primary encoding format.
131      *
132      * @throws    IOException on encoding errors.
133      */
engineGetEncoded()134     protected abstract byte[] engineGetEncoded() throws IOException;
135 
136     /**
137      * Returns the parameters encoded in the specified format.
138      * If {@code format} is null, the
139      * primary encoding format for parameters is used. The primary encoding
140      * format is ASN.1, if an ASN.1 specification for these parameters
141      * exists.
142      *
143      * @param format the name of the encoding format.
144      *
145      * @return the parameters encoded using the specified encoding scheme.
146      *
147      * @throws    IOException on encoding errors.
148      */
engineGetEncoded(String format)149     protected abstract byte[] engineGetEncoded(String format)
150         throws IOException;
151 
152     /**
153      * Returns a formatted string describing the parameters.
154      *
155      * @return a formatted string describing the parameters.
156      */
engineToString()157     protected abstract String engineToString();
158 }
159