author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 14405 | e7fff80005c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.InvalidKeyException; |
|
29 |
import java.security.spec.KeySpec; |
|
30 |
import java.security.spec.InvalidKeySpecException; |
|
31 |
import javax.crypto.SecretKey; |
|
32 |
import javax.crypto.SecretKeyFactorySpi; |
|
33 |
import javax.crypto.spec.PBEKeySpec; |
|
34 |
import java.util.HashSet; |
|
35 |
||
36 |
/** |
|
37 |
* This class implements a key factory for PBE keys according to PKCS#5, |
|
38 |
* meaning that the password must consist of printable ASCII characters |
|
39 |
* (values 32 to 126 decimal inclusive) and only the low order 8 bits |
|
40 |
* of each password character are used. |
|
41 |
* |
|
42 |
* @author Jan Luehe |
|
43 |
* |
|
44 |
*/ |
|
45 |
abstract class PBEKeyFactory extends SecretKeyFactorySpi { |
|
46 |
||
47 |
private String type; |
|
48 |
private static HashSet<String> validTypes; |
|
49 |
||
50 |
/** |
|
3353
ddbd63234844
6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents:
2
diff
changeset
|
51 |
* Simple constructor |
2 | 52 |
*/ |
53 |
private PBEKeyFactory(String keytype) { |
|
54 |
type = keytype; |
|
55 |
} |
|
56 |
||
57 |
static { |
|
58 |
validTypes = new HashSet<String>(4); |
|
59 |
validTypes.add("PBEWithMD5AndDES".toUpperCase()); |
|
60 |
validTypes.add("PBEWithSHA1AndDESede".toUpperCase()); |
|
61 |
validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase()); |
|
62 |
// Proprietary algorithm. |
|
63 |
validTypes.add("PBEWithMD5AndTripleDES".toUpperCase()); |
|
64 |
} |
|
65 |
||
66 |
public static final class PBEWithMD5AndDES |
|
67 |
extends PBEKeyFactory { |
|
68 |
public PBEWithMD5AndDES() { |
|
69 |
super("PBEWithMD5AndDES"); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public static final class PBEWithSHA1AndDESede |
|
74 |
extends PBEKeyFactory { |
|
75 |
public PBEWithSHA1AndDESede() { |
|
76 |
super("PBEWithSHA1AndDESede"); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
public static final class PBEWithSHA1AndRC2_40 |
|
81 |
extends PBEKeyFactory { |
|
82 |
public PBEWithSHA1AndRC2_40() { |
|
83 |
super("PBEWithSHA1AndRC2_40"); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
/* |
|
88 |
* Private proprietary algorithm for supporting JCEKS. |
|
89 |
*/ |
|
90 |
public static final class PBEWithMD5AndTripleDES |
|
91 |
extends PBEKeyFactory { |
|
92 |
public PBEWithMD5AndTripleDES() { |
|
93 |
super("PBEWithMD5AndTripleDES"); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
||
98 |
/** |
|
99 |
* Generates a <code>SecretKey</code> object from the provided key |
|
100 |
* specification (key material). |
|
101 |
* |
|
102 |
* @param keySpec the specification (key material) of the secret key |
|
103 |
* |
|
104 |
* @return the secret key |
|
105 |
* |
|
106 |
* @exception InvalidKeySpecException if the given key specification |
|
107 |
* is inappropriate for this key factory to produce a public key. |
|
108 |
*/ |
|
109 |
protected SecretKey engineGenerateSecret(KeySpec keySpec) |
|
110 |
throws InvalidKeySpecException |
|
111 |
{ |
|
112 |
if (!(keySpec instanceof PBEKeySpec)) { |
|
113 |
throw new InvalidKeySpecException("Invalid key spec"); |
|
114 |
} |
|
115 |
return new PBEKey((PBEKeySpec)keySpec, type); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Returns a specification (key material) of the given key |
|
120 |
* in the requested format. |
|
121 |
* |
|
122 |
* @param key the key |
|
123 |
* |
|
124 |
* @param keySpec the requested format in which the key material shall be |
|
125 |
* returned |
|
126 |
* |
|
127 |
* @return the underlying key specification (key material) in the |
|
128 |
* requested format |
|
129 |
* |
|
130 |
* @exception InvalidKeySpecException if the requested key specification is |
|
131 |
* inappropriate for the given key, or the given key cannot be processed |
|
132 |
* (e.g., the given key has an unrecognized algorithm or format). |
|
133 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
134 |
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl) |
2 | 135 |
throws InvalidKeySpecException { |
136 |
if ((key instanceof SecretKey) |
|
137 |
&& (validTypes.contains(key.getAlgorithm().toUpperCase())) |
|
138 |
&& (key.getFormat().equalsIgnoreCase("RAW"))) { |
|
139 |
||
140 |
// Check if requested key spec is amongst the valid ones |
|
141 |
if ((keySpecCl != null) |
|
142 |
&& PBEKeySpec.class.isAssignableFrom(keySpecCl)) { |
|
143 |
byte[] passwdBytes = key.getEncoded(); |
|
144 |
char[] passwdChars = new char[passwdBytes.length]; |
|
145 |
for (int i=0; i<passwdChars.length; i++) |
|
146 |
passwdChars[i] = (char) (passwdBytes[i] & 0x7f); |
|
147 |
PBEKeySpec ret = new PBEKeySpec(passwdChars); |
|
148 |
// password char[] was cloned in PBEKeySpec constructor, |
|
149 |
// so we can zero it out here |
|
150 |
java.util.Arrays.fill(passwdChars, ' '); |
|
151 |
java.util.Arrays.fill(passwdBytes, (byte)0x00); |
|
152 |
return ret; |
|
153 |
} else { |
|
154 |
throw new InvalidKeySpecException("Invalid key spec"); |
|
155 |
} |
|
156 |
} else { |
|
157 |
throw new InvalidKeySpecException("Invalid key " |
|
158 |
+ "format/algorithm"); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Translates a <code>SecretKey</code> object, whose provider may be |
|
164 |
* unknown or potentially untrusted, into a corresponding |
|
165 |
* <code>SecretKey</code> object of this key factory. |
|
166 |
* |
|
167 |
* @param key the key whose provider is unknown or untrusted |
|
168 |
* |
|
169 |
* @return the translated key |
|
170 |
* |
|
171 |
* @exception InvalidKeyException if the given key cannot be processed by |
|
172 |
* this key factory. |
|
173 |
*/ |
|
174 |
protected SecretKey engineTranslateKey(SecretKey key) |
|
175 |
throws InvalidKeyException |
|
176 |
{ |
|
177 |
try { |
|
178 |
if ((key != null) && |
|
179 |
(validTypes.contains(key.getAlgorithm().toUpperCase())) && |
|
180 |
(key.getFormat().equalsIgnoreCase("RAW"))) { |
|
181 |
||
182 |
// Check if key originates from this factory |
|
183 |
if (key instanceof com.sun.crypto.provider.PBEKey) { |
|
184 |
return key; |
|
185 |
} |
|
186 |
||
187 |
// Convert key to spec |
|
188 |
PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec |
|
189 |
(key, PBEKeySpec.class); |
|
190 |
||
191 |
// Create key from spec, and return it |
|
192 |
return engineGenerateSecret(pbeKeySpec); |
|
193 |
} else { |
|
194 |
throw new InvalidKeyException("Invalid key format/algorithm"); |
|
195 |
} |
|
196 |
||
197 |
} catch (InvalidKeySpecException ikse) { |
|
198 |
throw new InvalidKeyException("Cannot translate key: " |
|
199 |
+ ikse.getMessage()); |
|
200 |
} |
|
201 |
} |
|
202 |
} |