Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/EntryProtectionTest.java
38828 views
/*1* Copyright (c) 2012, 2016, 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.testlibrary.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 /lib/testlibrary ../43*/44public class EntryProtectionTest {45private static final char[] PASSWORD = "passwd".toCharArray();46private static final String ALIAS = "testkey";47private static final byte[] SALT = new byte[8];48private static final int ITERATION_COUNT = 1024;49private static final List<KeyStore.PasswordProtection> PASSWORD_PROTECTION50= new ArrayList<>();51private static final String KEYSTORE_PATH = System.getProperty(52"test.classes" + File.separator + "ks.pkcs12",53"." + File.separator + "ks.pkcs12");5455private void runTest() throws Exception {56KeyStore ksIn = Utils.loadKeyStore(KEYSTORE_PATH,57Utils.KeyStoreType.pkcs12, PASSWORD);58KeyStore ksTest = KeyStore59.getInstance(Utils.KeyStoreType.pkcs12.name());60ksTest.load(null);61Certificate cert = ksIn.getCertificate(ALIAS);62Key key = ksIn.getKey(ALIAS, PASSWORD);63KeyStore.Entry keyStoreEntry = new KeyStore.PrivateKeyEntry(64(PrivateKey) key, new Certificate[]{cert});65for (KeyStore.PasswordProtection passwordAlgorithm :66PASSWORD_PROTECTION) {67out.println("Try to use: " +68passwordAlgorithm.getProtectionAlgorithm());69ksTest.setEntry(ALIAS, keyStoreEntry, passwordAlgorithm);70KeyStore.Entry entryRead = ksTest.getEntry(ALIAS,71new KeyStore.PasswordProtection(PASSWORD));72if (!isPrivateKeyEntriesEqual((KeyStore.PrivateKeyEntry)73keyStoreEntry, (KeyStore.PrivateKeyEntry)entryRead)) {74err.println("Original entry in KeyStore: " + keyStoreEntry);75err.println("Enc/Dec entry : " + entryRead);76throw new RuntimeException(77String.format(78"Decrypted & original enities do "79+ "not match. Algo: %s, Actual: %s, "80+ "Expected: %s",81passwordAlgorithm.getProtectionAlgorithm(),82entryRead, keyStoreEntry));83}84ksTest.deleteEntry(ALIAS);85}86out.println("Test Passed");87}8889public static void main(String args[]) throws Exception {90EntryProtectionTest entryProtectionTest = new EntryProtectionTest();91entryProtectionTest.setUp();92entryProtectionTest.runTest();93}9495private void setUp() {96out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);97Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);98Random rand = RandomFactory.getRandom();99rand.nextBytes(SALT);100out.print("Salt: ");101for (byte b : SALT) {102out.format("%02X ", b);103}104out.println("");105PASSWORD_PROTECTION106.add(new KeyStore.PasswordProtection(PASSWORD,107"PBEWithMD5AndDES", new PBEParameterSpec(SALT,108ITERATION_COUNT)));109PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,110"PBEWithSHA1AndDESede", null));111PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,112"PBEWithSHA1AndRC2_40", null));113PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,114"PBEWithSHA1AndRC2_128", null));115PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,116"PBEWithSHA1AndRC4_40", null));117PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,118"PBEWithSHA1AndRC4_128", null));119}120121/**122* Checks whether given two KeyStore.PrivateKeyEntry parameters are equal123* The KeyStore.PrivateKeyEntry fields like {privateKey, certificateChain[]}124* are checked for equality and another field Set<attributes> is not checked125* as default implementation adds few PKCS12 attributes during read126* operation127* @param first128* parameter is of type KeyStore.PrivateKeyEntry129* @param second130* parameter is of type KeyStore.PrivateKeyEntry131* @return boolean132* true when both the KeyStore.PrivateKeyEntry fields are equal133*/134boolean isPrivateKeyEntriesEqual(KeyStore.PrivateKeyEntry first,135KeyStore.PrivateKeyEntry second) {136//compare privateKey137if (!Arrays.equals(first.getPrivateKey().getEncoded(),138second.getPrivateKey().getEncoded())) {139err.println("Mismatch found in privateKey!");140return false;141}142//compare certificateChain[]143if (!Arrays.equals(first.getCertificateChain(),144second.getCertificateChain())) {145err.println("Mismatch found in certificate chain!");146return false;147}148return true;149}150}151152153