< prev index next >

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

Print this page
rev 59107 : imported patch security


  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




  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


< prev index next >