2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, 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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4953554
|
|
27 |
* @summary Ensure either IllegalAlgorithmParameterException or
|
|
28 |
* InvalidKeyException is thrown instead of SecurityException when
|
|
29 |
* crypto permssion checks failed.
|
|
30 |
* @author Valerie Peng
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.*;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import java.security.*;
|
|
37 |
import java.security.spec.*;
|
|
38 |
|
|
39 |
import javax.crypto.*;
|
|
40 |
import javax.crypto.spec.*;
|
|
41 |
|
|
42 |
public class AllPermCheck {
|
|
43 |
|
|
44 |
private static String SYM_ALGOS[] = {
|
|
45 |
"AES", "Blowfish", "RC2", "ARCFOUR"
|
|
46 |
};
|
|
47 |
|
|
48 |
public static void runTest(Cipher c, Key key) throws Exception {
|
|
49 |
SecureRandom sr = new SecureRandom();
|
|
50 |
|
|
51 |
for (int i = 0; i < 6; i++) {
|
|
52 |
try {
|
|
53 |
switch (i) {
|
|
54 |
case 0:
|
|
55 |
c.init(Cipher.ENCRYPT_MODE, key);
|
|
56 |
break;
|
|
57 |
case 1:
|
|
58 |
c.init(Cipher.ENCRYPT_MODE, key, sr);
|
|
59 |
break;
|
|
60 |
case 2:
|
|
61 |
c.init(Cipher.ENCRYPT_MODE, key,
|
|
62 |
(AlgorithmParameters)null);
|
|
63 |
break;
|
|
64 |
case 3:
|
|
65 |
c.init(Cipher.ENCRYPT_MODE, key,
|
|
66 |
(AlgorithmParameters)null, sr);
|
|
67 |
break;
|
|
68 |
case 4:
|
|
69 |
c.init(Cipher.ENCRYPT_MODE, key,
|
|
70 |
(AlgorithmParameterSpec)null);
|
|
71 |
break;
|
|
72 |
case 5:
|
|
73 |
c.init(Cipher.ENCRYPT_MODE, key,
|
|
74 |
(AlgorithmParameterSpec)null, sr);
|
|
75 |
break;
|
|
76 |
}
|
|
77 |
throw new Exception("...#" + i + " should throw IKE for " +
|
|
78 |
key.getEncoded().length + "-byte keys");
|
|
79 |
} catch (InvalidKeyException ike) {
|
|
80 |
System.out.println("...#" + i + " expected IKE thrown");
|
|
81 |
}
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
public static void main(String[] args) throws Exception {
|
|
86 |
Provider p = Security.getProvider("SunJCE");
|
|
87 |
System.out.println("Testing provider " + p.getName() + "...");
|
|
88 |
if (Cipher.getMaxAllowedKeyLength("DES") == Integer.MAX_VALUE) {
|
|
89 |
// skip this test for unlimited jurisdiction policy files
|
|
90 |
System.out.println("Skip this test due to unlimited version");
|
|
91 |
return;
|
|
92 |
}
|
|
93 |
for (int i = 0; i < SYM_ALGOS.length; i++) {
|
|
94 |
String algo = SYM_ALGOS[i];
|
|
95 |
Cipher c = Cipher.getInstance(algo, p);
|
|
96 |
int keyLength = Cipher.getMaxAllowedKeyLength(algo);
|
|
97 |
SecretKey key = new SecretKeySpec(new byte[keyLength/8 + 8], algo);
|
|
98 |
System.out.println("Testing " + algo + " Cipher");
|
|
99 |
runTest(c, key);
|
|
100 |
}
|
|
101 |
System.out.println("All tests passed!");
|
|
102 |
}
|
|
103 |
}
|