1   /* $RCSfile: RSAKeyFactoryEngine.java,v $
2    * $Revision: 1.8 $
3    * $Date: 2003/10/04 19:18:38 $
4    * $Author: uwe_guenther $
5    * $State: Exp $
6    *
7    * Created on November 9, 2001 1:49 PM
8    *
9    * Copyright (C) 2001  Uwe Guenther  <uwe@cscc.de >
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.provider;
33  
34  import java.security.InvalidKeyException;
35  import java.security.Key;
36  import java.security.KeyFactorySpi;
37  import java.security.PrivateKey;
38  import java.security.PublicKey;
39  import java.security.spec.InvalidKeySpecException;
40  import java.security.spec.KeySpec;
41  
42  /** 
43   * RSAKeyFactoryEngine Class.
44   *
45   * @author  <a href=mailto:uwe@cscc.de >Uwe G&uuml;nther </a>
46   *
47   * @version $Revision: 1.8 $
48   */
49  public final class RSAKeyFactoryEngine extends KeyFactorySpi {
50  
51      /** The delegate for this wrapper object */
52      private RSAKeyFactoryImpl factory = new RSAKeyFactoryImpl();    
53      
54      /** Creates new RSAKeyFactoryEngine */
55      public RSAKeyFactoryEngine() {
56          if (JHBCI.selfIntegrityChecking() == false) {
57              throw new SecurityException("JHBCI-Provider is tampered.");
58          }        
59      }
60  
61      /**
62       * Returns a string representation of the object. 
63       *
64       * @return  a string representation of the object.
65       */
66      public String toString() {
67          return this.factory.toString();
68      }    
69      
70      /**
71       * Generates a private key object from the provided key
72       * specification (key material).
73       *
74       * <p>The following KeySpecs will produce the following instances of its 
75       * matching keys:
76       * <ul>
77       * <li> RSAPrivateCrtKeySpec produces:
78       * <ul>
79       * <li>a instance of RSAPrivateCrtKey</li>
80       * </ul>
81       * </li>
82       * <li> RSAPrivateKeySpec produces:
83       * <ul>
84       * <li>a instance of RSAPrivateKey</li>
85       * </ul>
86       * </li>
87       * </ul>
88       *
89       * @param keySpec the specification (key material) of the private key.
90       *
91       * @return the private key.
92       *
93       * @throws InvalidKeySpecException if the given key specification
94       * is inappropriate for this key factory to produce a private key.
95       * @throws NullPointerException If <code>keySpec</code> or one of the key 
96       * data in <code>keySpec</code> is <code>null</code>. So you have to 
97       * construct a valid keySpec without <code>null</code> fields.
98       * @see java.math.BigInteger
99       */
100     public PrivateKey engineGeneratePrivate(KeySpec keySpec) 
101             throws InvalidKeySpecException {
102         return this.factory.generatePrivate(keySpec);
103     }
104     
105     /**
106      * Generates a public key object from the provided key
107      * specification (key material).
108      *
109      * <p>The following KeySpecs will produce the following instances of its 
110      * matching keys:
111      * <ul>
112      * <li> RSAPublicKeySpec produces:
113      * <ul>
114      * <li>a instance of RSAPublicKey</li>
115      * </ul>
116      * </li>
117      * </ul>
118      *
119      * @param keySpec the specification (key material) of the public key.
120      *
121      * @return the public key.
122      *
123      * @throws InvalidKeySpecException if the given key specification
124      * is inappropriate for this key factory to produce a public key.
125      * @throws NullPointerException If <code>keySpec</code> or one of the key
126      * data in <code>keySpec</code> is <code>null</code>. So you have to 
127      * construct a valid keySpec without <code>null</code> fields.
128      * @throws IllegalArgumentException if any key material from keySpec is 
129      * negative.
130      */
131     public PublicKey engineGeneratePublic(KeySpec keySpec) 
132             throws InvalidKeySpecException {
133         return this.factory.generatePublic(keySpec);
134     }
135     
136     /**
137      * Returns a specification (key material) of the given key
138      * object.
139      * <code>keySpec</code> identifies the specification class in which
140      * the key material should be returned. It could, for example, be
141      * <code>RSAPublicKeySpec.class</code>, to indicate that the
142      * key material should be returned in an instance of the
143      * <code>RSAPublicKeySpec</code> class.
144      *
145      * <p>The following conversions are valid for this provider:
146      * <ul>
147      * <li>RSAPrivateCrtKey:
148      * <ul>
149      * <li>to RSAPrivateCrtKeySpec</li>
150      * <li>to RSAPrivateKeySpec</li>
151      * <li>to RSAPublicKeySpec</li>
152      * </ul>
153      * </li>
154      * <li>RSAPrivateKey:
155      * <ul>
156      * <li>to RSAPrivateKeySpec</li>
157      * </ul>
158      * </li>
159      * <li>RSAPublicKey:
160      * <ul>
161      * <li>to RSAPublicKeySpec</li>
162      * </ul>
163      * </li>
164      * </ul>
165      *
166      * @param key the key.
167      *
168      * @param keySpec the specification class in which
169      * the key material should be returned.
170      *
171      * @return the underlying key specification (key material) in an instance
172      * of the requested specification class.
173      *
174      * @throws InvalidKeySpecException if the requested key specification is
175      * inappropriate for the given key, or the given key cannot be dealt with
176      * (e.g., the given key has an unrecognized format).
177      * @throws NullPointerException If one of the params or one of the needed 
178      * key data in the <code>key</code> is <code>null</code>. So you have to 
179      * produce a valid key without <code>null</code> fields with your 
180      * KeyPairGenerator. This should not be a problem with keys from this 
181      * provider, but maybe with keys from other providers.
182      * @throws IllegalArgumentException if any key material in the key
183      * <code>key</code> is negative. This should not be a problem with keys 
184      * from this provider, but maybe with keys from other providers.
185      */
186     public KeySpec engineGetKeySpec(Key key, Class keySpec) 
187             throws InvalidKeySpecException {
188         return this.factory.getKeySpec(key, keySpec);            
189     }
190     
191     /**
192      * Translates a key object, whose provider may be unknown or
193      * potentially untrusted, into a corresponding key object of this key
194      * factory.
195      *
196      * <p>The following translations are valid for this provider:
197      * <ul>
198      * <li>RSAPrivateCrtKey:
199      * <ul>
200      * <li>to RSAPrivateCrtKey</li>
201      * </ul>
202      * </li>
203      * <li>RSAPrivateKey:
204      * <ul>
205      * <li>to RSAPrivateKey</li>
206      * </ul>
207      * </li>
208      * <li>RSAPublicKey:
209      * <ul>
210      * <li>to RSAPublicKey</li>
211      * </ul>
212      * </li>
213      * </ul>
214      *
215      * @param key the key whose provider is unknown or untrusted.
216      *
217      * @return the translated key.
218      *
219      * @throws InvalidKeyException if the given key cannot be processed
220      * by this key factory.
221      * @throws NullPointerException If the <code>key</code> it self or one of 
222      * the key data in the <code>key</code> is <code>null</code>. So you have to
223      * produce a valid key without <code>null</code> fields with your 
224      * KeyPairGenerator. This should not be a problem with this provider, but 
225      * maybe with other providers.
226      * @throws IllegalArgumentException if any key material in the key
227      * <code>key</code> is negative. This should not be a problem with keys 
228      * from this provider, but maybe with keys from other providers.     
229      */
230     public Key engineTranslateKey(Key key) throws InvalidKeyException {
231         return this.factory.translateKey(key);
232     }
233 }
234