PBETest.java revision 6426:0c86df653029
1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8006591
27 * @summary Protect keystore entries using stronger PBE algorithms
28 */
29
30import java.io.*;
31import java.security.*;
32import javax.crypto.spec.*;
33
34// Retrieve a keystore entry, protected by the default encryption algorithm.
35// Set the keystore entry, protected by a stronger encryption algorithm.
36
37public class PBETest {
38    private final static String DIR = System.getProperty("test.src", ".");
39    private static final String PBE_ALGO = "PBEWithHmacSHA1AndAES_128";
40    private static final char[] PASSWORD = "passphrase".toCharArray();
41    private static final String KEYSTORE_TYPE = "JKS";
42    private static final String KEYSTORE = DIR + "/keystore.jks";
43    private static final String NEW_KEYSTORE_TYPE = "PKCS12";
44    private static final String NEW_KEYSTORE = PBE_ALGO + ".p12";
45    private static final String ALIAS = "vajra";
46
47    private static final byte[] IV = {
48        0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
49        0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
50    };
51    private static final byte[] SALT = {
52        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
53    };
54    private static final int ITERATION_COUNT = 1024;
55
56    public static void main(String[] args) throws Exception {
57
58        new File(NEW_KEYSTORE).delete();
59
60        try {
61            KeyStore keystore = load(KEYSTORE_TYPE, KEYSTORE, PASSWORD);
62            KeyStore.Entry entry =
63                keystore.getEntry(ALIAS,
64                    new KeyStore.PasswordProtection(PASSWORD));
65            System.out.println("Retrieved entry named '" + ALIAS + "'");
66
67            // Set entry
68            KeyStore keystore2 = load(NEW_KEYSTORE_TYPE, null, null);
69            keystore2.setEntry(ALIAS, entry,
70                new KeyStore.PasswordProtection(PASSWORD, PBE_ALGO,
71                    new PBEParameterSpec(SALT, ITERATION_COUNT,
72                        new IvParameterSpec(IV))));
73            System.out.println("Encrypted entry using: " + PBE_ALGO);
74
75            System.out.println("Storing keystore to: " + NEW_KEYSTORE);
76            keystore2.store(new FileOutputStream(NEW_KEYSTORE), PASSWORD);
77
78            keystore2 = load(NEW_KEYSTORE_TYPE, NEW_KEYSTORE, PASSWORD);
79            entry = keystore2.getEntry(ALIAS,
80                new KeyStore.PasswordProtection(PASSWORD));
81            System.out.println("Retrieved entry named '" + ALIAS + "'");
82
83        } finally {
84            new File(NEW_KEYSTORE).delete();
85            System.out.println("Deleted keystore: " + NEW_KEYSTORE);
86        }
87    }
88
89    private static KeyStore load(String type, String path, char[] password)
90        throws Exception {
91
92        FileInputStream stream = null;
93        if (path != null) {
94            stream = new FileInputStream(path);
95        }
96        KeyStore keystore = KeyStore.getInstance(type);
97        System.out.println("Loading keystore from: " + path);
98        keystore.load(stream, password);
99
100        return keystore;
101    }
102}
103