001: /**
002: * Copyright (c) 2003, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.encryption;
031:
032: import org.pdfbox.cos.COSDictionary;
033: import org.pdfbox.cos.COSInteger;
034: import org.pdfbox.cos.COSName;
035: import org.pdfbox.cos.COSNumber;
036: import org.pdfbox.cos.COSString;
037:
038: import java.io.IOException;
039:
040: /**
041: * This class holds information that is related to the standard PDF encryption.
042: *
043: * See PDF Reference 1.4 section "3.5 Encryption"
044: *
045: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
046: * @version $Revision: 1.7 $
047: * @deprecated Made deprecated by the new security layer of PDFBox. Use SecurityHandlers instead.
048: */
049: public class PDStandardEncryption extends PDEncryptionDictionary {
050: /**
051: * The 'Filter' name for this security handler.
052: */
053: public static final String FILTER_NAME = "Standard";
054:
055: /**
056: * The default revision of one is not specified.
057: */
058: public static final int DEFAULT_REVISION = 3;
059:
060: /**
061: * Encryption revision 2.
062: */
063: public static final int REVISION2 = 2;
064: /**
065: * Encryption revision 3.
066: */
067: public static final int REVISION3 = 3;
068: /**
069: * Encryption revision 4.
070: */
071: public static final int REVISION4 = 4;
072:
073: /**
074: * The default set of permissions which is to allow all.
075: */
076: public static final int DEFAULT_PERMISSIONS = 0xFFFFFFFF ^ 3;//bits 0 & 1 need to be zero
077:
078: private static final int PRINT_BIT = 3;
079: private static final int MODIFICATION_BIT = 4;
080: private static final int EXTRACT_BIT = 5;
081: private static final int MODIFY_ANNOTATIONS_BIT = 6;
082: private static final int FILL_IN_FORM_BIT = 9;
083: private static final int EXTRACT_FOR_ACCESSIBILITY_BIT = 10;
084: private static final int ASSEMBLE_DOCUMENT_BIT = 11;
085: private static final int DEGRADED_PRINT_BIT = 12;
086:
087: /**
088: * Default constructor that uses Version 2, Revision 3, 40 bit encryption,
089: * all permissions allowed.
090: */
091: public PDStandardEncryption() {
092: super ();
093: encryptionDictionary.setItem(COSName.FILTER, COSName
094: .getPDFName(FILTER_NAME));
095: setVersion(PDEncryptionDictionary.VERSION1_40_BIT_ALGORITHM);
096: setRevision(PDStandardEncryption.REVISION2);
097: setPermissions(DEFAULT_PERMISSIONS);
098: }
099:
100: /**
101: * Constructor from existing dictionary.
102: *
103: * @param dict The existing encryption dictionary.
104: */
105: public PDStandardEncryption(COSDictionary dict) {
106: super (dict);
107: }
108:
109: /**
110: * This will return the R entry of the encryption dictionary.<br /><br />
111: * See PDF Reference 1.4 Table 3.14.
112: *
113: * @return The encryption revision to use.
114: */
115: public int getRevision() {
116: int revision = DEFAULT_VERSION;
117: COSNumber cosRevision = (COSNumber) encryptionDictionary
118: .getDictionaryObject(COSName.getPDFName("R"));
119: if (cosRevision != null) {
120: revision = cosRevision.intValue();
121: }
122: return revision;
123: }
124:
125: /**
126: * This will set the R entry of the encryption dictionary.<br /><br />
127: * See PDF Reference 1.4 Table 3.14. <br /><br/>
128: *
129: * <b>Note: This value is used to decrypt the pdf document. If you change this when
130: * the document is encrypted then decryption will fail!.</b>
131: *
132: * @param revision The new encryption version.
133: */
134: public void setRevision(int revision) {
135: encryptionDictionary.setItem(COSName.getPDFName("R"),
136: new COSInteger(revision));
137: }
138:
139: /**
140: * This will get the O entry in the standard encryption dictionary.
141: *
142: * @return A 32 byte array or null if there is no owner key.
143: */
144: public byte[] getOwnerKey() {
145: byte[] o = null;
146: COSString owner = (COSString) encryptionDictionary
147: .getDictionaryObject(COSName.getPDFName("O"));
148: if (owner != null) {
149: o = owner.getBytes();
150: }
151: return o;
152: }
153:
154: /**
155: * This will set the O entry in the standard encryption dictionary.
156: *
157: * @param o A 32 byte array or null if there is no owner key.
158: *
159: * @throws IOException If there is an error setting the data.
160: */
161: public void setOwnerKey(byte[] o) throws IOException {
162: COSString owner = new COSString();
163: owner.append(o);
164: encryptionDictionary.setItem(COSName.getPDFName("O"), owner);
165: }
166:
167: /**
168: * This will get the U entry in the standard encryption dictionary.
169: *
170: * @return A 32 byte array or null if there is no user key.
171: */
172: public byte[] getUserKey() {
173: byte[] u = null;
174: COSString user = (COSString) encryptionDictionary
175: .getDictionaryObject(COSName.getPDFName("U"));
176: if (user != null) {
177: u = user.getBytes();
178: }
179: return u;
180: }
181:
182: /**
183: * This will set the U entry in the standard encryption dictionary.
184: *
185: * @param u A 32 byte array.
186: *
187: * @throws IOException If there is an error setting the data.
188: */
189: public void setUserKey(byte[] u) throws IOException {
190: COSString user = new COSString();
191: user.append(u);
192: encryptionDictionary.setItem(COSName.getPDFName("U"), user);
193: }
194:
195: /**
196: * This will get the permissions bit mask.
197: *
198: * @return The permissions bit mask.
199: */
200: public int getPermissions() {
201: int permissions = 0;
202: COSInteger p = (COSInteger) encryptionDictionary
203: .getDictionaryObject(COSName.getPDFName("P"));
204: if (p != null) {
205: permissions = p.intValue();
206: }
207: return permissions;
208: }
209:
210: /**
211: * This will set the permissions bit mask.
212: *
213: * @param p The new permissions bit mask
214: */
215: public void setPermissions(int p) {
216: encryptionDictionary.setItem(COSName.getPDFName("P"),
217: new COSInteger(p));
218: }
219:
220: private boolean isPermissionBitOn(int bit) {
221: return (getPermissions() & (1 << (bit - 1))) != 0;
222: }
223:
224: private boolean setPermissionBit(int bit, boolean value) {
225: int permissions = getPermissions();
226: if (value) {
227: permissions = permissions | (1 << (bit - 1));
228: } else {
229: permissions = permissions & (0xFFFFFFFF ^ (1 << (bit - 1)));
230: }
231: setPermissions(permissions);
232:
233: return (getPermissions() & (1 << (bit - 1))) != 0;
234: }
235:
236: /**
237: * This will tell if the user can print.
238: *
239: * @return true If supplied with the user password they are allowed to print.
240: */
241: public boolean canPrint() {
242: return isPermissionBitOn(PRINT_BIT);
243: }
244:
245: /**
246: * Set if the user can print.
247: *
248: * @param allowPrinting A boolean determining if the user can print.
249: */
250: public void setCanPrint(boolean allowPrinting) {
251: setPermissionBit(PRINT_BIT, allowPrinting);
252: }
253:
254: /**
255: * This will tell if the user can modify contents of the document.
256: *
257: * @return true If supplied with the user password they are allowed to modify the document
258: */
259: public boolean canModify() {
260: return isPermissionBitOn(MODIFICATION_BIT);
261: }
262:
263: /**
264: * Set if the user can modify the document.
265: *
266: * @param allowModifications A boolean determining if the user can modify the document.
267: */
268: public void setCanModify(boolean allowModifications) {
269: setPermissionBit(MODIFICATION_BIT, allowModifications);
270: }
271:
272: /**
273: * This will tell if the user can extract text and images from the PDF document.
274: *
275: * @return true If supplied with the user password they are allowed to extract content
276: * from the PDF document
277: */
278: public boolean canExtractContent() {
279: return isPermissionBitOn(EXTRACT_BIT);
280: }
281:
282: /**
283: * Set if the user can extract content from the document.
284: *
285: * @param allowExtraction A boolean determining if the user can extract content
286: * from the document.
287: */
288: public void setCanExtractContent(boolean allowExtraction) {
289: setPermissionBit(EXTRACT_BIT, allowExtraction);
290: }
291:
292: /**
293: * This will tell if the user can add/modify text annotations, fill in interactive forms fields.
294: *
295: * @return true If supplied with the user password they are allowed to modify annotations.
296: */
297: public boolean canModifyAnnotations() {
298: return isPermissionBitOn(MODIFY_ANNOTATIONS_BIT);
299: }
300:
301: /**
302: * Set if the user can modify annotations.
303: *
304: * @param allowAnnotationModification A boolean determining if the user can modify annotations.
305: */
306: public void setCanModifyAnnotations(
307: boolean allowAnnotationModification) {
308: setPermissionBit(MODIFY_ANNOTATIONS_BIT,
309: allowAnnotationModification);
310: }
311:
312: /**
313: * This will tell if the user can fill in interactive forms.
314: *
315: * @return true If supplied with the user password they are allowed to fill in form fields.
316: */
317: public boolean canFillInForm() {
318: return isPermissionBitOn(FILL_IN_FORM_BIT);
319: }
320:
321: /**
322: * Set if the user can fill in interactive forms.
323: *
324: * @param allowFillingInForm A boolean determining if the user can fill in interactive forms.
325: */
326: public void setCanFillInForm(boolean allowFillingInForm) {
327: setPermissionBit(FILL_IN_FORM_BIT, allowFillingInForm);
328: }
329:
330: /**
331: * This will tell if the user can extract text and images from the PDF document
332: * for accessibility purposes.
333: *
334: * @return true If supplied with the user password they are allowed to extract content
335: * from the PDF document
336: */
337: public boolean canExtractForAccessibility() {
338: return isPermissionBitOn(EXTRACT_FOR_ACCESSIBILITY_BIT);
339: }
340:
341: /**
342: * Set if the user can extract content from the document for accessibility purposes.
343: *
344: * @param allowExtraction A boolean determining if the user can extract content
345: * from the document.
346: */
347: public void setCanExtractForAccessibility(boolean allowExtraction) {
348: setPermissionBit(EXTRACT_FOR_ACCESSIBILITY_BIT, allowExtraction);
349: }
350:
351: /**
352: * This will tell if the user can insert/rotate/delete pages.
353: *
354: * @return true If supplied with the user password they are allowed to extract content
355: * from the PDF document
356: */
357: public boolean canAssembleDocument() {
358: return isPermissionBitOn(ASSEMBLE_DOCUMENT_BIT);
359: }
360:
361: /**
362: * Set if the user can insert/rotate/delete pages.
363: *
364: * @param allowAssembly A boolean determining if the user can assemble the document.
365: */
366: public void setCanAssembleDocument(boolean allowAssembly) {
367: setPermissionBit(ASSEMBLE_DOCUMENT_BIT, allowAssembly);
368: }
369:
370: /**
371: * This will tell if the user can print the document in a degraded format.
372: *
373: * @return true If supplied with the user password they are allowed to print the
374: * document in a degraded format.
375: */
376: public boolean canPrintDegraded() {
377: return isPermissionBitOn(DEGRADED_PRINT_BIT);
378: }
379:
380: /**
381: * Set if the user can print the document in a degraded format.
382: *
383: * @param allowAssembly A boolean determining if the user can print the
384: * document in a degraded format.
385: */
386: public void setCanPrintDegraded(boolean allowAssembly) {
387: setPermissionBit(DEGRADED_PRINT_BIT, allowAssembly);
388: }
389: }
|