Path: blob/master/src/java.base/share/classes/java/security/KeyFactorySpi.java
41152 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.security.spec.KeySpec;28import java.security.spec.InvalidKeySpecException;2930/**31* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)32* for the {@code KeyFactory} class.33* All the abstract methods in this class must be implemented by each34* cryptographic service provider who wishes to supply the implementation35* of a key factory for a particular algorithm.36*37* <P> Key factories are used to convert <I>keys</I> (opaque38* cryptographic keys of type {@code Key}) into <I>key specifications</I>39* (transparent representations of the underlying key material), and vice40* versa.41*42* <P> Key factories are bi-directional. That is, they allow you to build an43* opaque key object from a given key specification (key material), or to44* retrieve the underlying key material of a key object in a suitable format.45*46* <P> Multiple compatible key specifications may exist for the same key.47* For example, a DSA public key may be specified using48* {@code DSAPublicKeySpec} or49* {@code X509EncodedKeySpec}. A key factory can be used to translate50* between compatible key specifications.51*52* <P> A provider should document all the key specifications supported by its53* key factory.54*55* @author Jan Luehe56*57*58* @see KeyFactory59* @see Key60* @see PublicKey61* @see PrivateKey62* @see java.security.spec.KeySpec63* @see java.security.spec.DSAPublicKeySpec64* @see java.security.spec.X509EncodedKeySpec65*66* @since 1.267*/6869public abstract class KeyFactorySpi {7071/**72* Constructor for subclasses to call.73*/74public KeyFactorySpi() {}7576/**77* Generates a public key object from the provided key78* specification (key material).79*80* @param keySpec the specification (key material) of the public key.81*82* @return the public key.83*84* @throws InvalidKeySpecException if the given key specification85* is inappropriate for this key factory to produce a public key.86*/87protected abstract PublicKey engineGeneratePublic(KeySpec keySpec)88throws InvalidKeySpecException;8990/**91* Generates a private key object from the provided key92* specification (key material).93*94* @param keySpec the specification (key material) of the private key.95*96* @return the private key.97*98* @throws InvalidKeySpecException if the given key specification99* is inappropriate for this key factory to produce a private key.100*/101protected abstract PrivateKey engineGeneratePrivate(KeySpec keySpec)102throws InvalidKeySpecException;103104/**105* Returns a specification (key material) of the given key106* object.107* {@code keySpec} identifies the specification class in which108* the key material should be returned. It could, for example, be109* {@code DSAPublicKeySpec.class}, to indicate that the110* key material should be returned in an instance of the111* {@code DSAPublicKeySpec} class.112*113* @param <T> the type of the key specification to be returned114*115* @param key the key.116*117* @param keySpec the specification class in which118* the key material should be returned.119*120* @return the underlying key specification (key material) in an instance121* of the requested specification class.122*123* @throws InvalidKeySpecException if the requested key specification is124* inappropriate for the given key, or the given key cannot be dealt with125* (e.g., the given key has an unrecognized format).126*/127protected abstract <T extends KeySpec>128T engineGetKeySpec(Key key, Class<T> keySpec)129throws InvalidKeySpecException;130131/**132* Translates a key object, whose provider may be unknown or133* potentially untrusted, into a corresponding key object of this key134* factory.135*136* @param key the key whose provider is unknown or untrusted.137*138* @return the translated key.139*140* @throws InvalidKeyException if the given key cannot be processed141* by this key factory.142*/143protected abstract Key engineTranslateKey(Key key)144throws InvalidKeyException;145146}147148149