Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/EntryProtectionTest.java
41153 views
/*1* Copyright (c) 2012, 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24import java.io.File;25import static java.lang.System.err;26import java.security.*;27import java.security.cert.Certificate;28import java.util.ArrayList;29import java.util.List;30import java.util.Random;31import javax.crypto.spec.PBEParameterSpec;32import jdk.test.lib.RandomFactory;33import static java.lang.System.out;34import java.util.Arrays;3536/**37* @test38* @bug 804883039* @summary Test for feature 'support stronger entry protection'. An entry is40* stored to keystore with different PasswordProtection objects which are41* specified by different PBE algorithms (use -Dseed=X to set PRNG seed)42* @library /test/lib ../43* @key randomness44* @build jdk.test.lib.RandomFactory45* @run main EntryProtectionTest46*/47public class EntryProtectionTest {48private static final char[] PASSWORD = "passwd".toCharArray();49private static final String ALIAS = "testkey";50private static final byte[] SALT = new byte[8];51private static final int ITERATION_COUNT = 1024;52private static final List<KeyStore.PasswordProtection> PASSWORD_PROTECTION53= new ArrayList<>();54private static final String KEYSTORE_PATH = System.getProperty(55"test.classes" + File.separator + "ks.pkcs12",56"." + File.separator + "ks.pkcs12");5758private void runTest() throws Exception {59KeyStore ksIn = Utils.loadKeyStore(KEYSTORE_PATH,60Utils.KeyStoreType.pkcs12, PASSWORD);61KeyStore ksTest = KeyStore62.getInstance(Utils.KeyStoreType.pkcs12.name());63ksTest.load(null);64Certificate cert = ksIn.getCertificate(ALIAS);65Key key = ksIn.getKey(ALIAS, PASSWORD);66KeyStore.Entry keyStoreEntry = new KeyStore.PrivateKeyEntry(67(PrivateKey) key, new Certificate[]{cert});68for (KeyStore.PasswordProtection passwordAlgorithm :69PASSWORD_PROTECTION) {70out.println("Try to use: " +71passwordAlgorithm.getProtectionAlgorithm());72ksTest.setEntry(ALIAS, keyStoreEntry, passwordAlgorithm);73KeyStore.Entry entryRead = ksTest.getEntry(ALIAS,74new KeyStore.PasswordProtection(PASSWORD));75if (!isPrivateKeyEntriesEqual((KeyStore.PrivateKeyEntry)76keyStoreEntry, (KeyStore.PrivateKeyEntry)entryRead)) {77err.println("Original entry in KeyStore: " + keyStoreEntry);78err.println("Enc/Dec entry : " + entryRead);79throw new RuntimeException(80String.format(81"Decrypted & original enities do "82+ "not match. Algo: %s, Actual: %s, "83+ "Expected: %s",84passwordAlgorithm.getProtectionAlgorithm(),85entryRead, keyStoreEntry));86}87ksTest.deleteEntry(ALIAS);88}89out.println("Test Passed");90}9192public static void main(String args[]) throws Exception {93EntryProtectionTest entryProtectionTest = new EntryProtectionTest();94entryProtectionTest.setUp();95entryProtectionTest.runTest();96}9798private void setUp() {99out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);100Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);101Random rand = RandomFactory.getRandom();102rand.nextBytes(SALT);103out.print("Salt: ");104for (byte b : SALT) {105out.format("%02X ", b);106}107out.println("");108PASSWORD_PROTECTION109.add(new KeyStore.PasswordProtection(PASSWORD,110"PBEWithMD5AndDES", new PBEParameterSpec(SALT,111ITERATION_COUNT)));112PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,113"PBEWithSHA1AndDESede", null));114PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,115"PBEWithSHA1AndRC2_40", null));116PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,117"PBEWithSHA1AndRC2_128", null));118PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,119"PBEWithSHA1AndRC4_40", null));120PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,121"PBEWithSHA1AndRC4_128", null));122}123124/**125* Checks whether given two KeyStore.PrivateKeyEntry parameters are equal126* The KeyStore.PrivateKeyEntry fields like {privateKey, certificateChain[]}127* are checked for equality and another field Set<attributes> is not checked128* as default implementation adds few PKCS12 attributes during read129* operation130* @param first131* parameter is of type KeyStore.PrivateKeyEntry132* @param second133* parameter is of type KeyStore.PrivateKeyEntry134* @return boolean135* true when both the KeyStore.PrivateKeyEntry fields are equal136*/137boolean isPrivateKeyEntriesEqual(KeyStore.PrivateKeyEntry first,138KeyStore.PrivateKeyEntry second) {139//compare privateKey140if (!Arrays.equals(first.getPrivateKey().getEncoded(),141second.getPrivateKey().getEncoded())) {142err.println("Mismatch found in privateKey!");143return false;144}145//compare certificateChain[]146if (!Arrays.equals(first.getCertificateChain(),147second.getCertificateChain())) {148err.println("Mismatch found in certificate chain!");149return false;150}151return true;152}153}154155156