KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > pdfbox > encryption > TestPublicKeyEncryption


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31
32 package test.pdfbox.encryption;
33
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.security.KeyStore JavaDoc;
38 import java.security.cert.CertificateFactory JavaDoc;
39 import java.security.cert.X509Certificate JavaDoc;
40
41 import junit.framework.Assert;
42 import junit.framework.Test;
43 import junit.framework.TestCase;
44 import junit.framework.TestSuite;
45
46 import org.pdfbox.exceptions.CryptographyException;
47 import org.pdfbox.pdmodel.PDDocument;
48 import org.pdfbox.pdmodel.encryption.AccessPermission;
49 import org.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial;
50 import org.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
51 import org.pdfbox.pdmodel.encryption.PublicKeyRecipient;
52
53 /**
54  * Tests for public key encryption.
55  *
56  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
57  * @version $Revision: 1.3 $
58  */

59 public class TestPublicKeyEncryption extends TestCase
60 {
61     
62     private AccessPermission accessPermission;
63     private AccessPermission accessPermission2;
64     
65     private File JavaDoc publicCert1;
66     private File JavaDoc privateCert1;
67     private File JavaDoc publicCert2;
68     private File JavaDoc privateCert2;
69     private File JavaDoc input;
70     private File JavaDoc output;
71     
72     private String JavaDoc password1 = "test1";
73     private String JavaDoc password2 = "test2";
74     
75     /**
76      * Constructor.
77      *
78      * @param name The junit test class name.
79      */

80     public TestPublicKeyEncryption( String JavaDoc name )
81     {
82         super( name );
83         accessPermission = new AccessPermission();
84         accessPermission.setCanAssembleDocument(false);
85         accessPermission.setCanExtractContent(false);
86         accessPermission.setCanExtractForAccessibility(true);
87         accessPermission.setCanFillInForm(false);
88         accessPermission.setCanModify(false);
89         accessPermission.setCanModifyAnnotations(false);
90         accessPermission.setCanPrint(false);
91         accessPermission.setCanPrintDegraded(false);
92         
93         accessPermission2 = new AccessPermission();
94         accessPermission2.setCanAssembleDocument(false);
95         accessPermission2.setCanExtractContent(false);
96         accessPermission2.setCanExtractForAccessibility(true);
97         accessPermission2.setCanFillInForm(false);
98         accessPermission2.setCanModify(false);
99         accessPermission2.setCanModifyAnnotations(false);
100         accessPermission2.setCanPrint(true); // it is true now !
101
accessPermission2.setCanPrintDegraded(false);
102             
103         publicCert1 = new File JavaDoc("test/encryption/test1.der");
104         privateCert1 = new File JavaDoc("test/encryption/test1.pfx");
105         publicCert2 = new File JavaDoc("test/encryption/test2.der");
106         privateCert2 = new File JavaDoc("test/encryption/test2.pfx");
107         input = new File JavaDoc("test/input/Exolab.pdf");
108         output = new File JavaDoc("test/encryption/output.pdf");
109         
110         Assert.assertTrue(publicCert1.exists() && publicCert1.isFile());
111         Assert.assertTrue(privateCert1.exists() && privateCert1.isFile());
112         
113         Assert.assertTrue(publicCert2.exists() && publicCert2.isFile());
114         Assert.assertTrue(privateCert2.exists() && privateCert2.isFile());
115         
116         Assert.assertTrue(input.exists() && input.isFile());
117         
118     }
119     
120     /**
121      * This will get the suite of test that this class holds.
122      *
123      * @return All of the tests that this class holds.
124      */

125     public static Test suite()
126     {
127         return new TestSuite( TestPublicKeyEncryption.class );
128     }
129     
130     /**
131      * Protect a document with certificate 1 and try to open it with certificate 2
132      * and catch the exception.
133      *
134      * @throws Exception If there is an error during the test.
135      */

136     public void testProtectionError() throws Exception JavaDoc
137     {
138                         
139         PDDocument doc = PDDocument.load(input);
140         protect(doc, publicCert1.getAbsolutePath());
141         
142         doc.save(output.getAbsolutePath());
143             
144         doc.close();
145                         
146         PDDocument doc2 = PDDocument.load(output);
147         
148         Exception JavaDoc e = null;
149         
150         try
151         {
152             open(doc2, privateCert2.getAbsolutePath(), password2);
153         }
154         catch(CryptographyException ex)
155         {
156             e = ex;
157             System.out.println(ex.getMessage());
158         }
159         finally
160         {
161             Assert.assertNotNull(e);
162         }
163     }
164     
165     
166     /**
167      * Protect a document with the public certificate and try to open it with
168      * the private certificate.
169      *
170      * @throws Exception If there is an error during the test.
171      */

172     public void testProtection() throws Exception JavaDoc
173     {
174         PDDocument doc = PDDocument.load(input);
175         protect(doc, publicCert1.getAbsolutePath());
176         
177         //Assert.assertTrue(doc.isEncrypted());
178

179         doc.save(output.getAbsolutePath());
180             
181         doc.close();
182                         
183         PDDocument doc2 = PDDocument.load(output);
184         
185         Assert.assertNotNull(doc2);
186         
187         open(doc2, privateCert1.getAbsolutePath(), password1);
188         
189         Assert.assertTrue(doc2.isEncrypted());
190         
191         AccessPermission currentAp = doc2.getCurrentAccessPermission();
192         
193         Assert.assertFalse(currentAp.canAssembleDocument());
194         Assert.assertFalse(currentAp.canExtractContent());
195         Assert.assertTrue(currentAp.canExtractForAccessibility());
196         Assert.assertFalse(currentAp.canFillInForm());
197         Assert.assertFalse(currentAp.canModify());
198         Assert.assertFalse(currentAp.canModifyAnnotations());
199         Assert.assertFalse(currentAp.canPrint());
200         Assert.assertFalse(currentAp.canPrintDegraded());
201         
202         doc2.close();
203             
204     }
205     
206     
207     /**
208      * Protect the document for 2 recipients and try to open it.
209      *
210      * @throws Exception If there is an error during the test.
211      */

212     public void testMultipleRecipients() throws Exception JavaDoc
213     {
214         
215         CertificateFactory JavaDoc cf = CertificateFactory.getInstance("X.509");
216         
217         PDDocument doc = PDDocument.load(input);
218         
219         PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
220         
221         PublicKeyRecipient recip1 = new PublicKeyRecipient();
222         PublicKeyRecipient recip2 = new PublicKeyRecipient();
223         
224         recip1.setPermission(accessPermission);
225         recip2.setPermission(accessPermission2);
226         
227         InputStream JavaDoc inStream = new FileInputStream JavaDoc(publicCert1);
228         Assert.assertNotNull(cf);
229         X509Certificate JavaDoc certificate1 = (X509Certificate JavaDoc)cf.generateCertificate(inStream);
230         inStream.close();
231         
232         InputStream JavaDoc inStream2 = new FileInputStream JavaDoc(publicCert2);
233         Assert.assertNotNull(cf);
234         X509Certificate JavaDoc certificate2 = (X509Certificate JavaDoc)cf.generateCertificate(inStream2);
235         inStream.close();
236         
237         recip1.setX509(certificate1);
238         recip2.setX509(certificate2);
239         
240         ppp.addRecipient(recip1);
241         ppp.addRecipient(recip2);
242         
243         doc.protect(ppp);
244         doc.save(output.getAbsolutePath());
245         doc.close();
246         
247         /* open first time */
248         
249         PDDocument docOpen1 = PDDocument.load(output);
250         
251         KeyStore JavaDoc ks1 = KeyStore.getInstance("PKCS12");
252         ks1.load(new FileInputStream JavaDoc(privateCert1), password1.toCharArray());
253         PublicKeyDecryptionMaterial pdm = new PublicKeyDecryptionMaterial(ks1, null, password1);
254         docOpen1.openProtection(pdm);
255         docOpen1.close();
256
257         /* open second time */
258         
259         PDDocument docOpen2 = PDDocument.load(output);
260         
261         KeyStore JavaDoc ks2 = KeyStore.getInstance("PKCS12");
262         ks2.load(new FileInputStream JavaDoc(privateCert2), password2.toCharArray());
263         PublicKeyDecryptionMaterial pdm2 = new PublicKeyDecryptionMaterial(ks2, null, password2);
264         docOpen2.openProtection(pdm2);
265         docOpen2.close();
266                 
267     }
268     
269     
270     
271     private void protect(PDDocument doc, String JavaDoc certPath) throws Exception JavaDoc
272     {
273         InputStream JavaDoc inStream = new FileInputStream JavaDoc(certPath);
274         CertificateFactory JavaDoc cf = CertificateFactory.getInstance("X.509");
275         Assert.assertNotNull(cf);
276         X509Certificate JavaDoc certificate = (X509Certificate JavaDoc)cf.generateCertificate(inStream);
277         Assert.assertNotNull(certificate);
278         inStream.close();
279         
280         PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
281         PublicKeyRecipient recip = new PublicKeyRecipient();
282         recip.setPermission(accessPermission);
283         recip.setX509(certificate);
284         
285         ppp.addRecipient(recip);
286         
287         doc.protect(ppp);
288         
289     }
290     
291     
292     private void open(PDDocument doc, String JavaDoc certPath, String JavaDoc password) throws Exception JavaDoc
293     {
294         KeyStore JavaDoc ks = KeyStore.getInstance("PKCS12");
295         ks.load(new FileInputStream JavaDoc(certPath), password.toCharArray());
296         
297         PublicKeyDecryptionMaterial pdm = new PublicKeyDecryptionMaterial(ks, null, password);
298         
299         doc.openProtection(pdm);
300
301     }
302
303 }
304
Popular Tags