KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > test > X509CertTest


1 /*
2   Name: X509CertTest.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18
19   (C) Copyright 2003 Gert Van Ham
20 */

21
22 package net.sourceforge.jcetaglib.test;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import net.sourceforge.jcetaglib.lib.X509Cert;
27 import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;
28 import org.bouncycastle.jce.PKCS10CertificationRequest;
29
30 import java.math.BigInteger JavaDoc;
31 import java.security.PrivateKey JavaDoc;
32 import java.security.cert.X509CRL JavaDoc;
33 import java.security.cert.X509Certificate JavaDoc;
34
35 /**
36  * A collection of X509 Certificate tests
37  * <P>
38  * NOTE: generating keys & certification tests can be found in KeyTest.java
39  * <P>
40  * These tests can be run using JUnit (http://www.junit.org)
41  *
42  * @author Gert Van Ham
43  * @author hamgert@users.sourceforge.net
44  * @author http://jcetaglib.sourceforge.net
45  * @version $Id: X509CertTest.java,v 1.3 2004/04/15 07:28:36 hamgert Exp $
46  */

47 public class X509CertTest extends TestCase {
48     private String JavaDoc pkcs10request =
49             "-----BEGIN CERTIFICATE REQUEST-----\r\nMIIBmTCCAQICAQAwXTELMAkGA1UEBhMCQkUxDDAKBgNVBAoTA25ldDEUMBIGA1UECxMLc291cmNlZm9yZ2UxDDAKBgNVBAMTA2JvYjEcMBoGA1UEChMTYm9iQHNvdXJjZWZvcmdlLm5ldDCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAjgiky6GyOaM1Yg5yVn/3FFX9/oZ/dEh778sfUVl3/GIa3ZJ5V7D/LzoDQhe813oej3f1xHFl0sw8zLiin7h00NshpLfuD1YQR7+0hCkZ4eSMWPJjBttn4BHF1xIM5RuvLhSoEmEQmCH1NIlddla2Ho/K53Bi03KzwdhTnYuKEksCAREwDQYJKoZIhvcNAQEEBQADgYEAfsks77dYx5SRFro2fpffJJw6wZidfaOvIbxll+9oKEjyWMFofe4Ni4TjcUuQKrjHM82hPfITB7qoIR+1Phv8YDUJF9ERP/UJJ/rj+4C+1utF62qRwMJ8XJn1S4R7/vSRfVT2/CENTTIYCh7TBDU/8BHvAOgYTqf7Jw7i/u13UJ0=\r\n-----END CERTIFICATE REQUEST-----";
50
51     /**
52      * test PKCS10 request
53      *
54      * @throws Exception
55      */

56
57     public void testPCKS10Request() throws Exception JavaDoc {
58         PKCS10CertificationRequest cr = X509Cert.getPKCS10Request(pkcs10request);
59
60         CertificationRequestInfo reqinfo = cr.getCertificationRequestInfo();
61
62         Assert.assertEquals("C=BE,O=net,OU=sourceforge,CN=bob,O=bob@sourceforge.net", reqinfo.getSubject().toString());
63     }
64
65     /**
66      * test CRL generation & certificate verification
67      *
68      * @throws Exception
69      */

70     public void testVerifyCertificate() throws Exception JavaDoc {
71         // load Alice's & Bob's certificate
72
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
73         X509Certificate JavaDoc bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
74
75         // load CA certificate & private key
76
X509Certificate JavaDoc cacert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "ca.p12", "ca", new StringBuffer JavaDoc("password"));
77         PrivateKey JavaDoc caprivkey = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "ca.p12", "ca", new StringBuffer JavaDoc("password"));
78
79         // we create a CRL where Alice's certificate is valid and Bob's is revoked
80
BigInteger JavaDoc[] certnumbers = new BigInteger JavaDoc[1];
81         certnumbers[0] = bobcert.getSerialNumber();
82
83         X509CRL JavaDoc crl = X509Cert.CreateCRL(certnumbers
84                 , 1
85                 , 24
86                 , "MD5WithRSAEncryption"
87                 , cacert
88                 , caprivkey);
89
90         Assert.assertEquals("VERIFIED", X509Cert.verifyCertificate(alicecert, cacert, crl));
91         Assert.assertEquals("REVOKED", X509Cert.verifyCertificate(bobcert, cacert, crl));
92
93     }
94 }
95
Popular Tags