Path: blob/master/test/jdk/java/security/KeyStore/TestKeyStoreEntry.java
41149 views
/*1* Copyright (c) 2001, 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*/2223import static java.lang.System.out;2425import java.io.FileInputStream;26import java.io.FileOutputStream;27import java.security.Key;28import java.security.KeyStore;29import java.security.Provider;30import java.security.Security;31import javax.crypto.KeyGenerator;32import javax.crypto.SecretKey;3334/*35* @test36* @bug 804862137* @summary Test the basic operations of KeyStore entry, provided by SunJCE38* (jceks)39* @author Yu-Ching Valerie PENG40*/4142public class TestKeyStoreEntry {43private static final char[] PASSWDK = new char[] {44't', 'e', 'r', 'c', 'e', 's'45};46private static final char[] PASSWDF = new String("guardian Angel")47.toCharArray();48private static final String[] KS_ALGOS = {49"DES", "DESede", "Blowfish"50};51private static final int NUM_ALGOS = KS_ALGOS.length;5253private final SecretKey[] sks = new SecretKey[NUM_ALGOS];5455TestKeyStoreEntry() throws Exception {56// generate secret keys which are to be stored in the jce57// key store object58KeyGenerator[] kgs = new KeyGenerator[NUM_ALGOS];59for (int i = 0; i < NUM_ALGOS; i++) {60kgs[i] = KeyGenerator.getInstance(KS_ALGOS[i], "SunJCE");61sks[i] = kgs[i].generateKey();62}6364}6566public static void main(String args[]) throws Exception {67TestKeyStoreEntry jstest = new TestKeyStoreEntry();68jstest.run();69}7071public void run() throws Exception {72try (FileOutputStream fos = new FileOutputStream("jceks");73FileInputStream fis = new FileInputStream("jceks");) {7475KeyStore ks = KeyStore.getInstance("jceks");76// create an empty key store77ks.load(null, null);7879// store the secret keys80String aliasHead = new String("secretKey");81for (int j = 0; j < NUM_ALGOS; j++) {82ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);83}8485// write the key store out to a file86ks.store(fos, PASSWDF);87// wipe clean the existing key store88for (int k = 0; k < NUM_ALGOS; k++) {89ks.deleteEntry(aliasHead + k);90}91if (ks.size() != 0) {92throw new RuntimeException("ERROR: re-initialization failed");93}9495// reload the key store with the file96ks.load(fis, PASSWDF);9798// check the integrity/validaty of the key store99Key temp = null;100String alias = null;101if (ks.size() != NUM_ALGOS) {102throw new RuntimeException("ERROR: wrong number of key"103+ " entries");104}105106for (int m = 0; m < ks.size(); m++) {107alias = aliasHead + m;108temp = ks.getKey(alias, PASSWDK);109// compare the keys110if (!temp.equals(sks[m])) {111throw new RuntimeException("ERROR: key comparison (" + m112+ ") failed");113}114// check the type of key115if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {116throw new RuntimeException("ERROR: type identification ("117+ m + ") failed");118}119}120}121}122123}124125126