1 /* 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import static java.lang.System.out; 25 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.security.Key; 29 import java.security.KeyStore; 30 import java.security.Provider; 31 import java.security.Security; 32 import javax.crypto.KeyGenerator; 33 import javax.crypto.SecretKey; 34 35 /* 36 * @test 37 * @bug 8048621 38 * @summary Test the basic operations of KeyStore entry, provided by SunJCE 39 * (jceks) 40 * @author Yu-Ching Valerie PENG 41 */ 42 43 public class TestKeyStoreEntry { 44 private static final char[] PASSWDK = new char[] { 45 't', 'e', 'r', 'c', 'e', 's' 46 }; 47 private static final char[] PASSWDF = new String("guardian Angel") 48 .toCharArray(); 49 private static final String[] KS_ALGOS = { 50 "DES", "DESede", "Blowfish" 51 }; 52 private static final int NUM_ALGOS = KS_ALGOS.length; 53 54 private static final String[] KS_TYPE = { 55 "jks", "jceks", "pkcs12", "PKCS11KeyStore" 56 }; 57 private static final String[] PRO_TYPE = { 58 "SUN", "SunJCE", "SunJSSE" 59 }; 60 61 private final SecretKey[] sks = new SecretKey[NUM_ALGOS]; 62 63 TestKeyStoreEntry() throws Exception { 64 // generate secret keys which are to be stored in the jce 65 // key store object 66 KeyGenerator[] kgs = new KeyGenerator[NUM_ALGOS]; 67 for (int i = 0; i < NUM_ALGOS; i++) { 68 kgs[i] = KeyGenerator.getInstance(KS_ALGOS[i], "SunJCE"); 69 sks[i] = kgs[i].generateKey(); 70 } 71 72 } 73 74 public static void main(String args[]) throws Exception { 75 TestKeyStoreEntry jstest = new TestKeyStoreEntry(); 76 jstest.run(); 77 } 78 79 public void run() throws Exception { 80 81 Provider[] providers = Security.getProviders(); 82 for (Provider p: providers) { 83 String prvName = p.getName(); 84 if (prvName.startsWith("SunJCE")) { 85 try { 86 runTest(p); 87 out.println("Test with provider " + p.getName() + "" 88 + " passed"); 89 90 } catch (java.security.KeyStoreException e) { 91 throw e; 92 } 93 } 94 } 95 } 96 97 public void runTest(Provider p) throws Exception { 98 try (FileOutputStream fos = new FileOutputStream("jceks"); 99 FileInputStream fis = new FileInputStream("jceks");) { 100 101 KeyStore ks = KeyStore.getInstance("jceks", p); 102 // create an empty key store 103 ks.load(null, null); 104 105 // store the secret keys 106 String aliasHead = new String("secretKey"); 107 for (int j = 0; j < NUM_ALGOS; j++) { 108 ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null); 109 } 110 111 // write the key store out to a file 112 ks.store(fos, PASSWDF); 113 // wipe clean the existing key store 114 for (int k = 0; k < NUM_ALGOS; k++) { 115 ks.deleteEntry(aliasHead + k); 116 } 117 if (ks.size() != 0) { 118 throw new RuntimeException("ERROR: re-initialization failed"); 119 } 120 121 // reload the key store with the file 122 ks.load(fis, PASSWDF); 123 124 // check the integrity/validaty of the key store 125 Key temp = null; 126 String alias = null; 127 if (ks.size() != NUM_ALGOS) { 128 throw new RuntimeException("ERROR: wrong number of key" 129 + " entries"); 130 } 131 132 for (int m = 0; m < ks.size(); m++) { 133 alias = aliasHead + m; 134 temp = ks.getKey(alias, PASSWDK); 135 // compare the keys 136 if (!temp.equals(sks[m])) { 137 throw new RuntimeException("ERROR: key comparison (" + m 138 + ") failed"); 139 } 140 // check the type of key 141 if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) { 142 throw new RuntimeException("ERROR: type identification (" 143 + m + ") failed"); 144 } 145 } 146 } 147 } 148 149 }