jdk/test/java/security/KeyStore/PBETest.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 15661 282a9cfb26ca
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     1
/*
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     4
 *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     8
 *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    13
 * accompanied this code).
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    14
 *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    18
 *
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    21
 * questions.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    22
 */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    23
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    24
/*
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    25
 * @test
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    26
 * @bug 8006591
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    27
 * @summary Protect keystore entries using stronger PBE algorithms
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    28
 */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    29
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    30
import java.io.*;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    31
import java.security.*;
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    32
import javax.crypto.*;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    33
import javax.crypto.spec.*;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    34
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    35
// Retrieve a keystore entry, protected by the default encryption algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    36
// Set the keystore entry, protected by a stronger encryption algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    37
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    38
public class PBETest {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    39
    private final static String DIR = System.getProperty("test.src", ".");
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    40
    private final static String KEY_PROTECTION_PROPERTY =
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    41
        "keystore.PKCS12.keyProtectionAlgorithm";
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    42
    private static final String[] PBE_ALGOS = {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    43
        "PBEWithSHA1AndDESede",
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    44
        "PBEWithHmacSHA1AndAES_128",
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    45
        "PBEWithHmacSHA224AndAES_128",
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    46
        "PBEWithHmacSHA256AndAES_128",
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    47
        "PBEWithHmacSHA384AndAES_128",
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    48
        "PBEWithHmacSHA512AndAES_128"
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    49
    };
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    50
    private static final char[] PASSWORD = "passphrase".toCharArray();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    51
    private static final String KEYSTORE_TYPE = "JKS";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    52
    private static final String KEYSTORE = DIR + "/keystore.jks";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    53
    private static final String NEW_KEYSTORE_TYPE = "PKCS12";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    54
    private static final String ALIAS = "vajra";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    55
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    56
    private static final byte[] IV = {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    57
        0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    58
        0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    59
    };
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    60
    private static final byte[] SALT = {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    61
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    62
    };
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    63
    private static final int ITERATION_COUNT = 1024;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    64
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    66
        for (String algo : PBE_ALGOS) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    67
            String filename = algo + ".p12";
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    68
            main0(algo, filename, true);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    69
            main0(algo, filename, false);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    70
            Security.setProperty(KEY_PROTECTION_PROPERTY, algo);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    71
            main0(null, "PBE.p12", false);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    72
            Security.setProperty(KEY_PROTECTION_PROPERTY, "");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    73
        }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    74
        main0(null, "default.p12", false); // default algorithm
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    75
    }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    76
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    77
    private static void main0(String algo, String filename, boolean useParams)
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    78
        throws Exception {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    79
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    80
        KeyStore keystore = load(KEYSTORE_TYPE, KEYSTORE, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    81
        KeyStore.Entry entry =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    82
            keystore.getEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    83
                new KeyStore.PasswordProtection(PASSWORD));
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    84
        System.out.println("Retrieved key entry named '" + ALIAS + "'");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    85
        Key originalKey = null;
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    86
        if (entry instanceof KeyStore.PrivateKeyEntry) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    87
            originalKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    88
        } else if (entry instanceof KeyStore.SecretKeyEntry) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    89
            originalKey = ((KeyStore.SecretKeyEntry) entry).getSecretKey();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    90
        }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    91
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    92
        // Set entry
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    93
        KeyStore keystore2 = load(NEW_KEYSTORE_TYPE, null, null);
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    94
        if (useParams) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    95
            keystore2.setEntry(ALIAS, entry,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    96
                new KeyStore.PasswordProtection(PASSWORD, algo,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    97
                    new PBEParameterSpec(SALT, ITERATION_COUNT,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    98
                        new IvParameterSpec(IV))));
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
    99
            System.out.println("Encrypted key entry using: " + algo +
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   100
                " (with PBE parameters)");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   101
        } else if (algo != null) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   102
            keystore2.setEntry(ALIAS, entry,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   103
                new KeyStore.PasswordProtection(PASSWORD, algo, null));
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   104
            System.out.println("Encrypted key entry using: " + algo +
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   105
                " (without PBE parameters)");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   106
        } else {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   107
            keystore2.setEntry(ALIAS, entry,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   108
                new KeyStore.PasswordProtection(PASSWORD));
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   109
            String prop = Security.getProperty(KEY_PROTECTION_PROPERTY);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   110
            if (prop == null || prop.isEmpty()) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   111
                System.out.println("Encrypted key entry using: " +
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   112
                    "default PKCS12 key protection algorithm");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   113
            } else {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   114
                System.out.println("Encrypted key entry using: " +
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   115
                    "keyProtectionAlgorithm=" + prop);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   116
            }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   117
        }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   118
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   119
        try (FileOutputStream outStream = new FileOutputStream(filename)) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   120
            System.out.println("Storing keystore to: " + filename);
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   121
            keystore2.store(outStream, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   122
        }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   123
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   124
        try {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   125
            keystore2 = load(NEW_KEYSTORE_TYPE, filename, PASSWORD);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   126
            entry = keystore2.getEntry(ALIAS,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   127
                new KeyStore.PasswordProtection(PASSWORD));
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   128
            Key key;
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   129
            if (entry instanceof KeyStore.PrivateKeyEntry) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   130
                key = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   131
            } else if (entry instanceof KeyStore.SecretKeyEntry) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   132
                key = ((KeyStore.SecretKeyEntry) entry).getSecretKey();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   133
            } else {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   134
                throw new Exception("Failed to retrieve key entry");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   135
            }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   136
            if (originalKey.equals(key)) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   137
                System.out.println("Retrieved key entry named '" + ALIAS + "'");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   138
                System.out.println();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   139
            } else {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   140
                throw new Exception(
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   141
                    "Failed: recovered key does not match the original key");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   142
            }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   143
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   144
        } finally {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   145
            new File(filename).delete();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15532
diff changeset
   146
        }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   147
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   148
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   149
    private static KeyStore load(String type, String path, char[] password)
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   150
        throws Exception {
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   151
        KeyStore keystore = KeyStore.getInstance(type);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   152
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   153
        if (path != null) {
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   154
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   155
            try (FileInputStream inStream = new FileInputStream(path)) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   156
                System.out.println("Loading keystore from: " + path);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   157
                keystore.load(inStream, password);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   158
                System.out.println("Loaded keystore with " + keystore.size() +
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   159
                    " entries");
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   160
            }
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   161
        } else {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
   162
            keystore.load(null, null);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   163
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   164
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   165
        return keystore;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   166
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   167
}