test/jdk/sun/security/pkcs12/P12SecretKey.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35958
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     1
/*
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     4
 *
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     8
 *
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    13
 * accompanied this code).
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    14
 *
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    18
 *
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    21
 * questions.
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    22
 */
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    23
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    24
/*
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    25
 * @test
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    26
 * @bug 8149411
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    27
 * @summary Get AES key from keystore (uses SecretKeySpec not SecretKeyFactory)
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    28
 */
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    29
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    30
import java.io.File;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    31
import java.io.FileInputStream;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    32
import java.io.FileOutputStream;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    33
import java.security.KeyStore;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    34
import java.security.cert.CertificateException;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    35
import java.util.Arrays;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    36
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    37
import javax.crypto.KeyGenerator;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    38
import javax.crypto.SecretKey;
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    39
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    40
public class P12SecretKey {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    41
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    42
    private static final String ALIAS = "alias";
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    43
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    45
        P12SecretKey testp12 = new P12SecretKey();
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    46
        String keystoreType = "pkcs12";
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    47
        if (args != null && args.length > 0) {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    48
            keystoreType = args[0];
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    49
        }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    50
        testp12.run(keystoreType);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    51
    }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    52
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    53
    private void run(String keystoreType) throws Exception {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    54
        char[] pw = "password".toCharArray();
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    55
        KeyStore ks = KeyStore.getInstance(keystoreType);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    56
        ks.load(null, pw);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    57
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    58
        KeyGenerator kg = KeyGenerator.getInstance("AES");
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    59
        kg.init(128);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    60
        SecretKey key = kg.generateKey();
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    61
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    62
        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    63
        KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    64
        ks.setEntry(ALIAS, ske, kspp);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    65
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    66
        File ksFile = File.createTempFile("test", ".test");
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    67
        try (FileOutputStream fos = new FileOutputStream(ksFile)) {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    68
            ks.store(fos, pw);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    69
            fos.flush();
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    70
        }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    71
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    72
        // now see if we can get it back
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    73
        try (FileInputStream fis = new FileInputStream(ksFile)) {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    74
            KeyStore ks2 = KeyStore.getInstance(keystoreType);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    75
            ks2.load(fis, pw);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    76
            KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp);
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    77
            SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey();
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    78
            if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    79
                System.err.println("OK: worked just fine with " + keystoreType +
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    80
                                   " keystore");
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    81
            } else {
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    82
                System.err.println("ERROR: keys are NOT equal after storing in "
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    83
                                   + keystoreType + " keystore");
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    84
            }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    85
        }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    86
    }
31866c4c84d3 8149411: PKCS12KeyStore cannot extract AES Secret Keys
vinnie
parents:
diff changeset
    87
}