001/*
002 * $Id: PdfEncryptor.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045
046import java.io.IOException;
047import java.io.OutputStream;
048import java.util.HashMap;
049
050import com.itextpdf.text.DocumentException;
051
052/** This class takes any PDF and returns exactly the same but
053 * encrypted. All the content, links, outlines, etc, are kept.
054 * It is also possible to change the info dictionary.
055 */
056public final class PdfEncryptor {
057
058    private PdfEncryptor(){
059    }
060
061    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
062     * <code>PdfWriter</code>. The userPassword and the
063     *  ownerPassword can be null or have zero length. In this case the ownerPassword
064     *  is replaced by a random string. The open permissions for the document can be
065     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
066     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
067     *  The permissions can be combined by ORing them.
068     * @param reader the read PDF
069     * @param os the output destination
070     * @param userPassword the user password. Can be null or empty
071     * @param ownerPassword the owner password. Can be null or empty
072     * @param permissions the user permissions
073     * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
074     * @throws DocumentException on error
075     * @throws IOException on error */
076    public static void encrypt(PdfReader reader, OutputStream os, byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException, IOException {
077        PdfStamper stamper = new PdfStamper(reader, os);
078        stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits);
079        stamper.close();
080    }
081
082    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
083     * <code>PdfWriter</code>. The userPassword and the
084     *  ownerPassword can be null or have zero length. In this case the ownerPassword
085     *  is replaced by a random string. The open permissions for the document can be
086     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
087     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
088     *  The permissions can be combined by ORing them.
089     * @param reader the read PDF
090     * @param os the output destination
091     * @param userPassword the user password. Can be null or empty
092     * @param ownerPassword the owner password. Can be null or empty
093     * @param permissions the user permissions
094     * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
095     * @param newInfo an optional <CODE>String</CODE> map to add or change
096     * the info dictionary. Entries with <CODE>null</CODE>
097     * values delete the key in the original info dictionary
098     * @throws DocumentException on error
099     * @throws IOException on error
100     * @since 5.0.1 (generic type in signature)
101     */
102    public static void encrypt(PdfReader reader, OutputStream os, byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits, HashMap<String, String> newInfo) throws DocumentException, IOException {
103        PdfStamper stamper = new PdfStamper(reader, os);
104        stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits);
105        stamper.setMoreInfo(newInfo);
106        stamper.close();
107    }
108
109    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
110     * <code>PdfWriter</code>. The userPassword and the
111     *  ownerPassword can be null or have zero length. In this case the ownerPassword
112     *  is replaced by a random string. The open permissions for the document can be
113     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
114     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
115     *  The permissions can be combined by ORing them.
116     * @param reader the read PDF
117     * @param os the output destination
118     * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
119     * @param userPassword the user password. Can be null or empty
120     * @param ownerPassword the owner password. Can be null or empty
121     * @param permissions the user permissions
122     * @throws DocumentException on error
123     * @throws IOException on error */
124    public static void encrypt(PdfReader reader, OutputStream os, boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException, IOException {
125        PdfStamper stamper = new PdfStamper(reader, os);
126        stamper.setEncryption(strength, userPassword, ownerPassword, permissions);
127        stamper.close();
128    }
129
130    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
131     * <code>PdfWriter</code>. The userPassword and the
132     *  ownerPassword can be null or have zero length. In this case the ownerPassword
133     *  is replaced by a random string. The open permissions for the document can be
134     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
135     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
136     *  The permissions can be combined by ORing them.
137     * @param reader the read PDF
138     * @param os the output destination
139     * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
140     * @param userPassword the user password. Can be null or empty
141     * @param ownerPassword the owner password. Can be null or empty
142     * @param permissions the user permissions
143     * @param newInfo an optional <CODE>String</CODE> map to add or change
144     * the info dictionary. Entries with <CODE>null</CODE>
145     * values delete the key in the original info dictionary
146     * @throws DocumentException on error
147     * @throws IOException on error
148     * @since 5.0.1 (generic type in signature)
149     */
150    public static void encrypt(PdfReader reader, OutputStream os, boolean strength, String userPassword, String ownerPassword, int permissions, HashMap<String, String> newInfo) throws DocumentException, IOException {
151        PdfStamper stamper = new PdfStamper(reader, os);
152        stamper.setEncryption(strength, userPassword, ownerPassword, permissions);
153        stamper.setMoreInfo(newInfo);
154        stamper.close();
155    }
156
157
158    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
159     * <code>PdfWriter</code>. The userPassword and the
160     *  ownerPassword can be null or have zero length. In this case the ownerPassword
161     *  is replaced by a random string. The open permissions for the document can be
162     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
163     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
164     *  The permissions can be combined by ORing them.
165     * @param reader the read PDF
166     * @param os the output destination
167     * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
168     * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
169     * @param userPassword the user password. Can be null or empty
170     * @param ownerPassword the owner password. Can be null or empty
171     * @param permissions the user permissions
172     * @param newInfo an optional <CODE>String</CODE> map to add or change
173     * the info dictionary. Entries with <CODE>null</CODE>
174     * values delete the key in the original info dictionary
175     * @throws DocumentException on error
176     * @throws IOException on error
177     * @since 5.0.1 (generic type in signature)
178     */
179    public static void encrypt(PdfReader reader, OutputStream os, int type, String userPassword, String ownerPassword, int permissions, HashMap<String, String> newInfo) throws DocumentException, IOException {
180        PdfStamper stamper = new PdfStamper(reader, os);
181        stamper.setEncryption(type, userPassword, ownerPassword, permissions);
182        stamper.setMoreInfo(newInfo);
183        stamper.close();
184    }
185
186    /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
187     * <code>PdfWriter</code>. The userPassword and the
188     *  ownerPassword can be null or have zero length. In this case the ownerPassword
189     *  is replaced by a random string. The open permissions for the document can be
190     *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
191     *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
192     *  The permissions can be combined by ORing them.
193     * @param reader the read PDF
194     * @param os the output destination
195     * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
196     * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
197     * @param userPassword the user password. Can be null or empty
198     * @param ownerPassword the owner password. Can be null or empty
199     * @param permissions the user permissions
200     * values delete the key in the original info dictionary
201     * @throws DocumentException on error
202     * @throws IOException on error
203     */
204    public static void encrypt(PdfReader reader, OutputStream os, int type, String userPassword, String ownerPassword, int permissions) throws DocumentException, IOException {
205        PdfStamper stamper = new PdfStamper(reader, os);
206        stamper.setEncryption(type, userPassword, ownerPassword, permissions);
207        stamper.close();
208    }
209
210    /**
211     * Give you a verbose analysis of the permissions.
212     * @param permissions the permissions value of a PDF file
213     * @return a String that explains the meaning of the permissions value
214     */
215    public static String getPermissionsVerbose(int permissions) {
216        StringBuffer buf = new StringBuffer("Allowed:");
217        if ((PdfWriter.ALLOW_PRINTING & permissions) == PdfWriter.ALLOW_PRINTING) buf.append(" Printing");
218        if ((PdfWriter.ALLOW_MODIFY_CONTENTS & permissions) == PdfWriter.ALLOW_MODIFY_CONTENTS) buf.append(" Modify contents");
219        if ((PdfWriter.ALLOW_COPY & permissions) == PdfWriter.ALLOW_COPY) buf.append(" Copy");
220        if ((PdfWriter.ALLOW_MODIFY_ANNOTATIONS & permissions) == PdfWriter.ALLOW_MODIFY_ANNOTATIONS) buf.append(" Modify annotations");
221        if ((PdfWriter.ALLOW_FILL_IN & permissions) == PdfWriter.ALLOW_FILL_IN) buf.append(" Fill in");
222        if ((PdfWriter.ALLOW_SCREENREADERS & permissions) == PdfWriter.ALLOW_SCREENREADERS) buf.append(" Screen readers");
223        if ((PdfWriter.ALLOW_ASSEMBLY & permissions) == PdfWriter.ALLOW_ASSEMBLY) buf.append(" Assembly");
224        if ((PdfWriter.ALLOW_DEGRADED_PRINTING & permissions) == PdfWriter.ALLOW_DEGRADED_PRINTING) buf.append(" Degraded printing");
225        return buf.toString();
226    }
227
228    /**
229     * Tells you if printing is allowed.
230     * @param permissions the permissions value of a PDF file
231     * @return  true if printing is allowed
232     *
233     * @since 2.0.7
234     */
235    public static boolean isPrintingAllowed(int permissions) {
236        return (PdfWriter.ALLOW_PRINTING & permissions) == PdfWriter.ALLOW_PRINTING;
237    }
238
239    /**
240     * Tells you if modifying content is allowed.
241     * @param permissions the permissions value of a PDF file
242     * @return  true if modifying content is allowed
243     *
244     * @since 2.0.7
245     */
246    public static boolean isModifyContentsAllowed(int permissions) {
247        return (PdfWriter.ALLOW_MODIFY_CONTENTS & permissions) == PdfWriter.ALLOW_MODIFY_CONTENTS;
248    }
249
250    /**
251     * Tells you if copying is allowed.
252     * @param permissions the permissions value of a PDF file
253     * @return  true if copying is allowed
254     *
255     * @since 2.0.7
256     */
257    public static boolean isCopyAllowed(int permissions) {
258        return (PdfWriter.ALLOW_COPY & permissions) == PdfWriter.ALLOW_COPY;
259    }
260
261    /**
262     * Tells you if modifying annotations is allowed.
263     * @param permissions the permissions value of a PDF file
264     * @return  true if modifying annotations is allowed
265     *
266     * @since 2.0.7
267     */
268    public static boolean isModifyAnnotationsAllowed(int permissions) {
269        return (PdfWriter.ALLOW_MODIFY_ANNOTATIONS & permissions) == PdfWriter.ALLOW_MODIFY_ANNOTATIONS;
270    }
271
272    /**
273     * Tells you if filling in fields is allowed.
274     * @param permissions the permissions value of a PDF file
275     * @return  true if filling in fields is allowed
276     *
277     * @since 2.0.7
278     */
279    public static boolean isFillInAllowed(int permissions) {
280        return (PdfWriter.ALLOW_FILL_IN & permissions) == PdfWriter.ALLOW_FILL_IN;
281    }
282
283    /**
284     * Tells you if repurposing for screenreaders is allowed.
285     * @param permissions the permissions value of a PDF file
286     * @return  true if repurposing for screenreaders is allowed
287     *
288     * @since 2.0.7
289     */
290    public static boolean isScreenReadersAllowed(int permissions) {
291        return (PdfWriter.ALLOW_SCREENREADERS & permissions) == PdfWriter.ALLOW_SCREENREADERS;
292    }
293
294    /**
295     * Tells you if document assembly is allowed.
296     * @param permissions the permissions value of a PDF file
297     * @return  true if document assembly is allowed
298     *
299     * @since 2.0.7
300     */
301    public static boolean isAssemblyAllowed(int permissions) {
302        return (PdfWriter.ALLOW_ASSEMBLY & permissions) == PdfWriter.ALLOW_ASSEMBLY;
303    }
304
305    /**
306     * Tells you if degraded printing is allowed.
307     * @param permissions the permissions value of a PDF file
308     * @return  true if degraded printing is allowed
309     *
310     * @since 2.0.7
311     */
312    public static boolean isDegradedPrintingAllowed(int permissions) {
313        return (PdfWriter.ALLOW_DEGRADED_PRINTING & permissions) == PdfWriter.ALLOW_DEGRADED_PRINTING;
314    }
315}