1 4 package org.ejbca.ui.cli; 5 6 import java.io.BufferedReader ; 7 import java.io.InputStreamReader ; 8 import java.security.KeyPair ; 9 import java.security.PrivateKey ; 10 import java.security.Signature ; 11 import java.security.interfaces.RSAKey ; 12 import java.util.Arrays ; 13 import java.util.Enumeration ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import javax.crypto.Cipher; 18 19 class KeyStoreContainerTest { 20 final private String alias; 21 final private KeyPair keyPair; 22 final private String providerName; 23 private KeyStoreContainerTest(String a, KeyPair kp, String pn) { 24 this.alias = a; 25 this.keyPair = kp; 26 this.providerName = pn; 27 } 28 private void doIt(int i) throws Exception { 29 signTest(i); 30 cryptTest(i); 31 } 32 private void cryptTest(int i) throws Exception { 33 final String 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 )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 pkcs1Padding="RSA/ECB/PKCS1Padding"; 38 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 (original)+'\"'); 58 System.out.println("Decoded: \""+new String (decoded)+'\"'); 59 } 60 } 61 private void signTest(int i) throws Exception { 62 final String sigAlgName = "SHA1withRSA"; 63 final byte signInput[] = "Lillan gick på vägen ut.".getBytes(); 64 final byte signBA[]; { 65 Signature signature = Signature.getInstance(sigAlgName, this.providerName); 66 signature.initSign( this.keyPair.getPrivate() ); 67 signature.update( signInput ); 68 signBA = signature.sign(); 69 } 70 { 71 Signature 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 providerName, 85 final String encryptProviderClassName, 86 final String keyStoreType, 87 final String storeID) throws Exception { 88 KeyStoreContainer keyStore = null; 89 while( keyStore==null ) { 90 try { 91 keyStore = new KeyStoreContainer(keyStoreType, providerName, 92 encryptProviderClassName, storeID); 93 } catch( Throwable t ) { 94 t.printStackTrace(System.err); 95 System.err.println("Card set not preloaded. Hit return when error fixed"); 96 new BufferedReader (new InputStreamReader (System.in)).readLine(); 97 } 98 } 99 return keyStore; 100 } 101 private static KeyStoreContainerTest[] getTests(final KeyStoreContainer keyStore) throws Exception { 102 Enumeration e = keyStore.getKeyStore().aliases(); 103 Set testSet = new HashSet (); 104 while( e.hasMoreElements() ) { 105 String alias = (String ) e.nextElement(); 106 if ( keyStore.getKeyStore().isKeyEntry(alias) ) { 107 PrivateKey privateKey = (PrivateKey )keyStore.getKey(alias); 108 testSet.add(new KeyStoreContainerTest(alias, 109 new KeyPair (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 providerClassName, 116 final String encryptProviderClassName, 117 final String keyStoreType, 118 final String storeID, 119 final int nrOfTests) throws Exception { 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 t ) { 131 tests = null; 132 t.printStackTrace(System.err); 133 } 134 } 135 } 136 } | Popular Tags |