author | juh |
Fri, 10 Jan 2014 13:42:44 -0800 | |
changeset 23897 | 911b1eb93667 |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
7043 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
7043 | 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 sun.security.ssl; |
|
27 |
||
28 |
import java.security.AlgorithmConstraints; |
|
29 |
import java.security.CryptoPrimitive; |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
30 |
import java.security.PrivateKey; |
7043 | 31 |
|
32 |
import java.util.Set; |
|
33 |
import java.util.HashSet; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.EnumSet; |
|
36 |
import java.util.TreeMap; |
|
37 |
import java.util.Collection; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.ArrayList; |
|
40 |
||
16080 | 41 |
import sun.security.util.KeyUtil; |
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
42 |
|
7043 | 43 |
/** |
44 |
* Signature and hash algorithm. |
|
45 |
* |
|
46 |
* [RFC5246] The client uses the "signature_algorithms" extension to |
|
47 |
* indicate to the server which signature/hash algorithm pairs may be |
|
48 |
* used in digital signatures. The "extension_data" field of this |
|
49 |
* extension contains a "supported_signature_algorithms" value. |
|
50 |
* |
|
51 |
* enum { |
|
52 |
* none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
|
53 |
* sha512(6), (255) |
|
54 |
* } HashAlgorithm; |
|
55 |
* |
|
56 |
* enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
|
57 |
* SignatureAlgorithm; |
|
58 |
* |
|
59 |
* struct { |
|
60 |
* HashAlgorithm hash; |
|
61 |
* SignatureAlgorithm signature; |
|
62 |
* } SignatureAndHashAlgorithm; |
|
63 |
*/ |
|
64 |
final class SignatureAndHashAlgorithm { |
|
65 |
||
66 |
// minimum priority for default enabled algorithms |
|
67 |
final static int SUPPORTED_ALG_PRIORITY_MAX_NUM = 0x00F0; |
|
68 |
||
69 |
// performance optimization |
|
70 |
private final static Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET = |
|
23897 | 71 |
Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE)); |
7043 | 72 |
|
73 |
// supported pairs of signature and hash algorithm |
|
74 |
private final static Map<Integer, SignatureAndHashAlgorithm> supportedMap; |
|
75 |
private final static Map<Integer, SignatureAndHashAlgorithm> priorityMap; |
|
76 |
||
77 |
// the hash algorithm |
|
78 |
private HashAlgorithm hash; |
|
79 |
||
80 |
// id in 16 bit MSB format, i.e. 0x0603 for SHA512withECDSA |
|
81 |
private int id; |
|
82 |
||
83 |
// the standard algorithm name, for example "SHA512withECDSA" |
|
84 |
private String algorithm; |
|
85 |
||
86 |
// Priority for the preference order. The lower the better. |
|
87 |
// |
|
88 |
// If the algorithm is unsupported, its priority should be bigger |
|
89 |
// than SUPPORTED_ALG_PRIORITY_MAX_NUM. |
|
90 |
private int priority; |
|
91 |
||
92 |
// constructor for supported algorithm |
|
93 |
private SignatureAndHashAlgorithm(HashAlgorithm hash, |
|
94 |
SignatureAlgorithm signature, String algorithm, int priority) { |
|
95 |
this.hash = hash; |
|
96 |
this.algorithm = algorithm; |
|
97 |
this.id = ((hash.value & 0xFF) << 8) | (signature.value & 0xFF); |
|
98 |
this.priority = priority; |
|
99 |
} |
|
100 |
||
101 |
// constructor for unsupported algorithm |
|
102 |
private SignatureAndHashAlgorithm(String algorithm, int id, int sequence) { |
|
103 |
this.hash = HashAlgorithm.valueOf((id >> 8) & 0xFF); |
|
104 |
this.algorithm = algorithm; |
|
105 |
this.id = id; |
|
106 |
||
14675
17224d0282f1
8004019: Removes unused method HandshakeHash.setCertificateVerifyAlg()
xuelei
parents:
11521
diff
changeset
|
107 |
// add one more to the sequence number, in case that the number is zero |
7043 | 108 |
this.priority = SUPPORTED_ALG_PRIORITY_MAX_NUM + sequence + 1; |
109 |
} |
|
110 |
||
111 |
// Note that we do not use the sequence argument for supported algorithms, |
|
112 |
// so please don't sort by comparing the objects read from handshake |
|
113 |
// messages. |
|
114 |
static SignatureAndHashAlgorithm valueOf(int hash, |
|
115 |
int signature, int sequence) { |
|
116 |
hash &= 0xFF; |
|
117 |
signature &= 0xFF; |
|
118 |
||
119 |
int id = (hash << 8) | signature; |
|
120 |
SignatureAndHashAlgorithm signAlg = supportedMap.get(id); |
|
121 |
if (signAlg == null) { |
|
122 |
// unsupported algorithm |
|
123 |
signAlg = new SignatureAndHashAlgorithm( |
|
124 |
"Unknown (hash:0x" + Integer.toString(hash, 16) + |
|
125 |
", signature:0x" + Integer.toString(signature, 16) + ")", |
|
126 |
id, sequence); |
|
127 |
} |
|
128 |
||
129 |
return signAlg; |
|
130 |
} |
|
131 |
||
132 |
int getHashValue() { |
|
133 |
return (id >> 8) & 0xFF; |
|
134 |
} |
|
135 |
||
136 |
int getSignatureValue() { |
|
137 |
return id & 0xFF; |
|
138 |
} |
|
139 |
||
140 |
String getAlgorithmName() { |
|
141 |
return algorithm; |
|
142 |
} |
|
143 |
||
144 |
// return the size of a SignatureAndHashAlgorithm structure in TLS record |
|
145 |
static int sizeInRecord() { |
|
146 |
return 2; |
|
147 |
} |
|
148 |
||
149 |
// Get local supported algorithm collection complying to |
|
150 |
// algorithm constraints |
|
151 |
static Collection<SignatureAndHashAlgorithm> |
|
152 |
getSupportedAlgorithms(AlgorithmConstraints constraints) { |
|
153 |
||
7990 | 154 |
Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>(); |
7043 | 155 |
synchronized (priorityMap) { |
156 |
for (SignatureAndHashAlgorithm sigAlg : priorityMap.values()) { |
|
157 |
if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM && |
|
158 |
constraints.permits(SIGNATURE_PRIMITIVE_SET, |
|
159 |
sigAlg.algorithm, null)) { |
|
160 |
supported.add(sigAlg); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
return supported; |
|
166 |
} |
|
167 |
||
168 |
// Get supported algorithm collection from an untrusted collection |
|
169 |
static Collection<SignatureAndHashAlgorithm> getSupportedAlgorithms( |
|
170 |
Collection<SignatureAndHashAlgorithm> algorithms ) { |
|
7990 | 171 |
Collection<SignatureAndHashAlgorithm> supported = new ArrayList<>(); |
7043 | 172 |
for (SignatureAndHashAlgorithm sigAlg : algorithms) { |
173 |
if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM) { |
|
174 |
supported.add(sigAlg); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
return supported; |
|
179 |
} |
|
180 |
||
181 |
static String[] getAlgorithmNames( |
|
182 |
Collection<SignatureAndHashAlgorithm> algorithms) { |
|
7990 | 183 |
ArrayList<String> algorithmNames = new ArrayList<>(); |
7043 | 184 |
if (algorithms != null) { |
185 |
for (SignatureAndHashAlgorithm sigAlg : algorithms) { |
|
186 |
algorithmNames.add(sigAlg.algorithm); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
String[] array = new String[algorithmNames.size()]; |
|
191 |
return algorithmNames.toArray(array); |
|
192 |
} |
|
193 |
||
194 |
static Set<String> getHashAlgorithmNames( |
|
195 |
Collection<SignatureAndHashAlgorithm> algorithms) { |
|
7990 | 196 |
Set<String> algorithmNames = new HashSet<>(); |
7043 | 197 |
if (algorithms != null) { |
198 |
for (SignatureAndHashAlgorithm sigAlg : algorithms) { |
|
199 |
if (sigAlg.hash.value > 0) { |
|
200 |
algorithmNames.add(sigAlg.hash.standardName); |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
return algorithmNames; |
|
206 |
} |
|
207 |
||
208 |
static String getHashAlgorithmName(SignatureAndHashAlgorithm algorithm) { |
|
209 |
return algorithm.hash.standardName; |
|
210 |
} |
|
211 |
||
212 |
private static void supports(HashAlgorithm hash, |
|
213 |
SignatureAlgorithm signature, String algorithm, int priority) { |
|
214 |
||
215 |
SignatureAndHashAlgorithm pair = |
|
216 |
new SignatureAndHashAlgorithm(hash, signature, algorithm, priority); |
|
217 |
if (supportedMap.put(pair.id, pair) != null) { |
|
218 |
throw new RuntimeException( |
|
219 |
"Duplicate SignatureAndHashAlgorithm definition, id: " + |
|
220 |
pair.id); |
|
221 |
} |
|
222 |
if (priorityMap.put(pair.priority, pair) != null) { |
|
223 |
throw new RuntimeException( |
|
224 |
"Duplicate SignatureAndHashAlgorithm definition, priority: " + |
|
225 |
pair.priority); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
static SignatureAndHashAlgorithm getPreferableAlgorithm( |
|
230 |
Collection<SignatureAndHashAlgorithm> algorithms, String expected) { |
|
231 |
||
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
232 |
return SignatureAndHashAlgorithm.getPreferableAlgorithm( |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
233 |
algorithms, expected, null); |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
234 |
} |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
235 |
|
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
236 |
static SignatureAndHashAlgorithm getPreferableAlgorithm( |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
237 |
Collection<SignatureAndHashAlgorithm> algorithms, |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
238 |
String expected, PrivateKey signingKey) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
239 |
|
7043 | 240 |
if (expected == null && !algorithms.isEmpty()) { |
241 |
for (SignatureAndHashAlgorithm sigAlg : algorithms) { |
|
242 |
if (sigAlg.priority <= SUPPORTED_ALG_PRIORITY_MAX_NUM) { |
|
243 |
return sigAlg; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
return null; // no supported algorithm |
|
248 |
} |
|
249 |
||
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
250 |
if (expected == null ) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
251 |
return null; // no expected algorithm, no supported algorithm |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
252 |
} |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
253 |
|
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
254 |
/* |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
255 |
* Need to check RSA key length to match the length of hash value |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
256 |
*/ |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
257 |
int maxDigestLength = Integer.MAX_VALUE; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
258 |
if (signingKey != null && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
259 |
"rsa".equalsIgnoreCase(signingKey.getAlgorithm()) && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
260 |
expected.equalsIgnoreCase("rsa")) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
261 |
/* |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
262 |
* RSA keys of 512 bits have been shown to be practically |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
263 |
* breakable, it does not make much sense to use the strong |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
264 |
* hash algorithm for keys whose key size less than 512 bits. |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
265 |
* So it is not necessary to caculate the required max digest |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
266 |
* length exactly. |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
267 |
* |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
268 |
* If key size is greater than or equals to 768, there is no max |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
269 |
* digest length limitation in currect implementation. |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
270 |
* |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
271 |
* If key size is greater than or equals to 512, but less than |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
272 |
* 768, the digest length should be less than or equal to 32 bytes. |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
273 |
* |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
274 |
* If key size is less than 512, the digest length should be |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
275 |
* less than or equal to 20 bytes. |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
276 |
*/ |
16080 | 277 |
int keySize = KeyUtil.getKeySize(signingKey); |
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
278 |
if (keySize >= 768) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
279 |
maxDigestLength = HashAlgorithm.SHA512.length; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
280 |
} else if ((keySize >= 512) && (keySize < 768)) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
281 |
maxDigestLength = HashAlgorithm.SHA256.length; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
282 |
} else if ((keySize > 0) && (keySize < 512)) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
283 |
maxDigestLength = HashAlgorithm.SHA1.length; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
284 |
} // Otherwise, cannot determine the key size, prefer the most |
21278 | 285 |
// preferable hash algorithm. |
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
286 |
} |
7043 | 287 |
|
288 |
for (SignatureAndHashAlgorithm algorithm : algorithms) { |
|
289 |
int signValue = algorithm.id & 0xFF; |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
290 |
if (expected.equalsIgnoreCase("rsa") && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
291 |
signValue == SignatureAlgorithm.RSA.value) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
292 |
if (algorithm.hash.length <= maxDigestLength) { |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
293 |
return algorithm; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
294 |
} |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
295 |
} else if ( |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
296 |
(expected.equalsIgnoreCase("dsa") && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
297 |
signValue == SignatureAlgorithm.DSA.value) || |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
298 |
(expected.equalsIgnoreCase("ecdsa") && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
299 |
signValue == SignatureAlgorithm.ECDSA.value) || |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
300 |
(expected.equalsIgnoreCase("ec") && |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
301 |
signValue == SignatureAlgorithm.ECDSA.value)) { |
7043 | 302 |
return algorithm; |
303 |
} |
|
304 |
} |
|
305 |
||
306 |
return null; |
|
307 |
} |
|
308 |
||
309 |
static enum HashAlgorithm { |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
310 |
UNDEFINED("undefined", "", -1, -1), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
311 |
NONE( "none", "NONE", 0, -1), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
312 |
MD5( "md5", "MD5", 1, 16), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
313 |
SHA1( "sha1", "SHA-1", 2, 20), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
314 |
SHA224( "sha224", "SHA-224", 3, 28), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
315 |
SHA256( "sha256", "SHA-256", 4, 32), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
316 |
SHA384( "sha384", "SHA-384", 5, 48), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
317 |
SHA512( "sha512", "SHA-512", 6, 64); |
7043 | 318 |
|
319 |
final String name; // not the standard signature algorithm name |
|
320 |
// except the UNDEFINED, other names are defined |
|
321 |
// by TLS 1.2 protocol |
|
322 |
final String standardName; // the standard MessageDigest algorithm name |
|
323 |
final int value; |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
324 |
final int length; // digest length in bytes, -1 means not applicable |
7043 | 325 |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
326 |
private HashAlgorithm(String name, String standardName, |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
327 |
int value, int length) { |
7043 | 328 |
this.name = name; |
329 |
this.standardName = standardName; |
|
330 |
this.value = value; |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
9035
diff
changeset
|
331 |
this.length = length; |
7043 | 332 |
} |
333 |
||
334 |
static HashAlgorithm valueOf(int value) { |
|
335 |
HashAlgorithm algorithm = UNDEFINED; |
|
336 |
switch (value) { |
|
337 |
case 0: |
|
338 |
algorithm = NONE; |
|
339 |
break; |
|
340 |
case 1: |
|
341 |
algorithm = MD5; |
|
342 |
break; |
|
343 |
case 2: |
|
344 |
algorithm = SHA1; |
|
345 |
break; |
|
346 |
case 3: |
|
347 |
algorithm = SHA224; |
|
348 |
break; |
|
349 |
case 4: |
|
350 |
algorithm = SHA256; |
|
351 |
break; |
|
352 |
case 5: |
|
353 |
algorithm = SHA384; |
|
354 |
break; |
|
355 |
case 6: |
|
356 |
algorithm = SHA512; |
|
357 |
break; |
|
358 |
} |
|
359 |
||
360 |
return algorithm; |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
static enum SignatureAlgorithm { |
|
365 |
UNDEFINED("undefined", -1), |
|
366 |
ANONYMOUS("anonymous", 0), |
|
367 |
RSA( "rsa", 1), |
|
368 |
DSA( "dsa", 2), |
|
369 |
ECDSA( "ecdsa", 3); |
|
370 |
||
371 |
final String name; // not the standard signature algorithm name |
|
372 |
// except the UNDEFINED, other names are defined |
|
373 |
// by TLS 1.2 protocol |
|
374 |
final int value; |
|
375 |
||
376 |
private SignatureAlgorithm(String name, int value) { |
|
377 |
this.name = name; |
|
378 |
this.value = value; |
|
379 |
} |
|
380 |
||
381 |
static SignatureAlgorithm valueOf(int value) { |
|
382 |
SignatureAlgorithm algorithm = UNDEFINED; |
|
383 |
switch (value) { |
|
384 |
case 0: |
|
385 |
algorithm = ANONYMOUS; |
|
386 |
break; |
|
387 |
case 1: |
|
388 |
algorithm = RSA; |
|
389 |
break; |
|
390 |
case 2: |
|
391 |
algorithm = DSA; |
|
392 |
break; |
|
393 |
case 3: |
|
394 |
algorithm = ECDSA; |
|
395 |
break; |
|
396 |
} |
|
397 |
||
398 |
return algorithm; |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
static { |
|
403 |
supportedMap = Collections.synchronizedSortedMap( |
|
404 |
new TreeMap<Integer, SignatureAndHashAlgorithm>()); |
|
405 |
priorityMap = Collections.synchronizedSortedMap( |
|
406 |
new TreeMap<Integer, SignatureAndHashAlgorithm>()); |
|
407 |
||
408 |
synchronized (supportedMap) { |
|
409 |
int p = SUPPORTED_ALG_PRIORITY_MAX_NUM; |
|
410 |
supports(HashAlgorithm.MD5, SignatureAlgorithm.RSA, |
|
411 |
"MD5withRSA", --p); |
|
412 |
supports(HashAlgorithm.SHA1, SignatureAlgorithm.DSA, |
|
413 |
"SHA1withDSA", --p); |
|
414 |
supports(HashAlgorithm.SHA1, SignatureAlgorithm.RSA, |
|
415 |
"SHA1withRSA", --p); |
|
416 |
supports(HashAlgorithm.SHA1, SignatureAlgorithm.ECDSA, |
|
417 |
"SHA1withECDSA", --p); |
|
418 |
supports(HashAlgorithm.SHA224, SignatureAlgorithm.RSA, |
|
419 |
"SHA224withRSA", --p); |
|
420 |
supports(HashAlgorithm.SHA224, SignatureAlgorithm.ECDSA, |
|
421 |
"SHA224withECDSA", --p); |
|
422 |
supports(HashAlgorithm.SHA256, SignatureAlgorithm.RSA, |
|
423 |
"SHA256withRSA", --p); |
|
424 |
supports(HashAlgorithm.SHA256, SignatureAlgorithm.ECDSA, |
|
425 |
"SHA256withECDSA", --p); |
|
426 |
supports(HashAlgorithm.SHA384, SignatureAlgorithm.RSA, |
|
427 |
"SHA384withRSA", --p); |
|
428 |
supports(HashAlgorithm.SHA384, SignatureAlgorithm.ECDSA, |
|
429 |
"SHA384withECDSA", --p); |
|
430 |
supports(HashAlgorithm.SHA512, SignatureAlgorithm.RSA, |
|
431 |
"SHA512withRSA", --p); |
|
432 |
supports(HashAlgorithm.SHA512, SignatureAlgorithm.ECDSA, |
|
433 |
"SHA512withECDSA", --p); |
|
434 |
} |
|
435 |
} |
|
436 |
} |
|
437 |