Path: blob/master/test/jdk/sun/security/pkcs12/P12SecretKey.java
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8149411 800763226* @summary Get AES key from keystore (uses SecretKeySpec not SecretKeyFactory)27* @run main P12SecretKey pkcs12 AES 12828* @run main P12SecretKey pkcs12 DES 5629* @run main P12SecretKey pkcs12 DESede 16830*/3132import java.io.File;33import java.io.FileInputStream;34import java.io.FileOutputStream;35import java.security.KeyStore;36import java.security.cert.CertificateException;37import java.util.Arrays;3839import javax.crypto.KeyGenerator;40import javax.crypto.SecretKey;4142public class P12SecretKey {4344private static final String ALIAS = "alias";4546public static void main(String[] args) throws Exception {47P12SecretKey testp12 = new P12SecretKey();48String keystoreType = args[0];49String algName = args[1];50int keySize = Integer.parseInt(args[2]);51testp12.run(keystoreType, algName, keySize);52}5354private void run(String keystoreType, String algName, int keySize) throws Exception {55char[] pw = "password".toCharArray();56KeyStore ks = KeyStore.getInstance(keystoreType);57ks.load(null, pw);5859KeyGenerator kg = KeyGenerator.getInstance(algName);60kg.init(keySize);61SecretKey key = kg.generateKey();6263KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key);64KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw);65ks.setEntry(ALIAS, ske, kspp);6667File ksFile = File.createTempFile("test", ".test");68try (FileOutputStream fos = new FileOutputStream(ksFile)) {69ks.store(fos, pw);70fos.flush();71}7273// now see if we can get it back74try (FileInputStream fis = new FileInputStream(ksFile)) {75KeyStore ks2 = KeyStore.getInstance(keystoreType);76ks2.load(fis, pw);77KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp);78SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey();79if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) {80System.err.println("OK: worked just fine with " + keystoreType +81" keystore");82} else {83System.err.println("ERROR: keys are NOT equal after storing in "84+ keystoreType + " keystore");85}86}87}88}899091