Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/NegativeLength.java
41161 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @bug 800159626* @modules java.base/javax.crypto.spec:open27* @summary Incorrect condition check in PBKDF2KeyImpl.java28*/2930import java.security.*;31import java.security.spec.*;32import javax.crypto.*;33import javax.crypto.spec.*;34import java.lang.reflect.*;3536public class NegativeLength {3738public static void main(String[] args) throws Exception {39SecretKeyFactory skf = SecretKeyFactory.getInstance(40"PBKDF2WithHmacSHA1", "SunJCE");4142// Create a valid PBEKeySpec43PBEKeySpec pbeks = new PBEKeySpec(44new char['p'], new byte[1], 1024, 8);4546// Use reflection to set it negative.47Class c = pbeks.getClass();48Field f = c.getDeclaredField("keyLength");49f.setAccessible(true);50f.setInt(pbeks, -8);5152System.out.println("pbeks.getKeyLength(): " + pbeks.getKeyLength());5354try {5556// A negative length is clearly wrong, we should get a57// InvalidKeySpecException. Anything else is wrong.58skf.generateSecret(pbeks);59throw new Exception("We shouldn't get here.");60} catch (InvalidKeySpecException ike) {61// swallow, this is the exception we want.62System.out.println("Test Passed.");63}64}65}66676869