< prev index next >

test/jdk/java/security/KeyStore/TestKeyStoreEntry.java

Print this page
rev 59383 : [mq]: final
   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), and SunPKCS11-Solaris(PKCS11KeyStore)
  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", "SunPKCS11-Solaris"
  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                     || prvName.startsWith("SunPKCS11-Solaris")) {
  86                 try {
  87                     runTest(p);
  88                     out.println("Test with provider " + p.getName() + ""
  89                             + " passed");
  90 
  91                 } catch (java.security.KeyStoreException e) {
  92                     if (prvName.startsWith("SunPKCS11-Solaris")) {
  93                         out.println("KeyStoreException is expected because "
  94                                 + "PKCS11KeyStore is invalid keystore type.");
  95                         e.printStackTrace();
  96                     } else {
  97                         throw e;
  98                     }
  99                 }
 100             }
 101         }
 102     }
 103 
 104     public void runTest(Provider p) throws Exception {
 105         try (FileOutputStream fos = new FileOutputStream("jceks");
 106                 FileInputStream fis = new FileInputStream("jceks");) {
 107 
 108             KeyStore ks = KeyStore.getInstance("jceks", p);
 109             // create an empty key store
 110             ks.load(null, null);
 111 
 112             // store the secret keys
 113             String aliasHead = new String("secretKey");
 114             for (int j = 0; j < NUM_ALGOS; j++) {
 115                 ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);
 116             }
 117 
 118             // write the key store out to a file
 119             ks.store(fos, PASSWDF);
 120             // wipe clean the existing key store
 121             for (int k = 0; k < NUM_ALGOS; k++) {
 122                 ks.deleteEntry(aliasHead + k);
 123             }
 124             if (ks.size() != 0) {
 125                 throw new RuntimeException("ERROR: re-initialization failed");
 126             }
 127 
 128             // reload the key store with the file


   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


< prev index next >