1 /* 2 * Copyright (c) 2001, 2020, 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 final SecretKey[] sks = new SecretKey[NUM_ALGOS]; 55 56 TestKeyStoreEntry() throws Exception { 57 // generate secret keys which are to be stored in the jce 58 // key store object 59 KeyGenerator[] kgs = new KeyGenerator[NUM_ALGOS]; 60 for (int i = 0; i < NUM_ALGOS; i++) { 61 kgs[i] = KeyGenerator.getInstance(KS_ALGOS[i], "SunJCE"); 62 sks[i] = kgs[i].generateKey(); 63 } 64 65 } 66 67 public static void main(String args[]) throws Exception { 68 TestKeyStoreEntry jstest = new TestKeyStoreEntry(); 69 jstest.run(); 70 } 71 72 public void run() throws Exception { 73 try (FileOutputStream fos = new FileOutputStream("jceks"); 74 FileInputStream fis = new FileInputStream("jceks");) { 75 76 KeyStore ks = KeyStore.getInstance("jceks"); 77 // create an empty key store 78 ks.load(null, null); 79 80 // store the secret keys 81 String aliasHead = new String("secretKey"); 82 for (int j = 0; j < NUM_ALGOS; j++) { 83 ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null); 84 } 85 86 // write the key store out to a file 87 ks.store(fos, PASSWDF); 88 // wipe clean the existing key store 89 for (int k = 0; k < NUM_ALGOS; k++) { 90 ks.deleteEntry(aliasHead + k); 91 } 92 if (ks.size() != 0) { 93 throw new RuntimeException("ERROR: re-initialization failed"); 94 } 95 96 // reload the key store with the file 97 ks.load(fis, PASSWDF); 98 99 // check the integrity/validaty of the key store 100 Key temp = null; 101 String alias = null; 102 if (ks.size() != NUM_ALGOS) { 103 throw new RuntimeException("ERROR: wrong number of key" 104 + " entries"); 105 } 106 107 for (int m = 0; m < ks.size(); m++) { 108 alias = aliasHead + m; 109 temp = ks.getKey(alias, PASSWDK); 110 // compare the keys 111 if (!temp.equals(sks[m])) { 112 throw new RuntimeException("ERROR: key comparison (" + m 113 + ") failed"); 114 } 115 // check the type of key 116 if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) { 117 throw new RuntimeException("ERROR: type identification (" 118 + m + ") failed"); 119 } 120 } 121 } 122 } 123 124 }