Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (c) 1997, 2013, 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      * Generates a public key object from the provided key
     74      * specification (key material).
     75      *
     76      * @param keySpec the specification (key material) of the public key.
     77      *
     78      * @return the public key.
     79      *
     80      * @exception InvalidKeySpecException if the given key specification
     81      * is inappropriate for this key factory to produce a public key.
     82      */
     83     protected abstract PublicKey engineGeneratePublic(KeySpec keySpec)
     84         throws InvalidKeySpecException;
     85 
     86     /**
     87      * Generates a private key object from the provided key
     88      * specification (key material).
     89      *
     90      * @param keySpec the specification (key material) of the private key.
     91      *
     92      * @return the private key.
     93      *
     94      * @exception InvalidKeySpecException if the given key specification
     95      * is inappropriate for this key factory to produce a private key.
     96      */
     97     protected abstract PrivateKey engineGeneratePrivate(KeySpec keySpec)
     98         throws InvalidKeySpecException;
     99 
    100     /**
    101      * Returns a specification (key material) of the given key
    102      * object.
    103      * {@code keySpec} identifies the specification class in which
    104      * the key material should be returned. It could, for example, be
    105      * {@code DSAPublicKeySpec.class}, to indicate that the
    106      * key material should be returned in an instance of the
    107      * {@code DSAPublicKeySpec} class.
    108      *
    109      * @param <T> the type of the key specification to be returned
    110      *
    111      * @param key the key.
    112      *
    113      * @param keySpec the specification class in which
    114      * the key material should be returned.
    115      *
    116      * @return the underlying key specification (key material) in an instance
    117      * of the requested specification class.
    118 
    119      * @exception InvalidKeySpecException if the requested key specification is
    120      * inappropriate for the given key, or the given key cannot be dealt with
    121      * (e.g., the given key has an unrecognized format).
    122      */
    123     protected abstract <T extends KeySpec>
    124         T engineGetKeySpec(Key key, Class<T> keySpec)
    125         throws InvalidKeySpecException;
    126 
    127     /**
    128      * Translates a key object, whose provider may be unknown or
    129      * potentially untrusted, into a corresponding key object of this key
    130      * factory.
    131      *
    132      * @param key the key whose provider is unknown or untrusted.
    133      *
    134      * @return the translated key.
    135      *
    136      * @exception InvalidKeyException if the given key cannot be processed
    137      * by this key factory.
    138      */
    139     protected abstract Key engineTranslateKey(Key key)
    140         throws InvalidKeyException;
    141 
    142 }
    143