Path: blob/master/test/jdk/sun/security/pkcs12/StorePasswordTest.java
41152 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 800540826* @summary KeyStore API enhancements27*/2829import java.io.*;30import java.security.*;31import java.util.*;32import javax.crypto.*;33import javax.crypto.spec.*;34import java.security.spec.InvalidKeySpecException;3536// Store a password in a keystore and retrieve it again.3738public class StorePasswordTest {39private final static String DIR = System.getProperty("test.src", ".");40private static final char[] PASSWORD = "passphrase".toCharArray();41private static final String KEYSTORE = "pwdstore.p12";42private static final String ALIAS = "my password";43private static final String USER_PASSWORD = "hello1";4445public static void main(String[] args) throws Exception {4647new File(KEYSTORE).delete();4849KeyStore keystore = KeyStore.getInstance("PKCS12");50keystore.load(null, null);5152// Set entry53Set<KeyStore.Entry.Attribute> attrs = new HashSet<>();54attrs.add(new PKCS12Attribute("1.3.5.7.9", "printable1"));55attrs.add(new PKCS12Attribute("2.4.6.8.10", "1F:2F:3F:4F:5F"));56int originalAttrCount = attrs.size() + 2;57keystore.setEntry(ALIAS,58new KeyStore.SecretKeyEntry(convertPassword(USER_PASSWORD), attrs),59new KeyStore.PasswordProtection(PASSWORD));6061try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {62System.out.println("Storing keystore to: " + KEYSTORE);63keystore.store(outStream, PASSWORD);64}6566try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {67System.out.println("Loading keystore from: " + KEYSTORE);68keystore.load(inStream, PASSWORD);69System.out.println("Loaded keystore with " + keystore.size() +70" entries");71}7273KeyStore.Entry entry = keystore.getEntry(ALIAS,74new KeyStore.PasswordProtection(PASSWORD));75int attrCount = entry.getAttributes().size();76System.out.println("Retrieved entry with " + attrCount + " attrs: " +77entry);78if (attrCount != originalAttrCount) {79throw new Exception("Failed to recover all the entry attributes");80}8182SecretKey key = (SecretKey) keystore.getKey(ALIAS, PASSWORD);83SecretKeyFactory factory =84SecretKeyFactory.getInstance(key.getAlgorithm());85PBEKeySpec keySpec =86(PBEKeySpec) factory.getKeySpec(key, PBEKeySpec.class);87char[] pwd = keySpec.getPassword();88System.out.println("Recovered credential: " + new String(pwd));8990if (!Arrays.equals(USER_PASSWORD.toCharArray(), pwd)) {91throw new Exception("Failed to recover the stored password");92}93}9495private static SecretKey convertPassword(String password)96throws NoSuchAlgorithmException, InvalidKeySpecException {97SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");98return factory.generateSecret(new PBEKeySpec(password.toCharArray()));99}100}101102103