1 /*
2  * Copyright (c) 2012, 2016, 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  * @author Valerie PENG
26  * @author Yun Ke
27  * @author Alexander Fomin
28  * @author rhalade
29  */
30 import java.security.spec.AlgorithmParameterSpec;
31 
32 import java.util.StringTokenizer;
33 
34 import java.security.InvalidKeyException;
35 import java.security.NoSuchAlgorithmException;
36 import java.security.Provider;
37 
38 import java.io.PrintStream;
39 
40 import javax.crypto.Cipher;
41 import javax.crypto.SecretKeyFactory;
42 import javax.crypto.spec.PBEKeySpec;
43 import javax.crypto.spec.PBEParameterSpec;
44 
45 public class PBECipherWrapper extends PBEWrapper {
46 
47     private final AlgorithmParameterSpec aps;
48 
PBECipherWrapper( Provider p, String algo, String passwd, PrintStream out)49     public PBECipherWrapper(
50             Provider p, String algo, String passwd, PrintStream out)
51             throws Exception {
52         super(algo,
53                 SecretKeyFactory.getInstance(
54                         new StringTokenizer(algo, "/").nextToken(), p).generateSecret(
55                         new PBEKeySpec(passwd.toCharArray())),
56                 Cipher.getInstance(algo, p), out);
57 
58         int SALT_SIZE = 8;
59         aps = new PBEParameterSpec(generateSalt(SALT_SIZE), ITERATION_COUNT);
60     }
61 
62     @Override
execute(int edMode, byte[] inputText, int offset, int len)63     public boolean execute(int edMode, byte[] inputText, int offset,
64             int len) {
65         StringTokenizer st = new StringTokenizer(algo, "/");
66         String baseAlgo = st.nextToken().toUpperCase();
67 
68         boolean isUnlimited;
69         try {
70             isUnlimited =
71                 (Cipher.getMaxAllowedKeyLength(this.algo) == Integer.MAX_VALUE);
72         } catch (NoSuchAlgorithmException nsae) {
73             out.println("Got unexpected exception for " + this.algo);
74             nsae.printStackTrace(out);
75             return false;
76         }
77 
78         // Perform encryption or decryption depends on the specified edMode
79         try {
80             ci.init(edMode, key, aps);
81             if ((baseAlgo.endsWith("TRIPLEDES")
82                     || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
83                 out.print("Expected InvalidKeyException not thrown: "
84                     + this.algo);
85                 return false;
86             }
87 
88             // First, generate the cipherText at an allocated buffer
89             byte[] outputText = ci.doFinal(inputText, offset, len);
90 
91             // Second, generate cipherText again at the same buffer of
92             // plainText
93             int myoff = offset / 2;
94             int off = ci.update(inputText, offset, len, inputText, myoff);
95 
96             ci.doFinal(inputText, myoff + off);
97 
98             // Compare to see whether the two results are the same or not
99             boolean result = equalsBlock(inputText, myoff, outputText, 0,
100                     outputText.length);
101 
102             return result;
103         } catch (Exception ex) {
104             if ((ex instanceof InvalidKeyException) &&
105                     (baseAlgo.endsWith("TRIPLEDES")
106                         || baseAlgo.endsWith("AES_256")) &&
107                 !isUnlimited) {
108                 out.println("Expected InvalidKeyException thrown for "
109                     + algo);
110                 return true;
111             } else {
112                 out.println("Got unexpected exception for " + algo);
113                 ex.printStackTrace(out);
114                 return false;
115             }
116         }
117     }
118 }
119