1 21 22 package net.sourceforge.jcetaglib.test; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 import net.sourceforge.jcetaglib.lib.Keystore; 27 import net.sourceforge.jcetaglib.lib.X509Cert; 28 29 import java.security.KeyPair ; 30 import java.security.cert.X509Certificate ; 31 32 42 public class KeyTest extends TestCase { 43 44 49 public void testGenerateKeys() throws Exception { 50 for (int i = 0; i < RunTest.alg.length; i++) { 51 String fileloc = RunTest.TEMPFOLDER + RunTest.alg[i][0] + "_" + RunTest.alg[i][1] + ".key"; 52 Keystore.generateKey(RunTest.alg[i][0] 53 , Integer.parseInt(RunTest.alg[i][1]) 54 , null 55 , fileloc 56 , new StringBuffer ("password")); 57 } 58 } 59 60 65 public void testGenerateCertificates() throws Exception { 66 67 KeyPair kp = X509Cert.generateKeyPair("RSA", 1024, null); 69 70 X509Certificate cacert = X509Cert.selfsign(kp.getPrivate() 72 , kp.getPublic() 73 , "MD5WithRSAEncryption" 74 , 365 75 , "C=BE, O=NET, OU=Sourceforge, CN=CertAuthority, EmailAddress=info@certauthority.org" 76 , true 77 , null); 78 79 X509Cert.saveAsP12(cacert, cacert, kp.getPrivate(), RunTest.TEMPFOLDER + "ca.p12", "ca", new StringBuffer ("password")); 81 82 KeyPair kpAlice = X509Cert.generateKeyPair("RSA", 1024, null); 84 85 X509Certificate alicecert = X509Cert.sign(kpAlice.getPublic() 87 , kp.getPrivate() 88 , cacert 89 , "MD5WithRSAEncryption" 90 , 365 91 , "C=BE, O=NET, OU=Sourceforge, CN=Alice, EmailAddress=alice@sourceforge.net" 92 , true 93 , null 94 , "client"); 95 96 X509Cert.saveAsP12(alicecert, cacert, kpAlice.getPrivate(), RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 98 99 KeyPair kpBob = X509Cert.generateKeyPair("RSA", 1024, null); 101 102 X509Certificate bobcert = X509Cert.sign(kpBob.getPublic() 104 , kp.getPrivate() 105 , cacert 106 , "MD5WithRSAEncryption" 107 , 365 108 , "C=BE, O=NET, OU=Sourceforge, CN=Bob, EmailAddress=bob@sourceforge.net" 109 , true 110 , null 111 , "all"); 112 113 X509Cert.saveAsP12(bobcert, cacert, kpBob.getPrivate(), RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 115 } 116 117 122 public void testReadCertificates() throws Exception { 123 X509Certificate alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer ("password")); 125 126 alicecert.checkValidity(); 127 128 Assert.assertEquals(alicecert.getSubjectDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=Alice,E=alice@sourceforge.net"); 129 Assert.assertEquals(alicecert.getSigAlgName(), "MD5WithRSAEncryption"); 130 Assert.assertEquals(alicecert.getIssuerDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=CertAuthority,E=info@certauthority.org"); 131 132 X509Certificate bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer ("password")); 134 135 bobcert.checkValidity(); 136 137 Assert.assertEquals(bobcert.getSubjectDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=Bob,E=bob@sourceforge.net"); 138 Assert.assertEquals(bobcert.getSigAlgName(), "MD5WithRSAEncryption"); 139 Assert.assertEquals(bobcert.getIssuerDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=CertAuthority,E=info@certauthority.org"); 140 } 141 } 142 | Popular Tags |