KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > encryption > PublicKeyProtectionPolicy


1 /**
2  * Copyright (c) 2003-2005, 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 org.pdfbox.pdmodel.encryption;
33
34 import java.security.cert.X509Certificate JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * This class represents the protection policy to use to protect
40  * a document with the public key security handler as described
41  * in the PDF specification 1.6 p104.
42  *
43  * PDF documents are encrypted so that they can be decrypted by
44  * one or more recipients. Each recipient have its own access permission.
45  *
46  * The following code sample shows how to protect a document using
47  * the public key security handler. In this code sample, <code>doc</code> is
48  * a <code>PDDocument</code> object.
49  *
50  * <pre>
51  * PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
52  * PublicKeyRecipient recip = new PublicKeyRecipient();
53  * AccessPermission ap = new AccessPermission();
54  * ap.setCanModify(false);
55  * recip.setPermission(ap);
56  *
57  * // load the recipient's certificate
58  * InputStream inStream = new FileInputStream(certificate_path);
59  * CertificateFactory cf = CertificateFactory.getInstance("X.509");
60  * X509Certificate certificate = (X509Certificate)cf.generateCertificate(inStream);
61  * inStream.close();
62  *
63  * recip.setX509(certificate); // set the recipient's certificate
64  * policy.addRecipient(recip);
65  * policy.setEncryptionKeyLength(128); // the document will be encrypted with 128 bits secret key
66  * doc.protect(policy);
67  * doc.save(out);
68  * </pre>
69  *
70  *
71  * @see org.pdfbox.pdmodel.PDDocument#protect(ProtectionPolicy)
72  * @see AccessPermission
73  * @see PublicKeyRecipient
74  *
75  * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
76  *
77  * @version $Revision: 1.2 $
78  */

79 public class PublicKeyProtectionPolicy extends ProtectionPolicy
80 {
81
82     /**
83      * The list of recipients.
84      */

85     private ArrayList JavaDoc recipients = null;
86
87     /**
88      * The X509 certificate used to decrypt the current document.
89      */

90     private X509Certificate JavaDoc decryptionCertificate;
91     
92     /**
93      * Constructor for encryption. Just creates an empty recipients list.
94      */

95     public PublicKeyProtectionPolicy()
96     {
97         recipients = new ArrayList JavaDoc();
98     }
99     
100     /**
101      * Adds a new recipient to the recipients list.
102      *
103      * @param r A new recipient.
104      */

105     public void addRecipient(PublicKeyRecipient r)
106     {
107         recipients.add(r);
108     }
109     
110     /**
111      * Removes a recipient from the recipients list.
112      *
113      * @param r The recipient to remove.
114      *
115      * @return true If a recipient was found and removed.
116      */

117     public boolean removeRecipient(PublicKeyRecipient r)
118     {
119         return recipients.remove(r);
120     }
121     
122     /**
123      * Returns an iterator to browse the list of recipients. Object
124      * found in this iterator are <code>PublicKeyRecipient</code>.
125      *
126      * @return The recipients list iterator.
127      */

128     public Iterator JavaDoc getRecipientsIterator()
129     {
130         return recipients.iterator();
131     }
132
133     /**
134      * Getter of the property <tt>decryptionCertificate</tt>.
135      *
136      * @return Returns the decryptionCertificate.
137      */

138     public X509Certificate JavaDoc getDecryptionCertificate()
139     {
140         return decryptionCertificate;
141     }
142
143     /**
144      * Setter of the property <tt>decryptionCertificate</tt>.
145      *
146      * @param aDecryptionCertificate The decryption certificate to set.
147      */

148     public void setDecryptionCertificate(X509Certificate JavaDoc aDecryptionCertificate)
149     {
150         this.decryptionCertificate = aDecryptionCertificate;
151     }
152     
153     /**
154      * Returns the number of recipients.
155      *
156      * @return The number of recipients.
157      */

158     public int getRecipientsNumber()
159     {
160         return recipients.size();
161     }
162 }
163
Popular Tags