Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/TestKeyStoreEntry.java
38811 views
/*1* Copyright (c) 2001, 2016, 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), and SunPKCS11-Solaris(PKCS11KeyStore)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 static final String[] KS_TYPE = {54"jks", "jceks", "pkcs12", "PKCS11KeyStore"55};56private static final String[] PRO_TYPE = {57"SUN", "SunJCE", "SunJSSE", "SunPKCS11-Solaris"58};5960private final SecretKey[] sks = new SecretKey[NUM_ALGOS];6162TestKeyStoreEntry() throws Exception {63// generate secret keys which are to be stored in the jce64// key store object65KeyGenerator[] kgs = new KeyGenerator[NUM_ALGOS];66for (int i = 0; i < NUM_ALGOS; i++) {67kgs[i] = KeyGenerator.getInstance(KS_ALGOS[i], "SunJCE");68sks[i] = kgs[i].generateKey();69}7071}7273public static void main(String args[]) throws Exception {74TestKeyStoreEntry jstest = new TestKeyStoreEntry();75jstest.run();76}7778public void run() throws Exception {7980Provider[] providers = Security.getProviders();81for (Provider p: providers) {82String prvName = p.getName();83if (prvName.startsWith("SunJCE")84|| prvName.startsWith("SunPKCS11-Solaris")) {85try {86runTest(p);87out.println("Test with provider " + p.getName() + ""88+ " passed");8990} catch (java.security.KeyStoreException e) {91if (prvName.startsWith("SunPKCS11-Solaris")) {92out.println("KeyStoreException is expected because "93+ "PKCS11KeyStore is invalid keystore type.");94e.printStackTrace();95} else {96throw e;97}98}99}100}101}102103public void runTest(Provider p) throws Exception {104try (FileOutputStream fos = new FileOutputStream("jceks");105FileInputStream fis = new FileInputStream("jceks");) {106107KeyStore ks = KeyStore.getInstance("jceks", p);108// create an empty key store109ks.load(null, null);110111// store the secret keys112String aliasHead = new String("secretKey");113for (int j = 0; j < NUM_ALGOS; j++) {114ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);115}116117// write the key store out to a file118ks.store(fos, PASSWDF);119// wipe clean the existing key store120for (int k = 0; k < NUM_ALGOS; k++) {121ks.deleteEntry(aliasHead + k);122}123if (ks.size() != 0) {124throw new RuntimeException("ERROR: re-initialization failed");125}126127// reload the key store with the file128ks.load(fis, PASSWDF);129130// check the integrity/validaty of the key store131Key temp = null;132String alias = null;133if (ks.size() != NUM_ALGOS) {134throw new RuntimeException("ERROR: wrong number of key"135+ " entries");136}137138for (int m = 0; m < ks.size(); m++) {139alias = aliasHead + m;140temp = ks.getKey(alias, PASSWDK);141// compare the keys142if (!temp.equals(sks[m])) {143throw new RuntimeException("ERROR: key comparison (" + m144+ ") failed");145}146// check the type of key147if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {148throw new RuntimeException("ERROR: type identification ("149+ m + ") failed");150}151}152}153}154155}156157158