jdk/test/java/security/KeyStore/PBETest.java
author vinnie
Mon, 04 Feb 2013 17:20:26 +0000
changeset 15532 859facd70580
parent 15301 215128369cab
child 15661 282a9cfb26ca
permissions -rw-r--r--
8006994: Cleanup PKCS12 tests to ensure streams get closed Reviewed-by: mullan
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.*;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    32
import javax.crypto.spec.*;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    33
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    34
// Retrieve a keystore entry, protected by the default encryption algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    35
// Set the keystore entry, protected by a stronger encryption algorithm.
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    36
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    37
public class PBETest {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    38
    private final static String DIR = System.getProperty("test.src", ".");
15301
215128369cab 8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents: 15297
diff changeset
    39
    //private static final String PBE_ALGO = "PBEWithHmacSHA1AndAES_128";
215128369cab 8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents: 15297
diff changeset
    40
    private static final String PBE_ALGO = "PBEWithSHA1AndDESede";
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    41
    private static final char[] PASSWORD = "passphrase".toCharArray();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    42
    private static final String KEYSTORE_TYPE = "JKS";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    43
    private static final String KEYSTORE = DIR + "/keystore.jks";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    44
    private static final String NEW_KEYSTORE_TYPE = "PKCS12";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    45
    private static final String NEW_KEYSTORE = PBE_ALGO + ".p12";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    46
    private static final String ALIAS = "vajra";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    47
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    48
    private static final byte[] IV = {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    49
        0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    50
        0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    51
    };
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    52
    private static final byte[] SALT = {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    53
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    54
    };
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    55
    private static final int ITERATION_COUNT = 1024;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    56
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    58
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    59
        new File(NEW_KEYSTORE).delete();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    60
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    61
        KeyStore keystore = load(KEYSTORE_TYPE, KEYSTORE, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    62
        KeyStore.Entry entry =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    63
            keystore.getEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    64
                new KeyStore.PasswordProtection(PASSWORD));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    65
        System.out.println("Retrieved entry named '" + ALIAS + "'");
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    66
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    67
        // Set entry
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    68
        KeyStore keystore2 = load(NEW_KEYSTORE_TYPE, null, null);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    69
        keystore2.setEntry(ALIAS, entry,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    70
            new KeyStore.PasswordProtection(PASSWORD, PBE_ALGO,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    71
                new PBEParameterSpec(SALT, ITERATION_COUNT,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    72
                    new IvParameterSpec(IV))));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    73
        System.out.println("Encrypted entry using: " + PBE_ALGO);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    74
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    75
        try (FileOutputStream outStream = new FileOutputStream(NEW_KEYSTORE)) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    76
            System.out.println("Storing keystore to: " + NEW_KEYSTORE);
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    77
            keystore2.store(outStream, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    78
        }
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
        keystore2 = load(NEW_KEYSTORE_TYPE, NEW_KEYSTORE, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    81
        entry = keystore2.getEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    82
            new KeyStore.PasswordProtection(PASSWORD));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    83
        System.out.println("Retrieved entry named '" + ALIAS + "'");
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    84
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    85
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    86
    private static KeyStore load(String type, String path, char[] password)
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    87
        throws Exception {
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    88
        KeyStore keystore = KeyStore.getInstance(type);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    89
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
    90
        if (path != null) {
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    91
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    92
            try (FileInputStream inStream = new FileInputStream(path)) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    93
                System.out.println("Loading keystore from: " + path);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    94
                keystore.load(inStream, password);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    95
                System.out.println("Loaded keystore with " + keystore.size() +
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    96
                    " entries");
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    97
            }
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    98
        } else {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15301
diff changeset
    99
            keystore.load(null, null);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   100
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   101
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   102
        return keystore;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   103
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents:
diff changeset
   104
}