Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyCleanupTest.java
41161 views
/*1* Copyright (c) 2017, 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* @modules java.base/com.sun.crypto.provider:+open26* @run main/othervm PBEKeyCleanupTest27* @summary Verify that key storage is cleared28*/2930import java.lang.ref.PhantomReference;31import java.lang.ref.Reference;32import java.lang.ref.ReferenceQueue;33import java.lang.reflect.Field;34import java.util.Arrays;35import java.util.Random;3637import javax.crypto.SecretKey;38import javax.crypto.SecretKeyFactory;39import javax.crypto.spec.PBEKeySpec;4041/**42* Test that the array holding the key bytes is cleared when it is43* no longer referenced by the key.44*/45public class PBEKeyCleanupTest {4647private final static String SunJCEProvider = "SunJCE";4849private static final String PASS_PHRASE = "some hidden string";50private static final int ITERATION_COUNT = 1000;51private static final int KEY_SIZE = 128;5253public static void main(String[] args) throws Exception {54testPBESecret("PBEWithMD5AndDES");55testPBKSecret("PBKDF2WithHmacSHA1");56}5758private static void testPBESecret(String algorithm) throws Exception {59char[] password = new char[] {'f', 'o', 'o'};60PBEKeySpec pbeKeySpec = new PBEKeySpec(password);61SecretKeyFactory keyFac =62SecretKeyFactory.getInstance(algorithm, SunJCEProvider);6364testCleanupSecret(algorithm, keyFac.generateSecret(pbeKeySpec));65}6667private static void testPBKSecret(String algorithm) throws Exception {68byte[] salt = new byte[8];69new Random().nextBytes(salt);70char[] password = new char[] {'f', 'o', 'o'};71PBEKeySpec pbeKeySpec = new PBEKeySpec(PASS_PHRASE.toCharArray(), salt,72ITERATION_COUNT, KEY_SIZE);73SecretKeyFactory keyFac =74SecretKeyFactory.getInstance(algorithm, SunJCEProvider);7576testCleanupSecret(algorithm, keyFac.generateSecret(pbeKeySpec));77}7879static void testCleanupSecret(String algorithm, SecretKey key) throws Exception {8081// Break into the implementation to observe the key byte array.82Class<?> keyClass = key.getClass();83Field keyField = keyClass.getDeclaredField("key");84keyField.setAccessible(true);85byte[] array = (byte[])keyField.get(key);8687byte[] zeros = new byte[array.length];88do {89// Wait for array to be cleared; if not cleared test will timeout90System.out.printf("%s array: %s%n", algorithm, Arrays.toString(array));91key = null;92System.gc(); // attempt to reclaim the key93} while (Arrays.compare(zeros, array) != 0);94System.out.printf("%s array: %s%n", algorithm, Arrays.toString(array));9596Reference.reachabilityFence(key); // Keep key alive97Reference.reachabilityFence(array); // Keep array alive98}99}100101102103104105