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.security.spec.KeySpec;
29 import java.security.spec.InvalidKeySpecException;
30 
31 /**
32  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
33  * for the {@code KeyFactory} class.
34  * All the abstract methods in this class must be implemented by each
35  * cryptographic service provider who wishes to supply the implementation
36  * of a key factory for a particular algorithm.
37  *
38  * <P> Key factories are used to convert <I>keys</I> (opaque
39  * cryptographic keys of type {@code Key}) into <I>key specifications</I>
40  * (transparent representations of the underlying key material), and vice
41  * versa.
42  *
43  * <P> Key factories are bi-directional. That is, they allow you to build an
44  * opaque key object from a given key specification (key material), or to
45  * retrieve the underlying key material of a key object in a suitable format.
46  *
47  * <P> Multiple compatible key specifications may exist for the same key.
48  * For example, a DSA public key may be specified using
49  * {@code DSAPublicKeySpec} or
50  * {@code X509EncodedKeySpec}. A key factory can be used to translate
51  * between compatible key specifications.
52  *
53  * <P> A provider should document all the key specifications supported by its
54  * key factory.
55  *
56  * @author Jan Luehe
57  *
58  *
59  * @see KeyFactory
60  * @see Key
61  * @see PublicKey
62  * @see PrivateKey
63  * @see java.security.spec.KeySpec
64  * @see java.security.spec.DSAPublicKeySpec
65  * @see java.security.spec.X509EncodedKeySpec
66  *
67  * @since 1.2
68  */
69 
70 public abstract class KeyFactorySpi {
71 
72     /**
73      * Constructor for subclasses to call.
74      */
KeyFactorySpi()75     public KeyFactorySpi() {}
76 
77     /**
78      * Generates a public key object from the provided key
79      * specification (key material).
80      *
81      * @param keySpec the specification (key material) of the public key.
82      *
83      * @return the public key.
84      *
85      * @throws    InvalidKeySpecException if the given key specification
86      * is inappropriate for this key factory to produce a public key.
87      */
engineGeneratePublic(KeySpec keySpec)88     protected abstract PublicKey engineGeneratePublic(KeySpec keySpec)
89         throws InvalidKeySpecException;
90 
91     /**
92      * Generates a private key object from the provided key
93      * specification (key material).
94      *
95      * @param keySpec the specification (key material) of the private key.
96      *
97      * @return the private key.
98      *
99      * @throws    InvalidKeySpecException if the given key specification
100      * is inappropriate for this key factory to produce a private key.
101      */
engineGeneratePrivate(KeySpec keySpec)102     protected abstract PrivateKey engineGeneratePrivate(KeySpec keySpec)
103         throws InvalidKeySpecException;
104 
105     /**
106      * Returns a specification (key material) of the given key
107      * object.
108      * {@code keySpec} identifies the specification class in which
109      * the key material should be returned. It could, for example, be
110      * {@code DSAPublicKeySpec.class}, to indicate that the
111      * key material should be returned in an instance of the
112      * {@code DSAPublicKeySpec} class.
113      *
114      * @param <T> the type of the key specification to be returned
115      *
116      * @param key the key.
117      *
118      * @param keySpec the specification class in which
119      * the key material should be returned.
120      *
121      * @return the underlying key specification (key material) in an instance
122      * of the requested specification class.
123      *
124      * @throws    InvalidKeySpecException if the requested key specification is
125      * inappropriate for the given key, or the given key cannot be dealt with
126      * (e.g., the given key has an unrecognized format).
127      */
128     protected abstract <T extends KeySpec>
engineGetKeySpec(Key key, Class<T> keySpec)129         T engineGetKeySpec(Key key, Class<T> keySpec)
130         throws InvalidKeySpecException;
131 
132     /**
133      * Translates a key object, whose provider may be unknown or
134      * potentially untrusted, into a corresponding key object of this key
135      * factory.
136      *
137      * @param key the key whose provider is unknown or untrusted.
138      *
139      * @return the translated key.
140      *
141      * @throws    InvalidKeyException if the given key cannot be processed
142      * by this key factory.
143      */
engineTranslateKey(Key key)144     protected abstract Key engineTranslateKey(Key key)
145         throws InvalidKeyException;
146 
147 }
148