Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBESealedObject.java
41161 views
/*1* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.PrintStream;24import java.security.AlgorithmParameters;25import java.security.InvalidKeyException;26import java.security.Provider;27import java.security.Security;28import java.security.spec.AlgorithmParameterSpec;29import java.util.Arrays;30import java.util.Random;31import java.util.StringTokenizer;32import javax.crypto.Cipher;33import javax.crypto.SealedObject;34import javax.crypto.SecretKey;35import javax.crypto.SecretKeyFactory;36import javax.crypto.spec.PBEKeySpec;37import javax.crypto.spec.PBEParameterSpec;3839/**40* @test41* @bug 804178142* @summary test if seal/unseal works correctly with PBE algorithms43* @author Yun Ke44* @author Bill Situ45* @author Alexander Fomin46* @run main PBESealedObject47* @key randomness48*/49public class PBESealedObject {5051private static final String[] PBEAlgorithms = {52"pbeWithMD5ANDdes",53"PBEWithMD5AndDES/CBC/PKCS5Padding",54"PBEWithMD5AndTripleDES",55"PBEWithMD5AndTripleDES/CBC/PKCS5Padding",56"PBEwithSHA1AndDESede",57"PBEwithSHA1AndDESede/CBC/PKCS5Padding",58"PBEwithSHA1AndRC2_40",59"PBEwithSHA1Andrc2_40/CBC/PKCS5Padding",60"PBEWithSHA1AndRC2_128",61"PBEWithSHA1andRC2_128/CBC/PKCS5Padding",62"PBEWithSHA1AndRC4_40",63"PBEWithsha1AndRC4_40/ECB/NoPadding",64"PBEWithSHA1AndRC4_128",65"pbeWithSHA1AndRC4_128/ECB/NoPadding",66"PBEWithHmacSHA1AndAES_128",67"PBEWithHmacSHA224AndAES_128",68"PBEWithHmacSHA256AndAES_128",69"PBEWithHmacSHA384AndAES_128",70"PBEWithHmacSHA512AndAES_128",71"PBEWithHmacSHA1AndAES_256",72"PBEWithHmacSHA224AndAES_256",73"PBEWithHmacSHA256AndAES_256",74"PBEWithHmacSHA384AndAES_256",75"PBEWithHmacSHA512AndAES_256"76};7778public static void main(String[] args) {79PBESealedObject test = new PBESealedObject();80Provider sunjce = Security.getProvider("SunJCE");8182if (!test.runAll(sunjce, System.out)) {83throw new RuntimeException("One or more tests have failed....");84}85}8687public boolean runAll(Provider p, PrintStream out) {88boolean finalResult = true;8990for (String algorithm : PBEAlgorithms) {91out.println("Running test with " + algorithm + ":");92try {93if (!runTest(p, algorithm, out)) {94finalResult = false;95out.println("STATUS: Failed");96} else {97out.println("STATUS: Passed");98}99} catch (Exception ex) {100finalResult = false;101ex.printStackTrace(out);102out.println("STATUS:Failed");103}104}105106return finalResult;107}108109// Have a generic throws Exception as it can throw many different exceptions110public boolean runTest(Provider p, String algo, PrintStream out)111throws Exception {112113byte[] salt = new byte[8];114int ITERATION_COUNT = 1000;115AlgorithmParameters pbeParams = null;116117String baseAlgo118= new StringTokenizer(algo, "/").nextToken().toUpperCase();119boolean isAES = baseAlgo.contains("AES");120121try {122// Initialization123Cipher ci = Cipher.getInstance(algo, p);124new Random().nextBytes(salt);125AlgorithmParameterSpec aps = new PBEParameterSpec(salt,126ITERATION_COUNT);127SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);128SecretKey key = skf.generateSecret(129new PBEKeySpec("Secret Lover".toCharArray()));130131// Seal132if (isAES) {133ci.init(Cipher.ENCRYPT_MODE, key);134pbeParams = ci.getParameters();135} else {136ci.init(Cipher.ENCRYPT_MODE, key, aps);137}138139SealedObject so = new SealedObject(key, ci);140141// Unseal and compare142if (isAES) {143ci.init(Cipher.DECRYPT_MODE, key, pbeParams);144} else {145ci.init(Cipher.DECRYPT_MODE, key, aps);146}147148SecretKey unsealedKey;149150unsealedKey = (SecretKey) so.getObject(ci);151if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {152return false;153}154155unsealedKey = (SecretKey) so.getObject(key);156if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {157return false;158}159160unsealedKey = (SecretKey) so.getObject(key, "SunJCE");161return Arrays.equals(unsealedKey.getEncoded(), key.getEncoded());162} catch (InvalidKeyException ex) {163if (baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256")) {164out.println(165"Expected exception , keyStrength > 128 within" + algo);166return true;167}168169throw ex;170}171}172173}174175176