|
1 /* |
|
2 * Copyright (c) 2010, Oracle and/or its affiliates. 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. |
|
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 * |
|
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. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 6687725 |
|
27 * @summary Test internal PKCS5Padding impl with various error conditions. |
|
28 * @author Valerie Peng |
|
29 * @library .. |
|
30 */ |
|
31 import java.io.*; |
|
32 import java.nio.*; |
|
33 import java.util.*; |
|
34 |
|
35 import java.security.*; |
|
36 import java.security.spec.AlgorithmParameterSpec; |
|
37 |
|
38 import javax.crypto.*; |
|
39 import javax.crypto.spec.IvParameterSpec; |
|
40 |
|
41 public class TestPKCS5PaddingError extends PKCS11Test { |
|
42 private static class CI { // class for holding Cipher Information |
|
43 String transformation; |
|
44 String keyAlgo; |
|
45 |
|
46 CI(String transformation, String keyAlgo) { |
|
47 this.transformation = transformation; |
|
48 this.keyAlgo = keyAlgo; |
|
49 } |
|
50 } |
|
51 |
|
52 private static final CI[] TEST_LIST = { |
|
53 // algorithms which use the native padding impl |
|
54 new CI("DES/CBC/PKCS5Padding", "DES"), |
|
55 new CI("DESede/CBC/PKCS5Padding", "DESede"), |
|
56 new CI("AES/CBC/PKCS5Padding", "AES"), |
|
57 // algorithms which use SunPKCS11's own padding impl |
|
58 new CI("DES/ECB/PKCS5Padding", "DES"), |
|
59 new CI("DESede/ECB/PKCS5Padding", "DESede"), |
|
60 new CI("AES/ECB/PKCS5Padding", "AES"), |
|
61 }; |
|
62 |
|
63 private static StringBuffer debugBuf = new StringBuffer(); |
|
64 |
|
65 public void main(Provider p) throws Exception { |
|
66 boolean status = true; |
|
67 Random random = new Random(); |
|
68 |
|
69 try { |
|
70 byte[] plainText = new byte[200]; |
|
71 |
|
72 for (int i = 0; i < TEST_LIST.length; i++) { |
|
73 CI currTest = TEST_LIST[i]; |
|
74 System.out.println("===" + currTest.transformation + "==="); |
|
75 try { |
|
76 KeyGenerator kg = |
|
77 KeyGenerator.getInstance(currTest.keyAlgo, p); |
|
78 SecretKey key = kg.generateKey(); |
|
79 Cipher c1 = Cipher.getInstance(currTest.transformation, |
|
80 "SunJCE"); |
|
81 c1.init(Cipher.ENCRYPT_MODE, key); |
|
82 byte[] cipherText = c1.doFinal(plainText); |
|
83 AlgorithmParameters params = c1.getParameters(); |
|
84 Cipher c2 = Cipher.getInstance(currTest.transformation, p); |
|
85 c2.init(Cipher.DECRYPT_MODE, key, params); |
|
86 |
|
87 // 1st test: wrong output length |
|
88 // NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when |
|
89 // the data passed to its EncryptUpdate/DecryptUpdate is |
|
90 // not multiple of blocks |
|
91 if (!p.getName().equals("SunPKCS11-NSS")) { |
|
92 try { |
|
93 System.out.println("Testing with wrong cipherText length"); |
|
94 c2.doFinal(cipherText, 0, cipherText.length - 2); |
|
95 } catch (IllegalBlockSizeException ibe) { |
|
96 // expected |
|
97 } catch (Exception ex) { |
|
98 System.out.println("Error: Unexpected Ex " + ex); |
|
99 ex.printStackTrace(); |
|
100 } |
|
101 } |
|
102 // 2nd test: wrong padding value |
|
103 try { |
|
104 System.out.println("Testing with wrong padding bytes"); |
|
105 cipherText[cipherText.length - 1]++; |
|
106 c2.doFinal(cipherText); |
|
107 } catch (BadPaddingException bpe) { |
|
108 // expected |
|
109 } catch (Exception ex) { |
|
110 System.out.println("Error: Unexpected Ex " + ex); |
|
111 ex.printStackTrace(); |
|
112 } |
|
113 System.out.println("DONE"); |
|
114 } catch (NoSuchAlgorithmException nsae) { |
|
115 System.out.println("Skipping unsupported algorithm: " + |
|
116 nsae); |
|
117 } |
|
118 } |
|
119 } catch (Exception ex) { |
|
120 // print out debug info when exception is encountered |
|
121 if (debugBuf != null) { |
|
122 System.out.println(debugBuf.toString()); |
|
123 debugBuf = new StringBuffer(); |
|
124 } |
|
125 throw ex; |
|
126 } |
|
127 } |
|
128 |
|
129 public static void main(String[] args) throws Exception { |
|
130 main(new TestPKCS5PaddingError()); |
|
131 } |
|
132 } |