2
|
1 |
/*
|
|
2 |
* Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.crypto.provider;
|
|
27 |
|
|
28 |
import javax.crypto.SecretKey;
|
|
29 |
import javax.crypto.SecretKeyFactorySpi;
|
|
30 |
import javax.crypto.spec.DESKeySpec;
|
|
31 |
import java.security.InvalidKeyException;
|
|
32 |
import java.security.spec.KeySpec;
|
|
33 |
import java.security.spec.InvalidKeySpecException;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* This class implements the DES key factory of the Sun provider.
|
|
37 |
*
|
|
38 |
* @author Jan Luehe
|
|
39 |
*
|
|
40 |
*/
|
|
41 |
|
|
42 |
public final class DESKeyFactory extends SecretKeyFactorySpi {
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Verify the SunJCE provider in the constructor.
|
|
46 |
*
|
|
47 |
* @exception SecurityException if fails to verify
|
|
48 |
* its own integrity
|
|
49 |
*/
|
|
50 |
public DESKeyFactory() {
|
|
51 |
if (!SunJCE.verifySelfIntegrity(this.getClass())) {
|
|
52 |
throw new SecurityException("The SunJCE provider may have " +
|
|
53 |
"been tampered.");
|
|
54 |
}
|
|
55 |
}
|
|
56 |
/**
|
|
57 |
* Generates a <code>SecretKey</code> object from the provided key
|
|
58 |
* specification (key material).
|
|
59 |
*
|
|
60 |
* @param keySpec the specification (key material) of the secret key
|
|
61 |
*
|
|
62 |
* @return the secret key
|
|
63 |
*
|
|
64 |
* @exception InvalidKeySpecException if the given key specification
|
|
65 |
* is inappropriate for this key factory to produce a public key.
|
|
66 |
*/
|
|
67 |
protected SecretKey engineGenerateSecret(KeySpec keySpec)
|
|
68 |
throws InvalidKeySpecException {
|
|
69 |
DESKey desKey = null;
|
|
70 |
|
|
71 |
try {
|
|
72 |
if (!(keySpec instanceof DESKeySpec)) {
|
|
73 |
throw new InvalidKeySpecException
|
|
74 |
("Inappropriate key specification");
|
|
75 |
}
|
|
76 |
else {
|
|
77 |
DESKeySpec desKeySpec = (DESKeySpec)keySpec;
|
|
78 |
desKey = new DESKey(desKeySpec.getKey());
|
|
79 |
}
|
|
80 |
} catch (InvalidKeyException e) {
|
|
81 |
}
|
|
82 |
return desKey;
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Returns a specification (key material) of the given key
|
|
87 |
* in the requested format.
|
|
88 |
*
|
|
89 |
* @param key the key
|
|
90 |
*
|
|
91 |
* @param keySpec the requested format in which the key material shall be
|
|
92 |
* returned
|
|
93 |
*
|
|
94 |
* @return the underlying key specification (key material) in the
|
|
95 |
* requested format
|
|
96 |
*
|
|
97 |
* @exception InvalidKeySpecException if the requested key specification is
|
|
98 |
* inappropriate for the given key, or the given key cannot be processed
|
|
99 |
* (e.g., the given key has an unrecognized algorithm or format).
|
|
100 |
*/
|
|
101 |
protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec)
|
|
102 |
throws InvalidKeySpecException {
|
|
103 |
|
|
104 |
try {
|
|
105 |
|
|
106 |
if ((key instanceof SecretKey)
|
|
107 |
&& (key.getAlgorithm().equalsIgnoreCase("DES"))
|
|
108 |
&& (key.getFormat().equalsIgnoreCase("RAW"))) {
|
|
109 |
|
|
110 |
// Check if requested key spec is amongst the valid ones
|
|
111 |
if ((keySpec != null) &&
|
|
112 |
DESKeySpec.class.isAssignableFrom(keySpec)) {
|
|
113 |
return new DESKeySpec(key.getEncoded());
|
|
114 |
|
|
115 |
} else {
|
|
116 |
throw new InvalidKeySpecException
|
|
117 |
("Inappropriate key specification");
|
|
118 |
}
|
|
119 |
|
|
120 |
} else {
|
|
121 |
throw new InvalidKeySpecException
|
|
122 |
("Inappropriate key format/algorithm");
|
|
123 |
}
|
|
124 |
|
|
125 |
} catch (InvalidKeyException e) {
|
|
126 |
throw new InvalidKeySpecException("Secret key has wrong size");
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Translates a <code>SecretKey</code> object, whose provider may be
|
|
132 |
* unknown or potentially untrusted, into a corresponding
|
|
133 |
* <code>SecretKey</code> object of this key factory.
|
|
134 |
*
|
|
135 |
* @param key the key whose provider is unknown or untrusted
|
|
136 |
*
|
|
137 |
* @return the translated key
|
|
138 |
*
|
|
139 |
* @exception InvalidKeyException if the given key cannot be processed by
|
|
140 |
* this key factory.
|
|
141 |
*/
|
|
142 |
protected SecretKey engineTranslateKey(SecretKey key)
|
|
143 |
throws InvalidKeyException {
|
|
144 |
|
|
145 |
try {
|
|
146 |
|
|
147 |
if ((key != null) &&
|
|
148 |
(key.getAlgorithm().equalsIgnoreCase("DES")) &&
|
|
149 |
(key.getFormat().equalsIgnoreCase("RAW"))) {
|
|
150 |
|
|
151 |
// Check if key originates from this factory
|
|
152 |
if (key instanceof com.sun.crypto.provider.DESKey) {
|
|
153 |
return key;
|
|
154 |
}
|
|
155 |
// Convert key to spec
|
|
156 |
DESKeySpec desKeySpec
|
|
157 |
= (DESKeySpec)engineGetKeySpec(key, DESKeySpec.class);
|
|
158 |
// Create key from spec, and return it
|
|
159 |
return engineGenerateSecret(desKeySpec);
|
|
160 |
|
|
161 |
} else {
|
|
162 |
throw new InvalidKeyException
|
|
163 |
("Inappropriate key format/algorithm");
|
|
164 |
}
|
|
165 |
|
|
166 |
} catch (InvalidKeySpecException e) {
|
|
167 |
throw new InvalidKeyException("Cannot translate key");
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|