|
DESede3KeySecretKeyFactoryImpl |
|
1 /* $RCSfile: DESede3KeySecretKeyFactoryImpl.java,v $
2 * $Revision: 1.3 $
3 * $Date: 2002/11/23 11:09:56 $
4 * $Author: uwe_guenther $
5 * $State: Exp $
6 *
7 * Created on August 13, 2001 9:45 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.spec.InvalidKeySpecException;
36 import java.security.spec.KeySpec;
37
38 import javax.crypto.SecretKey;
39
40 import de.cscc.crypto.provider.spec.DESede3KeySpec;
41
42 /**
43 * DESede3KeySecretKeyFactoryImpl Class.
44 *
45 * @author <a href=mailto:uwe@cscc.de>Uwe Günther</a>
46 * @version $Revision: 1.3 $
47 */
48 final class DESede3KeySecretKeyFactoryImpl {
49
50 /** Creates new DESede3KeySecretKeyFactoryImpl. */
51 DESede3KeySecretKeyFactoryImpl() {}
52
53 /**
54 * Returns a string representation of the object.
55 *
56 * @return a string representation of the object.
57 */
58 public String toString() {
59 return "[DESede3KeySecretKeyFactory]";
60 }
61
62 /**
63 * Generates a <code>SecretKey</code> object from the
64 * provided key specification (key material).
65 *
66 * @param keySpec the specification (key material) of the secret key.
67 * @return the secret key.
68 * @throws NullPointerException if keySpec is null.
69 * @throws InvalidKeySpecException if the given key specification
70 * is inappropriate for this secret-key factory to produce a secret key.
71 */
72 SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException {
73 if (keySpec == null) {
74 throw new NullPointerException("Parameter keySpec is null.");
75 }
76 if (keySpec instanceof DESede3KeySpec) {
77 //Downcast the KeySpec to a congret DESede3KeySpec.
78 DESede3KeySpec spec = (DESede3KeySpec) keySpec;
79 //Construct and return to an SecretKey upcasted
80 //DESede3KeySecretKeyImpl.
81 return new DESede3KeySecretKeyImpl(spec);
82
83 } else {
84 //If the KeySpec is any other type than DESede3KeySpec
85 //throw a InvalidKeySpecException.
86 throw new InvalidKeySpecException("Inapproptiate KeySpec."
87 + "KeySpec have to be a instance of DESede3KeySpec");
88 }
89 }
90
91 /**
92 * Returns a specification (key material) of the given key object in the
93 * requested format.
94 *
95 * @param key the key.
96 * @param keySpec the requested format in which the key material shall be
97 * returned.
98 * @return the underlying key specification (key material) in the requested
99 * format.
100 * @throws NullPointerException if key or keySpec is null.
101 * @throws InvalidKeySpecException if the requested key specification is
102 * inappropriate for the given key (e.g., the algorithms associated with
103 * <code>key</code> and <code>keySpec</code> do not match, or
104 * <code>key</code> references a key on a cryptographic hardware device
105 * whereas <code>keySpec</code> is the specification of a software-based
106 * key), or the given key cannot be dealt with (e.g., the given key has
107 * an algorithm or format not supported by this secret-key factory).
108 */
109 KeySpec getKeySpec(SecretKey key, Class keySpec)
110 throws InvalidKeySpecException {
111 if (key == null) {
112 throw new NullPointerException("Parameter key is null.");
113 }
114 if (keySpec == null) {
115 throw new NullPointerException("Parameter keySpec is null.");
116 }
117 if (key.getAlgorithm().equalsIgnoreCase("DESede3Key") &&
118 key.getFormat().equalsIgnoreCase("RAW") ) {
119
120 if (keySpec == DESede3KeySpec.class) {
121 DESede3KeySpec spec = null;
122 try{
123 spec = new DESede3KeySpec(key.getEncoded());
124 } catch (InvalidKeyException e) {
125 //this should never happends, because secretKey.getEncoded()
126 //should always return a byte[16].
127 throw (InvalidKeySpecException) new InvalidKeySpecException(
128 "Inapproptiate SecretKey.").initCause(e);
129 }
130 //Return to an KeySpec upcasted DESede3KeySpec.
131 return spec;
132
133 } else {
134 //If Class keySpec is any other type than DESede3KeySpec,
135 //throw a InvalidKeySpecException.
136 throw new InvalidKeySpecException("Inapproptiate KeySpec."
137 + "KeySpec have to be DESede3KeySpec.class");
138 }
139
140 } else {
141 //If the SecretKey key is any other type than
142 //DESede3KeySecretKeyImpl, throw a InvalidKeySpecException.
143 throw new InvalidKeySpecException("Inapproptiate SecretKey."
144 + "SecretKey must have Algorithm=\"DESede3Key\" and "
145 + "Format=\"RAW\".");
146 }
147 }
148
149 /**
150 * Translates a key object, whose provider may be unknown or potentially
151 * untrusted, into a corresponding key object of this secret-key factory.
152 *
153 * @return they key whose provider is unknown or untrusted.
154 * @param key the key whose provider is unknown or untrusted.
155 * @throws NullPointerException if key is null.
156 * @throws InvalidKeyException if the given key cannot be processed by this
157 * secret-key factory.
158 */
159 SecretKey translateKey(SecretKey key) throws InvalidKeyException {
160 if (key == null) {
161 throw new NullPointerException("Parameter key is null.");
162 }
163 if (key.getAlgorithm().equalsIgnoreCase("DESede3Key") &&
164 key.getFormat().equalsIgnoreCase("RAW") ) {
165 //Throws also InvalidKeyException, if key.getEncoded()
166 //returns a byte array, with a byte length shorter than 16 bytes.
167 DESede3KeySpec spec = new DESede3KeySpec(key.getEncoded());
168 //Construct and return to an SecretKey upcasted
169 //DESede3KeySecretKeyImpl.
170 return new DESede3KeySecretKeyImpl(spec);
171
172 } else {
173 throw new InvalidKeyException("Inapproptiate SecretKey."
174 + "SecretKey must have Algorithm=\"DESede3Key\" and "
175 + "Format=\"RAW\".");
176 }
177 }
178 }
179
|
DESede3KeySecretKeyFactoryImpl |
|