jdk/test/sun/security/pkcs12/StorePasswordTest.java
author vinnie
Mon, 04 Feb 2013 17:20:26 +0000
changeset 15532 859facd70580
parent 15298 06867bfe82ac
child 15538 02e547c0b530
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:
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     1
/*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     4
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     8
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    13
 * accompanied this code).
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    14
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    18
 *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    21
 * questions.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    22
 */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    23
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    24
/*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    25
 * @test
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    26
 * @bug 8005408
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    27
 * @summary KeyStore API enhancements
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    28
 */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    29
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    30
import java.io.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    31
import java.security.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    32
import java.util.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    33
import javax.crypto.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    34
import javax.crypto.spec.*;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    35
import java.security.spec.InvalidKeySpecException;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    36
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    37
// Store a password in a keystore and retrieve it again.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    38
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    39
public class StorePasswordTest {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    40
    private final static String DIR = System.getProperty("test.src", ".");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    41
    private static final char[] PASSWORD = "passphrase".toCharArray();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    42
    private static final String KEYSTORE = "pwdstore.p12";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    43
    private static final String ALIAS = "my password";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    44
    private static final String USER_PASSWORD = "hello1";
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    45
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    47
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    48
        new File(KEYSTORE).delete();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    49
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    50
        KeyStore keystore = KeyStore.getInstance("PKCS12");
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    51
        keystore.load(null, null);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    52
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    53
        // Set entry
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    54
        keystore.setEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    55
            new KeyStore.SecretKeyEntry(convertPassword(USER_PASSWORD)),
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    56
                new KeyStore.PasswordProtection(PASSWORD));
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    57
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    58
        try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    59
            System.out.println("Storing keystore to: " + KEYSTORE);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    60
            keystore.store(outStream, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    61
        }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    62
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    63
        try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    64
            System.out.println("Loading keystore from: " + KEYSTORE);
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    65
            keystore.load(inStream, PASSWORD);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    66
            System.out.println("Loaded keystore with " + keystore.size() +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    67
                " entries");
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    68
        }
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    69
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    70
        KeyStore.Entry entry = keystore.getEntry(ALIAS,
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    71
            new KeyStore.PasswordProtection(PASSWORD));
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    72
        System.out.println("Retrieved entry: " + entry);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    73
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    74
        SecretKey key = (SecretKey) keystore.getKey(ALIAS, PASSWORD);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    75
        SecretKeyFactory factory =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    76
            SecretKeyFactory.getInstance(key.getAlgorithm());
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    77
        PBEKeySpec keySpec =
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    78
            (PBEKeySpec) factory.getKeySpec(key, PBEKeySpec.class);
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    79
        char[] pwd = keySpec.getPassword();
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    80
        System.out.println("Recovered credential: " + new String(pwd));
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    81
15532
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    82
        if (!Arrays.equals(USER_PASSWORD.toCharArray(), pwd)) {
859facd70580 8006994: Cleanup PKCS12 tests to ensure streams get closed
vinnie
parents: 15298
diff changeset
    83
            throw new Exception("Failed to recover the stored password");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    84
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    85
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    86
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    87
    private static SecretKey convertPassword(String password)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    88
        throws NoSuchAlgorithmException, InvalidKeySpecException {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    89
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    90
        return factory.generateSecret(new PBEKeySpec(password.toCharArray()));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    91
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents:
diff changeset
    92
}