Path: blob/master/test/jdk/java/security/KeyStore/PBETest.java
41149 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 800659126* @summary Protect keystore entries using stronger PBE algorithms27*/2829import java.io.*;30import java.security.*;31import javax.crypto.*;32import javax.crypto.spec.*;3334// Retrieve a keystore entry, protected by the default encryption algorithm.35// Set the keystore entry, protected by a stronger encryption algorithm.3637public class PBETest {38private final static String DIR = System.getProperty("test.src", ".");39private final static String KEY_PROTECTION_PROPERTY =40"keystore.PKCS12.keyProtectionAlgorithm";41private static final String[] PBE_ALGOS = {42"PBEWithSHA1AndDESede",43"PBEWithHmacSHA1AndAES_128",44"PBEWithHmacSHA224AndAES_128",45"PBEWithHmacSHA256AndAES_128",46"PBEWithHmacSHA384AndAES_128",47"PBEWithHmacSHA512AndAES_128"48};49private static final char[] PASSWORD = "passphrase".toCharArray();50private static final String KEYSTORE_TYPE = "JKS";51private static final String KEYSTORE = DIR + "/keystore.jks";52private static final String NEW_KEYSTORE_TYPE = "PKCS12";53private static final String ALIAS = "vajra";5455private static final byte[] IV = {560x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,570x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x2058};59private static final byte[] SALT = {600x01,0x02,0x03,0x04,0x05,0x06,0x07,0x0861};62private static final int ITERATION_COUNT = 1024;6364public static void main(String[] args) throws Exception {65for (String algo : PBE_ALGOS) {66String filename = algo + ".p12";67main0(algo, filename, true);68main0(algo, filename, false);69Security.setProperty(KEY_PROTECTION_PROPERTY, algo);70main0(null, "PBE.p12", false);71Security.setProperty(KEY_PROTECTION_PROPERTY, "");72}73main0(null, "default.p12", false); // default algorithm74}7576private static void main0(String algo, String filename, boolean useParams)77throws Exception {7879KeyStore keystore = load(KEYSTORE_TYPE, KEYSTORE, PASSWORD);80KeyStore.Entry entry =81keystore.getEntry(ALIAS,82new KeyStore.PasswordProtection(PASSWORD));83System.out.println("Retrieved key entry named '" + ALIAS + "'");84Key originalKey = null;85if (entry instanceof KeyStore.PrivateKeyEntry) {86originalKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();87} else if (entry instanceof KeyStore.SecretKeyEntry) {88originalKey = ((KeyStore.SecretKeyEntry) entry).getSecretKey();89}9091// Set entry92KeyStore keystore2 = load(NEW_KEYSTORE_TYPE, null, null);93if (useParams) {94keystore2.setEntry(ALIAS, entry,95new KeyStore.PasswordProtection(PASSWORD, algo,96new PBEParameterSpec(SALT, ITERATION_COUNT,97new IvParameterSpec(IV))));98System.out.println("Encrypted key entry using: " + algo +99" (with PBE parameters)");100} else if (algo != null) {101keystore2.setEntry(ALIAS, entry,102new KeyStore.PasswordProtection(PASSWORD, algo, null));103System.out.println("Encrypted key entry using: " + algo +104" (without PBE parameters)");105} else {106keystore2.setEntry(ALIAS, entry,107new KeyStore.PasswordProtection(PASSWORD));108String prop = Security.getProperty(KEY_PROTECTION_PROPERTY);109if (prop == null || prop.isEmpty()) {110System.out.println("Encrypted key entry using: " +111"default PKCS12 key protection algorithm");112} else {113System.out.println("Encrypted key entry using: " +114"keyProtectionAlgorithm=" + prop);115}116}117118try (FileOutputStream outStream = new FileOutputStream(filename)) {119System.out.println("Storing keystore to: " + filename);120keystore2.store(outStream, PASSWORD);121}122123try {124keystore2 = load(NEW_KEYSTORE_TYPE, filename, PASSWORD);125entry = keystore2.getEntry(ALIAS,126new KeyStore.PasswordProtection(PASSWORD));127Key key;128if (entry instanceof KeyStore.PrivateKeyEntry) {129key = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();130} else if (entry instanceof KeyStore.SecretKeyEntry) {131key = ((KeyStore.SecretKeyEntry) entry).getSecretKey();132} else {133throw new Exception("Failed to retrieve key entry");134}135if (originalKey.equals(key)) {136System.out.println("Retrieved key entry named '" + ALIAS + "'");137System.out.println();138} else {139throw new Exception(140"Failed: recovered key does not match the original key");141}142143} finally {144new File(filename).delete();145}146}147148private static KeyStore load(String type, String path, char[] password)149throws Exception {150KeyStore keystore = KeyStore.getInstance(type);151152if (path != null) {153154try (FileInputStream inStream = new FileInputStream(path)) {155System.out.println("Loading keystore from: " + path);156keystore.load(inStream, password);157System.out.println("Loaded keystore with " + keystore.size() +158" entries");159}160} else {161keystore.load(null, null);162}163164return keystore;165}166}167168169