Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/TestCipherPBECons.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.NoSuchAlgorithmException;25import java.security.Provider;26import java.security.Security;27import javax.crypto.Cipher;28import javax.crypto.NoSuchPaddingException;2930/**31* @test32* @bug 804178733* @summary Verify that for PBEWithMD5AndDES cipher, only CBC mode and34* PKCS#5Padding is allowed35* @author Yun Ke36* @author Bill Situ37* @author Yu-Ching (Valerie) PENG38*/39public class TestCipherPBECons {4041private static final String[] PBEAlgorithms = {"pbeWithMD5ANDdes",42"PBEWithMD5AndTripleDES"};43private static final String[] cipherModes = {"ECb", "cbC", "cFB", "Cfb32",44"OfB", "oFb64", "pCbC"};45private static final String[] cipherPaddings = {"Pkcs5Padding", "NoPaDDing"};4647public static void main(String[] args) {48TestCipherPBECons test = new TestCipherPBECons();49Provider sunjce = Security.getProvider("SunJCE");5051if (!test.runAll(sunjce, System.out)) {52throw new RuntimeException("One or more tests have failed....");53}54}5556public boolean runAll(Provider p, PrintStream out) {57boolean finalResult = true;5859for (String algorithm : PBEAlgorithms) {60for (String mode : cipherModes) {61for (String padding : cipherPaddings) {62out.println("Running test with " + algorithm63+ "/" + mode + "/" + padding);64try {65if (!runTest(p, algorithm, mode, padding, out)) {66finalResult = false;67out.println("STATUS: Failed");68} else {69out.println("STATUS: Passed");70}71} catch (Exception ex) {72finalResult = false;73ex.printStackTrace(out);74out.println("STATUS:Failed");75}76}77}78}7980return finalResult;81}8283public boolean runTest(Provider p, String algo, String mo, String pad,84PrintStream out) throws Exception {85try {86// Initialization87Cipher ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);8889// No exception thrown, must be of the right mode and right90// padding scheme91return (mo.equalsIgnoreCase("CBC"))92&& (pad.equalsIgnoreCase("PKCS5Padding"));93} catch (NoSuchAlgorithmException ex) {94if (p.getName().compareTo("SunJCE") == 0) {95if (!(mo.equalsIgnoreCase("CBC")96&& pad.equalsIgnoreCase("PKCS5Padding"))) {97out.println("NoSuchAlgorithmException is as expected");98return true;99}100}101102out.println("Caught exception: " + ex.getMessage());103throw ex;104} catch (NoSuchPaddingException ex) {105if (mo.equalsIgnoreCase("CBC")106&& pad.equalsIgnoreCase("NoPadding")) {107out.println("NoSuchPaddingException is as expected");108return true;109} else {110out.println("Caught unexpected exception: " + ex.getMessage());111return false;112}113}114}115}116117118