KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > KeyStoreContainerTest


1 /**
2  *
3  */

4 package org.ejbca.ui.cli;
5
6 import java.io.BufferedReader JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.security.KeyPair JavaDoc;
9 import java.security.PrivateKey JavaDoc;
10 import java.security.Signature JavaDoc;
11 import java.security.interfaces.RSAKey JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.crypto.Cipher;
18
19 class KeyStoreContainerTest {
20     final private String JavaDoc alias;
21     final private KeyPair JavaDoc keyPair;
22     final private String JavaDoc providerName;
23     private KeyStoreContainerTest(String JavaDoc a, KeyPair JavaDoc kp, String JavaDoc pn) {
24         this.alias = a;
25         this.keyPair = kp;
26         this.providerName = pn;
27     }
28     private void doIt(int i) throws Exception JavaDoc {
29         signTest(i);
30         cryptTest(i);
31     }
32     private void cryptTest(int i) throws Exception JavaDoc {
33         final String JavaDoc testS = " 01 0123456789 02 0123456789 03 0123456789 04 0123456789 05 0123456789 06 0123456789 07 0123456789 08 0123456789 09 0123456789 10 0123456789 11 0123456789 12 0123456789 13 0123456789 14 0123456789 15 0123456789 16 0123456789 17 0123456789 18 0123456789 19 0123456789 20 0123456789 21 0123456789 22 0123456789 23 0123456789 24 0123456789 25 0123456789 26 0123456789 27 0123456789 28 0123456789 29 0123456789 30 0123456789 31 0123456789 32 0123456789 33 0123456789 34 0123456789 35 0123456789 36 0123456789 37 0123456789";
34         final int modulusLength = ((RSAKey JavaDoc)this.keyPair.getPublic()).getModulus().bitLength();
35         final int byteLength = (modulusLength+7)/8-11;
36         final byte original[] = testS.substring(0, byteLength).getBytes();
37         final String JavaDoc pkcs1Padding="RSA/ECB/PKCS1Padding";
38 // final String noPadding="RSA/ECB/NoPadding";
39
final byte encoded[]; {
40             final Cipher cipher = Cipher.getInstance(pkcs1Padding);
41             System.out.print("encryption provider: "+cipher.getProvider());
42             cipher.init(Cipher.ENCRYPT_MODE, this.keyPair.getPublic());
43             encoded = cipher.doFinal(original);
44         }
45         final byte decoded[]; {
46             final Cipher cipher = Cipher.getInstance(pkcs1Padding, this.providerName);
47             System.out.print("; decryption provider: "+cipher.getProvider());
48             cipher.init(Cipher.DECRYPT_MODE, this.keyPair.getPrivate());
49             decoded = cipher.doFinal(encoded);
50         }
51         final boolean isSame = Arrays.equals(original, decoded);
52         System.out.print("; modulus length: "+modulusLength+"; byte length "+byteLength);
53         if (isSame)
54             System.out.println(". The docoded byte string is equal to the original!");
55         else {
56             System.out.println("The original and the decoded byte array differs!");
57             System.out.println("Original: \""+new String JavaDoc(original)+'\"');
58             System.out.println("Decoded: \""+new String JavaDoc(decoded)+'\"');
59         }
60     }
61     private void signTest(int i) throws Exception JavaDoc {
62         final String JavaDoc sigAlgName = "SHA1withRSA";
63         final byte signInput[] = "Lillan gick på vägen ut.".getBytes();
64         final byte signBA[]; {
65             Signature JavaDoc signature = Signature.getInstance(sigAlgName, this.providerName);
66             signature.initSign( this.keyPair.getPrivate() );
67             signature.update( signInput );
68             signBA = signature.sign();
69         }
70         {
71             Signature JavaDoc signature = Signature.getInstance(sigAlgName);
72             signature.initVerify(this.keyPair.getPublic());
73             signature.update(signInput);
74             boolean result = signature.verify(signBA);
75             System.out.println("Signature test of key "+this.alias+
76                                ": signature length " + signBA.length +
77                                "; test nr " + i +
78                                "; first byte " + Integer.toHexString(0xff&signBA[0]) +
79                                "; verifying " + result);
80         }
81         System.gc();
82         System.runFinalization();
83     }
84     private static KeyStoreContainer getKeyStoreTest(final String JavaDoc providerName,
85                                                      final String JavaDoc encryptProviderClassName,
86                                                      final String JavaDoc keyStoreType,
87                                                      final String JavaDoc storeID) throws Exception JavaDoc {
88         KeyStoreContainer keyStore = null;
89         while( keyStore==null ) {
90             try {
91                 keyStore = new KeyStoreContainer(keyStoreType, providerName,
92                                                  encryptProviderClassName, storeID);
93             } catch( Throwable JavaDoc t ) {
94                 t.printStackTrace(System.err);
95                 System.err.println("Card set not preloaded. Hit return when error fixed");
96                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in)).readLine();
97             }
98         }
99         return keyStore;
100     }
101     private static KeyStoreContainerTest[] getTests(final KeyStoreContainer keyStore) throws Exception JavaDoc {
102         Enumeration JavaDoc e = keyStore.getKeyStore().aliases();
103         Set JavaDoc testSet = new HashSet JavaDoc();
104         while( e.hasMoreElements() ) {
105             String JavaDoc alias = (String JavaDoc) e.nextElement();
106             if ( keyStore.getKeyStore().isKeyEntry(alias) ) {
107                 PrivateKey JavaDoc privateKey = (PrivateKey JavaDoc)keyStore.getKey(alias);
108                 testSet.add(new KeyStoreContainerTest(alias,
109                                                       new KeyPair JavaDoc(keyStore.getKeyStore().getCertificate(alias).getPublicKey(), privateKey),
110                                                       keyStore.getProviderName()));
111             }
112         }
113         return (KeyStoreContainerTest[]) testSet.toArray(new KeyStoreContainerTest[0]);
114     }
115     static void test(final String JavaDoc providerClassName,
116                      final String JavaDoc encryptProviderClassName,
117                      final String JavaDoc keyStoreType,
118                      final String JavaDoc storeID,
119                      final int nrOfTests) throws Exception JavaDoc {
120         System.out.println("Test of keystore with ID "+storeID+'.');
121         KeyStoreContainerTest tests[] = null;
122         final KeyStoreContainer keyStore = getKeyStoreTest(providerClassName, encryptProviderClassName,
123                                                            keyStoreType, storeID);
124         for (int i = 0; i<nrOfTests || nrOfTests<1; i++) {
125             try {
126                 if ( tests==null || nrOfTests==-5 )
127                     tests = getTests(keyStore);
128                 for( int j = 0; j<tests.length; j++ )
129                     tests[j].doIt(i);
130             } catch( Throwable JavaDoc t ) {
131                 tests = null;
132                 t.printStackTrace(System.err);
133             }
134         }
135     }
136 }
Popular Tags